blob: e39036fb5b79c63d9b23041aeab7982562197ca9 [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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
123 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
Dale Johannesena359b8b2008-10-21 20:50:01 +0000124 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
126 }
127
128 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
129 // this operation.
130 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
132 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000133 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000135 // f32 and f64 cases are Legal, f80 case is not
136 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
137 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
139 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
140 }
141
Dale Johannesen958b08b2007-09-19 23:55:34 +0000142 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
143 // are Legal, f80 is custom lowered.
144 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
145 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
148 // this operation.
149 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
151
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000152 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000154 // f32 and f64 cases are Legal, f80 case is not
155 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 } else {
157 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
158 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
159 }
160
161 // Handle FP_TO_UINT by promoting the destination to a larger signed
162 // conversion.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
164 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
166
167 if (Subtarget->is64Bit()) {
168 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
169 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
170 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000171 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 // Expand FP_TO_UINT into a select.
173 // FIXME: We would like to use a Custom expander here eventually to do
174 // the optimal thing for SSE vs. the default expansion in the legalizer.
175 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
176 else
177 // With SSE3 we can use fisttpll to convert to a signed i64.
178 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
179 }
180
181 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000182 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
184 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
185 }
186
Dan Gohman8450d862008-02-18 19:34:53 +0000187 // Scalar integer divide and remainder are lowered to use operations that
188 // produce two results, to match the available instructions. This exposes
189 // the two-result form to trivial CSE, which is able to combine x/y and x%y
190 // into a single instruction.
191 //
192 // Scalar integer multiply-high is also lowered to use two-result
193 // operations, to match the available instructions. However, plain multiply
194 // (low) operations are left as Legal, as there are single-result
195 // instructions for this in x86. Using the two-result multiply instructions
196 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000197 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
198 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
199 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
200 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
201 setOperationAction(ISD::SREM , MVT::i8 , Expand);
202 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000203 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
204 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
205 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
206 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
207 setOperationAction(ISD::SREM , MVT::i16 , Expand);
208 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000209 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
210 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
211 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
212 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
213 setOperationAction(ISD::SREM , MVT::i32 , Expand);
214 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000215 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
216 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
217 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
218 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
219 setOperationAction(ISD::SREM , MVT::i64 , Expand);
220 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
223 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
224 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
225 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000227 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
228 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
229 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
231 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000232 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000234 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000235 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000238 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
239 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000241 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
242 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 if (Subtarget->is64Bit()) {
247 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000248 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
249 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 }
251
252 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
253 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
254
255 // These should be promoted to a larger select which is supported.
256 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
257 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
258 // X86 wants to expand cmov itself.
259 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
260 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
261 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
262 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000263 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
265 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
267 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
268 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000269 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (Subtarget->is64Bit()) {
271 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
272 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
273 }
274 // X86 ret instruction may pop stack.
275 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000276 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
278 // Darwin ABI issue.
279 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
280 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
281 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
282 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000283 if (Subtarget->is64Bit())
284 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000285 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 if (Subtarget->is64Bit()) {
287 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
288 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
289 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000290 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 }
292 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
293 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
294 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
295 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000296 if (Subtarget->is64Bit()) {
297 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
298 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
299 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
300 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301
Evan Cheng8d51ab32008-03-10 19:38:10 +0000302 if (Subtarget->hasSSE1())
303 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000304
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000305 if (!Subtarget->hasSSE2())
306 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
307
Mon P Wang078a62d2008-05-05 19:05:59 +0000308 // Expand certain atomics
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000309 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
310 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000313
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000314 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
315 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000318
Dale Johannesenf160d802008-10-02 18:53:47 +0000319 if (!Subtarget->is64Bit()) {
Dan Gohmanbebba8d2008-12-23 21:37:04 +0000320 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000327 }
328
Dan Gohman472d12c2008-06-30 20:59:49 +0000329 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
330 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 // FIXME - use subtarget debug flags
332 if (!Subtarget->isTargetDarwin() &&
333 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000334 !Subtarget->isTargetCygMing()) {
335 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
336 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
337 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
340 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
341 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
342 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
343 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 setExceptionPointerRegister(X86::RAX);
345 setExceptionSelectorRegister(X86::RDX);
346 } else {
347 setExceptionPointerRegister(X86::EAX);
348 setExceptionSelectorRegister(X86::EDX);
349 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000350 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000351 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
352
Duncan Sands7407a9f2007-09-11 14:10:23 +0000353 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000354
Chris Lattner56b941f2008-01-15 21:58:22 +0000355 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
358 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000360 if (Subtarget->is64Bit()) {
361 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000363 } else {
364 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
368 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
369 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
370 if (Subtarget->is64Bit())
371 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
372 if (Subtarget->isTargetCygMing())
373 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
374 else
375 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
376
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000377 if (X86ScalarSSEf64) {
378 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 // Set up the FP register classes.
380 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
381 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
382
383 // Use ANDPD to simulate FABS.
384 setOperationAction(ISD::FABS , MVT::f64, Custom);
385 setOperationAction(ISD::FABS , MVT::f32, Custom);
386
387 // Use XORP to simulate FNEG.
388 setOperationAction(ISD::FNEG , MVT::f64, Custom);
389 setOperationAction(ISD::FNEG , MVT::f32, Custom);
390
391 // Use ANDPD and ORPD to simulate FCOPYSIGN.
392 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
393 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
394
395 // We don't support sin/cos/fmod
396 setOperationAction(ISD::FSIN , MVT::f64, Expand);
397 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 setOperationAction(ISD::FSIN , MVT::f32, Expand);
399 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400
401 // Expand FP immediates into loads from the stack, except for the special
402 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000403 addLegalFPImmediate(APFloat(+0.0)); // xorpd
404 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000405
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000406 // Floating truncations from f80 and extensions to f80 go through memory.
407 // If optimizing, we lie about this though and handle it in
408 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
409 if (Fast) {
410 setConvertAction(MVT::f32, MVT::f80, Expand);
411 setConvertAction(MVT::f64, MVT::f80, Expand);
412 setConvertAction(MVT::f80, MVT::f32, Expand);
413 setConvertAction(MVT::f80, MVT::f64, Expand);
414 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000415 } else if (X86ScalarSSEf32) {
416 // Use SSE for f32, x87 for f64.
417 // Set up the FP register classes.
418 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
419 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
420
421 // Use ANDPS to simulate FABS.
422 setOperationAction(ISD::FABS , MVT::f32, Custom);
423
424 // Use XORP to simulate FNEG.
425 setOperationAction(ISD::FNEG , MVT::f32, Custom);
426
427 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
428
429 // Use ANDPS and ORPS to simulate FCOPYSIGN.
430 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
431 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
432
433 // We don't support sin/cos/fmod
434 setOperationAction(ISD::FSIN , MVT::f32, Expand);
435 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000436
Nate Begemane2ba64f2008-02-14 08:57:00 +0000437 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000438 addLegalFPImmediate(APFloat(+0.0f)); // xorps
439 addLegalFPImmediate(APFloat(+0.0)); // FLD0
440 addLegalFPImmediate(APFloat(+1.0)); // FLD1
441 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
442 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
443
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000444 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
445 // this though and handle it in InstructionSelectPreprocess so that
446 // dagcombine2 can hack on these.
447 if (Fast) {
448 setConvertAction(MVT::f32, MVT::f64, Expand);
449 setConvertAction(MVT::f32, MVT::f80, Expand);
450 setConvertAction(MVT::f80, MVT::f32, Expand);
451 setConvertAction(MVT::f64, MVT::f32, Expand);
452 // And x87->x87 truncations also.
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455
456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000461 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 // Set up the FP register classes.
463 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
464 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
465
466 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
467 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
468 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000470
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000471 // Floating truncations go through memory. If optimizing, we lie about
472 // this though and handle it in InstructionSelectPreprocess so that
473 // dagcombine2 can hack on these.
474 if (Fast) {
475 setConvertAction(MVT::f80, MVT::f32, Expand);
476 setConvertAction(MVT::f64, MVT::f32, Expand);
477 setConvertAction(MVT::f80, MVT::f64, Expand);
478 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000479
480 if (!UnsafeFPMath) {
481 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
482 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
483 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000484 addLegalFPImmediate(APFloat(+0.0)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000488 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
489 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
490 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
491 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 }
493
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000494 // Long double always uses X87.
495 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000496 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000498 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000499 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000500 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000501 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
502 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000503 addLegalFPImmediate(TmpFlt); // FLD0
504 TmpFlt.changeSign();
505 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
506 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000507 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
508 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000509 addLegalFPImmediate(TmpFlt2); // FLD1
510 TmpFlt2.changeSign();
511 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
512 }
513
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000514 if (!UnsafeFPMath) {
515 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
516 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
517 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000518
Dan Gohman2f7b1982007-10-11 23:21:31 +0000519 // Always use a library call for pow.
520 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
521 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
522 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
523
Dale Johannesen92b33082008-09-04 00:47:13 +0000524 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000525 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000526 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000527 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000528 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
529
Mon P Wanga5a239f2008-11-06 05:31:54 +0000530 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000531 // (for widening) or expand (for scalarization). Then we will selectively
532 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
534 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000535 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000548 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
550 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000551 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000573 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 }
579
Mon P Wang1f292322008-11-23 04:37:22 +0000580 if (!DisableMMX && Subtarget->hasMMX()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
582 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
583 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000584 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
586
587 // FIXME: add MMX packed arithmetics
588
589 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
590 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
591 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
592 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
593
594 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
595 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
596 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000597 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598
599 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
600 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
601
602 setOperationAction(ISD::AND, MVT::v8i8, Promote);
603 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
604 setOperationAction(ISD::AND, MVT::v4i16, Promote);
605 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
606 setOperationAction(ISD::AND, MVT::v2i32, Promote);
607 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
608 setOperationAction(ISD::AND, MVT::v1i64, Legal);
609
610 setOperationAction(ISD::OR, MVT::v8i8, Promote);
611 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
612 setOperationAction(ISD::OR, MVT::v4i16, Promote);
613 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
614 setOperationAction(ISD::OR, MVT::v2i32, Promote);
615 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
616 setOperationAction(ISD::OR, MVT::v1i64, Legal);
617
618 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
619 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
620 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
621 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
622 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
623 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
624 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
625
626 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
627 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
628 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
629 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
630 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
631 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000632 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
633 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
635
636 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
638 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000639 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000640 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
641
642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
644 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
645 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
646
Evan Cheng759fe022008-07-22 18:39:19 +0000647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
649 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000651
652 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000653
654 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
655 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
656 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
657 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
658 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
659 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 }
661
662 if (Subtarget->hasSSE1()) {
663 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
664
665 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
666 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
667 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
668 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
669 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
670 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000671 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
674 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
675 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000676 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000677 }
678
679 if (Subtarget->hasSSE2()) {
680 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
684 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
685
686 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
687 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
688 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
689 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wang14edb092008-12-18 21:42:19 +0000690 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
692 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
693 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
694 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
695 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
696 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
697 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
698 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
699 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
700 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
701 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702
Nate Begeman03605a02008-07-17 16:51:19 +0000703 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
706 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000707
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
709 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
713
714 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000715 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
716 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000717 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000718 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000719 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000720 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
721 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 }
724 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
725 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
726 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000728 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000730 if (Subtarget->is64Bit()) {
731 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000732 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000733 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734
735 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
736 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000737 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
738 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
739 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
740 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
741 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
742 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
743 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
744 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
745 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
746 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 }
748
Chris Lattner3bc08502008-01-17 19:59:44 +0000749 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000750
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000751 // Custom lower v2i64 and v2f64 selects.
752 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
753 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
754 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
755 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000758
759 if (Subtarget->hasSSE41()) {
760 // FIXME: Do we need to handle scalar-to-vector here?
761 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
762
763 // i8 and i16 vectors are custom , because the source register and source
764 // source memory operand types are not the same width. f32 vectors are
765 // custom since the immediate controlling the insert encodes additional
766 // information.
767 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
768 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000770 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
771
772 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangac2a3c52009-01-15 21:10:20 +0000774 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng6c249332008-03-24 21:52:23 +0000775 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000776
777 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000780 }
781 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782
Nate Begeman03605a02008-07-17 16:51:19 +0000783 if (Subtarget->hasSSE42()) {
784 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
785 }
786
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 // We want to custom lower some of our intrinsics.
788 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
789
Bill Wendling7e04be62008-12-09 22:08:41 +0000790 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000791 setOperationAction(ISD::SADDO, MVT::i32, Custom);
792 setOperationAction(ISD::SADDO, MVT::i64, Custom);
793 setOperationAction(ISD::UADDO, MVT::i32, Custom);
794 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000795 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
796 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
797 setOperationAction(ISD::USUBO, MVT::i32, Custom);
798 setOperationAction(ISD::USUBO, MVT::i64, Custom);
799 setOperationAction(ISD::SMULO, MVT::i32, Custom);
800 setOperationAction(ISD::SMULO, MVT::i64, Custom);
801 setOperationAction(ISD::UMULO, MVT::i32, Custom);
802 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000803
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000804 // We have target-specific dag combine patterns for the following nodes:
805 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000806 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000808 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809
810 computeRegisterProperties();
811
812 // FIXME: These should be based on subtarget info. Plus, the values should
813 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000814 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
815 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
816 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000818 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819}
820
Scott Michel502151f2008-03-10 15:42:14 +0000821
Duncan Sands4a361272009-01-01 15:52:00 +0000822MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel502151f2008-03-10 15:42:14 +0000823 return MVT::i8;
824}
825
826
Evan Cheng5a67b812008-01-23 23:17:41 +0000827/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
828/// the desired ByVal argument alignment.
829static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
830 if (MaxAlign == 16)
831 return;
832 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
833 if (VTy->getBitWidth() == 128)
834 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000835 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
836 unsigned EltAlign = 0;
837 getMaxByValAlign(ATy->getElementType(), EltAlign);
838 if (EltAlign > MaxAlign)
839 MaxAlign = EltAlign;
840 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
841 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
842 unsigned EltAlign = 0;
843 getMaxByValAlign(STy->getElementType(i), EltAlign);
844 if (EltAlign > MaxAlign)
845 MaxAlign = EltAlign;
846 if (MaxAlign == 16)
847 break;
848 }
849 }
850 return;
851}
852
853/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
854/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000855/// that contain SSE vectors are placed at 16-byte boundaries while the rest
856/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000857unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000858 if (Subtarget->is64Bit()) {
859 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000860 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000861 if (TyAlign > 8)
862 return TyAlign;
863 return 8;
864 }
865
Evan Cheng5a67b812008-01-23 23:17:41 +0000866 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000867 if (Subtarget->hasSSE1())
868 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000869 return Align;
870}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871
Evan Cheng8c590372008-05-15 08:39:06 +0000872/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000873/// and store operations as a result of memset, memcpy, and memmove
874/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000875/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000876MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000877X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
878 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000879 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
880 // linux. This is because the stack realignment code can't handle certain
881 // cases like PR2962. This should be removed when PR2962 is fixed.
882 if (Subtarget->getStackAlignment() >= 16) {
883 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
884 return MVT::v4i32;
885 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
886 return MVT::v4f32;
887 }
Evan Cheng8c590372008-05-15 08:39:06 +0000888 if (Subtarget->is64Bit() && Size >= 8)
889 return MVT::i64;
890 return MVT::i32;
891}
892
893
Evan Cheng6fb06762007-11-09 01:32:10 +0000894/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
895/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000896SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000897 SelectionDAG &DAG) const {
898 if (usesGlobalOffsetTable())
899 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
900 if (!Subtarget->isPICStyleRIPRel())
901 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
902 return Table;
903}
904
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905//===----------------------------------------------------------------------===//
906// Return Value Calling Convention Implementation
907//===----------------------------------------------------------------------===//
908
909#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000910
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000912SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
914
915 SmallVector<CCValAssign, 16> RVLocs;
916 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
917 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
918 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000919 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000920
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 // If this is the first return lowered for this function, add the regs to the
922 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000923 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 for (unsigned i = 0; i != RVLocs.size(); ++i)
925 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000926 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000928 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000929
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000930 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000931 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000932 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000933 SDValue TailCall = Chain;
934 SDValue TargetAddress = TailCall.getOperand(1);
935 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000936 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000937 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000938 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000939 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000940 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
941 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000942 assert(StackAdjustment.getOpcode() == ISD::Constant &&
943 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000944
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000946 Operands.push_back(Chain.getOperand(0));
947 Operands.push_back(TargetAddress);
948 Operands.push_back(StackAdjustment);
949 // Copy registers used by the call. Last operand is a flag so it is not
950 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000951 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000952 Operands.push_back(Chain.getOperand(i));
953 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000954 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
955 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000956 }
957
958 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000959 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000960
Dan Gohman8181bd12008-07-27 21:46:04 +0000961 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000962 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
963 // Operand #1 = Bytes To Pop
964 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
965
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000967 for (unsigned i = 0; i != RVLocs.size(); ++i) {
968 CCValAssign &VA = RVLocs[i];
969 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000970 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971
Chris Lattnerb56cc342008-03-11 03:23:40 +0000972 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
973 // the RET instruction and handled by the FP Stackifier.
974 if (RVLocs[i].getLocReg() == X86::ST0 ||
975 RVLocs[i].getLocReg() == X86::ST1) {
976 // If this is a copy from an xmm register to ST(0), use an FPExtend to
977 // change the value to the FP stack register class.
978 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
979 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
980 RetOps.push_back(ValToCopy);
981 // Don't emit a copytoreg.
982 continue;
983 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000984
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000985 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 Flag = Chain.getValue(1);
987 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000988
989 // The x86-64 ABI for returning structs by value requires that we copy
990 // the sret argument into %rax for the return. We saved the argument into
991 // a virtual register in the entry block, so now we copy the value out
992 // and into %rax.
993 if (Subtarget->is64Bit() &&
994 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
995 MachineFunction &MF = DAG.getMachineFunction();
996 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
997 unsigned Reg = FuncInfo->getSRetReturnReg();
998 if (!Reg) {
999 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1000 FuncInfo->setSRetReturnReg(Reg);
1001 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001002 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001003
1004 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
1005 Flag = Chain.getValue(1);
1006 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001007
Chris Lattnerb56cc342008-03-11 03:23:40 +00001008 RetOps[0] = Chain; // Update chain.
1009
1010 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001011 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001012 RetOps.push_back(Flag);
1013
1014 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015}
1016
1017
1018/// LowerCallResult - Lower the result values of an ISD::CALL into the
1019/// appropriate copies out of appropriate physical registers. This assumes that
1020/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1021/// being lowered. The returns a SDNode with the same number of values as the
1022/// ISD::CALL.
1023SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001024LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 unsigned CallingConv, SelectionDAG &DAG) {
1026
1027 // Assign locations to each value returned by this call.
1028 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001029 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001030 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1031 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1032
Dan Gohman8181bd12008-07-27 21:46:04 +00001033 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001034
1035 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001036 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001037 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001038
1039 // If this is a call to a function that returns an fp value on the floating
1040 // point stack, but where we prefer to use the value in xmm registers, copy
1041 // 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 +00001042 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1043 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001044 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1045 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001046 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001048 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1049 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001050 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001051 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001052
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001053 if (CopyVT != RVLocs[i].getValVT()) {
1054 // Round the F80 the right size, which also moves to the appropriate xmm
1055 // register.
1056 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1057 // This truncation won't change the value.
1058 DAG.getIntPtrConstant(1));
1059 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001060
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001061 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001062 }
Duncan Sands698842f2008-07-02 17:40:58 +00001063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 // Merge everything together with a MERGE_VALUES node.
1065 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001066 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1067 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068}
1069
1070
1071//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001072// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073//===----------------------------------------------------------------------===//
1074// StdCall calling convention seems to be standard for many Windows' API
1075// routines and around. It differs from C calling convention just a little:
1076// callee should clean up the stack, not caller. Symbols should be also
1077// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001078// For info on fast calling convention see Fast Calling Convention (tail call)
1079// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001080
1081/// AddLiveIn - This helper function adds the specified physical register to the
1082/// MachineFunction as a live in value. It also creates a corresponding virtual
1083/// register for it.
1084static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1085 const TargetRegisterClass *RC) {
1086 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001087 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1088 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 return VReg;
1090}
1091
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001092/// CallIsStructReturn - Determines whether a CALL node uses struct return
1093/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001094static bool CallIsStructReturn(CallSDNode *TheCall) {
1095 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001096 if (!NumOps)
1097 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001098
Dan Gohman705e3f72008-09-13 01:54:27 +00001099 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001100}
1101
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001102/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1103/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001104static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001105 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001106 if (!NumArgs)
1107 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001108
1109 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001110}
1111
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001112/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1113/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001114/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001115bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001116 if (IsVarArg)
1117 return false;
1118
Dan Gohman705e3f72008-09-13 01:54:27 +00001119 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001120 default:
1121 return false;
1122 case CallingConv::X86_StdCall:
1123 return !Subtarget->is64Bit();
1124 case CallingConv::X86_FastCall:
1125 return !Subtarget->is64Bit();
1126 case CallingConv::Fast:
1127 return PerformTailCallOpt;
1128 }
1129}
1130
Dan Gohman705e3f72008-09-13 01:54:27 +00001131/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1132/// given CallingConvention value.
1133CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001134 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001135 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001136 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001137 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1138 return CC_X86_64_TailCall;
1139 else
1140 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001141 }
1142
Gordon Henriksen18ace102008-01-05 16:56:59 +00001143 if (CC == CallingConv::X86_FastCall)
1144 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001145 else if (CC == CallingConv::Fast)
1146 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001147 else
1148 return CC_X86_32_C;
1149}
1150
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001151/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1152/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001153NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001154X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001155 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001156 if (CC == CallingConv::X86_FastCall)
1157 return FastCall;
1158 else if (CC == CallingConv::X86_StdCall)
1159 return StdCall;
1160 return None;
1161}
1162
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001163
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001164/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1165/// in a register before calling.
1166bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1167 return !IsTailCall && !Is64Bit &&
1168 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1169 Subtarget->isPICStyleGOT();
1170}
1171
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001172/// CallRequiresFnAddressInReg - Check whether the call requires the function
1173/// address to be loaded in a register.
1174bool
1175X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1176 return !Is64Bit && IsTailCall &&
1177 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1178 Subtarget->isPICStyleGOT();
1179}
1180
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001181/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1182/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001183/// the specific parameter attribute. The copy will be passed as a byval
1184/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001185static SDValue
1186CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001187 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001188 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001189 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001190 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001191}
1192
Dan Gohman8181bd12008-07-27 21:46:04 +00001193SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001194 const CCValAssign &VA,
1195 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001196 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001197 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001198 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001199 ISD::ArgFlagsTy Flags =
1200 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001201 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001202 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001203
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001204 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1205 // changed with more analysis.
1206 // In case of tail call optimization mark all arguments mutable. Since they
1207 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001208 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001209 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001210 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001211 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001212 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001213 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001214 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001215}
1216
Dan Gohman8181bd12008-07-27 21:46:04 +00001217SDValue
1218X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001219 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001220 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1221
1222 const Function* Fn = MF.getFunction();
1223 if (Fn->hasExternalLinkage() &&
1224 Subtarget->isTargetCygMing() &&
1225 Fn->getName() == "main")
1226 FuncInfo->setForceFramePointer(true);
1227
1228 // Decorate the function name.
1229 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1230
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001231 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001232 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001233 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001234 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001235 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001236 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001237
1238 assert(!(isVarArg && CC == CallingConv::Fast) &&
1239 "Var args not supported with calling convention fastcc");
1240
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 // Assign locations to all of the incoming arguments.
1242 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001243 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001244 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001245
Dan Gohman8181bd12008-07-27 21:46:04 +00001246 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001247 unsigned LastVal = ~0U;
1248 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1249 CCValAssign &VA = ArgLocs[i];
1250 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1251 // places.
1252 assert(VA.getValNo() != LastVal &&
1253 "Don't support value assigned to multiple locs yet");
1254 LastVal = VA.getValNo();
1255
1256 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001257 MVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001258 TargetRegisterClass *RC = NULL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 if (RegVT == MVT::i32)
1260 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001261 else if (Is64Bit && RegVT == MVT::i64)
1262 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001263 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001264 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001265 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001266 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001267 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001268 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001269 else if (RegVT.isVector()) {
1270 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001271 if (!Is64Bit)
1272 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1273 else {
1274 // Darwin calling convention passes MMX values in either GPRs or
1275 // XMMs in x86-64. Other targets pass them in memory.
1276 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1277 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1278 RegVT = MVT::v2i64;
1279 } else {
1280 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1281 RegVT = MVT::i64;
1282 }
1283 }
1284 } else {
1285 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001287
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001289 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290
1291 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1292 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1293 // right size.
1294 if (VA.getLocInfo() == CCValAssign::SExt)
1295 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1296 DAG.getValueType(VA.getValVT()));
1297 else if (VA.getLocInfo() == CCValAssign::ZExt)
1298 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1299 DAG.getValueType(VA.getValVT()));
1300
1301 if (VA.getLocInfo() != CCValAssign::Full)
1302 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1303
Gordon Henriksen18ace102008-01-05 16:56:59 +00001304 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001305 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001306 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001307 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1308 else if (RC == X86::VR128RegisterClass) {
1309 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1310 DAG.getConstant(0, MVT::i64));
1311 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1312 }
1313 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 ArgValues.push_back(ArgValue);
1316 } else {
1317 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001318 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001319 }
1320 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001321
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001322 // The x86-64 ABI for returning structs by value requires that we copy
1323 // the sret argument into %rax for the return. Save the argument into
1324 // a virtual register so that we can access it from the return points.
1325 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1326 MachineFunction &MF = DAG.getMachineFunction();
1327 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1328 unsigned Reg = FuncInfo->getSRetReturnReg();
1329 if (!Reg) {
1330 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1331 FuncInfo->setSRetReturnReg(Reg);
1332 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001333 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001334 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1335 }
1336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001338 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001339 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001340 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341
1342 // If the function takes variable number of arguments, make a frame index for
1343 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001344 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001345 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1346 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1347 }
1348 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001349 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1350
1351 // FIXME: We should really autogenerate these arrays
1352 static const unsigned GPR64ArgRegsWin64[] = {
1353 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001354 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001355 static const unsigned XMMArgRegsWin64[] = {
1356 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1357 };
1358 static const unsigned GPR64ArgRegs64Bit[] = {
1359 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1360 };
1361 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1363 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1364 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001365 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1366
1367 if (IsWin64) {
1368 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1369 GPR64ArgRegs = GPR64ArgRegsWin64;
1370 XMMArgRegs = XMMArgRegsWin64;
1371 } else {
1372 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1373 GPR64ArgRegs = GPR64ArgRegs64Bit;
1374 XMMArgRegs = XMMArgRegs64Bit;
1375 }
1376 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1377 TotalNumIntRegs);
1378 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1379 TotalNumXMMRegs);
1380
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381 // For X86-64, if there are vararg parameters that are passed via
1382 // registers, then we must store them to their spots on the stack so they
1383 // may be loaded by deferencing the result of va_next.
1384 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001385 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1386 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1387 TotalNumXMMRegs * 16, 16);
1388
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001390 SmallVector<SDValue, 8> MemOps;
1391 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1392 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001393 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001394 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1396 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001397 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1398 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001399 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001400 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001401 MemOps.push_back(Store);
1402 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001403 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001404 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001405
Gordon Henriksen18ace102008-01-05 16:56:59 +00001406 // Now store the XMM (fp + vector) parameter registers.
1407 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001408 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001409 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001410 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1411 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001412 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1413 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001414 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001415 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001416 MemOps.push_back(Store);
1417 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001418 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001419 }
1420 if (!MemOps.empty())
1421 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1422 &MemOps[0], MemOps.size());
1423 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001424 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001426 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001427
Gordon Henriksen18ace102008-01-05 16:56:59 +00001428 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001429 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001430 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 BytesCallerReserves = 0;
1432 } else {
1433 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001434 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001435 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001436 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 BytesCallerReserves = StackSize;
1438 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001439
Gordon Henriksen18ace102008-01-05 16:56:59 +00001440 if (!Is64Bit) {
1441 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1442 if (CC == CallingConv::X86_FastCall)
1443 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1444 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445
Anton Korobeynikove844e472007-08-15 17:12:32 +00001446 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447
1448 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001449 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1450 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001451}
1452
Dan Gohman8181bd12008-07-27 21:46:04 +00001453SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001454X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001455 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001456 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001457 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001458 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001459 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001460 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001461 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001462 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001463 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001464 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001465 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001466 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001467}
1468
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001469/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1470/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001471SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001472X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001473 SDValue &OutRetAddr,
1474 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001475 bool IsTailCall,
1476 bool Is64Bit,
1477 int FPDiff) {
1478 if (!IsTailCall || FPDiff==0) return Chain;
1479
1480 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001481 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001482 OutRetAddr = getReturnAddressFrameIndex(DAG);
1483 // Load the "old" Return address.
1484 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001485 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001486}
1487
1488/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1489/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001490static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001491EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001492 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001493 bool Is64Bit, int FPDiff) {
1494 // Store the return address to the appropriate stack slot.
1495 if (!FPDiff) return Chain;
1496 // Calculate the new stack slot for the return address.
1497 int SlotSize = Is64Bit ? 8 : 4;
1498 int NewReturnAddrFI =
1499 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001500 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001501 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001502 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001503 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001504 return Chain;
1505}
1506
Dan Gohman8181bd12008-07-27 21:46:04 +00001507SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001508 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001509 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1510 SDValue Chain = TheCall->getChain();
1511 unsigned CC = TheCall->getCallingConv();
1512 bool isVarArg = TheCall->isVarArg();
1513 bool IsTailCall = TheCall->isTailCall() &&
1514 CC == CallingConv::Fast && PerformTailCallOpt;
1515 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001516 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001517 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001518
1519 assert(!(isVarArg && CC == CallingConv::Fast) &&
1520 "Var args not supported with calling convention fastcc");
1521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 // Analyze operands of the call, assigning locations to each operand.
1523 SmallVector<CCValAssign, 16> ArgLocs;
1524 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001525 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526
1527 // Get a count of how many bytes are to be pushed on the stack.
1528 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001529 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001530 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531
Gordon Henriksen18ace102008-01-05 16:56:59 +00001532 int FPDiff = 0;
1533 if (IsTailCall) {
1534 // Lower arguments at fp - stackoffset + fpdiff.
1535 unsigned NumBytesCallerPushed =
1536 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1537 FPDiff = NumBytesCallerPushed - NumBytes;
1538
1539 // Set the delta of movement of the returnaddr stackslot.
1540 // But only set if delta is greater than previous delta.
1541 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1542 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1543 }
1544
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001545 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546
Dan Gohman8181bd12008-07-27 21:46:04 +00001547 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001548 // Load return adress for tail calls.
1549 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1550 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001551
Dan Gohman8181bd12008-07-27 21:46:04 +00001552 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1553 SmallVector<SDValue, 8> MemOpChains;
1554 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001556 // Walk the register/memloc assignments, inserting copies/loads. In the case
1557 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001558 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1559 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001560 SDValue Arg = TheCall->getArg(i);
1561 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1562 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564 // Promote the value if needed.
1565 switch (VA.getLocInfo()) {
1566 default: assert(0 && "Unknown loc info!");
1567 case CCValAssign::Full: break;
1568 case CCValAssign::SExt:
1569 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1570 break;
1571 case CCValAssign::ZExt:
1572 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1573 break;
1574 case CCValAssign::AExt:
1575 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1576 break;
1577 }
1578
1579 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001580 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001581 MVT RegVT = VA.getLocVT();
1582 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001583 switch (VA.getLocReg()) {
1584 default:
1585 break;
1586 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1587 case X86::R8: {
1588 // Special case: passing MMX values in GPR registers.
1589 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1590 break;
1591 }
1592 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1593 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1594 // Special case: passing MMX values in XMM registers.
1595 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1596 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1597 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1598 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1599 getMOVLMask(2, DAG));
1600 break;
1601 }
1602 }
1603 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001604 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1605 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001606 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001607 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001608 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001609 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1610
Dan Gohman705e3f72008-09-13 01:54:27 +00001611 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1612 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001613 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 }
1615 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001616
1617 if (!MemOpChains.empty())
1618 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1619 &MemOpChains[0], MemOpChains.size());
1620
1621 // Build a sequence of copy-to-reg nodes chained together with token chain
1622 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001623 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001624 // Tail call byval lowering might overwrite argument registers so in case of
1625 // tail call optimization the copies to registers are lowered later.
1626 if (!IsTailCall)
1627 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1628 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1629 InFlag);
1630 InFlag = Chain.getValue(1);
1631 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001634 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001635 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1636 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1637 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1638 InFlag);
1639 InFlag = Chain.getValue(1);
1640 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001641 // If we are tail calling and generating PIC/GOT style code load the address
1642 // of the callee into ecx. The value in ecx is used as target of the tail
1643 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1644 // calls on PIC/GOT architectures. Normally we would just put the address of
1645 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1646 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001647 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001648 // Note: The actual moving to ecx is done further down.
1649 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001650 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001651 !G->getGlobal()->hasProtectedVisibility())
1652 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001653 else if (isa<ExternalSymbolSDNode>(Callee))
1654 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001656
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 if (Is64Bit && isVarArg) {
1658 // From AMD64 ABI document:
1659 // For calls that may call functions that use varargs or stdargs
1660 // (prototype-less calls or calls to functions containing ellipsis (...) in
1661 // the declaration) %al is used as hidden argument to specify the number
1662 // of SSE registers used. The contents of %al do not need to match exactly
1663 // the number of registers, but must be an ubound on the number of SSE
1664 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001665
1666 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 // Count the number of XMM registers allocated.
1668 static const unsigned XMMArgRegs[] = {
1669 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1670 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1671 };
1672 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1673
1674 Chain = DAG.getCopyToReg(Chain, X86::AL,
1675 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1676 InFlag = Chain.getValue(1);
1677 }
1678
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001679
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001680 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001682 SmallVector<SDValue, 8> MemOpChains2;
1683 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001684 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001685 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
1689 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001690 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001691 SDValue Arg = TheCall->getArg(i);
1692 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001693 // Create frame index.
1694 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001695 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001697 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001698
Duncan Sandsc93fae32008-03-21 09:14:45 +00001699 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001700 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001701 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001702 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001703 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1704 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1705
1706 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001707 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001708 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001709 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001710 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001711 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001712 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001713 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714 }
1715 }
1716
1717 if (!MemOpChains2.empty())
1718 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001719 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001721 // Copy arguments to their registers.
1722 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1723 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1724 InFlag);
1725 InFlag = Chain.getValue(1);
1726 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001727 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001728
Gordon Henriksen18ace102008-01-05 16:56:59 +00001729 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001730 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1731 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 }
1733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 // If the callee is a GlobalAddress node (quite common, every direct call is)
1735 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1736 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1737 // We should use extra load for direct calls to dllimported functions in
1738 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001739 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1740 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001741 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1742 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001743 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1744 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001745 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001746 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747
1748 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001749 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001750 Callee,InFlag);
1751 Callee = DAG.getRegister(Opc, getPointerTy());
1752 // Add register as live out.
1753 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001754 }
1755
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001756 // Returns a chain & a flag for retval copy to use.
1757 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001758 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759
1760 if (IsTailCall) {
1761 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001762 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1763 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001764 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 Ops.push_back(InFlag);
1766 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1767 InFlag = Chain.getValue(1);
1768
1769 // Returns a chain & a flag for retval copy to use.
1770 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1771 Ops.clear();
1772 }
1773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 Ops.push_back(Chain);
1775 Ops.push_back(Callee);
1776
Gordon Henriksen18ace102008-01-05 16:56:59 +00001777 if (IsTailCall)
1778 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779
Gordon Henriksen18ace102008-01-05 16:56:59 +00001780 // Add argument registers to the end of the list so that they are known live
1781 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001782 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1783 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1784 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
Evan Cheng8ba45e62008-03-18 23:36:35 +00001786 // Add an implicit use GOT pointer in EBX.
1787 if (!IsTailCall && !Is64Bit &&
1788 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1789 Subtarget->isPICStyleGOT())
1790 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1791
1792 // Add an implicit use of AL for x86 vararg functions.
1793 if (Is64Bit && isVarArg)
1794 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1795
Gabor Greif1c80d112008-08-28 21:40:38 +00001796 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001800 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001801 "Flag must be set. Depend on flag being set in LowerRET");
1802 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001803 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001804
Gabor Greif1c80d112008-08-28 21:40:38 +00001805 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001806 }
1807
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001808 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 InFlag = Chain.getValue(1);
1810
1811 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001812 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001813 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001814 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001815 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001816 // If this is is a call to a struct-return function, the callee
1817 // pops the hidden struct pointer, so we have to push it back.
1818 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001819 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001820 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001821 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001822
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001823 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001824 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001825 DAG.getIntPtrConstant(NumBytes, true),
1826 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1827 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001828 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 InFlag = Chain.getValue(1);
1830
1831 // Handle result values, copying them out of physregs into vregs that we
1832 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001833 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001834 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835}
1836
1837
1838//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001839// Fast Calling Convention (tail call) implementation
1840//===----------------------------------------------------------------------===//
1841
1842// Like std call, callee cleans arguments, convention except that ECX is
1843// reserved for storing the tail called function address. Only 2 registers are
1844// free for argument passing (inreg). Tail call optimization is performed
1845// provided:
1846// * tailcallopt is enabled
1847// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001848// On X86_64 architecture with GOT-style position independent code only local
1849// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001850// To keep the stack aligned according to platform abi the function
1851// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1852// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001853// If a tail called function callee has more arguments than the caller the
1854// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001855// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856// original REtADDR, but before the saved framepointer or the spilled registers
1857// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1858// stack layout:
1859// arg1
1860// arg2
1861// RETADDR
1862// [ new RETADDR
1863// move area ]
1864// (possible EBP)
1865// ESI
1866// EDI
1867// local1 ..
1868
1869/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1870/// for a 16 byte align requirement.
1871unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1872 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001873 MachineFunction &MF = DAG.getMachineFunction();
1874 const TargetMachine &TM = MF.getTarget();
1875 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1876 unsigned StackAlignment = TFI.getStackAlignment();
1877 uint64_t AlignMask = StackAlignment - 1;
1878 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001879 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001880 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1881 // Number smaller than 12 so just add the difference.
1882 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1883 } else {
1884 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1885 Offset = ((~AlignMask) & Offset) + StackAlignment +
1886 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001887 }
Evan Chengded8f902008-09-07 09:07:23 +00001888 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001889}
1890
1891/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001892/// following the call is a return. A function is eligible if caller/callee
1893/// calling conventions match, currently only fastcc supports tail calls, and
1894/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001895bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001896 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001897 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001898 if (!PerformTailCallOpt)
1899 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001900
Dan Gohman705e3f72008-09-13 01:54:27 +00001901 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001902 MachineFunction &MF = DAG.getMachineFunction();
1903 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001904 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001905 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001906 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001907 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001908 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001909 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001910 return true;
1911
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001912 // Can only do local tail calls (in same module, hidden or protected) on
1913 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001914 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1915 return G->getGlobal()->hasHiddenVisibility()
1916 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001917 }
1918 }
Evan Chenge7a87392007-11-02 01:26:22 +00001919
1920 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001921}
1922
Dan Gohmanca4857a2008-09-03 23:12:08 +00001923FastISel *
1924X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001925 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00001926 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001927 DenseMap<const Value *, unsigned> &vm,
1928 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001929 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001930 DenseMap<const AllocaInst *, int> &am
1931#ifndef NDEBUG
1932 , SmallSet<Instruction*, 8> &cil
1933#endif
1934 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00001935 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00001936#ifndef NDEBUG
1937 , cil
1938#endif
1939 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001940}
1941
1942
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001943//===----------------------------------------------------------------------===//
1944// Other Lowering Hooks
1945//===----------------------------------------------------------------------===//
1946
1947
Dan Gohman8181bd12008-07-27 21:46:04 +00001948SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001949 MachineFunction &MF = DAG.getMachineFunction();
1950 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1951 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001952 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001953
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 if (ReturnAddrIndex == 0) {
1955 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001956 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001957 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001958 }
1959
1960 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1961}
1962
1963
Chris Lattnerebb91142008-12-24 23:53:05 +00001964/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
1965/// specific condition code, returning the condition code and the LHS/RHS of the
1966/// comparison to make.
1967static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1968 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 if (!isFP) {
1970 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1971 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1972 // X > -1 -> X == 0, jump !sign.
1973 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001974 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001975 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1976 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00001977 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001978 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001979 // X < 1 -> X <= 0
1980 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00001981 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 }
1983 }
1984
1985 switch (SetCCOpcode) {
Chris Lattnerb8397512008-12-23 23:42:27 +00001986 default: assert(0 && "Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00001987 case ISD::SETEQ: return X86::COND_E;
1988 case ISD::SETGT: return X86::COND_G;
1989 case ISD::SETGE: return X86::COND_GE;
1990 case ISD::SETLT: return X86::COND_L;
1991 case ISD::SETLE: return X86::COND_LE;
1992 case ISD::SETNE: return X86::COND_NE;
1993 case ISD::SETULT: return X86::COND_B;
1994 case ISD::SETUGT: return X86::COND_A;
1995 case ISD::SETULE: return X86::COND_BE;
1996 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 }
Chris Lattnerb8397512008-12-23 23:42:27 +00001998 }
1999
2000 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002001
Chris Lattnerb8397512008-12-23 23:42:27 +00002002 // If LHS is a foldable load, but RHS is not, flip the condition.
2003 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2004 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2005 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2006 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002007 }
2008
Chris Lattnerb8397512008-12-23 23:42:27 +00002009 switch (SetCCOpcode) {
2010 default: break;
2011 case ISD::SETOLT:
2012 case ISD::SETOLE:
2013 case ISD::SETUGT:
2014 case ISD::SETUGE:
2015 std::swap(LHS, RHS);
2016 break;
2017 }
2018
2019 // On a floating point condition, the flags are set as follows:
2020 // ZF PF CF op
2021 // 0 | 0 | 0 | X > Y
2022 // 0 | 0 | 1 | X < Y
2023 // 1 | 0 | 0 | X == Y
2024 // 1 | 1 | 1 | unordered
2025 switch (SetCCOpcode) {
Chris Lattnerebb91142008-12-24 23:53:05 +00002026 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002027 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002028 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002029 case ISD::SETOLT: // flipped
2030 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002031 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002032 case ISD::SETOLE: // flipped
2033 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002034 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002035 case ISD::SETUGT: // flipped
2036 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002037 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002038 case ISD::SETUGE: // flipped
2039 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002040 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002041 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002042 case ISD::SETNE: return X86::COND_NE;
2043 case ISD::SETUO: return X86::COND_P;
2044 case ISD::SETO: return X86::COND_NP;
Chris Lattnerb8397512008-12-23 23:42:27 +00002045 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046}
2047
2048/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2049/// code. Current x86 isa includes the following FP cmov instructions:
2050/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2051static bool hasFPCMov(unsigned X86CC) {
2052 switch (X86CC) {
2053 default:
2054 return false;
2055 case X86::COND_B:
2056 case X86::COND_BE:
2057 case X86::COND_E:
2058 case X86::COND_P:
2059 case X86::COND_A:
2060 case X86::COND_AE:
2061 case X86::COND_NE:
2062 case X86::COND_NP:
2063 return true;
2064 }
2065}
2066
2067/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2068/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002069static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002070 if (Op.getOpcode() == ISD::UNDEF)
2071 return true;
2072
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002073 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002074 return (Val >= Low && Val < Hi);
2075}
2076
2077/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2078/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002079static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 if (Op.getOpcode() == ISD::UNDEF)
2081 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002082 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002083}
2084
2085/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2086/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2087bool X86::isPSHUFDMask(SDNode *N) {
2088 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2089
Dan Gohman7dc19012007-08-02 21:17:01 +00002090 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 return false;
2092
2093 // Check if the value doesn't reference the second vector.
2094 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002095 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 if (Arg.getOpcode() == ISD::UNDEF) continue;
2097 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002098 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 return false;
2100 }
2101
2102 return true;
2103}
2104
2105/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2106/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2107bool X86::isPSHUFHWMask(SDNode *N) {
2108 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2109
2110 if (N->getNumOperands() != 8)
2111 return false;
2112
2113 // Lower quadword copied in order.
2114 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002115 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116 if (Arg.getOpcode() == ISD::UNDEF) continue;
2117 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002118 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002119 return false;
2120 }
2121
2122 // Upper quadword shuffled.
2123 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002124 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002125 if (Arg.getOpcode() == ISD::UNDEF) continue;
2126 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002127 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002128 if (Val < 4 || Val > 7)
2129 return false;
2130 }
2131
2132 return true;
2133}
2134
2135/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2136/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2137bool X86::isPSHUFLWMask(SDNode *N) {
2138 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2139
2140 if (N->getNumOperands() != 8)
2141 return false;
2142
2143 // Upper quadword copied in order.
2144 for (unsigned i = 4; i != 8; ++i)
2145 if (!isUndefOrEqual(N->getOperand(i), i))
2146 return false;
2147
2148 // Lower quadword shuffled.
2149 for (unsigned i = 0; i != 4; ++i)
2150 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2151 return false;
2152
2153 return true;
2154}
2155
2156/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2157/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002158static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002159 if (NumElems != 2 && NumElems != 4) return false;
2160
2161 unsigned Half = NumElems / 2;
2162 for (unsigned i = 0; i < Half; ++i)
2163 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2164 return false;
2165 for (unsigned i = Half; i < NumElems; ++i)
2166 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2167 return false;
2168
2169 return true;
2170}
2171
2172bool X86::isSHUFPMask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2175}
2176
2177/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2178/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2179/// half elements to come from vector 1 (which would equal the dest.) and
2180/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002181static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 if (NumOps != 2 && NumOps != 4) return false;
2183
2184 unsigned Half = NumOps / 2;
2185 for (unsigned i = 0; i < Half; ++i)
2186 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2187 return false;
2188 for (unsigned i = Half; i < NumOps; ++i)
2189 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2190 return false;
2191 return true;
2192}
2193
2194static bool isCommutedSHUFP(SDNode *N) {
2195 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2196 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2197}
2198
2199/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2200/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2201bool X86::isMOVHLPSMask(SDNode *N) {
2202 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2203
2204 if (N->getNumOperands() != 4)
2205 return false;
2206
2207 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2208 return isUndefOrEqual(N->getOperand(0), 6) &&
2209 isUndefOrEqual(N->getOperand(1), 7) &&
2210 isUndefOrEqual(N->getOperand(2), 2) &&
2211 isUndefOrEqual(N->getOperand(3), 3);
2212}
2213
2214/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2215/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2216/// <2, 3, 2, 3>
2217bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219
2220 if (N->getNumOperands() != 4)
2221 return false;
2222
2223 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2224 return isUndefOrEqual(N->getOperand(0), 2) &&
2225 isUndefOrEqual(N->getOperand(1), 3) &&
2226 isUndefOrEqual(N->getOperand(2), 2) &&
2227 isUndefOrEqual(N->getOperand(3), 3);
2228}
2229
2230/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2231/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2232bool X86::isMOVLPMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 unsigned NumElems = N->getNumOperands();
2236 if (NumElems != 2 && NumElems != 4)
2237 return false;
2238
2239 for (unsigned i = 0; i < NumElems/2; ++i)
2240 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2241 return false;
2242
2243 for (unsigned i = NumElems/2; i < NumElems; ++i)
2244 if (!isUndefOrEqual(N->getOperand(i), i))
2245 return false;
2246
2247 return true;
2248}
2249
2250/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2251/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2252/// and MOVLHPS.
2253bool X86::isMOVHPMask(SDNode *N) {
2254 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2255
2256 unsigned NumElems = N->getNumOperands();
2257 if (NumElems != 2 && NumElems != 4)
2258 return false;
2259
2260 for (unsigned i = 0; i < NumElems/2; ++i)
2261 if (!isUndefOrEqual(N->getOperand(i), i))
2262 return false;
2263
2264 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002265 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002266 if (!isUndefOrEqual(Arg, i + NumElems))
2267 return false;
2268 }
2269
2270 return true;
2271}
2272
2273/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2274/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002275bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 bool V2IsSplat = false) {
2277 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2278 return false;
2279
2280 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue BitI = Elts[i];
2282 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 if (!isUndefOrEqual(BitI, j))
2284 return false;
2285 if (V2IsSplat) {
2286 if (isUndefOrEqual(BitI1, NumElts))
2287 return false;
2288 } else {
2289 if (!isUndefOrEqual(BitI1, j + NumElts))
2290 return false;
2291 }
2292 }
2293
2294 return true;
2295}
2296
2297bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2300}
2301
2302/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002304bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 bool V2IsSplat = false) {
2306 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2307 return false;
2308
2309 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002310 SDValue BitI = Elts[i];
2311 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002312 if (!isUndefOrEqual(BitI, j + NumElts/2))
2313 return false;
2314 if (V2IsSplat) {
2315 if (isUndefOrEqual(BitI1, NumElts))
2316 return false;
2317 } else {
2318 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2319 return false;
2320 }
2321 }
2322
2323 return true;
2324}
2325
2326bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2327 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2328 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2329}
2330
2331/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2332/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2333/// <0, 0, 1, 1>
2334bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2335 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2336
2337 unsigned NumElems = N->getNumOperands();
2338 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2339 return false;
2340
2341 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002342 SDValue BitI = N->getOperand(i);
2343 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002344
2345 if (!isUndefOrEqual(BitI, j))
2346 return false;
2347 if (!isUndefOrEqual(BitI1, j))
2348 return false;
2349 }
2350
2351 return true;
2352}
2353
2354/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2355/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2356/// <2, 2, 3, 3>
2357bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359
2360 unsigned NumElems = N->getNumOperands();
2361 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2362 return false;
2363
2364 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002365 SDValue BitI = N->getOperand(i);
2366 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367
2368 if (!isUndefOrEqual(BitI, j))
2369 return false;
2370 if (!isUndefOrEqual(BitI1, j))
2371 return false;
2372 }
2373
2374 return true;
2375}
2376
2377/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2378/// specifies a shuffle of elements that is suitable for input to MOVSS,
2379/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002380static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002381 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 return false;
2383
2384 if (!isUndefOrEqual(Elts[0], NumElts))
2385 return false;
2386
2387 for (unsigned i = 1; i < NumElts; ++i) {
2388 if (!isUndefOrEqual(Elts[i], i))
2389 return false;
2390 }
2391
2392 return true;
2393}
2394
2395bool X86::isMOVLMask(SDNode *N) {
2396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2397 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2398}
2399
2400/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2401/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2402/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002403static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 bool V2IsSplat = false,
2405 bool V2IsUndef = false) {
2406 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2407 return false;
2408
2409 if (!isUndefOrEqual(Ops[0], 0))
2410 return false;
2411
2412 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002413 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2415 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2416 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2417 return false;
2418 }
2419
2420 return true;
2421}
2422
2423static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2424 bool V2IsUndef = false) {
2425 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2426 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2427 V2IsSplat, V2IsUndef);
2428}
2429
2430/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2431/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2432bool X86::isMOVSHDUPMask(SDNode *N) {
2433 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2434
2435 if (N->getNumOperands() != 4)
2436 return false;
2437
2438 // Expect 1, 1, 3, 3
2439 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002440 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 if (Arg.getOpcode() == ISD::UNDEF) continue;
2442 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002443 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 if (Val != 1) return false;
2445 }
2446
2447 bool HasHi = false;
2448 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002449 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002450 if (Arg.getOpcode() == ISD::UNDEF) continue;
2451 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002452 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002453 if (Val != 3) return false;
2454 HasHi = true;
2455 }
2456
2457 // Don't use movshdup if it can be done with a shufps.
2458 return HasHi;
2459}
2460
2461/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2462/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2463bool X86::isMOVSLDUPMask(SDNode *N) {
2464 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2465
2466 if (N->getNumOperands() != 4)
2467 return false;
2468
2469 // Expect 0, 0, 2, 2
2470 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002471 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002472 if (Arg.getOpcode() == ISD::UNDEF) continue;
2473 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002474 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002475 if (Val != 0) return false;
2476 }
2477
2478 bool HasHi = false;
2479 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002480 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002481 if (Arg.getOpcode() == ISD::UNDEF) continue;
2482 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002483 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 if (Val != 2) return false;
2485 HasHi = true;
2486 }
2487
2488 // Don't use movshdup if it can be done with a shufps.
2489 return HasHi;
2490}
2491
2492/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2493/// specifies a identity operation on the LHS or RHS.
2494static bool isIdentityMask(SDNode *N, bool RHS = false) {
2495 unsigned NumElems = N->getNumOperands();
2496 for (unsigned i = 0; i < NumElems; ++i)
2497 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2498 return false;
2499 return true;
2500}
2501
2502/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2503/// a splat of a single element.
2504static bool isSplatMask(SDNode *N) {
2505 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2506
2507 // This is a splat operation if each element of the permute is the same, and
2508 // if the value doesn't reference the second vector.
2509 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002510 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002511 unsigned i = 0;
2512 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002513 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514 if (isa<ConstantSDNode>(Elt)) {
2515 ElementBase = Elt;
2516 break;
2517 }
2518 }
2519
Gabor Greif1c80d112008-08-28 21:40:38 +00002520 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002521 return false;
2522
2523 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002524 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525 if (Arg.getOpcode() == ISD::UNDEF) continue;
2526 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2527 if (Arg != ElementBase) return false;
2528 }
2529
2530 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002531 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002532}
2533
Mon P Wang532c9632008-12-23 04:03:27 +00002534/// getSplatMaskEltNo - Given a splat mask, return the index to the element
2535/// we want to splat.
2536static SDValue getSplatMaskEltNo(SDNode *N) {
2537 assert(isSplatMask(N) && "Not a splat mask");
2538 unsigned NumElems = N->getNumOperands();
2539 SDValue ElementBase;
2540 unsigned i = 0;
2541 for (; i != NumElems; ++i) {
2542 SDValue Elt = N->getOperand(i);
2543 if (isa<ConstantSDNode>(Elt))
2544 return Elt;
2545 }
2546 assert(0 && " No splat value found!");
2547 return SDValue();
2548}
2549
2550
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2552/// a splat of a single element and it's a 2 or 4 element mask.
2553bool X86::isSplatMask(SDNode *N) {
2554 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2555
2556 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2557 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2558 return false;
2559 return ::isSplatMask(N);
2560}
2561
2562/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2563/// specifies a splat of zero element.
2564bool X86::isSplatLoMask(SDNode *N) {
2565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2566
2567 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2568 if (!isUndefOrEqual(N->getOperand(i), 0))
2569 return false;
2570 return true;
2571}
2572
Evan Chenga2497eb2008-09-25 20:50:48 +00002573/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2574/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2575bool X86::isMOVDDUPMask(SDNode *N) {
2576 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2577
2578 unsigned e = N->getNumOperands() / 2;
2579 for (unsigned i = 0; i < e; ++i)
2580 if (!isUndefOrEqual(N->getOperand(i), i))
2581 return false;
2582 for (unsigned i = 0; i < e; ++i)
2583 if (!isUndefOrEqual(N->getOperand(e+i), i))
2584 return false;
2585 return true;
2586}
2587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2589/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2590/// instructions.
2591unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2592 unsigned NumOperands = N->getNumOperands();
2593 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2594 unsigned Mask = 0;
2595 for (unsigned i = 0; i < NumOperands; ++i) {
2596 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002597 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002598 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002599 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 if (Val >= NumOperands) Val -= NumOperands;
2601 Mask |= Val;
2602 if (i != NumOperands - 1)
2603 Mask <<= Shift;
2604 }
2605
2606 return Mask;
2607}
2608
2609/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2610/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2611/// instructions.
2612unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2613 unsigned Mask = 0;
2614 // 8 nodes, but we only care about the last 4.
2615 for (unsigned i = 7; i >= 4; --i) {
2616 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002617 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002619 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 Mask |= (Val - 4);
2621 if (i != 4)
2622 Mask <<= 2;
2623 }
2624
2625 return Mask;
2626}
2627
2628/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2629/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2630/// instructions.
2631unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2632 unsigned Mask = 0;
2633 // 8 nodes, but we only care about the first 4.
2634 for (int i = 3; i >= 0; --i) {
2635 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002636 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002638 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 Mask |= Val;
2640 if (i != 0)
2641 Mask <<= 2;
2642 }
2643
2644 return Mask;
2645}
2646
2647/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2648/// specifies a 8 element shuffle that can be broken into a pair of
2649/// PSHUFHW and PSHUFLW.
2650static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2651 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2652
2653 if (N->getNumOperands() != 8)
2654 return false;
2655
2656 // Lower quadword shuffled.
2657 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002658 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 if (Arg.getOpcode() == ISD::UNDEF) continue;
2660 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002661 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002662 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 return false;
2664 }
2665
2666 // Upper quadword shuffled.
2667 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002668 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 if (Arg.getOpcode() == ISD::UNDEF) continue;
2670 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002671 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 if (Val < 4 || Val > 7)
2673 return false;
2674 }
2675
2676 return true;
2677}
2678
Chris Lattnere6aa3862007-11-25 00:24:49 +00002679/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002680/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002681static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2682 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002684 MVT VT = Op.getValueType();
2685 MVT MaskVT = Mask.getValueType();
2686 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002688 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689
2690 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002691 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 if (Arg.getOpcode() == ISD::UNDEF) {
2693 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2694 continue;
2695 }
2696 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002697 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002698 if (Val < NumElems)
2699 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2700 else
2701 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2702 }
2703
2704 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002705 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2707}
2708
Evan Chenga6769df2007-12-07 21:30:01 +00002709/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2710/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002711static
Dan Gohman8181bd12008-07-27 21:46:04 +00002712SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002713 MVT MaskVT = Mask.getValueType();
2714 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002715 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002716 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002717 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002718 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002719 if (Arg.getOpcode() == ISD::UNDEF) {
2720 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2721 continue;
2722 }
2723 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002724 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002725 if (Val < NumElems)
2726 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2727 else
2728 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2729 }
2730 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2731}
2732
2733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2735/// match movhlps. The lower half elements should come from upper half of
2736/// V1 (and in order), and the upper half elements should come from the upper
2737/// half of V2 (and in order).
2738static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2739 unsigned NumElems = Mask->getNumOperands();
2740 if (NumElems != 4)
2741 return false;
2742 for (unsigned i = 0, e = 2; i != e; ++i)
2743 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2744 return false;
2745 for (unsigned i = 2; i != 4; ++i)
2746 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2747 return false;
2748 return true;
2749}
2750
2751/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002752/// is promoted to a vector. It also returns the LoadSDNode by reference if
2753/// required.
2754static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002755 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2756 return false;
2757 N = N->getOperand(0).getNode();
2758 if (!ISD::isNON_EXTLoad(N))
2759 return false;
2760 if (LD)
2761 *LD = cast<LoadSDNode>(N);
2762 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763}
2764
2765/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2766/// match movlp{s|d}. The lower half elements should come from lower half of
2767/// V1 (and in order), and the upper half elements should come from the upper
2768/// half of V2 (and in order). And since V1 will become the source of the
2769/// MOVLP, it must be either a vector load or a scalar load to vector.
2770static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2771 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2772 return false;
2773 // Is V2 is a vector load, don't do this transformation. We will try to use
2774 // load folding shufps op.
2775 if (ISD::isNON_EXTLoad(V2))
2776 return false;
2777
2778 unsigned NumElems = Mask->getNumOperands();
2779 if (NumElems != 2 && NumElems != 4)
2780 return false;
2781 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2782 if (!isUndefOrEqual(Mask->getOperand(i), i))
2783 return false;
2784 for (unsigned i = NumElems/2; i != NumElems; ++i)
2785 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2786 return false;
2787 return true;
2788}
2789
2790/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2791/// all the same.
2792static bool isSplatVector(SDNode *N) {
2793 if (N->getOpcode() != ISD::BUILD_VECTOR)
2794 return false;
2795
Dan Gohman8181bd12008-07-27 21:46:04 +00002796 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2798 if (N->getOperand(i) != SplatValue)
2799 return false;
2800 return true;
2801}
2802
2803/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2804/// to an undef.
2805static bool isUndefShuffle(SDNode *N) {
2806 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2807 return false;
2808
Dan Gohman8181bd12008-07-27 21:46:04 +00002809 SDValue V1 = N->getOperand(0);
2810 SDValue V2 = N->getOperand(1);
2811 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 unsigned NumElems = Mask.getNumOperands();
2813 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002814 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002815 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002816 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002817 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2818 return false;
2819 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2820 return false;
2821 }
2822 }
2823 return true;
2824}
2825
2826/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2827/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002828static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002829 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002830 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002831 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002832 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833}
2834
2835/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2836/// to an zero vector.
2837static bool isZeroShuffle(SDNode *N) {
2838 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2839 return false;
2840
Dan Gohman8181bd12008-07-27 21:46:04 +00002841 SDValue V1 = N->getOperand(0);
2842 SDValue V2 = N->getOperand(1);
2843 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002844 unsigned NumElems = Mask.getNumOperands();
2845 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002847 if (Arg.getOpcode() == ISD::UNDEF)
2848 continue;
2849
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002850 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002851 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002852 unsigned Opc = V1.getNode()->getOpcode();
2853 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002854 continue;
2855 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002856 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002857 return false;
2858 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002859 unsigned Opc = V2.getNode()->getOpcode();
2860 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002861 continue;
2862 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002863 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002864 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865 }
2866 }
2867 return true;
2868}
2869
2870/// getZeroVector - Returns a vector of specified type with all zero elements.
2871///
Dan Gohman8181bd12008-07-27 21:46:04 +00002872static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002873 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002874
2875 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2876 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002877 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002878 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002879 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002880 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002881 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002882 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002883 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002884 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002885 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002886 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2887 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002888 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889}
2890
Chris Lattnere6aa3862007-11-25 00:24:49 +00002891/// getOnesVector - Returns a vector of specified type with all bits set.
2892///
Dan Gohman8181bd12008-07-27 21:46:04 +00002893static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002894 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002895
2896 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2897 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2899 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002900 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002901 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2902 else // SSE
2903 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2904 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2905}
2906
2907
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002908/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2909/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002910static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002911 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2912
2913 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002914 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 unsigned NumElems = Mask.getNumOperands();
2916 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002917 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002918 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002919 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920 if (Val > NumElems) {
2921 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2922 Changed = true;
2923 }
2924 }
2925 MaskVec.push_back(Arg);
2926 }
2927
2928 if (Changed)
2929 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2930 &MaskVec[0], MaskVec.size());
2931 return Mask;
2932}
2933
2934/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2935/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002936static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002937 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2938 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939
Dan Gohman8181bd12008-07-27 21:46:04 +00002940 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2942 for (unsigned i = 1; i != NumElems; ++i)
2943 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2944 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2945}
2946
2947/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2948/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002949static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002950 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2951 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002952 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2954 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2955 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2956 }
2957 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2958}
2959
2960/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2961/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002962static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002963 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2964 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002965 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002966 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967 for (unsigned i = 0; i != Half; ++i) {
2968 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2969 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2970 }
2971 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2972}
2973
Chris Lattner2d91b962008-03-09 01:05:04 +00002974/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2975/// element #0 of a vector with the specified index, leaving the rest of the
2976/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002977static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002978 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002979 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2980 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002981 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002982 // Element #0 of the result gets the elt we are replacing.
2983 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2984 for (unsigned i = 1; i != NumElems; ++i)
2985 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2986 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2987}
2988
Evan Chengbf8b2c52008-04-05 00:30:36 +00002989/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002990static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002991 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2992 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002993 if (PVT == VT)
2994 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002995 SDValue V1 = Op.getOperand(0);
2996 SDValue Mask = Op.getOperand(2);
Mon P Wang532c9632008-12-23 04:03:27 +00002997 unsigned MaskNumElems = Mask.getNumOperands();
2998 unsigned NumElems = MaskNumElems;
Evan Chengbf8b2c52008-04-05 00:30:36 +00002999 // Special handling of v4f32 -> v4i32.
3000 if (VT != MVT::v4f32) {
Mon P Wang532c9632008-12-23 04:03:27 +00003001 // Find which element we want to splat.
3002 SDNode* EltNoNode = getSplatMaskEltNo(Mask.getNode()).getNode();
3003 unsigned EltNo = cast<ConstantSDNode>(EltNoNode)->getZExtValue();
3004 // unpack elements to the correct location
Evan Chengbf8b2c52008-04-05 00:30:36 +00003005 while (NumElems > 4) {
Mon P Wang532c9632008-12-23 04:03:27 +00003006 if (EltNo < NumElems/2) {
3007 Mask = getUnpacklMask(MaskNumElems, DAG);
3008 } else {
3009 Mask = getUnpackhMask(MaskNumElems, DAG);
3010 EltNo -= NumElems/2;
3011 }
Evan Chengbf8b2c52008-04-05 00:30:36 +00003012 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3013 NumElems >>= 1;
3014 }
Mon P Wang532c9632008-12-23 04:03:27 +00003015 SDValue Cst = DAG.getConstant(EltNo, MVT::i32);
3016 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003018
Evan Chengbf8b2c52008-04-05 00:30:36 +00003019 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003020 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003021 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003022 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3023}
3024
Evan Chenga2497eb2008-09-25 20:50:48 +00003025/// isVectorLoad - Returns true if the node is a vector load, a scalar
3026/// load that's promoted to vector, or a load bitcasted.
3027static bool isVectorLoad(SDValue Op) {
3028 assert(Op.getValueType().isVector() && "Expected a vector type");
3029 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3030 Op.getOpcode() == ISD::BIT_CONVERT) {
3031 return isa<LoadSDNode>(Op.getOperand(0));
3032 }
3033 return isa<LoadSDNode>(Op);
3034}
3035
3036
3037/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3038///
3039static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3040 SelectionDAG &DAG, bool HasSSE3) {
3041 // If we have sse3 and shuffle has more than one use or input is a load, then
3042 // use movddup. Otherwise, use movlhps.
3043 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3044 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3045 MVT VT = Op.getValueType();
3046 if (VT == PVT)
3047 return Op;
3048 unsigned NumElems = PVT.getVectorNumElements();
3049 if (NumElems == 2) {
3050 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3051 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3052 } else {
3053 assert(NumElems == 4);
3054 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3055 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3056 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3057 }
3058
3059 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3060 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3061 DAG.getNode(ISD::UNDEF, PVT), Mask);
3062 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3063}
3064
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003065/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003066/// vector of zero or undef vector. This produces a shuffle where the low
3067/// element of V2 is swizzled into the zero/undef vector, landing at element
3068/// 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 +00003069static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003070 bool isZero, bool HasSSE2,
3071 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003072 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003073 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003074 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003075 unsigned NumElems = V2.getValueType().getVectorNumElements();
3076 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3077 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003078 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003079 for (unsigned i = 0; i != NumElems; ++i)
3080 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3081 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3082 else
3083 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003084 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 &MaskVec[0], MaskVec.size());
3086 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3087}
3088
Evan Chengdea99362008-05-29 08:22:04 +00003089/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3090/// a shuffle that is zero.
3091static
Dan Gohman8181bd12008-07-27 21:46:04 +00003092unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003093 unsigned NumElems, bool Low,
3094 SelectionDAG &DAG) {
3095 unsigned NumZeros = 0;
3096 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003097 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003098 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003099 if (Idx.getOpcode() == ISD::UNDEF) {
3100 ++NumZeros;
3101 continue;
3102 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003103 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3104 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003105 ++NumZeros;
3106 else
3107 break;
3108 }
3109 return NumZeros;
3110}
3111
3112/// isVectorShift - Returns true if the shuffle can be implemented as a
3113/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003114static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3115 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003116 unsigned NumElems = Mask.getNumOperands();
3117
3118 isLeft = true;
3119 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3120 if (!NumZeros) {
3121 isLeft = false;
3122 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3123 if (!NumZeros)
3124 return false;
3125 }
3126
3127 bool SeenV1 = false;
3128 bool SeenV2 = false;
3129 for (unsigned i = NumZeros; i < NumElems; ++i) {
3130 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003131 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003132 if (Idx.getOpcode() == ISD::UNDEF)
3133 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003134 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003135 if (Index < NumElems)
3136 SeenV1 = true;
3137 else {
3138 Index -= NumElems;
3139 SeenV2 = true;
3140 }
3141 if (Index != Val)
3142 return false;
3143 }
3144 if (SeenV1 && SeenV2)
3145 return false;
3146
3147 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3148 ShAmt = NumZeros;
3149 return true;
3150}
3151
3152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003153/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3154///
Dan Gohman8181bd12008-07-27 21:46:04 +00003155static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 unsigned NumNonZero, unsigned NumZero,
3157 SelectionDAG &DAG, TargetLowering &TLI) {
3158 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003159 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160
Dan Gohman8181bd12008-07-27 21:46:04 +00003161 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003162 bool First = true;
3163 for (unsigned i = 0; i < 16; ++i) {
3164 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3165 if (ThisIsNonZero && First) {
3166 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003167 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003168 else
3169 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3170 First = false;
3171 }
3172
3173 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003174 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3176 if (LastIsNonZero) {
3177 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3178 }
3179 if (ThisIsNonZero) {
3180 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3181 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3182 ThisElt, DAG.getConstant(8, MVT::i8));
3183 if (LastIsNonZero)
3184 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3185 } else
3186 ThisElt = LastElt;
3187
Gabor Greif1c80d112008-08-28 21:40:38 +00003188 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003189 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003190 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003191 }
3192 }
3193
3194 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3195}
3196
3197/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3198///
Dan Gohman8181bd12008-07-27 21:46:04 +00003199static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 unsigned NumNonZero, unsigned NumZero,
3201 SelectionDAG &DAG, TargetLowering &TLI) {
3202 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003203 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003204
Dan Gohman8181bd12008-07-27 21:46:04 +00003205 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 bool First = true;
3207 for (unsigned i = 0; i < 8; ++i) {
3208 bool isNonZero = (NonZeros & (1 << i)) != 0;
3209 if (isNonZero) {
3210 if (First) {
3211 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003212 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 else
3214 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3215 First = false;
3216 }
3217 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003218 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219 }
3220 }
3221
3222 return V;
3223}
3224
Evan Chengdea99362008-05-29 08:22:04 +00003225/// getVShift - Return a vector logical shift node.
3226///
Dan Gohman8181bd12008-07-27 21:46:04 +00003227static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003228 unsigned NumBits, SelectionDAG &DAG,
3229 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003230 bool isMMX = VT.getSizeInBits() == 64;
3231 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003232 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3233 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3234 return DAG.getNode(ISD::BIT_CONVERT, VT,
3235 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003236 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003237}
3238
Dan Gohman8181bd12008-07-27 21:46:04 +00003239SDValue
3240X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003241 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003242 if (ISD::isBuildVectorAllZeros(Op.getNode())
3243 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003244 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3245 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3246 // eliminated on x86-32 hosts.
3247 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3248 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249
Gabor Greif1c80d112008-08-28 21:40:38 +00003250 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003251 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003252 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003253 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254
Duncan Sands92c43912008-06-06 12:08:01 +00003255 MVT VT = Op.getValueType();
3256 MVT EVT = VT.getVectorElementType();
3257 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258
3259 unsigned NumElems = Op.getNumOperands();
3260 unsigned NumZero = 0;
3261 unsigned NumNonZero = 0;
3262 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003263 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003264 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003265 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003266 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003267 if (Elt.getOpcode() == ISD::UNDEF)
3268 continue;
3269 Values.insert(Elt);
3270 if (Elt.getOpcode() != ISD::Constant &&
3271 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003272 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003273 if (isZeroNode(Elt))
3274 NumZero++;
3275 else {
3276 NonZeros |= (1 << i);
3277 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 }
3279 }
3280
3281 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003282 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3283 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 }
3285
Chris Lattner66a4dda2008-03-09 05:42:06 +00003286 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003287 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003289 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003290
Chris Lattner2d91b962008-03-09 01:05:04 +00003291 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3292 // the value are obviously zero, truncate the value to i32 and do the
3293 // insertion that way. Only do this if the value is non-constant or if the
3294 // value is a constant being inserted into element 0. It is cheaper to do
3295 // a constant pool load than it is to do a movd + shuffle.
3296 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3297 (!IsAllConstants || Idx == 0)) {
3298 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3299 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003300 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3301 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003302
3303 // Truncate the value (which may itself be a constant) to i32, and
3304 // convert it to a vector with movd (S2V+shuffle to zero extend).
3305 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3306 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003307 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3308 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003309
3310 // Now we have our 32-bit value zero extended in the low element of
3311 // a vector. If Idx != 0, swizzle it into place.
3312 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003313 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003314 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3315 getSwapEltZeroMask(VecElts, Idx, DAG)
3316 };
3317 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3318 }
3319 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3320 }
3321 }
3322
Chris Lattnerac914892008-03-08 22:59:52 +00003323 // If we have a constant or non-constant insertion into the low element of
3324 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3325 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3326 // depending on what the source datatype is. Because we can only get here
3327 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3328 if (Idx == 0 &&
3329 // Don't do this for i64 values on x86-32.
3330 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003331 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003333 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3334 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003335 }
Evan Chengdea99362008-05-29 08:22:04 +00003336
3337 // Is it a vector logical left shift?
3338 if (NumElems == 2 && Idx == 1 &&
3339 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003340 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003341 return getVShift(true, VT,
3342 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3343 NumBits/2, DAG, *this);
3344 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003345
3346 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348
Chris Lattnerac914892008-03-08 22:59:52 +00003349 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3350 // is a non-constant being inserted into an element other than the low one,
3351 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3352 // movd/movss) to move this into the low element, then shuffle it into
3353 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003355 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003357 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003358 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3359 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003360 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3361 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003362 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363 for (unsigned i = 0; i < NumElems; i++)
3364 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003365 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003366 &MaskVec[0], MaskVec.size());
3367 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3368 DAG.getNode(ISD::UNDEF, VT), Mask);
3369 }
3370 }
3371
Chris Lattner66a4dda2008-03-09 05:42:06 +00003372 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3373 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003374 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003375
Dan Gohman21463242007-07-24 22:55:08 +00003376 // A vector full of immediates; various special cases are already
3377 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003378 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003379 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003380
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003382 if (EVTBits == 64) {
3383 if (NumNonZero == 1) {
3384 // One half is zero or undef.
3385 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003386 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003387 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003388 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3389 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003390 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003391 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003392 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393
3394 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3395 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003396 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003398 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003399 }
3400
3401 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003402 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003404 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003405 }
3406
3407 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003408 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003409 V.resize(NumElems);
3410 if (NumElems == 4 && NumZero > 0) {
3411 for (unsigned i = 0; i < 4; ++i) {
3412 bool isZero = !(NonZeros & (1 << i));
3413 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003414 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003415 else
3416 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3417 }
3418
3419 for (unsigned i = 0; i < 2; ++i) {
3420 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3421 default: break;
3422 case 0:
3423 V[i] = V[i*2]; // Must be a zero vector.
3424 break;
3425 case 1:
3426 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3427 getMOVLMask(NumElems, DAG));
3428 break;
3429 case 2:
3430 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3431 getMOVLMask(NumElems, DAG));
3432 break;
3433 case 3:
3434 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3435 getUnpacklMask(NumElems, DAG));
3436 break;
3437 }
3438 }
3439
Duncan Sands92c43912008-06-06 12:08:01 +00003440 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3441 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003442 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 bool Reverse = (NonZeros & 0x3) == 2;
3444 for (unsigned i = 0; i < 2; ++i)
3445 if (Reverse)
3446 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3447 else
3448 MaskVec.push_back(DAG.getConstant(i, EVT));
3449 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3450 for (unsigned i = 0; i < 2; ++i)
3451 if (Reverse)
3452 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3453 else
3454 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003455 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003456 &MaskVec[0], MaskVec.size());
3457 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3458 }
3459
3460 if (Values.size() > 2) {
3461 // Expand into a number of unpckl*.
3462 // e.g. for v4f32
3463 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3464 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3465 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003466 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003467 for (unsigned i = 0; i < NumElems; ++i)
3468 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3469 NumElems >>= 1;
3470 while (NumElems != 0) {
3471 for (unsigned i = 0; i < NumElems; ++i)
3472 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3473 UnpckMask);
3474 NumElems >>= 1;
3475 }
3476 return V[0];
3477 }
3478
Dan Gohman8181bd12008-07-27 21:46:04 +00003479 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003480}
3481
Evan Chengfca29242007-12-07 08:07:39 +00003482static
Dan Gohman8181bd12008-07-27 21:46:04 +00003483SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003484 SDValue PermMask, SelectionDAG &DAG,
3485 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003486 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003487 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3488 MVT MaskEVT = MaskVT.getVectorElementType();
3489 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003490 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3491 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003492
3493 // First record which half of which vector the low elements come from.
3494 SmallVector<unsigned, 4> LowQuad(4);
3495 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003496 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003497 if (Elt.getOpcode() == ISD::UNDEF)
3498 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003499 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003500 int QuadIdx = EltIdx / 4;
3501 ++LowQuad[QuadIdx];
3502 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003503
Evan Cheng75184a92007-12-11 01:46:18 +00003504 int BestLowQuad = -1;
3505 unsigned MaxQuad = 1;
3506 for (unsigned i = 0; i < 4; ++i) {
3507 if (LowQuad[i] > MaxQuad) {
3508 BestLowQuad = i;
3509 MaxQuad = LowQuad[i];
3510 }
Evan Chengfca29242007-12-07 08:07:39 +00003511 }
3512
Evan Cheng75184a92007-12-11 01:46:18 +00003513 // Record which half of which vector the high elements come from.
3514 SmallVector<unsigned, 4> HighQuad(4);
3515 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003516 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003517 if (Elt.getOpcode() == ISD::UNDEF)
3518 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003519 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003520 int QuadIdx = EltIdx / 4;
3521 ++HighQuad[QuadIdx];
3522 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003523
Evan Cheng75184a92007-12-11 01:46:18 +00003524 int BestHighQuad = -1;
3525 MaxQuad = 1;
3526 for (unsigned i = 0; i < 4; ++i) {
3527 if (HighQuad[i] > MaxQuad) {
3528 BestHighQuad = i;
3529 MaxQuad = HighQuad[i];
3530 }
3531 }
3532
3533 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3534 if (BestLowQuad != -1 || BestHighQuad != -1) {
3535 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003537
Evan Cheng75184a92007-12-11 01:46:18 +00003538 if (BestLowQuad != -1)
3539 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3540 else
3541 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003542
Evan Cheng75184a92007-12-11 01:46:18 +00003543 if (BestHighQuad != -1)
3544 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3545 else
3546 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003547
Dan Gohman8181bd12008-07-27 21:46:04 +00003548 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003549 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3550 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3551 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3552 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3553
3554 // Now sort high and low parts separately.
3555 BitVector InOrder(8);
3556 if (BestLowQuad != -1) {
3557 // Sort lower half in order using PSHUFLW.
3558 MaskVec.clear();
3559 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003560
Evan Cheng75184a92007-12-11 01:46:18 +00003561 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003562 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003563 if (Elt.getOpcode() == ISD::UNDEF) {
3564 MaskVec.push_back(Elt);
3565 InOrder.set(i);
3566 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003567 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003568 if (EltIdx != i)
3569 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003570
Evan Cheng75184a92007-12-11 01:46:18 +00003571 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003572
Evan Cheng75184a92007-12-11 01:46:18 +00003573 // If this element is in the right place after this shuffle, then
3574 // remember it.
3575 if ((int)(EltIdx / 4) == BestLowQuad)
3576 InOrder.set(i);
3577 }
3578 }
3579 if (AnyOutOrder) {
3580 for (unsigned i = 4; i != 8; ++i)
3581 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003583 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3584 }
3585 }
3586
3587 if (BestHighQuad != -1) {
3588 // Sort high half in order using PSHUFHW if possible.
3589 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003590
Evan Cheng75184a92007-12-11 01:46:18 +00003591 for (unsigned i = 0; i != 4; ++i)
3592 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003593
Evan Cheng75184a92007-12-11 01:46:18 +00003594 bool AnyOutOrder = false;
3595 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003596 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003597 if (Elt.getOpcode() == ISD::UNDEF) {
3598 MaskVec.push_back(Elt);
3599 InOrder.set(i);
3600 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003601 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003602 if (EltIdx != i)
3603 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003604
Evan Cheng75184a92007-12-11 01:46:18 +00003605 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003606
Evan Cheng75184a92007-12-11 01:46:18 +00003607 // If this element is in the right place after this shuffle, then
3608 // remember it.
3609 if ((int)(EltIdx / 4) == BestHighQuad)
3610 InOrder.set(i);
3611 }
3612 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003613
Evan Cheng75184a92007-12-11 01:46:18 +00003614 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003615 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003616 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3617 }
3618 }
3619
3620 // The other elements are put in the right place using pextrw and pinsrw.
3621 for (unsigned i = 0; i != 8; ++i) {
3622 if (InOrder[i])
3623 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003625 if (Elt.getOpcode() == ISD::UNDEF)
3626 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003627 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003628 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003629 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3630 DAG.getConstant(EltIdx, PtrVT))
3631 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3632 DAG.getConstant(EltIdx - 8, PtrVT));
3633 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3634 DAG.getConstant(i, PtrVT));
3635 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003636
Evan Cheng75184a92007-12-11 01:46:18 +00003637 return NewV;
3638 }
3639
Bill Wendling2c7cd592008-08-21 22:35:37 +00003640 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3641 // few as possible. First, let's find out how many elements are already in the
3642 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003643 unsigned V1InOrder = 0;
3644 unsigned V1FromV1 = 0;
3645 unsigned V2InOrder = 0;
3646 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003647 SmallVector<SDValue, 8> V1Elts;
3648 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003649 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003650 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003651 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003652 V1Elts.push_back(Elt);
3653 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003654 ++V1InOrder;
3655 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003656 continue;
3657 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003658 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003659 if (EltIdx == i) {
3660 V1Elts.push_back(Elt);
3661 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3662 ++V1InOrder;
3663 } else if (EltIdx == i+8) {
3664 V1Elts.push_back(Elt);
3665 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3666 ++V2InOrder;
3667 } else if (EltIdx < 8) {
3668 V1Elts.push_back(Elt);
Mon P Wang532c9632008-12-23 04:03:27 +00003669 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003670 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003671 } else {
Mon P Wang532c9632008-12-23 04:03:27 +00003672 V1Elts.push_back(Elt);
Evan Cheng75184a92007-12-11 01:46:18 +00003673 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3674 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003675 }
3676 }
3677
3678 if (V2InOrder > V1InOrder) {
3679 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3680 std::swap(V1, V2);
3681 std::swap(V1Elts, V2Elts);
3682 std::swap(V1FromV1, V2FromV2);
3683 }
3684
Evan Cheng75184a92007-12-11 01:46:18 +00003685 if ((V1FromV1 + V1InOrder) != 8) {
3686 // Some elements are from V2.
3687 if (V1FromV1) {
3688 // If there are elements that are from V1 but out of place,
3689 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003690 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003691 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003692 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003693 if (Elt.getOpcode() == ISD::UNDEF) {
3694 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3695 continue;
3696 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003697 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003698 if (EltIdx >= 8)
3699 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3700 else
3701 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3702 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003703 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003704 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003705 }
Evan Cheng75184a92007-12-11 01:46:18 +00003706
3707 NewV = V1;
3708 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003709 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003710 if (Elt.getOpcode() == ISD::UNDEF)
3711 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003712 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003713 if (EltIdx < 8)
3714 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003716 DAG.getConstant(EltIdx - 8, PtrVT));
3717 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3718 DAG.getConstant(i, PtrVT));
3719 }
3720 return NewV;
3721 } else {
3722 // All elements are from V1.
3723 NewV = V1;
3724 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003725 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003726 if (Elt.getOpcode() == ISD::UNDEF)
3727 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003728 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003729 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003730 DAG.getConstant(EltIdx, PtrVT));
3731 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3732 DAG.getConstant(i, PtrVT));
3733 }
3734 return NewV;
3735 }
3736}
3737
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003738/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3739/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3740/// done when every pair / quad of shuffle mask elements point to elements in
3741/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003742/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3743static
Dan Gohman8181bd12008-07-27 21:46:04 +00003744SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003745 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003746 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003747 TargetLowering &TLI) {
3748 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003749 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003750 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003751 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003752 MVT NewVT = MaskVT;
3753 switch (VT.getSimpleVT()) {
3754 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003755 case MVT::v4f32: NewVT = MVT::v2f64; break;
3756 case MVT::v4i32: NewVT = MVT::v2i64; break;
3757 case MVT::v8i16: NewVT = MVT::v4i32; break;
3758 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003759 }
3760
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003761 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003762 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003763 NewVT = MVT::v2i64;
3764 else
3765 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003766 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003767 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003768 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003769 for (unsigned i = 0; i < NumElems; i += Scale) {
3770 unsigned StartIdx = ~0U;
3771 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003772 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003773 if (Elt.getOpcode() == ISD::UNDEF)
3774 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003775 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003776 if (StartIdx == ~0U)
3777 StartIdx = EltIdx - (EltIdx % Scale);
3778 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003779 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003780 }
3781 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003782 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003783 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003784 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003785 }
3786
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003787 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3788 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3789 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3790 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3791 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003792}
3793
Evan Chenge9b9c672008-05-09 21:53:03 +00003794/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003795///
Dan Gohman8181bd12008-07-27 21:46:04 +00003796static SDValue getVZextMovL(MVT VT, MVT OpVT,
3797 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003798 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003799 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3800 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003801 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003802 LD = dyn_cast<LoadSDNode>(SrcOp);
3803 if (!LD) {
3804 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3805 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003806 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003807 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3808 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3809 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3810 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3811 // PR2108
3812 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3813 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003814 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003815 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003816 SrcOp.getOperand(0)
3817 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003818 }
3819 }
3820 }
3821
3822 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003823 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003824 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3825}
3826
Evan Chengf50554e2008-07-22 21:13:36 +00003827/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3828/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003829static SDValue
3830LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3831 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003832 MVT MaskVT = PermMask.getValueType();
3833 MVT MaskEVT = MaskVT.getVectorElementType();
3834 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003835 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003836 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003837 unsigned NumHi = 0;
3838 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003839 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003840 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003841 if (Elt.getOpcode() == ISD::UNDEF) {
3842 Locs[i] = std::make_pair(-1, -1);
3843 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003844 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003845 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003846 if (Val < 4) {
3847 Locs[i] = std::make_pair(0, NumLo);
3848 Mask1[NumLo] = Elt;
3849 NumLo++;
3850 } else {
3851 Locs[i] = std::make_pair(1, NumHi);
3852 if (2+NumHi < 4)
3853 Mask1[2+NumHi] = Elt;
3854 NumHi++;
3855 }
3856 }
3857 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003858
Evan Chengf50554e2008-07-22 21:13:36 +00003859 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003860 // If no more than two elements come from either vector. This can be
3861 // implemented with two shuffles. First shuffle gather the elements.
3862 // The second shuffle, which takes the first shuffle as both of its
3863 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003864 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3865 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3866 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003867
Dan Gohman8181bd12008-07-27 21:46:04 +00003868 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003869 for (unsigned i = 0; i != 4; ++i) {
3870 if (Locs[i].first == -1)
3871 continue;
3872 else {
3873 unsigned Idx = (i < 2) ? 0 : 4;
3874 Idx += Locs[i].first * 2 + Locs[i].second;
3875 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3876 }
3877 }
3878
3879 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3880 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3881 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003882 } else if (NumLo == 3 || NumHi == 3) {
3883 // Otherwise, we must have three elements from one vector, call it X, and
3884 // one element from the other, call it Y. First, use a shufps to build an
3885 // intermediate vector with the one element from Y and the element from X
3886 // that will be in the same half in the final destination (the indexes don't
3887 // matter). Then, use a shufps to build the final vector, taking the half
3888 // containing the element from Y from the intermediate, and the other half
3889 // from X.
3890 if (NumHi == 3) {
3891 // Normalize it so the 3 elements come from V1.
3892 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3893 std::swap(V1, V2);
3894 }
3895
3896 // Find the element from V2.
3897 unsigned HiIndex;
3898 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003899 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003900 if (Elt.getOpcode() == ISD::UNDEF)
3901 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003902 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003903 if (Val >= 4)
3904 break;
3905 }
3906
3907 Mask1[0] = PermMask.getOperand(HiIndex);
3908 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3909 Mask1[2] = PermMask.getOperand(HiIndex^1);
3910 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3911 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3912 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3913
3914 if (HiIndex >= 2) {
3915 Mask1[0] = PermMask.getOperand(0);
3916 Mask1[1] = PermMask.getOperand(1);
3917 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3918 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3919 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3920 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3921 } else {
3922 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3923 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3924 Mask1[2] = PermMask.getOperand(2);
3925 Mask1[3] = PermMask.getOperand(3);
3926 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003927 Mask1[2] =
3928 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3929 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003930 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003931 Mask1[3] =
3932 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3933 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003934 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3935 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3936 }
Evan Chengf50554e2008-07-22 21:13:36 +00003937 }
3938
3939 // Break it into (shuffle shuffle_hi, shuffle_lo).
3940 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003941 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3942 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3943 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003944 unsigned MaskIdx = 0;
3945 unsigned LoIdx = 0;
3946 unsigned HiIdx = 2;
3947 for (unsigned i = 0; i != 4; ++i) {
3948 if (i == 2) {
3949 MaskPtr = &HiMask;
3950 MaskIdx = 1;
3951 LoIdx = 0;
3952 HiIdx = 2;
3953 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003954 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003955 if (Elt.getOpcode() == ISD::UNDEF) {
3956 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003957 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003958 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3959 (*MaskPtr)[LoIdx] = Elt;
3960 LoIdx++;
3961 } else {
3962 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3963 (*MaskPtr)[HiIdx] = Elt;
3964 HiIdx++;
3965 }
3966 }
3967
Dan Gohman8181bd12008-07-27 21:46:04 +00003968 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003969 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3970 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003971 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003972 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3973 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003974 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003975 for (unsigned i = 0; i != 4; ++i) {
3976 if (Locs[i].first == -1) {
3977 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3978 } else {
3979 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3980 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3981 }
3982 }
3983 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3984 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3985 &MaskOps[0], MaskOps.size()));
3986}
3987
Dan Gohman8181bd12008-07-27 21:46:04 +00003988SDValue
3989X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3990 SDValue V1 = Op.getOperand(0);
3991 SDValue V2 = Op.getOperand(1);
3992 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003993 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003995 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3997 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3998 bool V1IsSplat = false;
3999 bool V2IsSplat = false;
4000
Gabor Greif1c80d112008-08-28 21:40:38 +00004001 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004002 return DAG.getNode(ISD::UNDEF, VT);
4003
Gabor Greif1c80d112008-08-28 21:40:38 +00004004 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00004005 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004006
Gabor Greif1c80d112008-08-28 21:40:38 +00004007 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00004009 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 return V2;
4011
Evan Chengae6c9212008-09-25 23:35:16 +00004012 // Canonicalize movddup shuffles.
4013 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004014 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004015 X86::isMOVDDUPMask(PermMask.getNode()))
4016 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4017
Gabor Greif1c80d112008-08-28 21:40:38 +00004018 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004019 if (isMMX || NumElems < 4) return Op;
4020 // Promote it to a v4{if}32 splat.
4021 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004022 }
4023
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004024 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4025 // do it!
4026 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004027 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004028 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004029 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4030 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4031 // FIXME: Figure out a cleaner way to do this.
4032 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004033 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004034 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004035 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004036 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004037 SDValue NewV1 = NewOp.getOperand(0);
4038 SDValue NewV2 = NewOp.getOperand(1);
4039 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004040 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004041 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004042 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004043 }
4044 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004045 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004046 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004047 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004048 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004049 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004050 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004051 }
4052 }
4053
Evan Chengdea99362008-05-29 08:22:04 +00004054 // Check if this can be converted into a logical shift.
4055 bool isLeft = false;
4056 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004057 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004058 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4059 if (isShift && ShVal.hasOneUse()) {
4060 // If the shifted value has multiple uses, it may be cheaper to use
4061 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004062 MVT EVT = VT.getVectorElementType();
4063 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004064 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4065 }
4066
Gabor Greif1c80d112008-08-28 21:40:38 +00004067 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004068 if (V1IsUndef)
4069 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004071 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004072 if (!isMMX)
4073 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004074 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075
Gabor Greif1c80d112008-08-28 21:40:38 +00004076 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4077 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4078 X86::isMOVHLPSMask(PermMask.getNode()) ||
4079 X86::isMOVHPMask(PermMask.getNode()) ||
4080 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 return Op;
4082
Gabor Greif1c80d112008-08-28 21:40:38 +00004083 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4084 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004085 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4086
Evan Chengdea99362008-05-29 08:22:04 +00004087 if (isShift) {
4088 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004089 MVT EVT = VT.getVectorElementType();
4090 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004091 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4092 }
4093
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004094 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004095 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4096 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004097 V1IsSplat = isSplatVector(V1.getNode());
4098 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004099
4100 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4102 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4103 std::swap(V1IsSplat, V2IsSplat);
4104 std::swap(V1IsUndef, V2IsUndef);
4105 Commuted = true;
4106 }
4107
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004108 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004109 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 if (V2IsUndef) return V1;
4111 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4112 if (V2IsSplat) {
4113 // V2 is a splat, so the mask may be malformed. That is, it may point
4114 // to any V2 element. The instruction selectior won't like this. Get
4115 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004116 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004117 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004118 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4119 }
4120 return Op;
4121 }
4122
Gabor Greif1c80d112008-08-28 21:40:38 +00004123 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4124 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4125 X86::isUNPCKLMask(PermMask.getNode()) ||
4126 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004127 return Op;
4128
4129 if (V2IsSplat) {
4130 // Normalize mask so all entries that point to V2 points to its first
4131 // element then try to match unpck{h|l} again. If match, return a
4132 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004133 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004134 if (NewMask.getNode() != PermMask.getNode()) {
4135 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004136 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004138 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004139 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4141 }
4142 }
4143 }
4144
4145 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004146 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4148
4149 if (Commuted) {
4150 // Commute is back and try unpck* again.
4151 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004152 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4153 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4154 X86::isUNPCKLMask(PermMask.getNode()) ||
4155 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004156 return Op;
4157 }
4158
Evan Chengbf8b2c52008-04-05 00:30:36 +00004159 // Try PSHUF* first, then SHUFP*.
4160 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4161 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004162 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004163 if (V2.getOpcode() != ISD::UNDEF)
4164 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4165 DAG.getNode(ISD::UNDEF, VT), PermMask);
4166 return Op;
4167 }
4168
4169 if (!isMMX) {
4170 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004171 (X86::isPSHUFDMask(PermMask.getNode()) ||
4172 X86::isPSHUFHWMask(PermMask.getNode()) ||
4173 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004174 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004175 if (VT == MVT::v4f32) {
4176 RVT = MVT::v4i32;
4177 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4178 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4179 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4180 } else if (V2.getOpcode() != ISD::UNDEF)
4181 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4182 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4183 if (RVT != VT)
4184 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004185 return Op;
4186 }
4187
Evan Chengbf8b2c52008-04-05 00:30:36 +00004188 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004189 if (X86::isSHUFPMask(PermMask.getNode()) ||
4190 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004191 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192 }
4193
Evan Cheng75184a92007-12-11 01:46:18 +00004194 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4195 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004196 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004197 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004198 return NewOp;
4199 }
4200
Evan Chengf50554e2008-07-22 21:13:36 +00004201 // Handle all 4 wide cases with a number of shuffles except for MMX.
4202 if (NumElems == 4 && !isMMX)
4203 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004204
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004206}
4207
Dan Gohman8181bd12008-07-27 21:46:04 +00004208SDValue
4209X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004210 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004211 MVT VT = Op.getValueType();
4212 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004213 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004214 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004215 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004216 DAG.getValueType(VT));
4217 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004218 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004219 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4220 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4221 if (Idx == 0)
4222 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4223 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4224 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32,
4225 Op.getOperand(0)),
4226 Op.getOperand(1)));
Dan Gohman8181bd12008-07-27 21:46:04 +00004227 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004228 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004229 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004230 DAG.getValueType(VT));
4231 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004232 } else if (VT == MVT::f32) {
4233 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4234 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004235 // result has a single use which is a store or a bitcast to i32. And in
4236 // the case of a store, it's not worth it if the index is a constant 0,
4237 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004238 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004239 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004240 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004241 if ((User->getOpcode() != ISD::STORE ||
4242 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4243 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004244 (User->getOpcode() != ISD::BIT_CONVERT ||
4245 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004246 return SDValue();
4247 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004248 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4249 Op.getOperand(1));
4250 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004251 } else if (VT == MVT::i32) {
4252 // ExtractPS works with constant index.
4253 if (isa<ConstantSDNode>(Op.getOperand(1)))
4254 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004255 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004256 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004257}
4258
4259
Dan Gohman8181bd12008-07-27 21:46:04 +00004260SDValue
4261X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004263 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264
Evan Cheng6c249332008-03-24 21:52:23 +00004265 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004266 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004267 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004268 return Res;
4269 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004270
Duncan Sands92c43912008-06-06 12:08:01 +00004271 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004272 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004273 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004274 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004275 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004276 if (Idx == 0)
4277 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4278 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4279 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4280 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004281 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004282 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004283 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004284 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004285 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 DAG.getValueType(VT));
4287 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004288 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004289 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 if (Idx == 0)
4291 return Op;
4292 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004293 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004294 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004295 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004296 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004297 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004298 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004299 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004300 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004301 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004302 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004303 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004305 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4307 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4308 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004309 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004310 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004311 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4312 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4313 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004314 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004315 if (Idx == 0)
4316 return Op;
4317
4318 // UNPCKHPD the element to the lowest double word, then movsd.
4319 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4320 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004321 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004322 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004323 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004324 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004325 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004326 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004327 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004328 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4330 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4331 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004332 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 }
4334
Dan Gohman8181bd12008-07-27 21:46:04 +00004335 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336}
4337
Dan Gohman8181bd12008-07-27 21:46:04 +00004338SDValue
4339X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004340 MVT VT = Op.getValueType();
4341 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004342
Dan Gohman8181bd12008-07-27 21:46:04 +00004343 SDValue N0 = Op.getOperand(0);
4344 SDValue N1 = Op.getOperand(1);
4345 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004346
Dan Gohman5a7af042008-08-14 22:53:18 +00004347 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4348 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004349 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004350 : X86ISD::PINSRW;
4351 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4352 // argument.
4353 if (N1.getValueType() != MVT::i32)
4354 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4355 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004356 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004357 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004358 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004359 // Bits [7:6] of the constant are the source select. This will always be
4360 // zero here. The DAG Combiner may combine an extract_elt index into these
4361 // bits. For example (insert (extract, 3), 2) could be matched by putting
4362 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4363 // Bits [5:4] of the constant are the destination select. This is the
4364 // value of the incoming immediate.
4365 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4366 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004367 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004368 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
Mon P Wangac2a3c52009-01-15 21:10:20 +00004369 } else if (EVT == MVT::i32) {
4370 // InsertPS works with constant index.
4371 if (isa<ConstantSDNode>(N2))
4372 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004373 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004374 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004375}
4376
Dan Gohman8181bd12008-07-27 21:46:04 +00004377SDValue
4378X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004379 MVT VT = Op.getValueType();
4380 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004381
4382 if (Subtarget->hasSSE41())
4383 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4384
Evan Chenge12a7eb2007-12-12 07:55:34 +00004385 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004387
Dan Gohman8181bd12008-07-27 21:46:04 +00004388 SDValue N0 = Op.getOperand(0);
4389 SDValue N1 = Op.getOperand(1);
4390 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004391
Duncan Sands92c43912008-06-06 12:08:01 +00004392 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004393 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4394 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004395 if (N1.getValueType() != MVT::i32)
4396 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4397 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004398 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004400 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004401 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402}
4403
Dan Gohman8181bd12008-07-27 21:46:04 +00004404SDValue
4405X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004406 if (Op.getValueType() == MVT::v2f32)
4407 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4408 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4409 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4410 Op.getOperand(0))));
4411
Dan Gohman8181bd12008-07-27 21:46:04 +00004412 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004413 MVT VT = MVT::v2i32;
4414 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004415 default: break;
4416 case MVT::v16i8:
4417 case MVT::v8i16:
4418 VT = MVT::v4i32;
4419 break;
4420 }
4421 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4422 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423}
4424
Bill Wendlingfef06052008-09-16 21:48:12 +00004425// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4426// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4427// one of the above mentioned nodes. It has to be wrapped because otherwise
4428// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4429// be used to form addressing mode. These wrapped nodes will be selected
4430// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004431SDValue
4432X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004433 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004434 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 getPointerTy(),
4436 CP->getAlignment());
4437 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4438 // With PIC, the address is actually $g + Offset.
4439 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4440 !Subtarget->isPICStyleRIPRel()) {
4441 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4442 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4443 Result);
4444 }
4445
4446 return Result;
4447}
4448
Dan Gohman8181bd12008-07-27 21:46:04 +00004449SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004450X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004451 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004452 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004453 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4454 bool ExtraLoadRequired =
4455 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4456
4457 // Create the TargetGlobalAddress node, folding in the constant
4458 // offset if it is legal.
4459 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004460 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004461 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4462 Offset = 0;
4463 } else
4464 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004465 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004467 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004468 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004469 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4470 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4471 Result);
4472 }
4473
4474 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4475 // load the value at address GV, not the value of GV itself. This means that
4476 // the GlobalAddress must be in the base or index register of the address, not
4477 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4478 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004479 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004480 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004481 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482
Dan Gohman36322c72008-10-18 02:06:02 +00004483 // If there was a non-zero offset that we didn't fold, create an explicit
4484 // addition for it.
4485 if (Offset != 0)
4486 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4487 DAG.getConstant(Offset, getPointerTy()));
4488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004489 return Result;
4490}
4491
Evan Cheng7f250d62008-09-24 00:05:32 +00004492SDValue
4493X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4494 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004495 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4496 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004497}
4498
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004499// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004500static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004501LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004502 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004503 SDValue InFlag;
4504 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 DAG.getNode(X86ISD::GlobalBaseReg,
4506 PtrVT), InFlag);
4507 InFlag = Chain.getValue(1);
4508
4509 // emit leal symbol@TLSGD(,%ebx,1), %eax
4510 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004512 GA->getValueType(0),
4513 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004514 SDValue Ops[] = { Chain, TGA, InFlag };
4515 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004516 InFlag = Result.getValue(2);
4517 Chain = Result.getValue(1);
4518
4519 // call ___tls_get_addr. This function receives its argument in
4520 // the register EAX.
4521 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4522 InFlag = Chain.getValue(1);
4523
4524 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004526 DAG.getTargetExternalSymbol("___tls_get_addr",
4527 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 DAG.getRegister(X86::EAX, PtrVT),
4529 DAG.getRegister(X86::EBX, PtrVT),
4530 InFlag };
4531 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4532 InFlag = Chain.getValue(1);
4533
4534 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4535}
4536
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004537// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004538static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004539LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004540 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004541 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004542
4543 // emit leaq symbol@TLSGD(%rip), %rdi
4544 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004545 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004546 GA->getValueType(0),
4547 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4549 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004550 Chain = Result.getValue(1);
4551 InFlag = Result.getValue(2);
4552
aslb204cd52008-08-16 12:58:29 +00004553 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004554 // the register RDI.
4555 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4556 InFlag = Chain.getValue(1);
4557
4558 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004559 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004560 DAG.getTargetExternalSymbol("__tls_get_addr",
4561 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004562 DAG.getRegister(X86::RDI, PtrVT),
4563 InFlag };
4564 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4565 InFlag = Chain.getValue(1);
4566
4567 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4568}
4569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4571// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004572static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004573 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004574 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004575 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004576 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4577 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004578 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 GA->getValueType(0),
4580 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582
4583 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004584 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004585 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586
4587 // The address of the thread local variable is the add of the thread
4588 // pointer with the offset of the variable.
4589 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4590}
4591
Dan Gohman8181bd12008-07-27 21:46:04 +00004592SDValue
4593X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004594 // TODO: implement the "local dynamic" model
4595 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004596 assert(Subtarget->isTargetELF() &&
4597 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4599 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4600 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004601 if (Subtarget->is64Bit()) {
4602 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4603 } else {
4604 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4605 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4606 else
4607 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4608 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609}
4610
Dan Gohman8181bd12008-07-27 21:46:04 +00004611SDValue
4612X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004613 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4614 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004615 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4616 // With PIC, the address is actually $g + Offset.
4617 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4618 !Subtarget->isPICStyleRIPRel()) {
4619 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4620 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4621 Result);
4622 }
4623
4624 return Result;
4625}
4626
Dan Gohman8181bd12008-07-27 21:46:04 +00004627SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004629 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004630 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4631 // With PIC, the address is actually $g + Offset.
4632 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4633 !Subtarget->isPICStyleRIPRel()) {
4634 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4635 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4636 Result);
4637 }
4638
4639 return Result;
4640}
4641
Chris Lattner62814a32007-10-17 06:02:13 +00004642/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4643/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004644SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004645 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004646 MVT VT = Op.getValueType();
4647 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004648 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 SDValue ShOpLo = Op.getOperand(0);
4650 SDValue ShOpHi = Op.getOperand(1);
4651 SDValue ShAmt = Op.getOperand(2);
4652 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004653 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4654 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655
Dan Gohman8181bd12008-07-27 21:46:04 +00004656 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004657 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004658 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4659 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004660 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004661 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4662 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004663 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664
Dan Gohman8181bd12008-07-27 21:46:04 +00004665 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004666 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004667 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004668 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004669
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 SDValue Hi, Lo;
4671 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4672 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4673 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004674
Chris Lattner62814a32007-10-17 06:02:13 +00004675 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004676 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4677 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004678 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004679 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4680 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004681 }
4682
Dan Gohman8181bd12008-07-27 21:46:04 +00004683 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004684 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685}
4686
Dan Gohman8181bd12008-07-27 21:46:04 +00004687SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004688 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004689 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004690 "Unknown SINT_TO_FP to lower!");
4691
4692 // These are really Legal; caller falls through into that case.
4693 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004694 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004695 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4696 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004697 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004698
Duncan Sands92c43912008-06-06 12:08:01 +00004699 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 MachineFunction &MF = DAG.getMachineFunction();
4701 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004702 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4703 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004704 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004705 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706
4707 // Build the FILD
4708 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004709 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004710 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4712 else
4713 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004714 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 Ops.push_back(Chain);
4716 Ops.push_back(StackSlot);
4717 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004718 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004719 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004720
Dale Johannesen2fc20782007-09-14 22:26:36 +00004721 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004722 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004723 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724
4725 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4726 // shouldn't be necessary except that RFP cannot be live across
4727 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4728 MachineFunction &MF = DAG.getMachineFunction();
4729 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004730 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004731 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004732 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004733 Ops.push_back(Chain);
4734 Ops.push_back(Result);
4735 Ops.push_back(StackSlot);
4736 Ops.push_back(DAG.getValueType(Op.getValueType()));
4737 Ops.push_back(InFlag);
4738 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004739 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004740 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 }
4742
4743 return Result;
4744}
4745
Dale Johannesena359b8b2008-10-21 20:50:01 +00004746SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4747 MVT SrcVT = Op.getOperand(0).getValueType();
4748 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4749
4750 // We only handle SSE2 f64 target here; caller can handle the rest.
4751 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4752 return SDValue();
4753
Dale Johannesenfb019af2008-10-21 23:07:49 +00004754 // This algorithm is not obvious. Here it is in C code, more or less:
4755/*
4756 double uint64_to_double( uint32_t hi, uint32_t lo )
4757 {
4758 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4759 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4760
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004761 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004762 __m128i xh = _mm_cvtsi32_si128( hi );
4763 __m128i xl = _mm_cvtsi32_si128( lo );
4764
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004765 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004766 __m128i x = _mm_unpacklo_epi32( xh, xl );
4767 __m128d d;
4768 double sd;
4769
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004770 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004771 // right magnitude
4772 x = _mm_unpacklo_epi32( x, exp );
4773
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004774 // subtract away the biases to deal with the IEEE-754 double precision
4775 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004776 d = _mm_sub_pd( (__m128d) x, bias );
4777
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004778 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004779 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004780 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004781 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4782 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004783 // store doesn't really need to be here (except maybe to zero the other
4784 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004785 return sd;
4786 }
4787*/
4788
Dale Johannesena359b8b2008-10-21 20:50:01 +00004789 // Build some magic constants.
4790 std::vector<Constant*>CV0;
4791 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4792 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4793 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4794 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4795 Constant *C0 = ConstantVector::get(CV0);
4796 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4797
4798 std::vector<Constant*>CV1;
4799 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4800 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4801 Constant *C1 = ConstantVector::get(CV1);
4802 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4803
4804 SmallVector<SDValue, 4> MaskVec;
4805 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4806 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4807 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4808 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4809 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4810 MaskVec.size());
4811 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004812 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4813 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4814 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004815 MaskVec2.size());
4816
4817 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004818 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4819 Op.getOperand(0),
4820 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004821 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004822 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4823 Op.getOperand(0),
4824 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004825 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4826 XR1, XR2, UnpcklMask);
4827 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4828 PseudoSourceValue::getConstantPool(), 0, false, 16);
4829 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4830 Unpck1, CLod0, UnpcklMask);
4831 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4832 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4833 PseudoSourceValue::getConstantPool(), 0, false, 16);
4834 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4835 // Add the halves; easiest way is to swap them into another reg first.
4836 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4837 Sub, Sub, ShufMask);
4838 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4839 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4840 DAG.getIntPtrConstant(0));
4841}
4842
Dan Gohman8181bd12008-07-27 21:46:04 +00004843std::pair<SDValue,SDValue> X86TargetLowering::
4844FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004845 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4846 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004847 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848
Dale Johannesen2fc20782007-09-14 22:26:36 +00004849 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004850 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004851 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004852 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004853 if (Subtarget->is64Bit() &&
4854 Op.getValueType() == MVT::i64 &&
4855 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004856 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004857
Evan Cheng05441e62007-10-15 20:11:21 +00004858 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4859 // stack slot.
4860 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004861 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004862 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004863 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004864 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004865 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004866 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4867 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4868 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4869 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004870 }
4871
Dan Gohman8181bd12008-07-27 21:46:04 +00004872 SDValue Chain = DAG.getEntryNode();
4873 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004874 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004876 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004877 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004878 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004879 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4881 };
4882 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4883 Chain = Value.getValue(1);
4884 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4885 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4886 }
4887
4888 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004889 SDValue Ops[] = { Chain, Value, StackSlot };
4890 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004892 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893}
4894
Dan Gohman8181bd12008-07-27 21:46:04 +00004895SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4896 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4897 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004898 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004899
4900 // Load the result.
4901 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4902}
4903
Dan Gohman8181bd12008-07-27 21:46:04 +00004904SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004905 MVT VT = Op.getValueType();
4906 MVT EltVT = VT;
4907 if (VT.isVector())
4908 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 std::vector<Constant*> CV;
4910 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004911 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912 CV.push_back(C);
4913 CV.push_back(C);
4914 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004915 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004916 CV.push_back(C);
4917 CV.push_back(C);
4918 CV.push_back(C);
4919 CV.push_back(C);
4920 }
Dan Gohman11821702007-07-27 17:16:43 +00004921 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004922 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4923 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004924 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004925 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004926 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4927}
4928
Dan Gohman8181bd12008-07-27 21:46:04 +00004929SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004930 MVT VT = Op.getValueType();
4931 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004932 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004933 if (VT.isVector()) {
4934 EltVT = VT.getVectorElementType();
4935 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004936 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937 std::vector<Constant*> CV;
4938 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004939 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 CV.push_back(C);
4941 CV.push_back(C);
4942 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004943 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 CV.push_back(C);
4945 CV.push_back(C);
4946 CV.push_back(C);
4947 CV.push_back(C);
4948 }
Dan Gohman11821702007-07-27 17:16:43 +00004949 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004950 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4951 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004952 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004953 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004954 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004955 return DAG.getNode(ISD::BIT_CONVERT, VT,
4956 DAG.getNode(ISD::XOR, MVT::v2i64,
4957 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4958 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4959 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004960 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4961 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962}
4963
Dan Gohman8181bd12008-07-27 21:46:04 +00004964SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4965 SDValue Op0 = Op.getOperand(0);
4966 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004967 MVT VT = Op.getValueType();
4968 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969
4970 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004971 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004972 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4973 SrcVT = VT;
4974 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004975 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004976 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004977 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004978 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004979 }
4980
4981 // At this point the operands and the result should have the same
4982 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983
4984 // First get the sign bit of second operand.
4985 std::vector<Constant*> CV;
4986 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004987 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004989 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004994 }
Dan Gohman11821702007-07-27 17:16:43 +00004995 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004996 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4997 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004998 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004999 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005000 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001
5002 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005003 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005004 // Op0 is MVT::f32, Op1 is MVT::f64.
5005 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
5006 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
5007 DAG.getConstant(32, MVT::i32));
5008 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
5009 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005010 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005011 }
5012
5013 // Clear first operand sign bit.
5014 CV.clear();
5015 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005016 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
5017 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005018 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00005019 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
5020 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5021 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
5022 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005023 }
Dan Gohman11821702007-07-27 17:16:43 +00005024 C = ConstantVector::get(CV);
5025 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00005026 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005027 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005028 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00005029 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005030
5031 // Or the value with the sign bit.
5032 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5033}
5034
Dan Gohman8181bd12008-07-27 21:46:04 +00005035SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005036 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005037 SDValue Op0 = Op.getOperand(0);
5038 SDValue Op1 = Op.getOperand(1);
Chris Lattner77a62312008-12-25 05:34:37 +00005039 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5040
5041 // Lower (X & (1 << N)) == 0 to BT.
5042 // Lower ((X >>u N) & 1) != 0 to BT.
5043 // Lower ((X >>s N) & 1) != 0 to BT.
Dan Gohman13dd9522009-01-13 23:25:30 +00005044 if (Op0.getOpcode() == ISD::AND &&
5045 Op0.hasOneUse() &&
5046 Op1.getOpcode() == ISD::Constant &&
Chris Lattner77a62312008-12-25 05:34:37 +00005047 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5048 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5049 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5050 ConstantSDNode *CmpRHS = cast<ConstantSDNode>(Op1);
5051 SDValue AndLHS = Op0.getOperand(0);
5052 if (CmpRHS->getZExtValue() == 0 && AndRHS->getZExtValue() == 1 &&
5053 AndLHS.getOpcode() == ISD::SRL) {
5054 SDValue LHS = AndLHS.getOperand(0);
5055 SDValue RHS = AndLHS.getOperand(1);
Evan Cheng950aac02007-09-25 01:57:46 +00005056
Chris Lattner77a62312008-12-25 05:34:37 +00005057 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5058 // instruction. Since the shift amount is in-range-or-undefined, we know
5059 // that doing a bittest on the i16 value is ok. We extend to i32 because
5060 // the encoding for the i16 version is larger than the i32 version.
5061 if (LHS.getValueType() == MVT::i8)
5062 LHS = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, LHS);
5063
5064 // If the operand types disagree, extend the shift amount to match. Since
5065 // BT ignores high bits (like shifts) we can use anyextend.
5066 if (LHS.getValueType() != RHS.getValueType())
5067 RHS = DAG.getNode(ISD::ANY_EXTEND, LHS.getValueType(), RHS);
5068
5069 SDValue BT = DAG.getNode(X86ISD::BT, MVT::i32, LHS, RHS);
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005070 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Chris Lattner77a62312008-12-25 05:34:37 +00005071 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5072 DAG.getConstant(Cond, MVT::i8), BT);
5073 }
5074 }
5075
5076 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5077 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Chris Lattner60435922008-12-24 00:11:37 +00005078
Chris Lattner77a62312008-12-25 05:34:37 +00005079 SDValue Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Chris Lattner60435922008-12-24 00:11:37 +00005080 return DAG.getNode(X86ISD::SETCC, MVT::i8,
5081 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005082}
5083
Dan Gohman8181bd12008-07-27 21:46:04 +00005084SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5085 SDValue Cond;
5086 SDValue Op0 = Op.getOperand(0);
5087 SDValue Op1 = Op.getOperand(1);
5088 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005089 MVT VT = Op.getValueType();
5090 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5091 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5092
5093 if (isFP) {
5094 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005095 MVT VT0 = Op0.getValueType();
5096 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5097 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005098 bool Swap = false;
5099
5100 switch (SetCCOpcode) {
5101 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005102 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005103 case ISD::SETEQ: SSECC = 0; break;
5104 case ISD::SETOGT:
5105 case ISD::SETGT: Swap = true; // Fallthrough
5106 case ISD::SETLT:
5107 case ISD::SETOLT: SSECC = 1; break;
5108 case ISD::SETOGE:
5109 case ISD::SETGE: Swap = true; // Fallthrough
5110 case ISD::SETLE:
5111 case ISD::SETOLE: SSECC = 2; break;
5112 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005113 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005114 case ISD::SETNE: SSECC = 4; break;
5115 case ISD::SETULE: Swap = true;
5116 case ISD::SETUGE: SSECC = 5; break;
5117 case ISD::SETULT: Swap = true;
5118 case ISD::SETUGT: SSECC = 6; break;
5119 case ISD::SETO: SSECC = 7; break;
5120 }
5121 if (Swap)
5122 std::swap(Op0, Op1);
5123
Nate Begeman6357f9d2008-07-25 19:05:58 +00005124 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005125 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005126 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005127 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005128 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5129 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5130 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5131 }
5132 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005133 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005134 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5135 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5136 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5137 }
5138 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005139 }
5140 // Handle all other FP comparisons here.
5141 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5142 }
5143
5144 // We are handling one of the integer comparisons here. Since SSE only has
5145 // GT and EQ comparisons for integer, swapping operands and multiple
5146 // operations may be required for some comparisons.
5147 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5148 bool Swap = false, Invert = false, FlipSigns = false;
5149
5150 switch (VT.getSimpleVT()) {
5151 default: break;
5152 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5153 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5154 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5155 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5156 }
5157
5158 switch (SetCCOpcode) {
5159 default: break;
5160 case ISD::SETNE: Invert = true;
5161 case ISD::SETEQ: Opc = EQOpc; break;
5162 case ISD::SETLT: Swap = true;
5163 case ISD::SETGT: Opc = GTOpc; break;
5164 case ISD::SETGE: Swap = true;
5165 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5166 case ISD::SETULT: Swap = true;
5167 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5168 case ISD::SETUGE: Swap = true;
5169 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5170 }
5171 if (Swap)
5172 std::swap(Op0, Op1);
5173
5174 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5175 // bits of the inputs before performing those operations.
5176 if (FlipSigns) {
5177 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005178 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5179 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5180 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005181 SignBits.size());
5182 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5183 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5184 }
5185
Dan Gohman8181bd12008-07-27 21:46:04 +00005186 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005187
5188 // If the logical-not of the result is required, perform that now.
5189 if (Invert) {
5190 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005191 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5192 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5193 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005194 NegOnes.size());
5195 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5196 }
5197 return Result;
5198}
Evan Cheng950aac02007-09-25 01:57:46 +00005199
Evan Chengd580f022008-12-03 08:38:43 +00005200// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5201static bool isX86LogicalCmp(unsigned Opc) {
5202 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5203}
5204
Dan Gohman8181bd12008-07-27 21:46:04 +00005205SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005206 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005207 SDValue Cond = Op.getOperand(0);
5208 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005209
5210 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005211 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005212
Evan Cheng50d37ab2007-10-08 22:16:29 +00005213 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5214 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005215 if (Cond.getOpcode() == X86ISD::SETCC) {
5216 CC = Cond.getOperand(0);
5217
Dan Gohman8181bd12008-07-27 21:46:04 +00005218 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005220 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005221
Evan Cheng50d37ab2007-10-08 22:16:29 +00005222 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005223 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005224 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005225 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005226
Evan Chengd580f022008-12-03 08:38:43 +00005227 if (isX86LogicalCmp(Opc) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005228 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005229 addTest = false;
5230 }
5231 }
5232
5233 if (addTest) {
5234 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005235 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005236 }
5237
Duncan Sands92c43912008-06-06 12:08:01 +00005238 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005239 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005240 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005241 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5242 // condition is true.
5243 Ops.push_back(Op.getOperand(2));
5244 Ops.push_back(Op.getOperand(1));
5245 Ops.push_back(CC);
5246 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005247 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005248}
5249
Evan Chengd580f022008-12-03 08:38:43 +00005250// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5251// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5252// from the AND / OR.
5253static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5254 Opc = Op.getOpcode();
5255 if (Opc != ISD::OR && Opc != ISD::AND)
5256 return false;
5257 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5258 Op.getOperand(0).hasOneUse() &&
5259 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5260 Op.getOperand(1).hasOneUse());
5261}
5262
Dan Gohman8181bd12008-07-27 21:46:04 +00005263SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005264 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 SDValue Chain = Op.getOperand(0);
5266 SDValue Cond = Op.getOperand(1);
5267 SDValue Dest = Op.getOperand(2);
5268 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005269
5270 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005271 Cond = LowerSETCC(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005272#if 0
5273 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00005274 else if (Cond.getOpcode() == X86ISD::ADD ||
5275 Cond.getOpcode() == X86ISD::SUB ||
5276 Cond.getOpcode() == X86ISD::SMUL ||
5277 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005278 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00005279#endif
5280
Evan Cheng50d37ab2007-10-08 22:16:29 +00005281 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5282 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005283 if (Cond.getOpcode() == X86ISD::SETCC) {
5284 CC = Cond.getOperand(0);
5285
Dan Gohman8181bd12008-07-27 21:46:04 +00005286 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00005288 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
5289 if (isX86LogicalCmp(Opc) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005290 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005291 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005292 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005293 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005294 default: break;
5295 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00005296 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00005297 // These can only come from an arithmetic instruction with overflow,
5298 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005299 Cond = Cond.getNode()->getOperand(1);
5300 addTest = false;
5301 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005302 }
Evan Cheng950aac02007-09-25 01:57:46 +00005303 }
Evan Chengd580f022008-12-03 08:38:43 +00005304 } else {
5305 unsigned CondOpc;
5306 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5307 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5308 unsigned Opc = Cmp.getOpcode();
5309 if (CondOpc == ISD::OR) {
5310 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5311 // two branches instead of an explicit OR instruction with a
5312 // separate test.
5313 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5314 isX86LogicalCmp(Opc)) {
5315 CC = Cond.getOperand(0).getOperand(0);
5316 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5317 Chain, Dest, CC, Cmp);
5318 CC = Cond.getOperand(1).getOperand(0);
5319 Cond = Cmp;
5320 addTest = false;
5321 }
5322 } else { // ISD::AND
5323 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5324 // two branches instead of an explicit AND instruction with a
5325 // separate test. However, we only do this if this block doesn't
5326 // have a fall-through edge, because this requires an explicit
5327 // jmp when the condition is false.
5328 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5329 isX86LogicalCmp(Opc) &&
5330 Op.getNode()->hasOneUse()) {
5331 X86::CondCode CCode =
5332 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5333 CCode = X86::GetOppositeBranchCondition(CCode);
5334 CC = DAG.getConstant(CCode, MVT::i8);
5335 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5336 // Look for an unconditional branch following this conditional branch.
5337 // We need this because we need to reverse the successors in order
5338 // to implement FCMP_OEQ.
5339 if (User.getOpcode() == ISD::BR) {
5340 SDValue FalseBB = User.getOperand(1);
5341 SDValue NewBR =
5342 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5343 assert(NewBR == User);
5344 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005345
Evan Chengd580f022008-12-03 08:38:43 +00005346 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5347 Chain, Dest, CC, Cmp);
5348 X86::CondCode CCode =
5349 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5350 CCode = X86::GetOppositeBranchCondition(CCode);
5351 CC = DAG.getConstant(CCode, MVT::i8);
5352 Cond = Cmp;
5353 addTest = false;
5354 }
5355 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005356 }
5357 }
Evan Cheng950aac02007-09-25 01:57:46 +00005358 }
5359
5360 if (addTest) {
5361 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005362 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005363 }
Evan Cheng621216e2007-09-29 00:00:36 +00005364 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005365 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005366}
5367
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005368
5369// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5370// Calls to _alloca is needed to probe the stack when allocating more than 4k
5371// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5372// that the guard pages used by the OS virtual memory manager are allocated in
5373// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005374SDValue
5375X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376 SelectionDAG &DAG) {
5377 assert(Subtarget->isTargetCygMing() &&
5378 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 SDValue Chain = Op.getOperand(0);
5382 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005383 // FIXME: Ensure alignment here
5384
Dan Gohman8181bd12008-07-27 21:46:04 +00005385 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005386
Duncan Sands92c43912008-06-06 12:08:01 +00005387 MVT IntPtr = getPointerTy();
5388 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005389
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005390 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5393 Flag = Chain.getValue(1);
5394
5395 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005396 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005397 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005398 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005399 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005400 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005401 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005402 Flag = Chain.getValue(1);
5403
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005404 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005405 DAG.getIntPtrConstant(0, true),
5406 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005407 Flag);
5408
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005410
Dan Gohman8181bd12008-07-27 21:46:04 +00005411 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005412 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005413}
5414
Dan Gohman8181bd12008-07-27 21:46:04 +00005415SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005416X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005417 SDValue Chain,
5418 SDValue Dst, SDValue Src,
5419 SDValue Size, unsigned Align,
5420 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005421 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005422 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005423
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005424 // If not DWORD aligned or size is more than the threshold, call the library.
5425 // The libc version is likely to be faster for these cases. It can use the
5426 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005427 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005428 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005429 ConstantSize->getZExtValue() >
5430 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005431 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005432
5433 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005434 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005435
Bill Wendling4b2e3782008-10-01 00:59:58 +00005436 if (const char *bzeroEntry = V &&
5437 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5438 MVT IntPtr = getPointerTy();
5439 const Type *IntPtrTy = TD->getIntPtrType();
5440 TargetLowering::ArgListTy Args;
5441 TargetLowering::ArgListEntry Entry;
5442 Entry.Node = Dst;
5443 Entry.Ty = IntPtrTy;
5444 Args.push_back(Entry);
5445 Entry.Node = Size;
5446 Args.push_back(Entry);
5447 std::pair<SDValue,SDValue> CallResult =
5448 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5449 CallingConv::C, false,
5450 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5451 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005452 }
5453
Dan Gohmane8b391e2008-04-12 04:36:06 +00005454 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005455 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456 }
5457
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005458 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005459 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005460 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005461 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005462 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005463 unsigned BytesLeft = 0;
5464 bool TwoRepStos = false;
5465 if (ValC) {
5466 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005467 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005468
5469 // If the value is a constant, then we can potentially use larger sets.
5470 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005471 case 2: // WORD aligned
5472 AVT = MVT::i16;
5473 ValReg = X86::AX;
5474 Val = (Val << 8) | Val;
5475 break;
5476 case 0: // DWORD aligned
5477 AVT = MVT::i32;
5478 ValReg = X86::EAX;
5479 Val = (Val << 8) | Val;
5480 Val = (Val << 16) | Val;
5481 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5482 AVT = MVT::i64;
5483 ValReg = X86::RAX;
5484 Val = (Val << 32) | Val;
5485 }
5486 break;
5487 default: // Byte aligned
5488 AVT = MVT::i8;
5489 ValReg = X86::AL;
5490 Count = DAG.getIntPtrConstant(SizeVal);
5491 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 }
5493
Duncan Sandsec142ee2008-06-08 20:54:56 +00005494 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005495 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005496 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5497 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005498 }
5499
5500 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5501 InFlag);
5502 InFlag = Chain.getValue(1);
5503 } else {
5504 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005505 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005506 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 InFlag = Chain.getValue(1);
5508 }
5509
5510 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5511 Count, InFlag);
5512 InFlag = Chain.getValue(1);
5513 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005514 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005515 InFlag = Chain.getValue(1);
5516
5517 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005518 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005519 Ops.push_back(Chain);
5520 Ops.push_back(DAG.getValueType(AVT));
5521 Ops.push_back(InFlag);
5522 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5523
5524 if (TwoRepStos) {
5525 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005526 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005527 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5530 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5531 Left, InFlag);
5532 InFlag = Chain.getValue(1);
5533 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5534 Ops.clear();
5535 Ops.push_back(Chain);
5536 Ops.push_back(DAG.getValueType(MVT::i8));
5537 Ops.push_back(InFlag);
5538 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5539 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005540 // Handle the last 1 - 7 bytes.
5541 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005542 MVT AddrVT = Dst.getValueType();
5543 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005544
5545 Chain = DAG.getMemset(Chain,
5546 DAG.getNode(ISD::ADD, AddrVT, Dst,
5547 DAG.getConstant(Offset, AddrVT)),
5548 Src,
5549 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005550 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005551 }
5552
Dan Gohmane8b391e2008-04-12 04:36:06 +00005553 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554 return Chain;
5555}
5556
Dan Gohman8181bd12008-07-27 21:46:04 +00005557SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005558X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005559 SDValue Chain, SDValue Dst, SDValue Src,
5560 SDValue Size, unsigned Align,
5561 bool AlwaysInline,
5562 const Value *DstSV, uint64_t DstSVOff,
5563 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005564 // This requires the copy size to be a constant, preferrably
5565 // within a subtarget-specific limit.
5566 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5567 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005568 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005569 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005570 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005571 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005572
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005573 /// If not DWORD aligned, call the library.
5574 if ((Align & 3) != 0)
5575 return SDValue();
5576
5577 // DWORD aligned
5578 MVT AVT = MVT::i32;
5579 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005580 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581
Duncan Sands92c43912008-06-06 12:08:01 +00005582 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005583 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005584 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005585 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5589 Count, InFlag);
5590 InFlag = Chain.getValue(1);
5591 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005592 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005593 InFlag = Chain.getValue(1);
5594 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005595 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 InFlag = Chain.getValue(1);
5597
5598 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005599 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005600 Ops.push_back(Chain);
5601 Ops.push_back(DAG.getValueType(AVT));
5602 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005603 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005604
Dan Gohman8181bd12008-07-27 21:46:04 +00005605 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005606 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005607 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005608 // Handle the last 1 - 7 bytes.
5609 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005610 MVT DstVT = Dst.getValueType();
5611 MVT SrcVT = Src.getValueType();
5612 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005613 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005614 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005615 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005616 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005617 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005618 DAG.getConstant(BytesLeft, SizeVT),
5619 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005620 DstSV, DstSVOff + Offset,
5621 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005622 }
5623
Dan Gohmane8b391e2008-04-12 04:36:06 +00005624 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625}
5626
Dan Gohman8181bd12008-07-27 21:46:04 +00005627SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005628 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629
5630 if (!Subtarget->is64Bit()) {
5631 // vastart just stores the address of the VarArgsFrameIndex slot into the
5632 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005633 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005634 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005635 }
5636
5637 // __va_list_tag:
5638 // gp_offset (0 - 6 * 8)
5639 // fp_offset (48 - 48 + 8 * 16)
5640 // overflow_arg_area (point to parameters coming in memory).
5641 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005642 SmallVector<SDValue, 8> MemOps;
5643 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005644 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005645 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005646 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005647 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005648 MemOps.push_back(Store);
5649
5650 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005651 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005652 Store = DAG.getStore(Op.getOperand(0),
5653 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005654 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005655 MemOps.push_back(Store);
5656
5657 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005658 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005659 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005660 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005661 MemOps.push_back(Store);
5662
5663 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005664 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005665 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005666 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005667 MemOps.push_back(Store);
5668 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5669}
5670
Dan Gohman8181bd12008-07-27 21:46:04 +00005671SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005672 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5673 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005674 SDValue Chain = Op.getOperand(0);
5675 SDValue SrcPtr = Op.getOperand(1);
5676 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005677
5678 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5679 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005680 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005681}
5682
Dan Gohman8181bd12008-07-27 21:46:04 +00005683SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005684 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005685 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005686 SDValue Chain = Op.getOperand(0);
5687 SDValue DstPtr = Op.getOperand(1);
5688 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005689 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5690 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691
Dan Gohman840ff5c2008-04-18 20:55:41 +00005692 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5693 DAG.getIntPtrConstant(24), 8, false,
5694 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005695}
5696
Dan Gohman8181bd12008-07-27 21:46:04 +00005697SDValue
5698X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005699 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005700 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005701 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005702 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 case Intrinsic::x86_sse_comieq_ss:
5704 case Intrinsic::x86_sse_comilt_ss:
5705 case Intrinsic::x86_sse_comile_ss:
5706 case Intrinsic::x86_sse_comigt_ss:
5707 case Intrinsic::x86_sse_comige_ss:
5708 case Intrinsic::x86_sse_comineq_ss:
5709 case Intrinsic::x86_sse_ucomieq_ss:
5710 case Intrinsic::x86_sse_ucomilt_ss:
5711 case Intrinsic::x86_sse_ucomile_ss:
5712 case Intrinsic::x86_sse_ucomigt_ss:
5713 case Intrinsic::x86_sse_ucomige_ss:
5714 case Intrinsic::x86_sse_ucomineq_ss:
5715 case Intrinsic::x86_sse2_comieq_sd:
5716 case Intrinsic::x86_sse2_comilt_sd:
5717 case Intrinsic::x86_sse2_comile_sd:
5718 case Intrinsic::x86_sse2_comigt_sd:
5719 case Intrinsic::x86_sse2_comige_sd:
5720 case Intrinsic::x86_sse2_comineq_sd:
5721 case Intrinsic::x86_sse2_ucomieq_sd:
5722 case Intrinsic::x86_sse2_ucomilt_sd:
5723 case Intrinsic::x86_sse2_ucomile_sd:
5724 case Intrinsic::x86_sse2_ucomigt_sd:
5725 case Intrinsic::x86_sse2_ucomige_sd:
5726 case Intrinsic::x86_sse2_ucomineq_sd: {
5727 unsigned Opc = 0;
5728 ISD::CondCode CC = ISD::SETCC_INVALID;
5729 switch (IntNo) {
5730 default: break;
5731 case Intrinsic::x86_sse_comieq_ss:
5732 case Intrinsic::x86_sse2_comieq_sd:
5733 Opc = X86ISD::COMI;
5734 CC = ISD::SETEQ;
5735 break;
5736 case Intrinsic::x86_sse_comilt_ss:
5737 case Intrinsic::x86_sse2_comilt_sd:
5738 Opc = X86ISD::COMI;
5739 CC = ISD::SETLT;
5740 break;
5741 case Intrinsic::x86_sse_comile_ss:
5742 case Intrinsic::x86_sse2_comile_sd:
5743 Opc = X86ISD::COMI;
5744 CC = ISD::SETLE;
5745 break;
5746 case Intrinsic::x86_sse_comigt_ss:
5747 case Intrinsic::x86_sse2_comigt_sd:
5748 Opc = X86ISD::COMI;
5749 CC = ISD::SETGT;
5750 break;
5751 case Intrinsic::x86_sse_comige_ss:
5752 case Intrinsic::x86_sse2_comige_sd:
5753 Opc = X86ISD::COMI;
5754 CC = ISD::SETGE;
5755 break;
5756 case Intrinsic::x86_sse_comineq_ss:
5757 case Intrinsic::x86_sse2_comineq_sd:
5758 Opc = X86ISD::COMI;
5759 CC = ISD::SETNE;
5760 break;
5761 case Intrinsic::x86_sse_ucomieq_ss:
5762 case Intrinsic::x86_sse2_ucomieq_sd:
5763 Opc = X86ISD::UCOMI;
5764 CC = ISD::SETEQ;
5765 break;
5766 case Intrinsic::x86_sse_ucomilt_ss:
5767 case Intrinsic::x86_sse2_ucomilt_sd:
5768 Opc = X86ISD::UCOMI;
5769 CC = ISD::SETLT;
5770 break;
5771 case Intrinsic::x86_sse_ucomile_ss:
5772 case Intrinsic::x86_sse2_ucomile_sd:
5773 Opc = X86ISD::UCOMI;
5774 CC = ISD::SETLE;
5775 break;
5776 case Intrinsic::x86_sse_ucomigt_ss:
5777 case Intrinsic::x86_sse2_ucomigt_sd:
5778 Opc = X86ISD::UCOMI;
5779 CC = ISD::SETGT;
5780 break;
5781 case Intrinsic::x86_sse_ucomige_ss:
5782 case Intrinsic::x86_sse2_ucomige_sd:
5783 Opc = X86ISD::UCOMI;
5784 CC = ISD::SETGE;
5785 break;
5786 case Intrinsic::x86_sse_ucomineq_ss:
5787 case Intrinsic::x86_sse2_ucomineq_sd:
5788 Opc = X86ISD::UCOMI;
5789 CC = ISD::SETNE;
5790 break;
5791 }
5792
Dan Gohman8181bd12008-07-27 21:46:04 +00005793 SDValue LHS = Op.getOperand(1);
5794 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00005795 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8181bd12008-07-27 21:46:04 +00005796 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5797 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005798 DAG.getConstant(X86CC, MVT::i8), Cond);
5799 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005800 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005801
5802 // Fix vector shift instructions where the last operand is a non-immediate
5803 // i32 value.
5804 case Intrinsic::x86_sse2_pslli_w:
5805 case Intrinsic::x86_sse2_pslli_d:
5806 case Intrinsic::x86_sse2_pslli_q:
5807 case Intrinsic::x86_sse2_psrli_w:
5808 case Intrinsic::x86_sse2_psrli_d:
5809 case Intrinsic::x86_sse2_psrli_q:
5810 case Intrinsic::x86_sse2_psrai_w:
5811 case Intrinsic::x86_sse2_psrai_d:
5812 case Intrinsic::x86_mmx_pslli_w:
5813 case Intrinsic::x86_mmx_pslli_d:
5814 case Intrinsic::x86_mmx_pslli_q:
5815 case Intrinsic::x86_mmx_psrli_w:
5816 case Intrinsic::x86_mmx_psrli_d:
5817 case Intrinsic::x86_mmx_psrli_q:
5818 case Intrinsic::x86_mmx_psrai_w:
5819 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005820 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005821 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005822 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005823
5824 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005825 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005826 switch (IntNo) {
5827 case Intrinsic::x86_sse2_pslli_w:
5828 NewIntNo = Intrinsic::x86_sse2_psll_w;
5829 break;
5830 case Intrinsic::x86_sse2_pslli_d:
5831 NewIntNo = Intrinsic::x86_sse2_psll_d;
5832 break;
5833 case Intrinsic::x86_sse2_pslli_q:
5834 NewIntNo = Intrinsic::x86_sse2_psll_q;
5835 break;
5836 case Intrinsic::x86_sse2_psrli_w:
5837 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5838 break;
5839 case Intrinsic::x86_sse2_psrli_d:
5840 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5841 break;
5842 case Intrinsic::x86_sse2_psrli_q:
5843 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5844 break;
5845 case Intrinsic::x86_sse2_psrai_w:
5846 NewIntNo = Intrinsic::x86_sse2_psra_w;
5847 break;
5848 case Intrinsic::x86_sse2_psrai_d:
5849 NewIntNo = Intrinsic::x86_sse2_psra_d;
5850 break;
5851 default: {
5852 ShAmtVT = MVT::v2i32;
5853 switch (IntNo) {
5854 case Intrinsic::x86_mmx_pslli_w:
5855 NewIntNo = Intrinsic::x86_mmx_psll_w;
5856 break;
5857 case Intrinsic::x86_mmx_pslli_d:
5858 NewIntNo = Intrinsic::x86_mmx_psll_d;
5859 break;
5860 case Intrinsic::x86_mmx_pslli_q:
5861 NewIntNo = Intrinsic::x86_mmx_psll_q;
5862 break;
5863 case Intrinsic::x86_mmx_psrli_w:
5864 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5865 break;
5866 case Intrinsic::x86_mmx_psrli_d:
5867 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5868 break;
5869 case Intrinsic::x86_mmx_psrli_q:
5870 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5871 break;
5872 case Intrinsic::x86_mmx_psrai_w:
5873 NewIntNo = Intrinsic::x86_mmx_psra_w;
5874 break;
5875 case Intrinsic::x86_mmx_psrai_d:
5876 NewIntNo = Intrinsic::x86_mmx_psra_d;
5877 break;
5878 default: abort(); // Can't reach here.
5879 }
5880 break;
5881 }
5882 }
Duncan Sands92c43912008-06-06 12:08:01 +00005883 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005884 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5885 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5886 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5887 DAG.getConstant(NewIntNo, MVT::i32),
5888 Op.getOperand(1), ShAmt);
5889 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005890 }
5891}
5892
Dan Gohman8181bd12008-07-27 21:46:04 +00005893SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005894 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005895 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005896 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005897
5898 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005899 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005900 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5901}
5902
Dan Gohman8181bd12008-07-27 21:46:04 +00005903SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005904 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5905 MFI->setFrameAddressIsTaken(true);
5906 MVT VT = Op.getValueType();
5907 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5908 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5909 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5910 while (Depth--)
5911 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5912 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005913}
5914
Dan Gohman8181bd12008-07-27 21:46:04 +00005915SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005916 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005917 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005918}
5919
Dan Gohman8181bd12008-07-27 21:46:04 +00005920SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005921{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005922 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue Chain = Op.getOperand(0);
5924 SDValue Offset = Op.getOperand(1);
5925 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005926
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005927 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5928 getPointerTy());
5929 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005930
Dan Gohman8181bd12008-07-27 21:46:04 +00005931 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005932 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005933 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5934 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005935 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5936 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005937
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005938 return DAG.getNode(X86ISD::EH_RETURN,
5939 MVT::Other,
5940 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005941}
5942
Dan Gohman8181bd12008-07-27 21:46:04 +00005943SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005944 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005945 SDValue Root = Op.getOperand(0);
5946 SDValue Trmp = Op.getOperand(1); // trampoline
5947 SDValue FPtr = Op.getOperand(2); // nested function
5948 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005949
Dan Gohman12a9c082008-02-06 22:27:42 +00005950 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005951
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005952 const X86InstrInfo *TII =
5953 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5954
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005955 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005956 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005957
5958 // Large code-model.
5959
5960 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5961 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5962
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005963 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5964 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005965
5966 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5967
5968 // Load the pointer to the nested function into R11.
5969 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005970 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005971 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005972 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005973
5974 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005975 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005976
5977 // Load the 'nest' parameter value into R10.
5978 // R10 is specified in X86CallingConv.td
5979 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5980 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5981 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005982 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005983
5984 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005985 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005986
5987 // Jump to the nested function.
5988 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5989 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5990 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005991 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005992
5993 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5994 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5995 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005996 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005997
Dan Gohman8181bd12008-07-27 21:46:04 +00005998 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005999 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00006000 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006001 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006002 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006003 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6004 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006005 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006006
6007 switch (CC) {
6008 default:
6009 assert(0 && "Unsupported calling convention");
6010 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006011 case CallingConv::X86_StdCall: {
6012 // Pass 'nest' parameter in ECX.
6013 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006014 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006015
6016 // Check that ECX wasn't needed by an 'inreg' parameter.
6017 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006018 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006019
Chris Lattner1c8733e2008-03-12 17:45:29 +00006020 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006021 unsigned InRegCount = 0;
6022 unsigned Idx = 1;
6023
6024 for (FunctionType::param_iterator I = FTy->param_begin(),
6025 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006026 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006027 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006028 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006029
6030 if (InRegCount > 2) {
6031 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6032 abort();
6033 }
6034 }
6035 break;
6036 }
6037 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006038 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006039 // Pass 'nest' parameter in EAX.
6040 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006041 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006042 break;
6043 }
6044
Dan Gohman8181bd12008-07-27 21:46:04 +00006045 SDValue OutChains[4];
6046 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006047
6048 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
6049 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6050
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006051 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006052 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006053 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006054 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006055
6056 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006057 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006058
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006059 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006060 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6061 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006062 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006063
6064 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006065 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006066
Dan Gohman8181bd12008-07-27 21:46:04 +00006067 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006068 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006069 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006070 }
6071}
6072
Dan Gohman8181bd12008-07-27 21:46:04 +00006073SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006074 /*
6075 The rounding mode is in bits 11:10 of FPSR, and has the following
6076 settings:
6077 00 Round to nearest
6078 01 Round to -inf
6079 10 Round to +inf
6080 11 Round to 0
6081
6082 FLT_ROUNDS, on the other hand, expects the following:
6083 -1 Undefined
6084 0 Round to 0
6085 1 Round to nearest
6086 2 Round to +inf
6087 3 Round to -inf
6088
6089 To perform the conversion, we do:
6090 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6091 */
6092
6093 MachineFunction &MF = DAG.getMachineFunction();
6094 const TargetMachine &TM = MF.getTarget();
6095 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6096 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006097 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006098
6099 // Save FP Control Word to stack slot
6100 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006101 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006102
Dan Gohman8181bd12008-07-27 21:46:04 +00006103 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006104 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006105
6106 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006107 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006108
6109 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006110 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006111 DAG.getNode(ISD::SRL, MVT::i16,
6112 DAG.getNode(ISD::AND, MVT::i16,
6113 CWD, DAG.getConstant(0x800, MVT::i16)),
6114 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006115 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006116 DAG.getNode(ISD::SRL, MVT::i16,
6117 DAG.getNode(ISD::AND, MVT::i16,
6118 CWD, DAG.getConstant(0x400, MVT::i16)),
6119 DAG.getConstant(9, MVT::i8));
6120
Dan Gohman8181bd12008-07-27 21:46:04 +00006121 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006122 DAG.getNode(ISD::AND, MVT::i16,
6123 DAG.getNode(ISD::ADD, MVT::i16,
6124 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6125 DAG.getConstant(1, MVT::i16)),
6126 DAG.getConstant(3, MVT::i16));
6127
6128
Duncan Sands92c43912008-06-06 12:08:01 +00006129 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006130 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6131}
6132
Dan Gohman8181bd12008-07-27 21:46:04 +00006133SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006134 MVT VT = Op.getValueType();
6135 MVT OpVT = VT;
6136 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006137
6138 Op = Op.getOperand(0);
6139 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006140 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006141 OpVT = MVT::i32;
6142 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6143 }
Evan Cheng48679f42007-12-14 02:13:44 +00006144
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006145 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6146 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6147 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6148
6149 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006150 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006151 Ops.push_back(Op);
6152 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6153 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6154 Ops.push_back(Op.getValue(1));
6155 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6156
6157 // Finally xor with NumBits-1.
6158 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6159
Evan Cheng48679f42007-12-14 02:13:44 +00006160 if (VT == MVT::i8)
6161 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6162 return Op;
6163}
6164
Dan Gohman8181bd12008-07-27 21:46:04 +00006165SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006166 MVT VT = Op.getValueType();
6167 MVT OpVT = VT;
6168 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006169
6170 Op = Op.getOperand(0);
6171 if (VT == MVT::i8) {
6172 OpVT = MVT::i32;
6173 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6174 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006175
6176 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6177 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6178 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6179
6180 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006181 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006182 Ops.push_back(Op);
6183 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6184 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6185 Ops.push_back(Op.getValue(1));
6186 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6187
Evan Cheng48679f42007-12-14 02:13:44 +00006188 if (VT == MVT::i8)
6189 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6190 return Op;
6191}
6192
Mon P Wang14edb092008-12-18 21:42:19 +00006193SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6194 MVT VT = Op.getValueType();
6195 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
6196
6197 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6198 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6199 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6200 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6201 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6202 //
6203 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6204 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6205 // return AloBlo + AloBhi + AhiBlo;
6206
6207 SDValue A = Op.getOperand(0);
6208 SDValue B = Op.getOperand(1);
6209
6210 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6211 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6212 A, DAG.getConstant(32, MVT::i32));
6213 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6214 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6215 B, DAG.getConstant(32, MVT::i32));
6216 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6217 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6218 A, B);
6219 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6220 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6221 A, Bhi);
6222 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6223 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6224 Ahi, B);
6225 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6226 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6227 AloBhi, DAG.getConstant(32, MVT::i32));
6228 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
6229 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6230 AhiBlo, DAG.getConstant(32, MVT::i32));
6231 SDValue Res = DAG.getNode(ISD::ADD, VT, AloBlo, AloBhi);
6232 Res = DAG.getNode(ISD::ADD, VT, Res, AhiBlo);
6233 return Res;
6234}
6235
6236
Bill Wendling7e04be62008-12-09 22:08:41 +00006237SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6238 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6239 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006240 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6241 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006242 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006243 SDValue LHS = N->getOperand(0);
6244 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006245 unsigned BaseOp = 0;
6246 unsigned Cond = 0;
6247
6248 switch (Op.getOpcode()) {
6249 default: assert(0 && "Unknown ovf instruction!");
6250 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006251 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006252 Cond = X86::COND_O;
6253 break;
6254 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006255 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006256 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006257 break;
6258 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006259 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006260 Cond = X86::COND_O;
6261 break;
6262 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006263 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006264 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006265 break;
6266 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006267 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006268 Cond = X86::COND_O;
6269 break;
6270 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00006271 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006272 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00006273 break;
6274 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006275
Bill Wendlingd3511522008-12-02 01:06:39 +00006276 // Also sets EFLAGS.
6277 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006278 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006279
Bill Wendlingd3511522008-12-02 01:06:39 +00006280 SDValue SetCC =
6281 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006282 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006283
Bill Wendlingd3511522008-12-02 01:06:39 +00006284 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6285 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006286}
6287
Dan Gohman8181bd12008-07-27 21:46:04 +00006288SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006289 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006290 unsigned Reg = 0;
6291 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006292 switch(T.getSimpleVT()) {
6293 default:
6294 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006295 case MVT::i8: Reg = X86::AL; size = 1; break;
6296 case MVT::i16: Reg = X86::AX; size = 2; break;
6297 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006298 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006299 assert(Subtarget->is64Bit() && "Node not type legal!");
6300 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006301 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006302 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006303 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006304 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006305 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006306 Op.getOperand(1),
6307 Op.getOperand(3),
6308 DAG.getTargetConstant(size, MVT::i8),
6309 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006310 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006311 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6312 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006313 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6314 return cpOut;
6315}
6316
Duncan Sands7d9834b2008-12-01 11:39:25 +00006317SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006318 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006319 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006320 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006321 SDValue TheChain = Op.getOperand(0);
6322 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6323 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6324 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6325 rax.getValue(2));
6326 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6327 DAG.getConstant(32, MVT::i8));
6328 SDValue Ops[] = {
6329 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6330 rdx.getValue(1)
6331 };
6332 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006333}
6334
Dale Johannesen9011d872008-09-29 22:25:26 +00006335SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6336 SDNode *Node = Op.getNode();
6337 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006338 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006339 DAG.getConstant(0, T), Node->getOperand(2));
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006340 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD,
6341 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00006342 Node->getOperand(0),
6343 Node->getOperand(1), negOp,
6344 cast<AtomicSDNode>(Node)->getSrcValue(),
6345 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006346}
6347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006348/// LowerOperation - Provide custom lowering hooks for some operations.
6349///
Dan Gohman8181bd12008-07-27 21:46:04 +00006350SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006351 switch (Op.getOpcode()) {
6352 default: assert(0 && "Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006353 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6354 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006355 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6356 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6357 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6358 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6359 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6360 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6361 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6362 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006363 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006364 case ISD::SHL_PARTS:
6365 case ISD::SRA_PARTS:
6366 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6367 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006368 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6370 case ISD::FABS: return LowerFABS(Op, DAG);
6371 case ISD::FNEG: return LowerFNEG(Op, DAG);
6372 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006373 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006374 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006375 case ISD::SELECT: return LowerSELECT(Op, DAG);
6376 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006377 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6378 case ISD::CALL: return LowerCALL(Op, DAG);
6379 case ISD::RET: return LowerRET(Op, DAG);
6380 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006381 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006382 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006383 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6384 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6385 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6386 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6387 case ISD::FRAME_TO_ARGS_OFFSET:
6388 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6389 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6390 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006391 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006392 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006393 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6394 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00006395 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006396 case ISD::SADDO:
6397 case ISD::UADDO:
6398 case ISD::SSUBO:
6399 case ISD::USUBO:
6400 case ISD::SMULO:
6401 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006402 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006404}
6405
Duncan Sands7d9834b2008-12-01 11:39:25 +00006406void X86TargetLowering::
6407ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6408 SelectionDAG &DAG, unsigned NewOp) {
6409 MVT T = Node->getValueType(0);
6410 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6411
6412 SDValue Chain = Node->getOperand(0);
6413 SDValue In1 = Node->getOperand(1);
6414 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6415 Node->getOperand(2), DAG.getIntPtrConstant(0));
6416 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6417 Node->getOperand(2), DAG.getIntPtrConstant(1));
6418 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6419 // have a MemOperand. Pass the info through as a normal operand.
6420 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6421 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6422 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6423 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6424 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6425 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6426 Results.push_back(Result.getValue(2));
6427}
6428
Duncan Sandsac496a12008-07-04 11:47:58 +00006429/// ReplaceNodeResults - Replace a node with an illegal result type
6430/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006431void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6432 SmallVectorImpl<SDValue>&Results,
6433 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006434 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006435 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006436 assert(false && "Do not know how to custom type legalize this operation!");
6437 return;
6438 case ISD::FP_TO_SINT: {
6439 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6440 SDValue FIST = Vals.first, StackSlot = Vals.second;
6441 if (FIST.getNode() != 0) {
6442 MVT VT = N->getValueType(0);
6443 // Return a load from the stack slot.
6444 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6445 }
6446 return;
6447 }
6448 case ISD::READCYCLECOUNTER: {
6449 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6450 SDValue TheChain = N->getOperand(0);
6451 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6452 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6453 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6454 eax.getValue(2));
6455 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6456 SDValue Ops[] = { eax, edx };
6457 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6458 Results.push_back(edx.getValue(1));
6459 return;
6460 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006461 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006462 MVT T = N->getValueType(0);
6463 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6464 SDValue cpInL, cpInH;
6465 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6466 DAG.getConstant(0, MVT::i32));
6467 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6468 DAG.getConstant(1, MVT::i32));
6469 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6470 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6471 cpInL.getValue(1));
6472 SDValue swapInL, swapInH;
6473 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6474 DAG.getConstant(0, MVT::i32));
6475 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6476 DAG.getConstant(1, MVT::i32));
6477 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6478 cpInH.getValue(1));
6479 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6480 swapInL.getValue(1));
6481 SDValue Ops[] = { swapInH.getValue(0),
6482 N->getOperand(1),
6483 swapInH.getValue(1) };
6484 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6485 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6486 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6487 Result.getValue(1));
6488 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6489 cpOutL.getValue(2));
6490 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6491 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6492 Results.push_back(cpOutH.getValue(1));
6493 return;
6494 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006495 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006496 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6497 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006498 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006499 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6500 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006501 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006502 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6503 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006504 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006505 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6506 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006507 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006508 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6509 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006510 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006511 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6512 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00006513 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006514 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6515 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006517}
6518
6519const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6520 switch (Opcode) {
6521 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006522 case X86ISD::BSF: return "X86ISD::BSF";
6523 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006524 case X86ISD::SHLD: return "X86ISD::SHLD";
6525 case X86ISD::SHRD: return "X86ISD::SHRD";
6526 case X86ISD::FAND: return "X86ISD::FAND";
6527 case X86ISD::FOR: return "X86ISD::FOR";
6528 case X86ISD::FXOR: return "X86ISD::FXOR";
6529 case X86ISD::FSRL: return "X86ISD::FSRL";
6530 case X86ISD::FILD: return "X86ISD::FILD";
6531 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6532 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6533 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6534 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6535 case X86ISD::FLD: return "X86ISD::FLD";
6536 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006537 case X86ISD::CALL: return "X86ISD::CALL";
6538 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6539 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00006540 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006541 case X86ISD::CMP: return "X86ISD::CMP";
6542 case X86ISD::COMI: return "X86ISD::COMI";
6543 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6544 case X86ISD::SETCC: return "X86ISD::SETCC";
6545 case X86ISD::CMOV: return "X86ISD::CMOV";
6546 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6547 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6548 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6549 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006550 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6551 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006552 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006553 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006554 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6555 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006556 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6557 case X86ISD::FMAX: return "X86ISD::FMAX";
6558 case X86ISD::FMIN: return "X86ISD::FMIN";
6559 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6560 case X86ISD::FRCP: return "X86ISD::FRCP";
6561 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6562 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6563 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006564 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006565 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006566 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6567 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006568 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6569 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6570 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6571 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6572 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6573 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006574 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6575 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006576 case X86ISD::VSHL: return "X86ISD::VSHL";
6577 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006578 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6579 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6580 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6581 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6582 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6583 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6584 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6585 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6586 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6587 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006588 case X86ISD::ADD: return "X86ISD::ADD";
6589 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00006590 case X86ISD::SMUL: return "X86ISD::SMUL";
6591 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006592 }
6593}
6594
6595// isLegalAddressingMode - Return true if the addressing mode represented
6596// by AM is legal for this target, for a load/store of the specified type.
6597bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6598 const Type *Ty) const {
6599 // X86 supports extremely general addressing modes.
6600
6601 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6602 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6603 return false;
6604
6605 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006606 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006607 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6608 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006609 // If BaseGV requires a register, we cannot also have a BaseReg.
6610 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6611 AM.HasBaseReg)
6612 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006613
6614 // X86-64 only supports addr of globals in small code model.
6615 if (Subtarget->is64Bit()) {
6616 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6617 return false;
6618 // If lower 4G is not available, then we must use rip-relative addressing.
6619 if (AM.BaseOffs || AM.Scale > 1)
6620 return false;
6621 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006622 }
6623
6624 switch (AM.Scale) {
6625 case 0:
6626 case 1:
6627 case 2:
6628 case 4:
6629 case 8:
6630 // These scales always work.
6631 break;
6632 case 3:
6633 case 5:
6634 case 9:
6635 // These scales are formed with basereg+scalereg. Only accept if there is
6636 // no basereg yet.
6637 if (AM.HasBaseReg)
6638 return false;
6639 break;
6640 default: // Other stuff never works.
6641 return false;
6642 }
6643
6644 return true;
6645}
6646
6647
Evan Cheng27a820a2007-10-26 01:56:11 +00006648bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6649 if (!Ty1->isInteger() || !Ty2->isInteger())
6650 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006651 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6652 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006653 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006654 return false;
6655 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006656}
6657
Duncan Sands92c43912008-06-06 12:08:01 +00006658bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6659 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006660 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006661 unsigned NumBits1 = VT1.getSizeInBits();
6662 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006663 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006664 return false;
6665 return Subtarget->is64Bit() || NumBits1 < 64;
6666}
Evan Cheng27a820a2007-10-26 01:56:11 +00006667
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006668/// isShuffleMaskLegal - Targets can use this to indicate that they only
6669/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6670/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6671/// are assumed to be legal.
6672bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006673X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006674 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006675 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006676 return (Mask.getNode()->getNumOperands() <= 4 ||
6677 isIdentityMask(Mask.getNode()) ||
6678 isIdentityMask(Mask.getNode(), true) ||
6679 isSplatMask(Mask.getNode()) ||
6680 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6681 X86::isUNPCKLMask(Mask.getNode()) ||
6682 X86::isUNPCKHMask(Mask.getNode()) ||
6683 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6684 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006685}
6686
Dan Gohman48d5f062008-04-09 20:09:42 +00006687bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006688X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006689 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690 unsigned NumElts = BVOps.size();
6691 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006692 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006693 if (NumElts == 2) return true;
6694 if (NumElts == 4) {
6695 return (isMOVLMask(&BVOps[0], 4) ||
6696 isCommutedMOVL(&BVOps[0], 4, true) ||
6697 isSHUFPMask(&BVOps[0], 4) ||
6698 isCommutedSHUFP(&BVOps[0], 4));
6699 }
6700 return false;
6701}
6702
6703//===----------------------------------------------------------------------===//
6704// X86 Scheduler Hooks
6705//===----------------------------------------------------------------------===//
6706
Mon P Wang078a62d2008-05-05 19:05:59 +00006707// private utility function
6708MachineBasicBlock *
6709X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6710 MachineBasicBlock *MBB,
6711 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006712 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006713 unsigned LoadOpc,
6714 unsigned CXchgOpc,
6715 unsigned copyOpc,
6716 unsigned notOpc,
6717 unsigned EAXreg,
6718 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006719 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006720 // For the atomic bitwise operator, we generate
6721 // thisMBB:
6722 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006723 // ld t1 = [bitinstr.addr]
6724 // op t2 = t1, [bitinstr.val]
6725 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006726 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6727 // bz newMBB
6728 // fallthrough -->nextMBB
6729 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6730 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006731 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006732 ++MBBIter;
6733
6734 /// First build the CFG
6735 MachineFunction *F = MBB->getParent();
6736 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006737 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6738 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6739 F->insert(MBBIter, newMBB);
6740 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006741
6742 // Move all successors to thisMBB to nextMBB
6743 nextMBB->transferSuccessors(thisMBB);
6744
6745 // Update thisMBB to fall through to newMBB
6746 thisMBB->addSuccessor(newMBB);
6747
6748 // newMBB jumps to itself and fall through to nextMBB
6749 newMBB->addSuccessor(nextMBB);
6750 newMBB->addSuccessor(newMBB);
6751
6752 // Insert instructions into newMBB based on incoming instruction
6753 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6754 MachineOperand& destOper = bInstr->getOperand(0);
6755 MachineOperand* argOpers[6];
6756 int numArgs = bInstr->getNumOperands() - 1;
6757 for (int i=0; i < numArgs; ++i)
6758 argOpers[i] = &bInstr->getOperand(i+1);
6759
6760 // x86 address has 4 operands: base, index, scale, and displacement
6761 int lastAddrIndx = 3; // [0,3]
6762 int valArgIndx = 4;
6763
Dale Johannesend20e4452008-08-19 18:47:28 +00006764 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6765 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006766 for (int i=0; i <= lastAddrIndx; ++i)
6767 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006768
Dale Johannesend20e4452008-08-19 18:47:28 +00006769 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006770 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006771 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006772 }
6773 else
6774 tt = t1;
6775
Dale Johannesend20e4452008-08-19 18:47:28 +00006776 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006777 assert((argOpers[valArgIndx]->isReg() ||
6778 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006779 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006780 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006781 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6782 else
6783 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006784 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006785 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006786
Dale Johannesend20e4452008-08-19 18:47:28 +00006787 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006788 MIB.addReg(t1);
6789
Dale Johannesend20e4452008-08-19 18:47:28 +00006790 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006791 for (int i=0; i <= lastAddrIndx; ++i)
6792 (*MIB).addOperand(*argOpers[i]);
6793 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006794 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6795 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6796
Dale Johannesend20e4452008-08-19 18:47:28 +00006797 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6798 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006799
6800 // insert branch
6801 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6802
Dan Gohman221a4372008-07-07 23:14:23 +00006803 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006804 return nextMBB;
6805}
6806
Dale Johannesen44eb5372008-10-03 19:41:08 +00006807// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006808MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006809X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6810 MachineBasicBlock *MBB,
6811 unsigned regOpcL,
6812 unsigned regOpcH,
6813 unsigned immOpcL,
6814 unsigned immOpcH,
6815 bool invSrc) {
6816 // For the atomic bitwise operator, we generate
6817 // thisMBB (instructions are in pairs, except cmpxchg8b)
6818 // ld t1,t2 = [bitinstr.addr]
6819 // newMBB:
6820 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6821 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006822 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006823 // mov ECX, EBX <- t5, t6
6824 // mov EAX, EDX <- t1, t2
6825 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6826 // mov t3, t4 <- EAX, EDX
6827 // bz newMBB
6828 // result in out1, out2
6829 // fallthrough -->nextMBB
6830
6831 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6832 const unsigned LoadOpc = X86::MOV32rm;
6833 const unsigned copyOpc = X86::MOV32rr;
6834 const unsigned NotOpc = X86::NOT32r;
6835 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6836 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6837 MachineFunction::iterator MBBIter = MBB;
6838 ++MBBIter;
6839
6840 /// First build the CFG
6841 MachineFunction *F = MBB->getParent();
6842 MachineBasicBlock *thisMBB = MBB;
6843 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6844 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6845 F->insert(MBBIter, newMBB);
6846 F->insert(MBBIter, nextMBB);
6847
6848 // Move all successors to thisMBB to nextMBB
6849 nextMBB->transferSuccessors(thisMBB);
6850
6851 // Update thisMBB to fall through to newMBB
6852 thisMBB->addSuccessor(newMBB);
6853
6854 // newMBB jumps to itself and fall through to nextMBB
6855 newMBB->addSuccessor(nextMBB);
6856 newMBB->addSuccessor(newMBB);
6857
6858 // Insert instructions into newMBB based on incoming instruction
6859 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6860 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6861 MachineOperand& dest1Oper = bInstr->getOperand(0);
6862 MachineOperand& dest2Oper = bInstr->getOperand(1);
6863 MachineOperand* argOpers[6];
6864 for (int i=0; i < 6; ++i)
6865 argOpers[i] = &bInstr->getOperand(i+2);
6866
6867 // x86 address has 4 operands: base, index, scale, and displacement
6868 int lastAddrIndx = 3; // [0,3]
6869
6870 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6871 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6872 for (int i=0; i <= lastAddrIndx; ++i)
6873 (*MIB).addOperand(*argOpers[i]);
6874 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6875 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006876 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006877 for (int i=0; i <= lastAddrIndx-1; ++i)
6878 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006879 MachineOperand newOp3 = *(argOpers[3]);
6880 if (newOp3.isImm())
6881 newOp3.setImm(newOp3.getImm()+4);
6882 else
6883 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006884 (*MIB).addOperand(newOp3);
6885
6886 // t3/4 are defined later, at the bottom of the loop
6887 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6888 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6889 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6890 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6891 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6892 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6893
6894 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6895 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6896 if (invSrc) {
6897 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6898 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6899 } else {
6900 tt1 = t1;
6901 tt2 = t2;
6902 }
6903
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006904 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006905 "invalid operand");
6906 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6907 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006908 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006909 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6910 else
6911 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006912 if (regOpcL != X86::MOV32rr)
6913 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006914 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006915 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6916 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6917 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006918 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6919 else
6920 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006921 if (regOpcH != X86::MOV32rr)
6922 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006923 (*MIB).addOperand(*argOpers[5]);
6924
6925 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6926 MIB.addReg(t1);
6927 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6928 MIB.addReg(t2);
6929
6930 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6931 MIB.addReg(t5);
6932 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6933 MIB.addReg(t6);
6934
6935 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6936 for (int i=0; i <= lastAddrIndx; ++i)
6937 (*MIB).addOperand(*argOpers[i]);
6938
6939 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6940 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6941
6942 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6943 MIB.addReg(X86::EAX);
6944 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6945 MIB.addReg(X86::EDX);
6946
6947 // insert branch
6948 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6949
6950 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6951 return nextMBB;
6952}
6953
6954// private utility function
6955MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006956X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6957 MachineBasicBlock *MBB,
6958 unsigned cmovOpc) {
6959 // For the atomic min/max operator, we generate
6960 // thisMBB:
6961 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006962 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006963 // mov t2 = [min/max.val]
6964 // cmp t1, t2
6965 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006966 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006967 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6968 // bz newMBB
6969 // fallthrough -->nextMBB
6970 //
6971 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6972 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006973 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006974 ++MBBIter;
6975
6976 /// First build the CFG
6977 MachineFunction *F = MBB->getParent();
6978 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006979 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6980 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6981 F->insert(MBBIter, newMBB);
6982 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006983
6984 // Move all successors to thisMBB to nextMBB
6985 nextMBB->transferSuccessors(thisMBB);
6986
6987 // Update thisMBB to fall through to newMBB
6988 thisMBB->addSuccessor(newMBB);
6989
6990 // newMBB jumps to newMBB and fall through to nextMBB
6991 newMBB->addSuccessor(nextMBB);
6992 newMBB->addSuccessor(newMBB);
6993
6994 // Insert instructions into newMBB based on incoming instruction
6995 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6996 MachineOperand& destOper = mInstr->getOperand(0);
6997 MachineOperand* argOpers[6];
6998 int numArgs = mInstr->getNumOperands() - 1;
6999 for (int i=0; i < numArgs; ++i)
7000 argOpers[i] = &mInstr->getOperand(i+1);
7001
7002 // x86 address has 4 operands: base, index, scale, and displacement
7003 int lastAddrIndx = 3; // [0,3]
7004 int valArgIndx = 4;
7005
Mon P Wang318b0372008-05-05 22:56:23 +00007006 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7007 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007008 for (int i=0; i <= lastAddrIndx; ++i)
7009 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00007010
Mon P Wang078a62d2008-05-05 19:05:59 +00007011 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007012 assert((argOpers[valArgIndx]->isReg() ||
7013 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007014 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00007015
7016 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007017 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00007018 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7019 else
7020 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
7021 (*MIB).addOperand(*argOpers[valArgIndx]);
7022
Mon P Wang318b0372008-05-05 22:56:23 +00007023 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
7024 MIB.addReg(t1);
7025
Mon P Wang078a62d2008-05-05 19:05:59 +00007026 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
7027 MIB.addReg(t1);
7028 MIB.addReg(t2);
7029
7030 // Generate movc
7031 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
7032 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
7033 MIB.addReg(t2);
7034 MIB.addReg(t1);
7035
7036 // Cmp and exchange if none has modified the memory location
7037 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
7038 for (int i=0; i <= lastAddrIndx; ++i)
7039 (*MIB).addOperand(*argOpers[i]);
7040 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00007041 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7042 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00007043
7044 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
7045 MIB.addReg(X86::EAX);
7046
7047 // insert branch
7048 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
7049
Dan Gohman221a4372008-07-07 23:14:23 +00007050 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007051 return nextMBB;
7052}
7053
7054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007055MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00007056X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
7057 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007058 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7059 switch (MI->getOpcode()) {
7060 default: assert(false && "Unexpected instr type to insert");
Mon P Wang83edba52008-12-12 01:25:51 +00007061 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007062 case X86::CMOV_FR32:
7063 case X86::CMOV_FR64:
7064 case X86::CMOV_V4F32:
7065 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00007066 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007067 // To "insert" a SELECT_CC instruction, we actually have to insert the
7068 // diamond control-flow pattern. The incoming instruction knows the
7069 // destination vreg to set, the condition code register to branch on, the
7070 // true/false values to select between, and a branch opcode to use.
7071 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007072 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073 ++It;
7074
7075 // thisMBB:
7076 // ...
7077 // TrueVal = ...
7078 // cmpTY ccX, r1, r2
7079 // bCC copy1MBB
7080 // fallthrough --> copy0MBB
7081 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00007082 MachineFunction *F = BB->getParent();
7083 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7084 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085 unsigned Opc =
7086 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
7087 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00007088 F->insert(It, copy0MBB);
7089 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007090 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007091 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00007092 sinkMBB->transferSuccessors(BB);
7093
7094 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007095 BB->addSuccessor(copy0MBB);
7096 BB->addSuccessor(sinkMBB);
7097
7098 // copy0MBB:
7099 // %FalseValue = ...
7100 // # fallthrough to sinkMBB
7101 BB = copy0MBB;
7102
7103 // Update machine-CFG edges
7104 BB->addSuccessor(sinkMBB);
7105
7106 // sinkMBB:
7107 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7108 // ...
7109 BB = sinkMBB;
7110 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7111 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7112 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7113
Dan Gohman221a4372008-07-07 23:14:23 +00007114 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007115 return BB;
7116 }
7117
7118 case X86::FP32_TO_INT16_IN_MEM:
7119 case X86::FP32_TO_INT32_IN_MEM:
7120 case X86::FP32_TO_INT64_IN_MEM:
7121 case X86::FP64_TO_INT16_IN_MEM:
7122 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007123 case X86::FP64_TO_INT64_IN_MEM:
7124 case X86::FP80_TO_INT16_IN_MEM:
7125 case X86::FP80_TO_INT32_IN_MEM:
7126 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007127 // Change the floating point control register to use "round towards zero"
7128 // mode when truncating to an integer value.
7129 MachineFunction *F = BB->getParent();
7130 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7131 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7132
7133 // Load the old value of the high byte of the control word...
7134 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007135 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7137
7138 // Set the high part to be round to zero...
7139 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7140 .addImm(0xC7F);
7141
7142 // Reload the modified control word now...
7143 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7144
7145 // Restore the memory image of control word to original value
7146 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7147 .addReg(OldCW);
7148
7149 // Get the X86 opcode to use.
7150 unsigned Opc;
7151 switch (MI->getOpcode()) {
7152 default: assert(0 && "illegal opcode!");
7153 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7154 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7155 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7156 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7157 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7158 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007159 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7160 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7161 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162 }
7163
7164 X86AddressMode AM;
7165 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007166 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007167 AM.BaseType = X86AddressMode::RegBase;
7168 AM.Base.Reg = Op.getReg();
7169 } else {
7170 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007171 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007172 }
7173 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007174 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007175 AM.Scale = Op.getImm();
7176 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007177 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007178 AM.IndexReg = Op.getImm();
7179 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007180 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007181 AM.GV = Op.getGlobal();
7182 } else {
7183 AM.Disp = Op.getImm();
7184 }
7185 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7186 .addReg(MI->getOperand(4).getReg());
7187
7188 // Reload the original control word now.
7189 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7190
Dan Gohman221a4372008-07-07 23:14:23 +00007191 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192 return BB;
7193 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007194 case X86::ATOMAND32:
7195 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007196 X86::AND32ri, X86::MOV32rm,
7197 X86::LCMPXCHG32, X86::MOV32rr,
7198 X86::NOT32r, X86::EAX,
7199 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007200 case X86::ATOMOR32:
7201 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007202 X86::OR32ri, X86::MOV32rm,
7203 X86::LCMPXCHG32, X86::MOV32rr,
7204 X86::NOT32r, X86::EAX,
7205 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007206 case X86::ATOMXOR32:
7207 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007208 X86::XOR32ri, X86::MOV32rm,
7209 X86::LCMPXCHG32, X86::MOV32rr,
7210 X86::NOT32r, X86::EAX,
7211 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007212 case X86::ATOMNAND32:
7213 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007214 X86::AND32ri, X86::MOV32rm,
7215 X86::LCMPXCHG32, X86::MOV32rr,
7216 X86::NOT32r, X86::EAX,
7217 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007218 case X86::ATOMMIN32:
7219 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7220 case X86::ATOMMAX32:
7221 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7222 case X86::ATOMUMIN32:
7223 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7224 case X86::ATOMUMAX32:
7225 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007226
7227 case X86::ATOMAND16:
7228 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7229 X86::AND16ri, X86::MOV16rm,
7230 X86::LCMPXCHG16, X86::MOV16rr,
7231 X86::NOT16r, X86::AX,
7232 X86::GR16RegisterClass);
7233 case X86::ATOMOR16:
7234 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7235 X86::OR16ri, X86::MOV16rm,
7236 X86::LCMPXCHG16, X86::MOV16rr,
7237 X86::NOT16r, X86::AX,
7238 X86::GR16RegisterClass);
7239 case X86::ATOMXOR16:
7240 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7241 X86::XOR16ri, X86::MOV16rm,
7242 X86::LCMPXCHG16, X86::MOV16rr,
7243 X86::NOT16r, X86::AX,
7244 X86::GR16RegisterClass);
7245 case X86::ATOMNAND16:
7246 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7247 X86::AND16ri, X86::MOV16rm,
7248 X86::LCMPXCHG16, X86::MOV16rr,
7249 X86::NOT16r, X86::AX,
7250 X86::GR16RegisterClass, true);
7251 case X86::ATOMMIN16:
7252 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7253 case X86::ATOMMAX16:
7254 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7255 case X86::ATOMUMIN16:
7256 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7257 case X86::ATOMUMAX16:
7258 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7259
7260 case X86::ATOMAND8:
7261 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7262 X86::AND8ri, X86::MOV8rm,
7263 X86::LCMPXCHG8, X86::MOV8rr,
7264 X86::NOT8r, X86::AL,
7265 X86::GR8RegisterClass);
7266 case X86::ATOMOR8:
7267 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7268 X86::OR8ri, X86::MOV8rm,
7269 X86::LCMPXCHG8, X86::MOV8rr,
7270 X86::NOT8r, X86::AL,
7271 X86::GR8RegisterClass);
7272 case X86::ATOMXOR8:
7273 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7274 X86::XOR8ri, X86::MOV8rm,
7275 X86::LCMPXCHG8, X86::MOV8rr,
7276 X86::NOT8r, X86::AL,
7277 X86::GR8RegisterClass);
7278 case X86::ATOMNAND8:
7279 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7280 X86::AND8ri, X86::MOV8rm,
7281 X86::LCMPXCHG8, X86::MOV8rr,
7282 X86::NOT8r, X86::AL,
7283 X86::GR8RegisterClass, true);
7284 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007285 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007286 case X86::ATOMAND64:
7287 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7288 X86::AND64ri32, X86::MOV64rm,
7289 X86::LCMPXCHG64, X86::MOV64rr,
7290 X86::NOT64r, X86::RAX,
7291 X86::GR64RegisterClass);
7292 case X86::ATOMOR64:
7293 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7294 X86::OR64ri32, X86::MOV64rm,
7295 X86::LCMPXCHG64, X86::MOV64rr,
7296 X86::NOT64r, X86::RAX,
7297 X86::GR64RegisterClass);
7298 case X86::ATOMXOR64:
7299 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7300 X86::XOR64ri32, X86::MOV64rm,
7301 X86::LCMPXCHG64, X86::MOV64rr,
7302 X86::NOT64r, X86::RAX,
7303 X86::GR64RegisterClass);
7304 case X86::ATOMNAND64:
7305 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7306 X86::AND64ri32, X86::MOV64rm,
7307 X86::LCMPXCHG64, X86::MOV64rr,
7308 X86::NOT64r, X86::RAX,
7309 X86::GR64RegisterClass, true);
7310 case X86::ATOMMIN64:
7311 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7312 case X86::ATOMMAX64:
7313 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7314 case X86::ATOMUMIN64:
7315 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7316 case X86::ATOMUMAX64:
7317 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007318
7319 // This group does 64-bit operations on a 32-bit host.
7320 case X86::ATOMAND6432:
7321 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7322 X86::AND32rr, X86::AND32rr,
7323 X86::AND32ri, X86::AND32ri,
7324 false);
7325 case X86::ATOMOR6432:
7326 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7327 X86::OR32rr, X86::OR32rr,
7328 X86::OR32ri, X86::OR32ri,
7329 false);
7330 case X86::ATOMXOR6432:
7331 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7332 X86::XOR32rr, X86::XOR32rr,
7333 X86::XOR32ri, X86::XOR32ri,
7334 false);
7335 case X86::ATOMNAND6432:
7336 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7337 X86::AND32rr, X86::AND32rr,
7338 X86::AND32ri, X86::AND32ri,
7339 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007340 case X86::ATOMADD6432:
7341 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7342 X86::ADD32rr, X86::ADC32rr,
7343 X86::ADD32ri, X86::ADC32ri,
7344 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007345 case X86::ATOMSUB6432:
7346 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7347 X86::SUB32rr, X86::SBB32rr,
7348 X86::SUB32ri, X86::SBB32ri,
7349 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007350 case X86::ATOMSWAP6432:
7351 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7352 X86::MOV32rr, X86::MOV32rr,
7353 X86::MOV32ri, X86::MOV32ri,
7354 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007355 }
7356}
7357
7358//===----------------------------------------------------------------------===//
7359// X86 Optimization Hooks
7360//===----------------------------------------------------------------------===//
7361
Dan Gohman8181bd12008-07-27 21:46:04 +00007362void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007363 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007364 APInt &KnownZero,
7365 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007366 const SelectionDAG &DAG,
7367 unsigned Depth) const {
7368 unsigned Opc = Op.getOpcode();
7369 assert((Opc >= ISD::BUILTIN_OP_END ||
7370 Opc == ISD::INTRINSIC_WO_CHAIN ||
7371 Opc == ISD::INTRINSIC_W_CHAIN ||
7372 Opc == ISD::INTRINSIC_VOID) &&
7373 "Should use MaskedValueIsZero if you don't know whether Op"
7374 " is a target node!");
7375
Dan Gohman1d79e432008-02-13 23:07:24 +00007376 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007377 switch (Opc) {
7378 default: break;
7379 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007380 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7381 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007382 break;
7383 }
7384}
7385
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007386/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007387/// node is a GlobalAddress + offset.
7388bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7389 GlobalValue* &GA, int64_t &Offset) const{
7390 if (N->getOpcode() == X86ISD::Wrapper) {
7391 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007392 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007393 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007394 return true;
7395 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007396 }
Evan Chengef7be082008-05-12 19:56:52 +00007397 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007398}
7399
Evan Chengef7be082008-05-12 19:56:52 +00007400static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7401 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007402 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007403 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007404 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007405 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007406 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007407 return false;
7408}
7409
Dan Gohman8181bd12008-07-27 21:46:04 +00007410static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007411 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007412 SDNode *&Base,
7413 SelectionDAG &DAG, MachineFrameInfo *MFI,
7414 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007415 Base = NULL;
7416 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007417 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007418 if (Idx.getOpcode() == ISD::UNDEF) {
7419 if (!Base)
7420 return false;
7421 continue;
7422 }
7423
Dan Gohman8181bd12008-07-27 21:46:04 +00007424 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007425 if (!Elt.getNode() ||
7426 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007427 return false;
7428 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007429 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007430 if (Base->getOpcode() == ISD::UNDEF)
7431 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007432 continue;
7433 }
7434 if (Elt.getOpcode() == ISD::UNDEF)
7435 continue;
7436
Gabor Greif1c80d112008-08-28 21:40:38 +00007437 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007438 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007439 return false;
7440 }
7441 return true;
7442}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007443
7444/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7445/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7446/// if the load addresses are consecutive, non-overlapping, and in the right
7447/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007448static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007449 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007450 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007451 MVT VT = N->getValueType(0);
7452 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007453 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007454 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007455 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007456 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7457 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007458 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007459
Dan Gohman11821702007-07-27 17:16:43 +00007460 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007461 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007462 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007463 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007464 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7465 LD->getSrcValueOffset(), LD->isVolatile(),
7466 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007467}
7468
Evan Chengb6290462008-05-12 23:04:07 +00007469/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007470static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007471 const X86Subtarget *Subtarget,
7472 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007473 unsigned NumOps = N->getNumOperands();
7474
Evan Chenge9b9c672008-05-09 21:53:03 +00007475 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007476 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007477 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007478
Duncan Sands92c43912008-06-06 12:08:01 +00007479 MVT VT = N->getValueType(0);
7480 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007481 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7482 // We are looking for load i64 and zero extend. We want to transform
7483 // it before legalizer has a chance to expand it. Also look for i64
7484 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007485 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007486 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007487 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007488 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007489 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007490
7491 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007492 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007493 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007494 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007495 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007496 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007497 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007498 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007499 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007500
7501 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007502 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007503
7504 // Load must not be an extload.
7505 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007506 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007507
Evan Cheng6617eed2008-09-24 23:26:36 +00007508 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7509 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7510 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7511 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7512 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007513}
7514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007515/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007516static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007517 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007518 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007519
7520 // If we have SSE[12] support, try to form min/max nodes.
7521 if (Subtarget->hasSSE2() &&
7522 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7523 if (Cond.getOpcode() == ISD::SETCC) {
7524 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007525 SDValue LHS = N->getOperand(1);
7526 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007527 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7528
7529 unsigned Opcode = 0;
7530 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7531 switch (CC) {
7532 default: break;
7533 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7534 case ISD::SETULE:
7535 case ISD::SETLE:
7536 if (!UnsafeFPMath) break;
7537 // FALL THROUGH.
7538 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7539 case ISD::SETLT:
7540 Opcode = X86ISD::FMIN;
7541 break;
7542
7543 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7544 case ISD::SETUGT:
7545 case ISD::SETGT:
7546 if (!UnsafeFPMath) break;
7547 // FALL THROUGH.
7548 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7549 case ISD::SETGE:
7550 Opcode = X86ISD::FMAX;
7551 break;
7552 }
7553 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7554 switch (CC) {
7555 default: break;
7556 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7557 case ISD::SETUGT:
7558 case ISD::SETGT:
7559 if (!UnsafeFPMath) break;
7560 // FALL THROUGH.
7561 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7562 case ISD::SETGE:
7563 Opcode = X86ISD::FMIN;
7564 break;
7565
7566 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7567 case ISD::SETULE:
7568 case ISD::SETLE:
7569 if (!UnsafeFPMath) break;
7570 // FALL THROUGH.
7571 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7572 case ISD::SETLT:
7573 Opcode = X86ISD::FMAX;
7574 break;
7575 }
7576 }
7577
7578 if (Opcode)
7579 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7580 }
7581
7582 }
7583
Dan Gohman8181bd12008-07-27 21:46:04 +00007584 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007585}
7586
Chris Lattnerce84ae42008-02-22 02:09:43 +00007587/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007588static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007589 const X86Subtarget *Subtarget) {
7590 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7591 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007592 // A preferable solution to the general problem is to figure out the right
7593 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007594 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007595 if (St->getValue().getValueType().isVector() &&
7596 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007597 isa<LoadSDNode>(St->getValue()) &&
7598 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7599 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007600 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007601 LoadSDNode *Ld = 0;
7602 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007603 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007604 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007605 // Must be a store of a load. We currently handle two cases: the load
7606 // is a direct child, and it's under an intervening TokenFactor. It is
7607 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007608 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007609 Ld = cast<LoadSDNode>(St->getChain());
7610 else if (St->getValue().hasOneUse() &&
7611 ChainVal->getOpcode() == ISD::TokenFactor) {
7612 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007613 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007614 TokenFactorIndex = i;
7615 Ld = cast<LoadSDNode>(St->getValue());
7616 } else
7617 Ops.push_back(ChainVal->getOperand(i));
7618 }
7619 }
7620 if (Ld) {
7621 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7622 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007623 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007624 Ld->getBasePtr(), Ld->getSrcValue(),
7625 Ld->getSrcValueOffset(), Ld->isVolatile(),
7626 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007627 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007628 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007629 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007630 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7631 Ops.size());
7632 }
7633 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7634 St->getSrcValue(), St->getSrcValueOffset(),
7635 St->isVolatile(), St->getAlignment());
7636 }
7637
7638 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007639 SDValue LoAddr = Ld->getBasePtr();
7640 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007641 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007642
Dan Gohman8181bd12008-07-27 21:46:04 +00007643 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007644 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7645 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007646 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007647 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7648 Ld->isVolatile(),
7649 MinAlign(Ld->getAlignment(), 4));
7650
Dan Gohman8181bd12008-07-27 21:46:04 +00007651 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007652 if (TokenFactorIndex != -1) {
7653 Ops.push_back(LoLd);
7654 Ops.push_back(HiLd);
7655 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7656 Ops.size());
7657 }
7658
7659 LoAddr = St->getBasePtr();
7660 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007661 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007662
Dan Gohman8181bd12008-07-27 21:46:04 +00007663 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007664 St->getSrcValue(), St->getSrcValueOffset(),
7665 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007666 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007667 St->getSrcValue(),
7668 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007669 St->isVolatile(),
7670 MinAlign(St->getAlignment(), 4));
7671 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007672 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007673 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007674 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007675}
7676
Chris Lattner470d5dc2008-01-25 06:14:17 +00007677/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7678/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007679static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007680 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7681 // F[X]OR(0.0, x) -> x
7682 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007683 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7684 if (C->getValueAPF().isPosZero())
7685 return N->getOperand(1);
7686 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7687 if (C->getValueAPF().isPosZero())
7688 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007689 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007690}
7691
7692/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007693static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007694 // FAND(0.0, x) -> 0.0
7695 // FAND(x, 0.0) -> 0.0
7696 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7697 if (C->getValueAPF().isPosZero())
7698 return N->getOperand(0);
7699 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7700 if (C->getValueAPF().isPosZero())
7701 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007702 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007703}
7704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007705
Dan Gohman8181bd12008-07-27 21:46:04 +00007706SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007707 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007708 SelectionDAG &DAG = DCI.DAG;
7709 switch (N->getOpcode()) {
7710 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007711 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7712 case ISD::BUILD_VECTOR:
7713 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007714 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007715 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007716 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007717 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7718 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007719 }
7720
Dan Gohman8181bd12008-07-27 21:46:04 +00007721 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007722}
7723
7724//===----------------------------------------------------------------------===//
7725// X86 Inline Assembly Support
7726//===----------------------------------------------------------------------===//
7727
7728/// getConstraintType - Given a constraint letter, return the type of
7729/// constraint it is for this target.
7730X86TargetLowering::ConstraintType
7731X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7732 if (Constraint.size() == 1) {
7733 switch (Constraint[0]) {
7734 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007735 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007736 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007737 case 'r':
7738 case 'R':
7739 case 'l':
7740 case 'q':
7741 case 'Q':
7742 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007743 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007744 case 'Y':
7745 return C_RegisterClass;
7746 default:
7747 break;
7748 }
7749 }
7750 return TargetLowering::getConstraintType(Constraint);
7751}
7752
Dale Johannesene99fc902008-01-29 02:21:21 +00007753/// LowerXConstraint - try to replace an X constraint, which matches anything,
7754/// with another that has more specific requirements based on the type of the
7755/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007756const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007757LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007758 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7759 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007760 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007761 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007762 return "Y";
7763 if (Subtarget->hasSSE1())
7764 return "x";
7765 }
7766
7767 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007768}
7769
Chris Lattnera531abc2007-08-25 00:47:38 +00007770/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7771/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007772void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007773 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007774 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007775 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007776 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007777 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007778
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007779 switch (Constraint) {
7780 default: break;
7781 case 'I':
7782 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007783 if (C->getZExtValue() <= 31) {
7784 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007785 break;
7786 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007787 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007788 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007789 case 'J':
7790 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7791 if (C->getZExtValue() <= 63) {
7792 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7793 break;
7794 }
7795 }
7796 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007797 case 'N':
7798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007799 if (C->getZExtValue() <= 255) {
7800 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007801 break;
7802 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007803 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007804 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007805 case 'i': {
7806 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007807 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007808 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007809 break;
7810 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007811
7812 // If we are in non-pic codegen mode, we allow the address of a global (with
7813 // an optional displacement) to be used with 'i'.
7814 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7815 int64_t Offset = 0;
7816
7817 // Match either (GA) or (GA+C)
7818 if (GA) {
7819 Offset = GA->getOffset();
7820 } else if (Op.getOpcode() == ISD::ADD) {
7821 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7822 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7823 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007824 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007825 } else {
7826 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7827 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7828 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007829 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007830 else
7831 C = 0, GA = 0;
7832 }
7833 }
7834
7835 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007836 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007837 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007838 else
7839 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7840 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007841 Result = Op;
7842 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007843 }
7844
7845 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007846 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007847 }
7848 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007849
Gabor Greif1c80d112008-08-28 21:40:38 +00007850 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007851 Ops.push_back(Result);
7852 return;
7853 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007854 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7855 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007856}
7857
7858std::vector<unsigned> X86TargetLowering::
7859getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007860 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007861 if (Constraint.size() == 1) {
7862 // FIXME: not handling fp-stack yet!
7863 switch (Constraint[0]) { // GCC X86 Constraint Letters
7864 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007865 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7866 case 'Q': // Q_REGS
7867 if (VT == MVT::i32)
7868 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7869 else if (VT == MVT::i16)
7870 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7871 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007872 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007873 else if (VT == MVT::i64)
7874 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7875 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007876 }
7877 }
7878
7879 return std::vector<unsigned>();
7880}
7881
7882std::pair<unsigned, const TargetRegisterClass*>
7883X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007884 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007885 // First, see if this is a constraint that directly corresponds to an LLVM
7886 // register class.
7887 if (Constraint.size() == 1) {
7888 // GCC Constraint Letters
7889 switch (Constraint[0]) {
7890 default: break;
7891 case 'r': // GENERAL_REGS
7892 case 'R': // LEGACY_REGS
7893 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007894 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007895 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007896 if (VT == MVT::i16)
7897 return std::make_pair(0U, X86::GR16RegisterClass);
7898 if (VT == MVT::i32 || !Subtarget->is64Bit())
7899 return std::make_pair(0U, X86::GR32RegisterClass);
7900 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007901 case 'f': // FP Stack registers.
7902 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7903 // value to the correct fpstack register class.
7904 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7905 return std::make_pair(0U, X86::RFP32RegisterClass);
7906 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7907 return std::make_pair(0U, X86::RFP64RegisterClass);
7908 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007909 case 'y': // MMX_REGS if MMX allowed.
7910 if (!Subtarget->hasMMX()) break;
7911 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007912 case 'Y': // SSE_REGS if SSE2 allowed
7913 if (!Subtarget->hasSSE2()) break;
7914 // FALL THROUGH.
7915 case 'x': // SSE_REGS if SSE1 allowed
7916 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007917
7918 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007919 default: break;
7920 // Scalar SSE types.
7921 case MVT::f32:
7922 case MVT::i32:
7923 return std::make_pair(0U, X86::FR32RegisterClass);
7924 case MVT::f64:
7925 case MVT::i64:
7926 return std::make_pair(0U, X86::FR64RegisterClass);
7927 // Vector types.
7928 case MVT::v16i8:
7929 case MVT::v8i16:
7930 case MVT::v4i32:
7931 case MVT::v2i64:
7932 case MVT::v4f32:
7933 case MVT::v2f64:
7934 return std::make_pair(0U, X86::VR128RegisterClass);
7935 }
7936 break;
7937 }
7938 }
7939
7940 // Use the default implementation in TargetLowering to convert the register
7941 // constraint into a member of a register class.
7942 std::pair<unsigned, const TargetRegisterClass*> Res;
7943 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7944
7945 // Not found as a standard register?
7946 if (Res.second == 0) {
7947 // GCC calls "st(0)" just plain "st".
7948 if (StringsEqualNoCase("{st}", Constraint)) {
7949 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007950 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007951 }
Dale Johannesen73920c02008-11-13 21:52:36 +00007952 // 'A' means EAX + EDX.
7953 if (Constraint == "A") {
7954 Res.first = X86::EAX;
7955 Res.second = X86::GRADRegisterClass;
7956 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007957 return Res;
7958 }
7959
7960 // Otherwise, check to see if this is a register class of the wrong value
7961 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7962 // turn into {ax},{dx}.
7963 if (Res.second->hasType(VT))
7964 return Res; // Correct type already, nothing to do.
7965
7966 // All of the single-register GCC register classes map their values onto
7967 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7968 // really want an 8-bit or 32-bit register, map to the appropriate register
7969 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007970 if (Res.second == X86::GR16RegisterClass) {
7971 if (VT == MVT::i8) {
7972 unsigned DestReg = 0;
7973 switch (Res.first) {
7974 default: break;
7975 case X86::AX: DestReg = X86::AL; break;
7976 case X86::DX: DestReg = X86::DL; break;
7977 case X86::CX: DestReg = X86::CL; break;
7978 case X86::BX: DestReg = X86::BL; break;
7979 }
7980 if (DestReg) {
7981 Res.first = DestReg;
7982 Res.second = Res.second = X86::GR8RegisterClass;
7983 }
7984 } else if (VT == MVT::i32) {
7985 unsigned DestReg = 0;
7986 switch (Res.first) {
7987 default: break;
7988 case X86::AX: DestReg = X86::EAX; break;
7989 case X86::DX: DestReg = X86::EDX; break;
7990 case X86::CX: DestReg = X86::ECX; break;
7991 case X86::BX: DestReg = X86::EBX; break;
7992 case X86::SI: DestReg = X86::ESI; break;
7993 case X86::DI: DestReg = X86::EDI; break;
7994 case X86::BP: DestReg = X86::EBP; break;
7995 case X86::SP: DestReg = X86::ESP; break;
7996 }
7997 if (DestReg) {
7998 Res.first = DestReg;
7999 Res.second = Res.second = X86::GR32RegisterClass;
8000 }
8001 } else if (VT == MVT::i64) {
8002 unsigned DestReg = 0;
8003 switch (Res.first) {
8004 default: break;
8005 case X86::AX: DestReg = X86::RAX; break;
8006 case X86::DX: DestReg = X86::RDX; break;
8007 case X86::CX: DestReg = X86::RCX; break;
8008 case X86::BX: DestReg = X86::RBX; break;
8009 case X86::SI: DestReg = X86::RSI; break;
8010 case X86::DI: DestReg = X86::RDI; break;
8011 case X86::BP: DestReg = X86::RBP; break;
8012 case X86::SP: DestReg = X86::RSP; break;
8013 }
8014 if (DestReg) {
8015 Res.first = DestReg;
8016 Res.second = Res.second = X86::GR64RegisterClass;
8017 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008018 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00008019 } else if (Res.second == X86::FR32RegisterClass ||
8020 Res.second == X86::FR64RegisterClass ||
8021 Res.second == X86::VR128RegisterClass) {
8022 // Handle references to XMM physical registers that got mapped into the
8023 // wrong class. This can happen with constraints like {xmm0} where the
8024 // target independent register mapper will just pick the first match it can
8025 // find, ignoring the required type.
8026 if (VT == MVT::f32)
8027 Res.second = X86::FR32RegisterClass;
8028 else if (VT == MVT::f64)
8029 Res.second = X86::FR64RegisterClass;
8030 else if (X86::VR128RegisterClass->hasType(VT))
8031 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008032 }
8033
8034 return Res;
8035}
Mon P Wang1448aad2008-10-30 08:01:45 +00008036
8037//===----------------------------------------------------------------------===//
8038// X86 Widen vector type
8039//===----------------------------------------------------------------------===//
8040
8041/// getWidenVectorType: given a vector type, returns the type to widen
8042/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8043/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00008044/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00008045/// scalarizing vs using the wider vector type.
8046
Dan Gohman0fe66c92009-01-15 17:34:08 +00008047MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +00008048 assert(VT.isVector());
8049 if (isTypeLegal(VT))
8050 return VT;
8051
8052 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8053 // type based on element type. This would speed up our search (though
8054 // it may not be worth it since the size of the list is relatively
8055 // small).
8056 MVT EltVT = VT.getVectorElementType();
8057 unsigned NElts = VT.getVectorNumElements();
8058
8059 // On X86, it make sense to widen any vector wider than 1
8060 if (NElts <= 1)
8061 return MVT::Other;
8062
8063 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
8064 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8065 MVT SVT = (MVT::SimpleValueType)nVT;
8066
8067 if (isTypeLegal(SVT) &&
8068 SVT.getVectorElementType() == EltVT &&
8069 SVT.getVectorNumElements() > NElts)
8070 return SVT;
8071 }
8072 return MVT::Other;
8073}