blob: 7590c2e4da99b5c89f77c7a9e1a89ed0f88492bf [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
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005369static bool isXor1OfSetCC(SDValue Op) {
5370 if (Op.getOpcode() != ISD::XOR)
5371 return false;
5372 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5373 if (N1C && N1C->getAPIntValue() == 1) {
5374 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5375 Op.getOperand(0).hasOneUse();
5376 }
5377 return false;
5378}
5379
Dan Gohman8181bd12008-07-27 21:46:04 +00005380SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005381 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005382 SDValue Chain = Op.getOperand(0);
5383 SDValue Cond = Op.getOperand(1);
5384 SDValue Dest = Op.getOperand(2);
5385 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005386
5387 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005388 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005389#if 0
5390 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005391 else if (Cond.getOpcode() == X86ISD::ADD ||
5392 Cond.getOpcode() == X86ISD::SUB ||
5393 Cond.getOpcode() == X86ISD::SMUL ||
5394 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005395 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005396#endif
5397
Evan Cheng50d37ab2007-10-08 22:16:29 +00005398 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5399 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 if (Cond.getOpcode() == X86ISD::SETCC) {
5401 CC = Cond.getOperand(0);
5402
Dan Gohman8181bd12008-07-27 21:46:04 +00005403 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005405 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5406 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005407 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005408 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005409 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005410 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005411 default: break;
5412 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005413 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005414 // These can only come from an arithmetic instruction with overflow,
5415 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005416 Cond = Cond.getNode()->getOperand(1);
5417 addTest = false;
5418 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005419 }
Evan Cheng950aac02007-09-25 01:57:46 +00005420 }
Evan Chengd580f022008-12-03 08:38:43 +00005421 } else {
5422 unsigned CondOpc;
5423 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5424 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5425 unsigned Opc = Cmp.getOpcode();
5426 if (CondOpc == ISD::OR) {
5427 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5428 // two branches instead of an explicit OR instruction with a
5429 // separate test.
5430 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5431 isX86LogicalCmp(Opc)) {
5432 CC = Cond.getOperand(0).getOperand(0);
5433 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5434 Chain, Dest, CC, Cmp);
5435 CC = Cond.getOperand(1).getOperand(0);
5436 Cond = Cmp;
5437 addTest = false;
5438 }
5439 } else { // ISD::AND
5440 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5441 // two branches instead of an explicit AND instruction with a
5442 // separate test. However, we only do this if this block doesn't
5443 // have a fall-through edge, because this requires an explicit
5444 // jmp when the condition is false.
5445 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5446 isX86LogicalCmp(Opc) &&
5447 Op.getNode()->hasOneUse()) {
5448 X86::CondCode CCode =
5449 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5450 CCode = X86::GetOppositeBranchCondition(CCode);
5451 CC = DAG.getConstant(CCode, MVT::i8);
5452 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5453 // Look for an unconditional branch following this conditional branch.
5454 // We need this because we need to reverse the successors in order
5455 // to implement FCMP_OEQ.
5456 if (User.getOpcode() == ISD::BR) {
5457 SDValue FalseBB = User.getOperand(1);
5458 SDValue NewBR =
5459 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5460 assert(NewBR == User);
5461 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005462
Evan Chengd580f022008-12-03 08:38:43 +00005463 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5464 Chain, Dest, CC, Cmp);
5465 X86::CondCode CCode =
5466 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5467 CCode = X86::GetOppositeBranchCondition(CCode);
5468 CC = DAG.getConstant(CCode, MVT::i8);
5469 Cond = Cmp;
5470 addTest = false;
5471 }
5472 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005473 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00005474 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5475 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5476 // It should be transformed during dag combiner except when the condition
5477 // is set by a arithmetics with overflow node.
5478 X86::CondCode CCode =
5479 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5480 CCode = X86::GetOppositeBranchCondition(CCode);
5481 CC = DAG.getConstant(CCode, MVT::i8);
5482 Cond = Cond.getOperand(0).getOperand(1);
5483 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005484 }
Evan Cheng950aac02007-09-25 01:57:46 +00005485 }
5486
5487 if (addTest) {
5488 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005489 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005490 }
Evan Cheng621216e2007-09-29 00:00:36 +00005491 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005492 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005493}
5494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495
5496// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5497// Calls to _alloca is needed to probe the stack when allocating more than 4k
5498// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5499// that the guard pages used by the OS virtual memory manager are allocated in
5500// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005501SDValue
5502X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005503 SelectionDAG &DAG) {
5504 assert(Subtarget->isTargetCygMing() &&
5505 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005506
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005508 SDValue Chain = Op.getOperand(0);
5509 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005510 // FIXME: Ensure alignment here
5511
Dan Gohman8181bd12008-07-27 21:46:04 +00005512 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005513
Duncan Sands92c43912008-06-06 12:08:01 +00005514 MVT IntPtr = getPointerTy();
5515 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005517 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005518
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5520 Flag = Chain.getValue(1);
5521
5522 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005524 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005525 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005526 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005527 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005528 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 Flag = Chain.getValue(1);
5530
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005531 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005532 DAG.getIntPtrConstant(0, true),
5533 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005534 Flag);
5535
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005537
Dan Gohman8181bd12008-07-27 21:46:04 +00005538 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005539 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540}
5541
Dan Gohman8181bd12008-07-27 21:46:04 +00005542SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005543X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005544 SDValue Chain,
5545 SDValue Dst, SDValue Src,
5546 SDValue Size, unsigned Align,
5547 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005548 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005549 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005550
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005551 // If not DWORD aligned or size is more than the threshold, call the library.
5552 // The libc version is likely to be faster for these cases. It can use the
5553 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005554 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005555 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005556 ConstantSize->getZExtValue() >
5557 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005558 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005559
5560 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005561 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005562
Bill Wendling4b2e3782008-10-01 00:59:58 +00005563 if (const char *bzeroEntry = V &&
5564 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5565 MVT IntPtr = getPointerTy();
5566 const Type *IntPtrTy = TD->getIntPtrType();
5567 TargetLowering::ArgListTy Args;
5568 TargetLowering::ArgListEntry Entry;
5569 Entry.Node = Dst;
5570 Entry.Ty = IntPtrTy;
5571 Args.push_back(Entry);
5572 Entry.Node = Size;
5573 Args.push_back(Entry);
Dale Johannesenca6237b2009-01-30 23:10:59 +00005574 // FIXME provide DebugLoc info
Bill Wendling4b2e3782008-10-01 00:59:58 +00005575 std::pair<SDValue,SDValue> CallResult =
5576 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5577 CallingConv::C, false,
Dale Johannesenca6237b2009-01-30 23:10:59 +00005578 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG,
5579 DebugLoc::getUnknownLoc());
Bill Wendling4b2e3782008-10-01 00:59:58 +00005580 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005581 }
5582
Dan Gohmane8b391e2008-04-12 04:36:06 +00005583 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005584 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005585 }
5586
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005587 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005588 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005589 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005590 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005591 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 unsigned BytesLeft = 0;
5593 bool TwoRepStos = false;
5594 if (ValC) {
5595 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005596 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597
5598 // If the value is a constant, then we can potentially use larger sets.
5599 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005600 case 2: // WORD aligned
5601 AVT = MVT::i16;
5602 ValReg = X86::AX;
5603 Val = (Val << 8) | Val;
5604 break;
5605 case 0: // DWORD aligned
5606 AVT = MVT::i32;
5607 ValReg = X86::EAX;
5608 Val = (Val << 8) | Val;
5609 Val = (Val << 16) | Val;
5610 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5611 AVT = MVT::i64;
5612 ValReg = X86::RAX;
5613 Val = (Val << 32) | Val;
5614 }
5615 break;
5616 default: // Byte aligned
5617 AVT = MVT::i8;
5618 ValReg = X86::AL;
5619 Count = DAG.getIntPtrConstant(SizeVal);
5620 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 }
5622
Duncan Sandsec142ee2008-06-08 20:54:56 +00005623 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005624 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005625 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5626 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627 }
5628
5629 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5630 InFlag);
5631 InFlag = Chain.getValue(1);
5632 } else {
5633 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005634 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005635 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 InFlag = Chain.getValue(1);
5637 }
5638
5639 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5640 Count, InFlag);
5641 InFlag = Chain.getValue(1);
5642 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005643 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 InFlag = Chain.getValue(1);
5645
5646 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005647 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648 Ops.push_back(Chain);
5649 Ops.push_back(DAG.getValueType(AVT));
5650 Ops.push_back(InFlag);
5651 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5652
5653 if (TwoRepStos) {
5654 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005655 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005656 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005657 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5659 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5660 Left, InFlag);
5661 InFlag = Chain.getValue(1);
5662 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5663 Ops.clear();
5664 Ops.push_back(Chain);
5665 Ops.push_back(DAG.getValueType(MVT::i8));
5666 Ops.push_back(InFlag);
5667 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5668 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005669 // Handle the last 1 - 7 bytes.
5670 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005671 MVT AddrVT = Dst.getValueType();
5672 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005673
5674 Chain = DAG.getMemset(Chain,
5675 DAG.getNode(ISD::ADD, AddrVT, Dst,
5676 DAG.getConstant(Offset, AddrVT)),
5677 Src,
5678 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005679 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005680 }
5681
Dan Gohmane8b391e2008-04-12 04:36:06 +00005682 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005683 return Chain;
5684}
5685
Dan Gohman8181bd12008-07-27 21:46:04 +00005686SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005687X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005688 SDValue Chain, SDValue Dst, SDValue Src,
5689 SDValue Size, unsigned Align,
5690 bool AlwaysInline,
5691 const Value *DstSV, uint64_t DstSVOff,
5692 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005693 // This requires the copy size to be a constant, preferrably
5694 // within a subtarget-specific limit.
5695 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5696 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005697 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005698 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005699 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005700 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005701
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005702 /// If not DWORD aligned, call the library.
5703 if ((Align & 3) != 0)
5704 return SDValue();
5705
5706 // DWORD aligned
5707 MVT AVT = MVT::i32;
5708 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005709 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710
Duncan Sands92c43912008-06-06 12:08:01 +00005711 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005712 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005713 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005714 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005715
Dan Gohman8181bd12008-07-27 21:46:04 +00005716 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005717 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5718 Count, InFlag);
5719 InFlag = Chain.getValue(1);
5720 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005721 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005722 InFlag = Chain.getValue(1);
5723 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005724 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005725 InFlag = Chain.getValue(1);
5726
5727 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005728 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005729 Ops.push_back(Chain);
5730 Ops.push_back(DAG.getValueType(AVT));
5731 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005732 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005733
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005735 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005736 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005737 // Handle the last 1 - 7 bytes.
5738 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005739 MVT DstVT = Dst.getValueType();
5740 MVT SrcVT = Src.getValueType();
5741 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005742 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005743 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005744 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005745 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005746 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005747 DAG.getConstant(BytesLeft, SizeVT),
5748 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005749 DstSV, DstSVOff + Offset,
5750 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005751 }
5752
Dan Gohmane8b391e2008-04-12 04:36:06 +00005753 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754}
5755
Dan Gohman8181bd12008-07-27 21:46:04 +00005756SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005757 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005758
5759 if (!Subtarget->is64Bit()) {
5760 // vastart just stores the address of the VarArgsFrameIndex slot into the
5761 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005762 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005763 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 }
5765
5766 // __va_list_tag:
5767 // gp_offset (0 - 6 * 8)
5768 // fp_offset (48 - 48 + 8 * 16)
5769 // overflow_arg_area (point to parameters coming in memory).
5770 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005771 SmallVector<SDValue, 8> MemOps;
5772 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005773 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005774 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005776 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005777 MemOps.push_back(Store);
5778
5779 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005780 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005781 Store = DAG.getStore(Op.getOperand(0),
5782 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005783 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005784 MemOps.push_back(Store);
5785
5786 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005787 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005788 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005789 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005790 MemOps.push_back(Store);
5791
5792 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005793 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005794 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005795 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005796 MemOps.push_back(Store);
5797 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5798}
5799
Dan Gohman8181bd12008-07-27 21:46:04 +00005800SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005801 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5802 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005803 SDValue Chain = Op.getOperand(0);
5804 SDValue SrcPtr = Op.getOperand(1);
5805 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005806
5807 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5808 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005809 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005810}
5811
Dan Gohman8181bd12008-07-27 21:46:04 +00005812SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005813 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005814 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005815 SDValue Chain = Op.getOperand(0);
5816 SDValue DstPtr = Op.getOperand(1);
5817 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005818 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5819 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820
Dan Gohman840ff5c2008-04-18 20:55:41 +00005821 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5822 DAG.getIntPtrConstant(24), 8, false,
5823 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005824}
5825
Dan Gohman8181bd12008-07-27 21:46:04 +00005826SDValue
5827X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005828 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005829 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005830 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005831 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 case Intrinsic::x86_sse_comieq_ss:
5833 case Intrinsic::x86_sse_comilt_ss:
5834 case Intrinsic::x86_sse_comile_ss:
5835 case Intrinsic::x86_sse_comigt_ss:
5836 case Intrinsic::x86_sse_comige_ss:
5837 case Intrinsic::x86_sse_comineq_ss:
5838 case Intrinsic::x86_sse_ucomieq_ss:
5839 case Intrinsic::x86_sse_ucomilt_ss:
5840 case Intrinsic::x86_sse_ucomile_ss:
5841 case Intrinsic::x86_sse_ucomigt_ss:
5842 case Intrinsic::x86_sse_ucomige_ss:
5843 case Intrinsic::x86_sse_ucomineq_ss:
5844 case Intrinsic::x86_sse2_comieq_sd:
5845 case Intrinsic::x86_sse2_comilt_sd:
5846 case Intrinsic::x86_sse2_comile_sd:
5847 case Intrinsic::x86_sse2_comigt_sd:
5848 case Intrinsic::x86_sse2_comige_sd:
5849 case Intrinsic::x86_sse2_comineq_sd:
5850 case Intrinsic::x86_sse2_ucomieq_sd:
5851 case Intrinsic::x86_sse2_ucomilt_sd:
5852 case Intrinsic::x86_sse2_ucomile_sd:
5853 case Intrinsic::x86_sse2_ucomigt_sd:
5854 case Intrinsic::x86_sse2_ucomige_sd:
5855 case Intrinsic::x86_sse2_ucomineq_sd: {
5856 unsigned Opc = 0;
5857 ISD::CondCode CC = ISD::SETCC_INVALID;
5858 switch (IntNo) {
5859 default: break;
5860 case Intrinsic::x86_sse_comieq_ss:
5861 case Intrinsic::x86_sse2_comieq_sd:
5862 Opc = X86ISD::COMI;
5863 CC = ISD::SETEQ;
5864 break;
5865 case Intrinsic::x86_sse_comilt_ss:
5866 case Intrinsic::x86_sse2_comilt_sd:
5867 Opc = X86ISD::COMI;
5868 CC = ISD::SETLT;
5869 break;
5870 case Intrinsic::x86_sse_comile_ss:
5871 case Intrinsic::x86_sse2_comile_sd:
5872 Opc = X86ISD::COMI;
5873 CC = ISD::SETLE;
5874 break;
5875 case Intrinsic::x86_sse_comigt_ss:
5876 case Intrinsic::x86_sse2_comigt_sd:
5877 Opc = X86ISD::COMI;
5878 CC = ISD::SETGT;
5879 break;
5880 case Intrinsic::x86_sse_comige_ss:
5881 case Intrinsic::x86_sse2_comige_sd:
5882 Opc = X86ISD::COMI;
5883 CC = ISD::SETGE;
5884 break;
5885 case Intrinsic::x86_sse_comineq_ss:
5886 case Intrinsic::x86_sse2_comineq_sd:
5887 Opc = X86ISD::COMI;
5888 CC = ISD::SETNE;
5889 break;
5890 case Intrinsic::x86_sse_ucomieq_ss:
5891 case Intrinsic::x86_sse2_ucomieq_sd:
5892 Opc = X86ISD::UCOMI;
5893 CC = ISD::SETEQ;
5894 break;
5895 case Intrinsic::x86_sse_ucomilt_ss:
5896 case Intrinsic::x86_sse2_ucomilt_sd:
5897 Opc = X86ISD::UCOMI;
5898 CC = ISD::SETLT;
5899 break;
5900 case Intrinsic::x86_sse_ucomile_ss:
5901 case Intrinsic::x86_sse2_ucomile_sd:
5902 Opc = X86ISD::UCOMI;
5903 CC = ISD::SETLE;
5904 break;
5905 case Intrinsic::x86_sse_ucomigt_ss:
5906 case Intrinsic::x86_sse2_ucomigt_sd:
5907 Opc = X86ISD::UCOMI;
5908 CC = ISD::SETGT;
5909 break;
5910 case Intrinsic::x86_sse_ucomige_ss:
5911 case Intrinsic::x86_sse2_ucomige_sd:
5912 Opc = X86ISD::UCOMI;
5913 CC = ISD::SETGE;
5914 break;
5915 case Intrinsic::x86_sse_ucomineq_ss:
5916 case Intrinsic::x86_sse2_ucomineq_sd:
5917 Opc = X86ISD::UCOMI;
5918 CC = ISD::SETNE;
5919 break;
5920 }
5921
Dan Gohman8181bd12008-07-27 21:46:04 +00005922 SDValue LHS = Op.getOperand(1);
5923 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005924 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8181bd12008-07-27 21:46:04 +00005925 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5926 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005927 DAG.getConstant(X86CC, MVT::i8), Cond);
5928 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005929 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005930
5931 // Fix vector shift instructions where the last operand is a non-immediate
5932 // i32 value.
5933 case Intrinsic::x86_sse2_pslli_w:
5934 case Intrinsic::x86_sse2_pslli_d:
5935 case Intrinsic::x86_sse2_pslli_q:
5936 case Intrinsic::x86_sse2_psrli_w:
5937 case Intrinsic::x86_sse2_psrli_d:
5938 case Intrinsic::x86_sse2_psrli_q:
5939 case Intrinsic::x86_sse2_psrai_w:
5940 case Intrinsic::x86_sse2_psrai_d:
5941 case Intrinsic::x86_mmx_pslli_w:
5942 case Intrinsic::x86_mmx_pslli_d:
5943 case Intrinsic::x86_mmx_pslli_q:
5944 case Intrinsic::x86_mmx_psrli_w:
5945 case Intrinsic::x86_mmx_psrli_d:
5946 case Intrinsic::x86_mmx_psrli_q:
5947 case Intrinsic::x86_mmx_psrai_w:
5948 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005949 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005950 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005951 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005952
5953 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005954 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005955 switch (IntNo) {
5956 case Intrinsic::x86_sse2_pslli_w:
5957 NewIntNo = Intrinsic::x86_sse2_psll_w;
5958 break;
5959 case Intrinsic::x86_sse2_pslli_d:
5960 NewIntNo = Intrinsic::x86_sse2_psll_d;
5961 break;
5962 case Intrinsic::x86_sse2_pslli_q:
5963 NewIntNo = Intrinsic::x86_sse2_psll_q;
5964 break;
5965 case Intrinsic::x86_sse2_psrli_w:
5966 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5967 break;
5968 case Intrinsic::x86_sse2_psrli_d:
5969 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5970 break;
5971 case Intrinsic::x86_sse2_psrli_q:
5972 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5973 break;
5974 case Intrinsic::x86_sse2_psrai_w:
5975 NewIntNo = Intrinsic::x86_sse2_psra_w;
5976 break;
5977 case Intrinsic::x86_sse2_psrai_d:
5978 NewIntNo = Intrinsic::x86_sse2_psra_d;
5979 break;
5980 default: {
5981 ShAmtVT = MVT::v2i32;
5982 switch (IntNo) {
5983 case Intrinsic::x86_mmx_pslli_w:
5984 NewIntNo = Intrinsic::x86_mmx_psll_w;
5985 break;
5986 case Intrinsic::x86_mmx_pslli_d:
5987 NewIntNo = Intrinsic::x86_mmx_psll_d;
5988 break;
5989 case Intrinsic::x86_mmx_pslli_q:
5990 NewIntNo = Intrinsic::x86_mmx_psll_q;
5991 break;
5992 case Intrinsic::x86_mmx_psrli_w:
5993 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5994 break;
5995 case Intrinsic::x86_mmx_psrli_d:
5996 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5997 break;
5998 case Intrinsic::x86_mmx_psrli_q:
5999 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6000 break;
6001 case Intrinsic::x86_mmx_psrai_w:
6002 NewIntNo = Intrinsic::x86_mmx_psra_w;
6003 break;
6004 case Intrinsic::x86_mmx_psrai_d:
6005 NewIntNo = Intrinsic::x86_mmx_psra_d;
6006 break;
6007 default: abort(); // Can't reach here.
6008 }
6009 break;
6010 }
6011 }
Duncan Sands92c43912008-06-06 12:08:01 +00006012 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006013 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
6014 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
6015 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6016 DAG.getConstant(NewIntNo, MVT::i32),
6017 Op.getOperand(1), ShAmt);
6018 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 }
6020}
6021
Dan Gohman8181bd12008-07-27 21:46:04 +00006022SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006023 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6024
6025 if (Depth > 0) {
6026 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6027 SDValue Offset =
6028 DAG.getConstant(TD->getPointerSize(),
6029 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
6030 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(),
6031 DAG.getNode(ISD::ADD, getPointerTy(), FrameAddr, Offset),
6032 NULL, 0);
6033 }
6034
6035 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006036 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006037 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
6038}
6039
Dan Gohman8181bd12008-07-27 21:46:04 +00006040SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006041 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6042 MFI->setFrameAddressIsTaken(true);
6043 MVT VT = Op.getValueType();
6044 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6045 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
6046 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
6047 while (Depth--)
6048 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
6049 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050}
6051
Dan Gohman8181bd12008-07-27 21:46:04 +00006052SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006053 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006054 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006055}
6056
Dan Gohman8181bd12008-07-27 21:46:04 +00006057SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006058{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006059 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006060 SDValue Chain = Op.getOperand(0);
6061 SDValue Offset = Op.getOperand(1);
6062 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006063
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006064 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6065 getPointerTy());
6066 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006067
Dan Gohman8181bd12008-07-27 21:46:04 +00006068 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006069 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006070 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
6071 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006072 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
6073 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006074
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006075 return DAG.getNode(X86ISD::EH_RETURN,
6076 MVT::Other,
6077 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006078}
6079
Dan Gohman8181bd12008-07-27 21:46:04 +00006080SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006081 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006082 SDValue Root = Op.getOperand(0);
6083 SDValue Trmp = Op.getOperand(1); // trampoline
6084 SDValue FPtr = Op.getOperand(2); // nested function
6085 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006086
Dan Gohman12a9c082008-02-06 22:27:42 +00006087 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006088
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006089 const X86InstrInfo *TII =
6090 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6091
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006092 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006093 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006094
6095 // Large code-model.
6096
6097 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6098 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6099
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006100 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6101 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006102
6103 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6104
6105 // Load the pointer to the nested function into R11.
6106 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006107 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006108 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006109 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006110
6111 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006112 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006113
6114 // Load the 'nest' parameter value into R10.
6115 // R10 is specified in X86CallingConv.td
6116 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
6117 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
6118 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006119 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006120
6121 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00006122 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006123
6124 // Jump to the nested function.
6125 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
6126 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
6127 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006128 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006129
6130 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
6131 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
6132 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006133 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006134
Dan Gohman8181bd12008-07-27 21:46:04 +00006135 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006136 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00006137 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006138 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006139 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006140 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6141 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006142 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006143
6144 switch (CC) {
6145 default:
6146 assert(0 && "Unsupported calling convention");
6147 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006148 case CallingConv::X86_StdCall: {
6149 // Pass 'nest' parameter in ECX.
6150 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006151 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006152
6153 // Check that ECX wasn't needed by an 'inreg' parameter.
6154 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006155 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006156
Chris Lattner1c8733e2008-03-12 17:45:29 +00006157 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006158 unsigned InRegCount = 0;
6159 unsigned Idx = 1;
6160
6161 for (FunctionType::param_iterator I = FTy->param_begin(),
6162 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006163 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006164 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006165 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006166
6167 if (InRegCount > 2) {
6168 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6169 abort();
6170 }
6171 }
6172 break;
6173 }
6174 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006175 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006176 // Pass 'nest' parameter in EAX.
6177 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006178 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006179 break;
6180 }
6181
Dan Gohman8181bd12008-07-27 21:46:04 +00006182 SDValue OutChains[4];
6183 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006184
6185 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6186 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6187
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006188 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006189 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006190 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006191 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006192
6193 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006194 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006195
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006196 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006197 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6198 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006199 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006200
6201 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006202 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006203
Dan Gohman8181bd12008-07-27 21:46:04 +00006204 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006205 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006206 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006207 }
6208}
6209
Dan Gohman8181bd12008-07-27 21:46:04 +00006210SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006211 /*
6212 The rounding mode is in bits 11:10 of FPSR, and has the following
6213 settings:
6214 00 Round to nearest
6215 01 Round to -inf
6216 10 Round to +inf
6217 11 Round to 0
6218
6219 FLT_ROUNDS, on the other hand, expects the following:
6220 -1 Undefined
6221 0 Round to 0
6222 1 Round to nearest
6223 2 Round to +inf
6224 3 Round to -inf
6225
6226 To perform the conversion, we do:
6227 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6228 */
6229
6230 MachineFunction &MF = DAG.getMachineFunction();
6231 const TargetMachine &TM = MF.getTarget();
6232 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6233 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006234 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006235
6236 // Save FP Control Word to stack slot
6237 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006238 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006239
Dan Gohman8181bd12008-07-27 21:46:04 +00006240 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006241 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006242
6243 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006244 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006245
6246 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006247 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006248 DAG.getNode(ISD::SRL, MVT::i16,
6249 DAG.getNode(ISD::AND, MVT::i16,
6250 CWD, DAG.getConstant(0x800, MVT::i16)),
6251 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006252 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006253 DAG.getNode(ISD::SRL, MVT::i16,
6254 DAG.getNode(ISD::AND, MVT::i16,
6255 CWD, DAG.getConstant(0x400, MVT::i16)),
6256 DAG.getConstant(9, MVT::i8));
6257
Dan Gohman8181bd12008-07-27 21:46:04 +00006258 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006259 DAG.getNode(ISD::AND, MVT::i16,
6260 DAG.getNode(ISD::ADD, MVT::i16,
6261 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6262 DAG.getConstant(1, MVT::i16)),
6263 DAG.getConstant(3, MVT::i16));
6264
6265
Duncan Sands92c43912008-06-06 12:08:01 +00006266 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006267 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6268}
6269
Dan Gohman8181bd12008-07-27 21:46:04 +00006270SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006271 MVT VT = Op.getValueType();
6272 MVT OpVT = VT;
6273 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006274
6275 Op = Op.getOperand(0);
6276 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006277 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006278 OpVT = MVT::i32;
6279 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6280 }
Evan Cheng48679f42007-12-14 02:13:44 +00006281
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006282 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6283 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6284 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6285
6286 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006287 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006288 Ops.push_back(Op);
6289 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6290 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6291 Ops.push_back(Op.getValue(1));
6292 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6293
6294 // Finally xor with NumBits-1.
6295 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6296
Evan Cheng48679f42007-12-14 02:13:44 +00006297 if (VT == MVT::i8)
6298 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6299 return Op;
6300}
6301
Dan Gohman8181bd12008-07-27 21:46:04 +00006302SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006303 MVT VT = Op.getValueType();
6304 MVT OpVT = VT;
6305 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006306
6307 Op = Op.getOperand(0);
6308 if (VT == MVT::i8) {
6309 OpVT = MVT::i32;
6310 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6311 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006312
6313 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6314 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6315 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6316
6317 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006318 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006319 Ops.push_back(Op);
6320 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6321 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6322 Ops.push_back(Op.getValue(1));
6323 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6324
Evan Cheng48679f42007-12-14 02:13:44 +00006325 if (VT == MVT::i8)
6326 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6327 return Op;
6328}
6329
Mon P Wang14edb092008-12-18 21:42:19 +00006330SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6331 MVT VT = Op.getValueType();
6332 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
6333
6334 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6335 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6336 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6337 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6338 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6339 //
6340 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6341 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6342 // return AloBlo + AloBhi + AhiBlo;
6343
6344 SDValue A = Op.getOperand(0);
6345 SDValue B = Op.getOperand(1);
6346
6347 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6348 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6349 A, DAG.getConstant(32, MVT::i32));
6350 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6351 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6352 B, DAG.getConstant(32, MVT::i32));
6353 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6354 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6355 A, B);
6356 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6357 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6358 A, Bhi);
6359 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6360 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6361 Ahi, B);
6362 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6363 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6364 AloBhi, DAG.getConstant(32, MVT::i32));
6365 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6366 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6367 AhiBlo, DAG.getConstant(32, MVT::i32));
6368 SDValue Res = DAG.getNode(ISD::ADD, VT, AloBlo, AloBhi);
6369 Res = DAG.getNode(ISD::ADD, VT, Res, AhiBlo);
6370 return Res;
6371}
6372
6373
Bill Wendling7e04be62008-12-09 22:08:41 +00006374SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6375 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6376 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006377 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6378 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006379 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006380 SDValue LHS = N->getOperand(0);
6381 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006382 unsigned BaseOp = 0;
6383 unsigned Cond = 0;
6384
6385 switch (Op.getOpcode()) {
6386 default: assert(0 && "Unknown ovf instruction!");
6387 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006388 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006389 Cond = X86::COND_O;
6390 break;
6391 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006392 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006393 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006394 break;
6395 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006396 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006397 Cond = X86::COND_O;
6398 break;
6399 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006400 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006401 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006402 break;
6403 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006404 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006405 Cond = X86::COND_O;
6406 break;
6407 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006408 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006409 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006410 break;
6411 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006412
Bill Wendlingd3511522008-12-02 01:06:39 +00006413 // Also sets EFLAGS.
6414 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006415 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006416
Bill Wendlingd3511522008-12-02 01:06:39 +00006417 SDValue SetCC =
6418 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006419 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006420
Bill Wendlingd3511522008-12-02 01:06:39 +00006421 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6422 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006423}
6424
Dan Gohman8181bd12008-07-27 21:46:04 +00006425SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006426 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006427 unsigned Reg = 0;
6428 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006429 switch(T.getSimpleVT()) {
6430 default:
6431 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006432 case MVT::i8: Reg = X86::AL; size = 1; break;
6433 case MVT::i16: Reg = X86::AX; size = 2; break;
6434 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006435 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006436 assert(Subtarget->is64Bit() && "Node not type legal!");
6437 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006438 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006439 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006440 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006441 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006442 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006443 Op.getOperand(1),
6444 Op.getOperand(3),
6445 DAG.getTargetConstant(size, MVT::i8),
6446 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006447 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006448 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6449 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006450 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6451 return cpOut;
6452}
6453
Duncan Sands7d9834b2008-12-01 11:39:25 +00006454SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006455 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006456 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006457 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006458 SDValue TheChain = Op.getOperand(0);
6459 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6460 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6461 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6462 rax.getValue(2));
6463 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6464 DAG.getConstant(32, MVT::i8));
6465 SDValue Ops[] = {
6466 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6467 rdx.getValue(1)
6468 };
6469 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006470}
6471
Dale Johannesen9011d872008-09-29 22:25:26 +00006472SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6473 SDNode *Node = Op.getNode();
6474 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006475 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006476 DAG.getConstant(0, T), Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006477 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD,
6478 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006479 Node->getOperand(0),
6480 Node->getOperand(1), negOp,
6481 cast<AtomicSDNode>(Node)->getSrcValue(),
6482 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006483}
6484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006485/// LowerOperation - Provide custom lowering hooks for some operations.
6486///
Dan Gohman8181bd12008-07-27 21:46:04 +00006487SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006488 switch (Op.getOpcode()) {
6489 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006490 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6491 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006492 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6493 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6494 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6495 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6496 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6497 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6498 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6499 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006500 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006501 case ISD::SHL_PARTS:
6502 case ISD::SRA_PARTS:
6503 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6504 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006505 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006506 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6507 case ISD::FABS: return LowerFABS(Op, DAG);
6508 case ISD::FNEG: return LowerFNEG(Op, DAG);
6509 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006510 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006511 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006512 case ISD::SELECT: return LowerSELECT(Op, DAG);
6513 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6515 case ISD::CALL: return LowerCALL(Op, DAG);
6516 case ISD::RET: return LowerRET(Op, DAG);
6517 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006519 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006520 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6521 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6522 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6523 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6524 case ISD::FRAME_TO_ARGS_OFFSET:
6525 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6526 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6527 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006528 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006529 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006530 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6531 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006532 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006533 case ISD::SADDO:
6534 case ISD::UADDO:
6535 case ISD::SSUBO:
6536 case ISD::USUBO:
6537 case ISD::SMULO:
6538 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006539 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006540 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006541}
6542
Duncan Sands7d9834b2008-12-01 11:39:25 +00006543void X86TargetLowering::
6544ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6545 SelectionDAG &DAG, unsigned NewOp) {
6546 MVT T = Node->getValueType(0);
6547 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6548
6549 SDValue Chain = Node->getOperand(0);
6550 SDValue In1 = Node->getOperand(1);
6551 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6552 Node->getOperand(2), DAG.getIntPtrConstant(0));
6553 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6554 Node->getOperand(2), DAG.getIntPtrConstant(1));
6555 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6556 // have a MemOperand. Pass the info through as a normal operand.
6557 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6558 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6559 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6560 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6561 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6562 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6563 Results.push_back(Result.getValue(2));
6564}
6565
Duncan Sandsac496a12008-07-04 11:47:58 +00006566/// ReplaceNodeResults - Replace a node with an illegal result type
6567/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006568void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6569 SmallVectorImpl<SDValue>&Results,
6570 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006571 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006572 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006573 assert(false && "Do not know how to custom type legalize this operation!");
6574 return;
6575 case ISD::FP_TO_SINT: {
6576 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6577 SDValue FIST = Vals.first, StackSlot = Vals.second;
6578 if (FIST.getNode() != 0) {
6579 MVT VT = N->getValueType(0);
6580 // Return a load from the stack slot.
6581 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6582 }
6583 return;
6584 }
6585 case ISD::READCYCLECOUNTER: {
6586 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6587 SDValue TheChain = N->getOperand(0);
6588 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6589 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6590 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6591 eax.getValue(2));
6592 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6593 SDValue Ops[] = { eax, edx };
6594 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6595 Results.push_back(edx.getValue(1));
6596 return;
6597 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006598 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006599 MVT T = N->getValueType(0);
6600 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6601 SDValue cpInL, cpInH;
6602 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6603 DAG.getConstant(0, MVT::i32));
6604 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6605 DAG.getConstant(1, MVT::i32));
6606 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6607 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6608 cpInL.getValue(1));
6609 SDValue swapInL, swapInH;
6610 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6611 DAG.getConstant(0, MVT::i32));
6612 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6613 DAG.getConstant(1, MVT::i32));
6614 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6615 cpInH.getValue(1));
6616 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6617 swapInL.getValue(1));
6618 SDValue Ops[] = { swapInH.getValue(0),
6619 N->getOperand(1),
6620 swapInH.getValue(1) };
6621 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6622 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6623 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6624 Result.getValue(1));
6625 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6626 cpOutL.getValue(2));
6627 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6628 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6629 Results.push_back(cpOutH.getValue(1));
6630 return;
6631 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006632 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006633 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6634 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006635 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006636 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6637 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006638 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006639 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6640 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006641 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006642 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6643 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006644 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006645 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6646 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006647 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006648 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6649 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006650 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006651 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6652 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006653 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006654}
6655
6656const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6657 switch (Opcode) {
6658 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006659 case X86ISD::BSF: return "X86ISD::BSF";
6660 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006661 case X86ISD::SHLD: return "X86ISD::SHLD";
6662 case X86ISD::SHRD: return "X86ISD::SHRD";
6663 case X86ISD::FAND: return "X86ISD::FAND";
6664 case X86ISD::FOR: return "X86ISD::FOR";
6665 case X86ISD::FXOR: return "X86ISD::FXOR";
6666 case X86ISD::FSRL: return "X86ISD::FSRL";
6667 case X86ISD::FILD: return "X86ISD::FILD";
6668 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6669 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6670 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6671 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6672 case X86ISD::FLD: return "X86ISD::FLD";
6673 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 case X86ISD::CALL: return "X86ISD::CALL";
6675 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6676 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006677 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006678 case X86ISD::CMP: return "X86ISD::CMP";
6679 case X86ISD::COMI: return "X86ISD::COMI";
6680 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6681 case X86ISD::SETCC: return "X86ISD::SETCC";
6682 case X86ISD::CMOV: return "X86ISD::CMOV";
6683 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6684 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6685 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6686 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6688 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006689 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006691 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6692 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006693 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6694 case X86ISD::FMAX: return "X86ISD::FMAX";
6695 case X86ISD::FMIN: return "X86ISD::FMIN";
6696 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6697 case X86ISD::FRCP: return "X86ISD::FRCP";
6698 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6699 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6700 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006701 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006702 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006703 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6704 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006705 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6706 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6707 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6708 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6709 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6710 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006711 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6712 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006713 case X86ISD::VSHL: return "X86ISD::VSHL";
6714 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006715 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6716 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6717 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6718 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6719 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6720 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6721 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6722 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6723 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6724 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006725 case X86ISD::ADD: return "X86ISD::ADD";
6726 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006727 case X86ISD::SMUL: return "X86ISD::SMUL";
6728 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006729 }
6730}
6731
6732// isLegalAddressingMode - Return true if the addressing mode represented
6733// by AM is legal for this target, for a load/store of the specified type.
6734bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6735 const Type *Ty) const {
6736 // X86 supports extremely general addressing modes.
6737
6738 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6739 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6740 return false;
6741
6742 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006743 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006744 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6745 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006746 // If BaseGV requires a register, we cannot also have a BaseReg.
6747 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6748 AM.HasBaseReg)
6749 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006750
6751 // X86-64 only supports addr of globals in small code model.
6752 if (Subtarget->is64Bit()) {
6753 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6754 return false;
6755 // If lower 4G is not available, then we must use rip-relative addressing.
6756 if (AM.BaseOffs || AM.Scale > 1)
6757 return false;
6758 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759 }
6760
6761 switch (AM.Scale) {
6762 case 0:
6763 case 1:
6764 case 2:
6765 case 4:
6766 case 8:
6767 // These scales always work.
6768 break;
6769 case 3:
6770 case 5:
6771 case 9:
6772 // These scales are formed with basereg+scalereg. Only accept if there is
6773 // no basereg yet.
6774 if (AM.HasBaseReg)
6775 return false;
6776 break;
6777 default: // Other stuff never works.
6778 return false;
6779 }
6780
6781 return true;
6782}
6783
6784
Evan Cheng27a820a2007-10-26 01:56:11 +00006785bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6786 if (!Ty1->isInteger() || !Ty2->isInteger())
6787 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006788 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6789 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006790 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006791 return false;
6792 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006793}
6794
Duncan Sands92c43912008-06-06 12:08:01 +00006795bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6796 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006797 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006798 unsigned NumBits1 = VT1.getSizeInBits();
6799 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006800 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006801 return false;
6802 return Subtarget->is64Bit() || NumBits1 < 64;
6803}
Evan Cheng27a820a2007-10-26 01:56:11 +00006804
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006805/// isShuffleMaskLegal - Targets can use this to indicate that they only
6806/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6807/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6808/// are assumed to be legal.
6809bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006810X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006811 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006812 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006813 return (Mask.getNode()->getNumOperands() <= 4 ||
6814 isIdentityMask(Mask.getNode()) ||
6815 isIdentityMask(Mask.getNode(), true) ||
6816 isSplatMask(Mask.getNode()) ||
6817 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6818 X86::isUNPCKLMask(Mask.getNode()) ||
6819 X86::isUNPCKHMask(Mask.getNode()) ||
6820 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6821 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006822}
6823
Dan Gohman48d5f062008-04-09 20:09:42 +00006824bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006825X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006826 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006827 unsigned NumElts = BVOps.size();
6828 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006829 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 if (NumElts == 2) return true;
6831 if (NumElts == 4) {
6832 return (isMOVLMask(&BVOps[0], 4) ||
6833 isCommutedMOVL(&BVOps[0], 4, true) ||
6834 isSHUFPMask(&BVOps[0], 4) ||
6835 isCommutedSHUFP(&BVOps[0], 4));
6836 }
6837 return false;
6838}
6839
6840//===----------------------------------------------------------------------===//
6841// X86 Scheduler Hooks
6842//===----------------------------------------------------------------------===//
6843
Mon P Wang078a62d2008-05-05 19:05:59 +00006844// private utility function
6845MachineBasicBlock *
6846X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6847 MachineBasicBlock *MBB,
6848 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006849 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006850 unsigned LoadOpc,
6851 unsigned CXchgOpc,
6852 unsigned copyOpc,
6853 unsigned notOpc,
6854 unsigned EAXreg,
6855 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006856 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006857 // For the atomic bitwise operator, we generate
6858 // thisMBB:
6859 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006860 // ld t1 = [bitinstr.addr]
6861 // op t2 = t1, [bitinstr.val]
6862 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006863 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6864 // bz newMBB
6865 // fallthrough -->nextMBB
6866 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6867 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006868 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006869 ++MBBIter;
6870
6871 /// First build the CFG
6872 MachineFunction *F = MBB->getParent();
6873 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006874 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6875 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6876 F->insert(MBBIter, newMBB);
6877 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006878
6879 // Move all successors to thisMBB to nextMBB
6880 nextMBB->transferSuccessors(thisMBB);
6881
6882 // Update thisMBB to fall through to newMBB
6883 thisMBB->addSuccessor(newMBB);
6884
6885 // newMBB jumps to itself and fall through to nextMBB
6886 newMBB->addSuccessor(nextMBB);
6887 newMBB->addSuccessor(newMBB);
6888
6889 // Insert instructions into newMBB based on incoming instruction
6890 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6891 MachineOperand& destOper = bInstr->getOperand(0);
6892 MachineOperand* argOpers[6];
6893 int numArgs = bInstr->getNumOperands() - 1;
6894 for (int i=0; i < numArgs; ++i)
6895 argOpers[i] = &bInstr->getOperand(i+1);
6896
6897 // x86 address has 4 operands: base, index, scale, and displacement
6898 int lastAddrIndx = 3; // [0,3]
6899 int valArgIndx = 4;
6900
Dale Johannesend20e4452008-08-19 18:47:28 +00006901 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6902 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006903 for (int i=0; i <= lastAddrIndx; ++i)
6904 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006905
Dale Johannesend20e4452008-08-19 18:47:28 +00006906 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006907 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006908 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006909 }
6910 else
6911 tt = t1;
6912
Dale Johannesend20e4452008-08-19 18:47:28 +00006913 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006914 assert((argOpers[valArgIndx]->isReg() ||
6915 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006916 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006917 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006918 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6919 else
6920 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006921 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006922 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006923
Dale Johannesend20e4452008-08-19 18:47:28 +00006924 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006925 MIB.addReg(t1);
6926
Dale Johannesend20e4452008-08-19 18:47:28 +00006927 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006928 for (int i=0; i <= lastAddrIndx; ++i)
6929 (*MIB).addOperand(*argOpers[i]);
6930 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006931 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6932 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6933
Dale Johannesend20e4452008-08-19 18:47:28 +00006934 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6935 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006936
6937 // insert branch
6938 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6939
Dan Gohman221a4372008-07-07 23:14:23 +00006940 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006941 return nextMBB;
6942}
6943
Dale Johannesen44eb5372008-10-03 19:41:08 +00006944// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006945MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006946X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6947 MachineBasicBlock *MBB,
6948 unsigned regOpcL,
6949 unsigned regOpcH,
6950 unsigned immOpcL,
6951 unsigned immOpcH,
6952 bool invSrc) {
6953 // For the atomic bitwise operator, we generate
6954 // thisMBB (instructions are in pairs, except cmpxchg8b)
6955 // ld t1,t2 = [bitinstr.addr]
6956 // newMBB:
6957 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6958 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006959 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006960 // mov ECX, EBX <- t5, t6
6961 // mov EAX, EDX <- t1, t2
6962 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6963 // mov t3, t4 <- EAX, EDX
6964 // bz newMBB
6965 // result in out1, out2
6966 // fallthrough -->nextMBB
6967
6968 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6969 const unsigned LoadOpc = X86::MOV32rm;
6970 const unsigned copyOpc = X86::MOV32rr;
6971 const unsigned NotOpc = X86::NOT32r;
6972 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6973 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6974 MachineFunction::iterator MBBIter = MBB;
6975 ++MBBIter;
6976
6977 /// First build the CFG
6978 MachineFunction *F = MBB->getParent();
6979 MachineBasicBlock *thisMBB = MBB;
6980 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6981 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6982 F->insert(MBBIter, newMBB);
6983 F->insert(MBBIter, nextMBB);
6984
6985 // Move all successors to thisMBB to nextMBB
6986 nextMBB->transferSuccessors(thisMBB);
6987
6988 // Update thisMBB to fall through to newMBB
6989 thisMBB->addSuccessor(newMBB);
6990
6991 // newMBB jumps to itself and fall through to nextMBB
6992 newMBB->addSuccessor(nextMBB);
6993 newMBB->addSuccessor(newMBB);
6994
6995 // Insert instructions into newMBB based on incoming instruction
6996 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6997 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6998 MachineOperand& dest1Oper = bInstr->getOperand(0);
6999 MachineOperand& dest2Oper = bInstr->getOperand(1);
7000 MachineOperand* argOpers[6];
7001 for (int i=0; i < 6; ++i)
7002 argOpers[i] = &bInstr->getOperand(i+2);
7003
7004 // x86 address has 4 operands: base, index, scale, and displacement
7005 int lastAddrIndx = 3; // [0,3]
7006
7007 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
7008 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
7009 for (int i=0; i <= lastAddrIndx; ++i)
7010 (*MIB).addOperand(*argOpers[i]);
7011 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
7012 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007013 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00007014 for (int i=0; i <= lastAddrIndx-1; ++i)
7015 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007016 MachineOperand newOp3 = *(argOpers[3]);
7017 if (newOp3.isImm())
7018 newOp3.setImm(newOp3.getImm()+4);
7019 else
7020 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007021 (*MIB).addOperand(newOp3);
7022
7023 // t3/4 are defined later, at the bottom of the loop
7024 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7025 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
7026 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
7027 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
7028 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
7029 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7030
7031 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7032 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
7033 if (invSrc) {
7034 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
7035 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
7036 } else {
7037 tt1 = t1;
7038 tt2 = t2;
7039 }
7040
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007041 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007042 "invalid operand");
7043 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7044 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007045 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007046 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
7047 else
7048 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007049 if (regOpcL != X86::MOV32rr)
7050 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007051 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007052 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
7053 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
7054 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007055 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
7056 else
7057 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007058 if (regOpcH != X86::MOV32rr)
7059 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00007060 (*MIB).addOperand(*argOpers[5]);
7061
7062 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
7063 MIB.addReg(t1);
7064 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
7065 MIB.addReg(t2);
7066
7067 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
7068 MIB.addReg(t5);
7069 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
7070 MIB.addReg(t6);
7071
7072 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
7073 for (int i=0; i <= lastAddrIndx; ++i)
7074 (*MIB).addOperand(*argOpers[i]);
7075
7076 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7077 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7078
7079 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
7080 MIB.addReg(X86::EAX);
7081 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
7082 MIB.addReg(X86::EDX);
7083
7084 // insert branch
7085 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7086
7087 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7088 return nextMBB;
7089}
7090
7091// private utility function
7092MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007093X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7094 MachineBasicBlock *MBB,
7095 unsigned cmovOpc) {
7096 // For the atomic min/max operator, we generate
7097 // thisMBB:
7098 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007099 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00007100 // mov t2 = [min/max.val]
7101 // cmp t1, t2
7102 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007103 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007104 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7105 // bz newMBB
7106 // fallthrough -->nextMBB
7107 //
7108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7109 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007110 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007111 ++MBBIter;
7112
7113 /// First build the CFG
7114 MachineFunction *F = MBB->getParent();
7115 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007116 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7117 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7118 F->insert(MBBIter, newMBB);
7119 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007120
7121 // Move all successors to thisMBB to nextMBB
7122 nextMBB->transferSuccessors(thisMBB);
7123
7124 // Update thisMBB to fall through to newMBB
7125 thisMBB->addSuccessor(newMBB);
7126
7127 // newMBB jumps to newMBB and fall through to nextMBB
7128 newMBB->addSuccessor(nextMBB);
7129 newMBB->addSuccessor(newMBB);
7130
7131 // Insert instructions into newMBB based on incoming instruction
7132 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
7133 MachineOperand& destOper = mInstr->getOperand(0);
7134 MachineOperand* argOpers[6];
7135 int numArgs = mInstr->getNumOperands() - 1;
7136 for (int i=0; i < numArgs; ++i)
7137 argOpers[i] = &mInstr->getOperand(i+1);
7138
7139 // x86 address has 4 operands: base, index, scale, and displacement
7140 int lastAddrIndx = 3; // [0,3]
7141 int valArgIndx = 4;
7142
Mon P Wang318b0372008-05-05 22:56:23 +00007143 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7144 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007145 for (int i=0; i <= lastAddrIndx; ++i)
7146 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007147
Mon P Wang078a62d2008-05-05 19:05:59 +00007148 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007149 assert((argOpers[valArgIndx]->isReg() ||
7150 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007151 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007152
7153 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007154 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00007155 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7156 else
7157 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7158 (*MIB).addOperand(*argOpers[valArgIndx]);
7159
Mon P Wang318b0372008-05-05 22:56:23 +00007160 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
7161 MIB.addReg(t1);
7162
Mon P Wang078a62d2008-05-05 19:05:59 +00007163 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
7164 MIB.addReg(t1);
7165 MIB.addReg(t2);
7166
7167 // Generate movc
7168 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7169 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
7170 MIB.addReg(t2);
7171 MIB.addReg(t1);
7172
7173 // Cmp and exchange if none has modified the memory location
7174 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
7175 for (int i=0; i <= lastAddrIndx; ++i)
7176 (*MIB).addOperand(*argOpers[i]);
7177 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007178 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7179 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007180
7181 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
7182 MIB.addReg(X86::EAX);
7183
7184 // insert branch
7185 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7186
Dan Gohman221a4372008-07-07 23:14:23 +00007187 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007188 return nextMBB;
7189}
7190
7191
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007193X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7194 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007195 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7196 switch (MI->getOpcode()) {
7197 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007198 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007199 case X86::CMOV_FR32:
7200 case X86::CMOV_FR64:
7201 case X86::CMOV_V4F32:
7202 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007203 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007204 // To "insert" a SELECT_CC instruction, we actually have to insert the
7205 // diamond control-flow pattern. The incoming instruction knows the
7206 // destination vreg to set, the condition code register to branch on, the
7207 // true/false values to select between, and a branch opcode to use.
7208 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007209 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007210 ++It;
7211
7212 // thisMBB:
7213 // ...
7214 // TrueVal = ...
7215 // cmpTY ccX, r1, r2
7216 // bCC copy1MBB
7217 // fallthrough --> copy0MBB
7218 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007219 MachineFunction *F = BB->getParent();
7220 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7221 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007222 unsigned Opc =
7223 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
7224 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007225 F->insert(It, copy0MBB);
7226 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007227 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007228 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007229 sinkMBB->transferSuccessors(BB);
7230
7231 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007232 BB->addSuccessor(copy0MBB);
7233 BB->addSuccessor(sinkMBB);
7234
7235 // copy0MBB:
7236 // %FalseValue = ...
7237 // # fallthrough to sinkMBB
7238 BB = copy0MBB;
7239
7240 // Update machine-CFG edges
7241 BB->addSuccessor(sinkMBB);
7242
7243 // sinkMBB:
7244 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7245 // ...
7246 BB = sinkMBB;
7247 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7248 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7249 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7250
Dan Gohman221a4372008-07-07 23:14:23 +00007251 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007252 return BB;
7253 }
7254
7255 case X86::FP32_TO_INT16_IN_MEM:
7256 case X86::FP32_TO_INT32_IN_MEM:
7257 case X86::FP32_TO_INT64_IN_MEM:
7258 case X86::FP64_TO_INT16_IN_MEM:
7259 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007260 case X86::FP64_TO_INT64_IN_MEM:
7261 case X86::FP80_TO_INT16_IN_MEM:
7262 case X86::FP80_TO_INT32_IN_MEM:
7263 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007264 // Change the floating point control register to use "round towards zero"
7265 // mode when truncating to an integer value.
7266 MachineFunction *F = BB->getParent();
7267 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7268 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7269
7270 // Load the old value of the high byte of the control word...
7271 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007272 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007273 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7274
7275 // Set the high part to be round to zero...
7276 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7277 .addImm(0xC7F);
7278
7279 // Reload the modified control word now...
7280 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7281
7282 // Restore the memory image of control word to original value
7283 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7284 .addReg(OldCW);
7285
7286 // Get the X86 opcode to use.
7287 unsigned Opc;
7288 switch (MI->getOpcode()) {
7289 default: assert(0 && "illegal opcode!");
7290 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7291 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7292 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7293 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7294 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7295 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007296 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7297 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7298 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007299 }
7300
7301 X86AddressMode AM;
7302 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007303 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007304 AM.BaseType = X86AddressMode::RegBase;
7305 AM.Base.Reg = Op.getReg();
7306 } else {
7307 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007308 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309 }
7310 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007311 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007312 AM.Scale = Op.getImm();
7313 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007314 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007315 AM.IndexReg = Op.getImm();
7316 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007317 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007318 AM.GV = Op.getGlobal();
7319 } else {
7320 AM.Disp = Op.getImm();
7321 }
7322 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7323 .addReg(MI->getOperand(4).getReg());
7324
7325 // Reload the original control word now.
7326 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7327
Dan Gohman221a4372008-07-07 23:14:23 +00007328 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007329 return BB;
7330 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007331 case X86::ATOMAND32:
7332 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007333 X86::AND32ri, X86::MOV32rm,
7334 X86::LCMPXCHG32, X86::MOV32rr,
7335 X86::NOT32r, X86::EAX,
7336 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007337 case X86::ATOMOR32:
7338 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007339 X86::OR32ri, X86::MOV32rm,
7340 X86::LCMPXCHG32, X86::MOV32rr,
7341 X86::NOT32r, X86::EAX,
7342 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007343 case X86::ATOMXOR32:
7344 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007345 X86::XOR32ri, X86::MOV32rm,
7346 X86::LCMPXCHG32, X86::MOV32rr,
7347 X86::NOT32r, X86::EAX,
7348 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007349 case X86::ATOMNAND32:
7350 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007351 X86::AND32ri, X86::MOV32rm,
7352 X86::LCMPXCHG32, X86::MOV32rr,
7353 X86::NOT32r, X86::EAX,
7354 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007355 case X86::ATOMMIN32:
7356 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7357 case X86::ATOMMAX32:
7358 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7359 case X86::ATOMUMIN32:
7360 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7361 case X86::ATOMUMAX32:
7362 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007363
7364 case X86::ATOMAND16:
7365 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7366 X86::AND16ri, X86::MOV16rm,
7367 X86::LCMPXCHG16, X86::MOV16rr,
7368 X86::NOT16r, X86::AX,
7369 X86::GR16RegisterClass);
7370 case X86::ATOMOR16:
7371 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7372 X86::OR16ri, X86::MOV16rm,
7373 X86::LCMPXCHG16, X86::MOV16rr,
7374 X86::NOT16r, X86::AX,
7375 X86::GR16RegisterClass);
7376 case X86::ATOMXOR16:
7377 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7378 X86::XOR16ri, X86::MOV16rm,
7379 X86::LCMPXCHG16, X86::MOV16rr,
7380 X86::NOT16r, X86::AX,
7381 X86::GR16RegisterClass);
7382 case X86::ATOMNAND16:
7383 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7384 X86::AND16ri, X86::MOV16rm,
7385 X86::LCMPXCHG16, X86::MOV16rr,
7386 X86::NOT16r, X86::AX,
7387 X86::GR16RegisterClass, true);
7388 case X86::ATOMMIN16:
7389 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7390 case X86::ATOMMAX16:
7391 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7392 case X86::ATOMUMIN16:
7393 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7394 case X86::ATOMUMAX16:
7395 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7396
7397 case X86::ATOMAND8:
7398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7399 X86::AND8ri, X86::MOV8rm,
7400 X86::LCMPXCHG8, X86::MOV8rr,
7401 X86::NOT8r, X86::AL,
7402 X86::GR8RegisterClass);
7403 case X86::ATOMOR8:
7404 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7405 X86::OR8ri, X86::MOV8rm,
7406 X86::LCMPXCHG8, X86::MOV8rr,
7407 X86::NOT8r, X86::AL,
7408 X86::GR8RegisterClass);
7409 case X86::ATOMXOR8:
7410 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7411 X86::XOR8ri, X86::MOV8rm,
7412 X86::LCMPXCHG8, X86::MOV8rr,
7413 X86::NOT8r, X86::AL,
7414 X86::GR8RegisterClass);
7415 case X86::ATOMNAND8:
7416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7417 X86::AND8ri, X86::MOV8rm,
7418 X86::LCMPXCHG8, X86::MOV8rr,
7419 X86::NOT8r, X86::AL,
7420 X86::GR8RegisterClass, true);
7421 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007422 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007423 case X86::ATOMAND64:
7424 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7425 X86::AND64ri32, X86::MOV64rm,
7426 X86::LCMPXCHG64, X86::MOV64rr,
7427 X86::NOT64r, X86::RAX,
7428 X86::GR64RegisterClass);
7429 case X86::ATOMOR64:
7430 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7431 X86::OR64ri32, X86::MOV64rm,
7432 X86::LCMPXCHG64, X86::MOV64rr,
7433 X86::NOT64r, X86::RAX,
7434 X86::GR64RegisterClass);
7435 case X86::ATOMXOR64:
7436 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7437 X86::XOR64ri32, X86::MOV64rm,
7438 X86::LCMPXCHG64, X86::MOV64rr,
7439 X86::NOT64r, X86::RAX,
7440 X86::GR64RegisterClass);
7441 case X86::ATOMNAND64:
7442 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7443 X86::AND64ri32, X86::MOV64rm,
7444 X86::LCMPXCHG64, X86::MOV64rr,
7445 X86::NOT64r, X86::RAX,
7446 X86::GR64RegisterClass, true);
7447 case X86::ATOMMIN64:
7448 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7449 case X86::ATOMMAX64:
7450 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7451 case X86::ATOMUMIN64:
7452 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7453 case X86::ATOMUMAX64:
7454 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007455
7456 // This group does 64-bit operations on a 32-bit host.
7457 case X86::ATOMAND6432:
7458 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7459 X86::AND32rr, X86::AND32rr,
7460 X86::AND32ri, X86::AND32ri,
7461 false);
7462 case X86::ATOMOR6432:
7463 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7464 X86::OR32rr, X86::OR32rr,
7465 X86::OR32ri, X86::OR32ri,
7466 false);
7467 case X86::ATOMXOR6432:
7468 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7469 X86::XOR32rr, X86::XOR32rr,
7470 X86::XOR32ri, X86::XOR32ri,
7471 false);
7472 case X86::ATOMNAND6432:
7473 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7474 X86::AND32rr, X86::AND32rr,
7475 X86::AND32ri, X86::AND32ri,
7476 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007477 case X86::ATOMADD6432:
7478 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7479 X86::ADD32rr, X86::ADC32rr,
7480 X86::ADD32ri, X86::ADC32ri,
7481 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007482 case X86::ATOMSUB6432:
7483 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7484 X86::SUB32rr, X86::SBB32rr,
7485 X86::SUB32ri, X86::SBB32ri,
7486 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007487 case X86::ATOMSWAP6432:
7488 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7489 X86::MOV32rr, X86::MOV32rr,
7490 X86::MOV32ri, X86::MOV32ri,
7491 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007492 }
7493}
7494
7495//===----------------------------------------------------------------------===//
7496// X86 Optimization Hooks
7497//===----------------------------------------------------------------------===//
7498
Dan Gohman8181bd12008-07-27 21:46:04 +00007499void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007500 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007501 APInt &KnownZero,
7502 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007503 const SelectionDAG &DAG,
7504 unsigned Depth) const {
7505 unsigned Opc = Op.getOpcode();
7506 assert((Opc >= ISD::BUILTIN_OP_END ||
7507 Opc == ISD::INTRINSIC_WO_CHAIN ||
7508 Opc == ISD::INTRINSIC_W_CHAIN ||
7509 Opc == ISD::INTRINSIC_VOID) &&
7510 "Should use MaskedValueIsZero if you don't know whether Op"
7511 " is a target node!");
7512
Dan Gohman1d79e432008-02-13 23:07:24 +00007513 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007514 switch (Opc) {
7515 default: break;
7516 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007517 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7518 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007519 break;
7520 }
7521}
7522
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007523/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007524/// node is a GlobalAddress + offset.
7525bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7526 GlobalValue* &GA, int64_t &Offset) const{
7527 if (N->getOpcode() == X86ISD::Wrapper) {
7528 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007529 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007530 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007531 return true;
7532 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007533 }
Evan Chengef7be082008-05-12 19:56:52 +00007534 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007535}
7536
Evan Chengef7be082008-05-12 19:56:52 +00007537static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7538 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007539 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007540 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007541 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007542 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007543 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007544 return false;
7545}
7546
Dan Gohman8181bd12008-07-27 21:46:04 +00007547static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007548 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007549 SDNode *&Base,
7550 SelectionDAG &DAG, MachineFrameInfo *MFI,
7551 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007552 Base = NULL;
7553 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007554 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007555 if (Idx.getOpcode() == ISD::UNDEF) {
7556 if (!Base)
7557 return false;
7558 continue;
7559 }
7560
Dan Gohman8181bd12008-07-27 21:46:04 +00007561 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007562 if (!Elt.getNode() ||
7563 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007564 return false;
7565 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007566 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007567 if (Base->getOpcode() == ISD::UNDEF)
7568 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007569 continue;
7570 }
7571 if (Elt.getOpcode() == ISD::UNDEF)
7572 continue;
7573
Gabor Greif1c80d112008-08-28 21:40:38 +00007574 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007575 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007576 return false;
7577 }
7578 return true;
7579}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007580
7581/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7582/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7583/// if the load addresses are consecutive, non-overlapping, and in the right
7584/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007585static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007586 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007587 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007588 MVT VT = N->getValueType(0);
7589 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007590 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007591 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007592 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007593 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7594 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007595 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007596
Dan Gohman11821702007-07-27 17:16:43 +00007597 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007598 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007599 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007600 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007601 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7602 LD->getSrcValueOffset(), LD->isVolatile(),
7603 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007604}
7605
Evan Chengb6290462008-05-12 23:04:07 +00007606/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007607static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohman22cefb02009-01-29 01:59:02 +00007608 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng6617eed2008-09-24 23:26:36 +00007609 const X86Subtarget *Subtarget,
7610 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007611 unsigned NumOps = N->getNumOperands();
7612
Evan Chenge9b9c672008-05-09 21:53:03 +00007613 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007614 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007615 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007616
Duncan Sands92c43912008-06-06 12:08:01 +00007617 MVT VT = N->getValueType(0);
7618 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007619 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7620 // We are looking for load i64 and zero extend. We want to transform
7621 // it before legalizer has a chance to expand it. Also look for i64
7622 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007623 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007624 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007625 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007626 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007627 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007628
7629 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007630 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007631 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007632 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007633 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007634 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007635 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007636 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007637 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007638
7639 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007640 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007641
7642 // Load must not be an extload.
7643 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007644 return SDValue();
Mon P Wang28b36412009-01-30 07:07:40 +00007645
7646 // Load type should legal type so we don't have to legalize it.
7647 if (!TLI.isTypeLegal(VT))
7648 return SDValue();
7649
Evan Cheng6617eed2008-09-24 23:26:36 +00007650 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7651 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7652 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
Dan Gohman22cefb02009-01-29 01:59:02 +00007653 TargetLowering::TargetLoweringOpt TLO(DAG);
7654 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7655 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng6617eed2008-09-24 23:26:36 +00007656 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007657}
7658
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007659/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007660static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007661 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007662 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007663
7664 // If we have SSE[12] support, try to form min/max nodes.
7665 if (Subtarget->hasSSE2() &&
7666 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7667 if (Cond.getOpcode() == ISD::SETCC) {
7668 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007669 SDValue LHS = N->getOperand(1);
7670 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007671 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7672
7673 unsigned Opcode = 0;
7674 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7675 switch (CC) {
7676 default: break;
7677 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7678 case ISD::SETULE:
7679 case ISD::SETLE:
7680 if (!UnsafeFPMath) break;
7681 // FALL THROUGH.
7682 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7683 case ISD::SETLT:
7684 Opcode = X86ISD::FMIN;
7685 break;
7686
7687 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7688 case ISD::SETUGT:
7689 case ISD::SETGT:
7690 if (!UnsafeFPMath) break;
7691 // FALL THROUGH.
7692 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7693 case ISD::SETGE:
7694 Opcode = X86ISD::FMAX;
7695 break;
7696 }
7697 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7698 switch (CC) {
7699 default: break;
7700 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7701 case ISD::SETUGT:
7702 case ISD::SETGT:
7703 if (!UnsafeFPMath) break;
7704 // FALL THROUGH.
7705 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7706 case ISD::SETGE:
7707 Opcode = X86ISD::FMIN;
7708 break;
7709
7710 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7711 case ISD::SETULE:
7712 case ISD::SETLE:
7713 if (!UnsafeFPMath) break;
7714 // FALL THROUGH.
7715 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7716 case ISD::SETLT:
7717 Opcode = X86ISD::FMAX;
7718 break;
7719 }
7720 }
7721
7722 if (Opcode)
7723 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7724 }
7725
7726 }
7727
Dan Gohman8181bd12008-07-27 21:46:04 +00007728 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007729}
7730
sampo025b75c2009-01-26 00:52:55 +00007731/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
7732/// when possible.
7733static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
7734 const X86Subtarget *Subtarget) {
7735 // On X86 with SSE2 support, we can transform this to a vector shift if
7736 // all elements are shifted by the same amount. We can't do this in legalize
7737 // because the a constant vector is typically transformed to a constant pool
7738 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00007739 if (!Subtarget->hasSSE2())
7740 return SDValue();
7741
sampo025b75c2009-01-26 00:52:55 +00007742 MVT VT = N->getValueType(0);
sampo087d53c2009-01-26 03:15:31 +00007743 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
7744 return SDValue();
7745
Mon P Wanga91e9642009-01-28 08:12:05 +00007746 SDValue ShAmtOp = N->getOperand(1);
7747 MVT EltVT = VT.getVectorElementType();
7748 SDValue BaseShAmt;
7749 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
7750 unsigned NumElts = VT.getVectorNumElements();
7751 unsigned i = 0;
7752 for (; i != NumElts; ++i) {
7753 SDValue Arg = ShAmtOp.getOperand(i);
7754 if (Arg.getOpcode() == ISD::UNDEF) continue;
7755 BaseShAmt = Arg;
7756 break;
7757 }
7758 for (; i != NumElts; ++i) {
7759 SDValue Arg = ShAmtOp.getOperand(i);
7760 if (Arg.getOpcode() == ISD::UNDEF) continue;
7761 if (Arg != BaseShAmt) {
7762 return SDValue();
7763 }
7764 }
7765 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
7766 isSplatMask(ShAmtOp.getOperand(2).getNode())) {
7767 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, ShAmtOp,
7768 DAG.getIntPtrConstant(0));
7769 } else
sampo087d53c2009-01-26 03:15:31 +00007770 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00007771
sampo087d53c2009-01-26 03:15:31 +00007772 if (EltVT.bitsGT(MVT::i32))
7773 BaseShAmt = DAG.getNode(ISD::TRUNCATE, MVT::i32, BaseShAmt);
7774 else if (EltVT.bitsLT(MVT::i32))
7775 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00007776
sampo087d53c2009-01-26 03:15:31 +00007777 // The shift amount is identical so we can do a vector shift.
7778 SDValue ValOp = N->getOperand(0);
7779 switch (N->getOpcode()) {
7780 default:
7781 assert(0 && "Unknown shift opcode!");
7782 break;
7783 case ISD::SHL:
7784 if (VT == MVT::v2i64)
7785 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007786 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7787 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007788 if (VT == MVT::v4i32)
7789 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007790 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
7791 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007792 if (VT == MVT::v8i16)
7793 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007794 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
7795 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007796 break;
7797 case ISD::SRA:
7798 if (VT == MVT::v4i32)
7799 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007800 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
7801 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007802 if (VT == MVT::v8i16)
7803 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007804 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
7805 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007806 break;
7807 case ISD::SRL:
7808 if (VT == MVT::v2i64)
7809 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007810 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7811 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007812 if (VT == MVT::v4i32)
7813 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007814 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
7815 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007816 if (VT == MVT::v8i16)
7817 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
sampo025b75c2009-01-26 00:52:55 +00007818 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
7819 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00007820 break;
sampo025b75c2009-01-26 00:52:55 +00007821 }
7822 return SDValue();
7823}
7824
Chris Lattnerce84ae42008-02-22 02:09:43 +00007825/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007826static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007827 const X86Subtarget *Subtarget) {
7828 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7829 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007830 // A preferable solution to the general problem is to figure out the right
7831 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007832 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007833 if (St->getValue().getValueType().isVector() &&
7834 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007835 isa<LoadSDNode>(St->getValue()) &&
7836 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7837 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007838 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007839 LoadSDNode *Ld = 0;
7840 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007841 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007842 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007843 // Must be a store of a load. We currently handle two cases: the load
7844 // is a direct child, and it's under an intervening TokenFactor. It is
7845 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007846 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007847 Ld = cast<LoadSDNode>(St->getChain());
7848 else if (St->getValue().hasOneUse() &&
7849 ChainVal->getOpcode() == ISD::TokenFactor) {
7850 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007851 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007852 TokenFactorIndex = i;
7853 Ld = cast<LoadSDNode>(St->getValue());
7854 } else
7855 Ops.push_back(ChainVal->getOperand(i));
7856 }
7857 }
7858 if (Ld) {
7859 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7860 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007861 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007862 Ld->getBasePtr(), Ld->getSrcValue(),
7863 Ld->getSrcValueOffset(), Ld->isVolatile(),
7864 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007865 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007866 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007867 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007868 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7869 Ops.size());
7870 }
7871 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7872 St->getSrcValue(), St->getSrcValueOffset(),
7873 St->isVolatile(), St->getAlignment());
7874 }
7875
7876 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007877 SDValue LoAddr = Ld->getBasePtr();
7878 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007879 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007880
Dan Gohman8181bd12008-07-27 21:46:04 +00007881 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007882 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7883 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007884 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007885 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7886 Ld->isVolatile(),
7887 MinAlign(Ld->getAlignment(), 4));
7888
Dan Gohman8181bd12008-07-27 21:46:04 +00007889 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007890 if (TokenFactorIndex != -1) {
7891 Ops.push_back(LoLd);
7892 Ops.push_back(HiLd);
7893 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7894 Ops.size());
7895 }
7896
7897 LoAddr = St->getBasePtr();
7898 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007899 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007900
Dan Gohman8181bd12008-07-27 21:46:04 +00007901 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007902 St->getSrcValue(), St->getSrcValueOffset(),
7903 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007904 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007905 St->getSrcValue(),
7906 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007907 St->isVolatile(),
7908 MinAlign(St->getAlignment(), 4));
7909 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007910 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007911 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007912 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007913}
7914
Chris Lattner470d5dc2008-01-25 06:14:17 +00007915/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7916/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007917static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007918 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7919 // F[X]OR(0.0, x) -> x
7920 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007921 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7922 if (C->getValueAPF().isPosZero())
7923 return N->getOperand(1);
7924 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7925 if (C->getValueAPF().isPosZero())
7926 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007927 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007928}
7929
7930/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007931static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007932 // FAND(0.0, x) -> 0.0
7933 // FAND(x, 0.0) -> 0.0
7934 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7935 if (C->getValueAPF().isPosZero())
7936 return N->getOperand(0);
7937 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7938 if (C->getValueAPF().isPosZero())
7939 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007940 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007941}
7942
Dan Gohman22cefb02009-01-29 01:59:02 +00007943static SDValue PerformBTCombine(SDNode *N,
7944 SelectionDAG &DAG,
7945 TargetLowering::DAGCombinerInfo &DCI) {
7946 // BT ignores high bits in the bit index operand.
7947 SDValue Op1 = N->getOperand(1);
7948 if (Op1.hasOneUse()) {
7949 unsigned BitWidth = Op1.getValueSizeInBits();
7950 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
7951 APInt KnownZero, KnownOne;
7952 TargetLowering::TargetLoweringOpt TLO(DAG);
7953 TargetLowering &TLI = DAG.getTargetLoweringInfo();
7954 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
7955 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
7956 DCI.CommitTargetLoweringOpt(TLO);
7957 }
7958 return SDValue();
7959}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007960
Dan Gohman8181bd12008-07-27 21:46:04 +00007961SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007962 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007963 SelectionDAG &DAG = DCI.DAG;
7964 switch (N->getOpcode()) {
7965 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007966 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7967 case ISD::BUILD_VECTOR:
Dan Gohman22cefb02009-01-29 01:59:02 +00007968 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007969 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
sampo025b75c2009-01-26 00:52:55 +00007970 case ISD::SHL:
7971 case ISD::SRA:
7972 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007973 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007974 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007975 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7976 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00007977 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007978 }
7979
Dan Gohman8181bd12008-07-27 21:46:04 +00007980 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007981}
7982
7983//===----------------------------------------------------------------------===//
7984// X86 Inline Assembly Support
7985//===----------------------------------------------------------------------===//
7986
7987/// getConstraintType - Given a constraint letter, return the type of
7988/// constraint it is for this target.
7989X86TargetLowering::ConstraintType
7990X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7991 if (Constraint.size() == 1) {
7992 switch (Constraint[0]) {
7993 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007994 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007995 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007996 case 'r':
7997 case 'R':
7998 case 'l':
7999 case 'q':
8000 case 'Q':
8001 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00008002 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008003 case 'Y':
8004 return C_RegisterClass;
8005 default:
8006 break;
8007 }
8008 }
8009 return TargetLowering::getConstraintType(Constraint);
8010}
8011
Dale Johannesene99fc902008-01-29 02:21:21 +00008012/// LowerXConstraint - try to replace an X constraint, which matches anything,
8013/// with another that has more specific requirements based on the type of the
8014/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00008015const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00008016LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00008017 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8018 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00008019 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00008020 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00008021 return "Y";
8022 if (Subtarget->hasSSE1())
8023 return "x";
8024 }
8025
8026 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00008027}
8028
Chris Lattnera531abc2007-08-25 00:47:38 +00008029/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8030/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00008031void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00008032 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00008033 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00008034 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00008035 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00008036 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00008037
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008038 switch (Constraint) {
8039 default: break;
8040 case 'I':
8041 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008042 if (C->getZExtValue() <= 31) {
8043 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008044 break;
8045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008046 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008047 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00008048 case 'J':
8049 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8050 if (C->getZExtValue() <= 63) {
8051 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8052 break;
8053 }
8054 }
8055 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008056 case 'N':
8057 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008058 if (C->getZExtValue() <= 255) {
8059 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008060 break;
8061 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008062 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008063 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008064 case 'i': {
8065 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00008066 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008067 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00008068 break;
8069 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008070
8071 // If we are in non-pic codegen mode, we allow the address of a global (with
8072 // an optional displacement) to be used with 'i'.
8073 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
8074 int64_t Offset = 0;
8075
8076 // Match either (GA) or (GA+C)
8077 if (GA) {
8078 Offset = GA->getOffset();
8079 } else if (Op.getOpcode() == ISD::ADD) {
8080 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8081 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8082 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008083 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008084 } else {
8085 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
8086 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
8087 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00008088 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008089 else
8090 C = 0, GA = 0;
8091 }
8092 }
8093
8094 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00008095 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00008096 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00008097 else
8098 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8099 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00008100 Result = Op;
8101 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008102 }
8103
8104 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00008105 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008106 }
8107 }
Chris Lattnera531abc2007-08-25 00:47:38 +00008108
Gabor Greif1c80d112008-08-28 21:40:38 +00008109 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00008110 Ops.push_back(Result);
8111 return;
8112 }
Evan Cheng7f250d62008-09-24 00:05:32 +00008113 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8114 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008115}
8116
8117std::vector<unsigned> X86TargetLowering::
8118getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008119 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008120 if (Constraint.size() == 1) {
8121 // FIXME: not handling fp-stack yet!
8122 switch (Constraint[0]) { // GCC X86 Constraint Letters
8123 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008124 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8125 case 'Q': // Q_REGS
8126 if (VT == MVT::i32)
8127 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8128 else if (VT == MVT::i16)
8129 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8130 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00008131 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00008132 else if (VT == MVT::i64)
8133 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8134 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008135 }
8136 }
8137
8138 return std::vector<unsigned>();
8139}
8140
8141std::pair<unsigned, const TargetRegisterClass*>
8142X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00008143 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008144 // First, see if this is a constraint that directly corresponds to an LLVM
8145 // register class.
8146 if (Constraint.size() == 1) {
8147 // GCC Constraint Letters
8148 switch (Constraint[0]) {
8149 default: break;
8150 case 'r': // GENERAL_REGS
8151 case 'R': // LEGACY_REGS
8152 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00008153 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008154 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00008155 if (VT == MVT::i16)
8156 return std::make_pair(0U, X86::GR16RegisterClass);
8157 if (VT == MVT::i32 || !Subtarget->is64Bit())
8158 return std::make_pair(0U, X86::GR32RegisterClass);
8159 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00008160 case 'f': // FP Stack registers.
8161 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8162 // value to the correct fpstack register class.
8163 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8164 return std::make_pair(0U, X86::RFP32RegisterClass);
8165 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8166 return std::make_pair(0U, X86::RFP64RegisterClass);
8167 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008168 case 'y': // MMX_REGS if MMX allowed.
8169 if (!Subtarget->hasMMX()) break;
8170 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008171 case 'Y': // SSE_REGS if SSE2 allowed
8172 if (!Subtarget->hasSSE2()) break;
8173 // FALL THROUGH.
8174 case 'x': // SSE_REGS if SSE1 allowed
8175 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00008176
8177 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008178 default: break;
8179 // Scalar SSE types.
8180 case MVT::f32:
8181 case MVT::i32:
8182 return std::make_pair(0U, X86::FR32RegisterClass);
8183 case MVT::f64:
8184 case MVT::i64:
8185 return std::make_pair(0U, X86::FR64RegisterClass);
8186 // Vector types.
8187 case MVT::v16i8:
8188 case MVT::v8i16:
8189 case MVT::v4i32:
8190 case MVT::v2i64:
8191 case MVT::v4f32:
8192 case MVT::v2f64:
8193 return std::make_pair(0U, X86::VR128RegisterClass);
8194 }
8195 break;
8196 }
8197 }
8198
8199 // Use the default implementation in TargetLowering to convert the register
8200 // constraint into a member of a register class.
8201 std::pair<unsigned, const TargetRegisterClass*> Res;
8202 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
8203
8204 // Not found as a standard register?
8205 if (Res.second == 0) {
8206 // GCC calls "st(0)" just plain "st".
8207 if (StringsEqualNoCase("{st}", Constraint)) {
8208 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00008209 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008210 }
Dale Johannesen73920c02008-11-13 21:52:36 +00008211 // 'A' means EAX + EDX.
8212 if (Constraint == "A") {
8213 Res.first = X86::EAX;
8214 Res.second = X86::GRADRegisterClass;
8215 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008216 return Res;
8217 }
8218
8219 // Otherwise, check to see if this is a register class of the wrong value
8220 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8221 // turn into {ax},{dx}.
8222 if (Res.second->hasType(VT))
8223 return Res; // Correct type already, nothing to do.
8224
8225 // All of the single-register GCC register classes map their values onto
8226 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8227 // really want an 8-bit or 32-bit register, map to the appropriate register
8228 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00008229 if (Res.second == X86::GR16RegisterClass) {
8230 if (VT == MVT::i8) {
8231 unsigned DestReg = 0;
8232 switch (Res.first) {
8233 default: break;
8234 case X86::AX: DestReg = X86::AL; break;
8235 case X86::DX: DestReg = X86::DL; break;
8236 case X86::CX: DestReg = X86::CL; break;
8237 case X86::BX: DestReg = X86::BL; break;
8238 }
8239 if (DestReg) {
8240 Res.first = DestReg;
8241 Res.second = Res.second = X86::GR8RegisterClass;
8242 }
8243 } else if (VT == MVT::i32) {
8244 unsigned DestReg = 0;
8245 switch (Res.first) {
8246 default: break;
8247 case X86::AX: DestReg = X86::EAX; break;
8248 case X86::DX: DestReg = X86::EDX; break;
8249 case X86::CX: DestReg = X86::ECX; break;
8250 case X86::BX: DestReg = X86::EBX; break;
8251 case X86::SI: DestReg = X86::ESI; break;
8252 case X86::DI: DestReg = X86::EDI; break;
8253 case X86::BP: DestReg = X86::EBP; break;
8254 case X86::SP: DestReg = X86::ESP; break;
8255 }
8256 if (DestReg) {
8257 Res.first = DestReg;
8258 Res.second = Res.second = X86::GR32RegisterClass;
8259 }
8260 } else if (VT == MVT::i64) {
8261 unsigned DestReg = 0;
8262 switch (Res.first) {
8263 default: break;
8264 case X86::AX: DestReg = X86::RAX; break;
8265 case X86::DX: DestReg = X86::RDX; break;
8266 case X86::CX: DestReg = X86::RCX; break;
8267 case X86::BX: DestReg = X86::RBX; break;
8268 case X86::SI: DestReg = X86::RSI; break;
8269 case X86::DI: DestReg = X86::RDI; break;
8270 case X86::BP: DestReg = X86::RBP; break;
8271 case X86::SP: DestReg = X86::RSP; break;
8272 }
8273 if (DestReg) {
8274 Res.first = DestReg;
8275 Res.second = Res.second = X86::GR64RegisterClass;
8276 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008277 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008278 } else if (Res.second == X86::FR32RegisterClass ||
8279 Res.second == X86::FR64RegisterClass ||
8280 Res.second == X86::VR128RegisterClass) {
8281 // Handle references to XMM physical registers that got mapped into the
8282 // wrong class. This can happen with constraints like {xmm0} where the
8283 // target independent register mapper will just pick the first match it can
8284 // find, ignoring the required type.
8285 if (VT == MVT::f32)
8286 Res.second = X86::FR32RegisterClass;
8287 else if (VT == MVT::f64)
8288 Res.second = X86::FR64RegisterClass;
8289 else if (X86::VR128RegisterClass->hasType(VT))
8290 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008291 }
8292
8293 return Res;
8294}
Mon P Wang1448aad2008-10-30 08:01:45 +00008295
8296//===----------------------------------------------------------------------===//
8297// X86 Widen vector type
8298//===----------------------------------------------------------------------===//
8299
8300/// getWidenVectorType: given a vector type, returns the type to widen
8301/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8302/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008303/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008304/// scalarizing vs using the wider vector type.
8305
Dan Gohman0fe66c92009-01-15 17:34:08 +00008306MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008307 assert(VT.isVector());
8308 if (isTypeLegal(VT))
8309 return VT;
8310
8311 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8312 // type based on element type. This would speed up our search (though
8313 // it may not be worth it since the size of the list is relatively
8314 // small).
8315 MVT EltVT = VT.getVectorElementType();
8316 unsigned NElts = VT.getVectorNumElements();
8317
8318 // On X86, it make sense to widen any vector wider than 1
8319 if (NElts <= 1)
8320 return MVT::Other;
8321
8322 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8323 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8324 MVT SVT = (MVT::SimpleValueType)nVT;
8325
8326 if (isTypeLegal(SVT) &&
8327 SVT.getVectorElementType() == EltVT &&
8328 SVT.getVectorNumElements() > NElts)
8329 return SVT;
8330 }
8331 return MVT::Other;
8332}