blob: 496112dc0942a307bde80f5af61b6e3804844295 [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
Dale Johannesenbc187662008-08-28 02:44:49 +0000309 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
310 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
311 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
312 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000313
Dale Johannesen9011d872008-09-29 22:25:26 +0000314 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
315 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000318
Dale Johannesenf160d802008-10-02 18:53:47 +0000319 if (!Subtarget->is64Bit()) {
320 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
324 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
325 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
326 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
327 }
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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 }
654
655 if (Subtarget->hasSSE1()) {
656 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
657
658 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
659 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
660 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
661 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
663 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
665 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
666 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
667 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
668 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000669 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000670 }
671
672 if (Subtarget->hasSSE2()) {
673 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
676 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
677 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
678
679 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
680 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
681 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
682 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
683 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
684 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
685 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
686 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
687 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
688 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
689 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
690 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
691 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
692 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
693 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694
Nate Begeman03605a02008-07-17 16:51:19 +0000695 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
698 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000699
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
701 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
703 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
705
706 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000707 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
708 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000709 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000710 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000711 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000712 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
713 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
714 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 }
716 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
717 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
718 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
719 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000722 if (Subtarget->is64Bit()) {
723 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000725 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726
727 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
728 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000729 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
731 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
732 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
733 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
734 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
735 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
736 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
737 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
738 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 }
740
Chris Lattner3bc08502008-01-17 19:59:44 +0000741 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000742
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 // Custom lower v2i64 and v2f64 selects.
744 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
745 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
746 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
747 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000748
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000750
751 if (Subtarget->hasSSE41()) {
752 // FIXME: Do we need to handle scalar-to-vector here?
753 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000754 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000755
756 // i8 and i16 vectors are custom , because the source register and source
757 // source memory operand types are not the same width. f32 vectors are
758 // custom since the immediate controlling the insert encodes additional
759 // information.
760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
761 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
762 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
763 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
764
765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
767 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000768 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000769
770 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000771 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
772 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000773 }
774 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775
Nate Begeman03605a02008-07-17 16:51:19 +0000776 if (Subtarget->hasSSE42()) {
777 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
778 }
779
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000780 // We want to custom lower some of our intrinsics.
781 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
782
Bill Wendling7e04be62008-12-09 22:08:41 +0000783 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling4c134df2008-11-24 19:21:46 +0000784 setOperationAction(ISD::SADDO, MVT::i32, Custom);
785 setOperationAction(ISD::SADDO, MVT::i64, Custom);
786 setOperationAction(ISD::UADDO, MVT::i32, Custom);
787 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling7e04be62008-12-09 22:08:41 +0000788 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
789 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
790 setOperationAction(ISD::USUBO, MVT::i32, Custom);
791 setOperationAction(ISD::USUBO, MVT::i64, Custom);
792 setOperationAction(ISD::SMULO, MVT::i32, Custom);
793 setOperationAction(ISD::SMULO, MVT::i64, Custom);
794 setOperationAction(ISD::UMULO, MVT::i32, Custom);
795 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000796
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000797 // We have target-specific dag combine patterns for the following nodes:
798 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000799 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000801 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000802
803 computeRegisterProperties();
804
805 // FIXME: These should be based on subtarget info. Plus, the values should
806 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000807 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
808 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
809 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000811 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812}
813
Scott Michel502151f2008-03-10 15:42:14 +0000814
Dan Gohman8181bd12008-07-27 21:46:04 +0000815MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000816 return MVT::i8;
817}
818
819
Evan Cheng5a67b812008-01-23 23:17:41 +0000820/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
821/// the desired ByVal argument alignment.
822static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
823 if (MaxAlign == 16)
824 return;
825 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
826 if (VTy->getBitWidth() == 128)
827 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000828 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
829 unsigned EltAlign = 0;
830 getMaxByValAlign(ATy->getElementType(), EltAlign);
831 if (EltAlign > MaxAlign)
832 MaxAlign = EltAlign;
833 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
834 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
835 unsigned EltAlign = 0;
836 getMaxByValAlign(STy->getElementType(i), EltAlign);
837 if (EltAlign > MaxAlign)
838 MaxAlign = EltAlign;
839 if (MaxAlign == 16)
840 break;
841 }
842 }
843 return;
844}
845
846/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
847/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000848/// that contain SSE vectors are placed at 16-byte boundaries while the rest
849/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000850unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000851 if (Subtarget->is64Bit()) {
852 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000853 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000854 if (TyAlign > 8)
855 return TyAlign;
856 return 8;
857 }
858
Evan Cheng5a67b812008-01-23 23:17:41 +0000859 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000860 if (Subtarget->hasSSE1())
861 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000862 return Align;
863}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864
Evan Cheng8c590372008-05-15 08:39:06 +0000865/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000866/// and store operations as a result of memset, memcpy, and memmove
867/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000868/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000869MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000870X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
871 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000872 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
873 // linux. This is because the stack realignment code can't handle certain
874 // cases like PR2962. This should be removed when PR2962 is fixed.
875 if (Subtarget->getStackAlignment() >= 16) {
876 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
877 return MVT::v4i32;
878 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
879 return MVT::v4f32;
880 }
Evan Cheng8c590372008-05-15 08:39:06 +0000881 if (Subtarget->is64Bit() && Size >= 8)
882 return MVT::i64;
883 return MVT::i32;
884}
885
886
Evan Cheng6fb06762007-11-09 01:32:10 +0000887/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
888/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000889SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000890 SelectionDAG &DAG) const {
891 if (usesGlobalOffsetTable())
892 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
893 if (!Subtarget->isPICStyleRIPRel())
894 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
895 return Table;
896}
897
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898//===----------------------------------------------------------------------===//
899// Return Value Calling Convention Implementation
900//===----------------------------------------------------------------------===//
901
902#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000903
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000905SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
907
908 SmallVector<CCValAssign, 16> RVLocs;
909 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
910 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
911 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000912 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000913
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 // If this is the first return lowered for this function, add the regs to the
915 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000916 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917 for (unsigned i = 0; i != RVLocs.size(); ++i)
918 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000919 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000920 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000921 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000923 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000924 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000925 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000926 SDValue TailCall = Chain;
927 SDValue TargetAddress = TailCall.getOperand(1);
928 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000929 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000930 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000931 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000932 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000933 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
934 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000935 assert(StackAdjustment.getOpcode() == ISD::Constant &&
936 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000937
Dan Gohman8181bd12008-07-27 21:46:04 +0000938 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000939 Operands.push_back(Chain.getOperand(0));
940 Operands.push_back(TargetAddress);
941 Operands.push_back(StackAdjustment);
942 // Copy registers used by the call. Last operand is a flag so it is not
943 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000944 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000945 Operands.push_back(Chain.getOperand(i));
946 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000947 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
948 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000949 }
950
951 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000952 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000953
Dan Gohman8181bd12008-07-27 21:46:04 +0000954 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000955 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
956 // Operand #1 = Bytes To Pop
957 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
958
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000960 for (unsigned i = 0; i != RVLocs.size(); ++i) {
961 CCValAssign &VA = RVLocs[i];
962 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000963 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964
Chris Lattnerb56cc342008-03-11 03:23:40 +0000965 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
966 // the RET instruction and handled by the FP Stackifier.
967 if (RVLocs[i].getLocReg() == X86::ST0 ||
968 RVLocs[i].getLocReg() == X86::ST1) {
969 // If this is a copy from an xmm register to ST(0), use an FPExtend to
970 // change the value to the FP stack register class.
971 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
972 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
973 RetOps.push_back(ValToCopy);
974 // Don't emit a copytoreg.
975 continue;
976 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000977
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000978 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 Flag = Chain.getValue(1);
980 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000981
982 // The x86-64 ABI for returning structs by value requires that we copy
983 // the sret argument into %rax for the return. We saved the argument into
984 // a virtual register in the entry block, so now we copy the value out
985 // and into %rax.
986 if (Subtarget->is64Bit() &&
987 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
988 MachineFunction &MF = DAG.getMachineFunction();
989 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
990 unsigned Reg = FuncInfo->getSRetReturnReg();
991 if (!Reg) {
992 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
993 FuncInfo->setSRetReturnReg(Reg);
994 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000995 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000996
997 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
998 Flag = Chain.getValue(1);
999 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000
Chris Lattnerb56cc342008-03-11 03:23:40 +00001001 RetOps[0] = Chain; // Update chain.
1002
1003 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001004 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001005 RetOps.push_back(Flag);
1006
1007 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008}
1009
1010
1011/// LowerCallResult - Lower the result values of an ISD::CALL into the
1012/// appropriate copies out of appropriate physical registers. This assumes that
1013/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1014/// being lowered. The returns a SDNode with the same number of values as the
1015/// ISD::CALL.
1016SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +00001017LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001018 unsigned CallingConv, SelectionDAG &DAG) {
1019
1020 // Assign locations to each value returned by this call.
1021 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001022 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001023 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1024 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1025
Dan Gohman8181bd12008-07-27 21:46:04 +00001026 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001027
1028 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001029 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001030 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001031
1032 // If this is a call to a function that returns an fp value on the floating
1033 // point stack, but where we prefer to use the value in xmm registers, copy
1034 // 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 +00001035 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1036 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001037 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1038 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001041 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1042 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001043 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001044 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001045
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001046 if (CopyVT != RVLocs[i].getValVT()) {
1047 // Round the F80 the right size, which also moves to the appropriate xmm
1048 // register.
1049 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1050 // This truncation won't change the value.
1051 DAG.getIntPtrConstant(1));
1052 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001053
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001054 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055 }
Duncan Sands698842f2008-07-02 17:40:58 +00001056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001057 // Merge everything together with a MERGE_VALUES node.
1058 ResultVals.push_back(Chain);
Duncan Sands42d7bb82008-12-01 11:41:29 +00001059 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(), &ResultVals[0],
1060 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001061}
1062
1063
1064//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001065// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001066//===----------------------------------------------------------------------===//
1067// StdCall calling convention seems to be standard for many Windows' API
1068// routines and around. It differs from C calling convention just a little:
1069// callee should clean up the stack, not caller. Symbols should be also
1070// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001071// For info on fast calling convention see Fast Calling Convention (tail call)
1072// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073
1074/// AddLiveIn - This helper function adds the specified physical register to the
1075/// MachineFunction as a live in value. It also creates a corresponding virtual
1076/// register for it.
1077static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1078 const TargetRegisterClass *RC) {
1079 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001080 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1081 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001082 return VReg;
1083}
1084
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001085/// CallIsStructReturn - Determines whether a CALL node uses struct return
1086/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001087static bool CallIsStructReturn(CallSDNode *TheCall) {
1088 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001089 if (!NumOps)
1090 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001091
Dan Gohman705e3f72008-09-13 01:54:27 +00001092 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001093}
1094
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001095/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1096/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001097static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001098 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099 if (!NumArgs)
1100 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001101
1102 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001103}
1104
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001105/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1106/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001107/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001108bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001109 if (IsVarArg)
1110 return false;
1111
Dan Gohman705e3f72008-09-13 01:54:27 +00001112 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001113 default:
1114 return false;
1115 case CallingConv::X86_StdCall:
1116 return !Subtarget->is64Bit();
1117 case CallingConv::X86_FastCall:
1118 return !Subtarget->is64Bit();
1119 case CallingConv::Fast:
1120 return PerformTailCallOpt;
1121 }
1122}
1123
Dan Gohman705e3f72008-09-13 01:54:27 +00001124/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1125/// given CallingConvention value.
1126CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001127 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001128 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001129 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001130 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1131 return CC_X86_64_TailCall;
1132 else
1133 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001134 }
1135
Gordon Henriksen18ace102008-01-05 16:56:59 +00001136 if (CC == CallingConv::X86_FastCall)
1137 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001138 else if (CC == CallingConv::Fast)
1139 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001140 else
1141 return CC_X86_32_C;
1142}
1143
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001144/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1145/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001146NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001147X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001148 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001149 if (CC == CallingConv::X86_FastCall)
1150 return FastCall;
1151 else if (CC == CallingConv::X86_StdCall)
1152 return StdCall;
1153 return None;
1154}
1155
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001156
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001157/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1158/// in a register before calling.
1159bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1160 return !IsTailCall && !Is64Bit &&
1161 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1162 Subtarget->isPICStyleGOT();
1163}
1164
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001165/// CallRequiresFnAddressInReg - Check whether the call requires the function
1166/// address to be loaded in a register.
1167bool
1168X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1169 return !Is64Bit && IsTailCall &&
1170 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1171 Subtarget->isPICStyleGOT();
1172}
1173
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001174/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1175/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001176/// the specific parameter attribute. The copy will be passed as a byval
1177/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001178static SDValue
1179CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001180 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001181 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001182 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001183 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001184}
1185
Dan Gohman8181bd12008-07-27 21:46:04 +00001186SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001187 const CCValAssign &VA,
1188 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001189 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001190 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001191 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001192 ISD::ArgFlagsTy Flags =
1193 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001194 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001195 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001196
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001197 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1198 // changed with more analysis.
1199 // In case of tail call optimization mark all arguments mutable. Since they
1200 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001201 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001202 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001203 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001204 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001205 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001206 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001207 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001208}
1209
Dan Gohman8181bd12008-07-27 21:46:04 +00001210SDValue
1211X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001212 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001213 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1214
1215 const Function* Fn = MF.getFunction();
1216 if (Fn->hasExternalLinkage() &&
1217 Subtarget->isTargetCygMing() &&
1218 Fn->getName() == "main")
1219 FuncInfo->setForceFramePointer(true);
1220
1221 // Decorate the function name.
1222 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001224 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001225 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001226 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001227 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001228 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001229 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001230
1231 assert(!(isVarArg && CC == CallingConv::Fast) &&
1232 "Var args not supported with calling convention fastcc");
1233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 // Assign locations to all of the incoming arguments.
1235 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001236 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001237 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001238
Dan Gohman8181bd12008-07-27 21:46:04 +00001239 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 unsigned LastVal = ~0U;
1241 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1242 CCValAssign &VA = ArgLocs[i];
1243 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1244 // places.
1245 assert(VA.getValNo() != LastVal &&
1246 "Don't support value assigned to multiple locs yet");
1247 LastVal = VA.getValNo();
1248
1249 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001250 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 TargetRegisterClass *RC;
1252 if (RegVT == MVT::i32)
1253 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001254 else if (Is64Bit && RegVT == MVT::i64)
1255 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001256 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001257 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001258 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001259 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001260 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001261 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001262 else if (RegVT.isVector()) {
1263 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001264 if (!Is64Bit)
1265 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1266 else {
1267 // Darwin calling convention passes MMX values in either GPRs or
1268 // XMMs in x86-64. Other targets pass them in memory.
1269 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1270 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1271 RegVT = MVT::v2i64;
1272 } else {
1273 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1274 RegVT = MVT::i64;
1275 }
1276 }
1277 } else {
1278 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001279 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001280
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001282 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283
1284 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1285 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1286 // right size.
1287 if (VA.getLocInfo() == CCValAssign::SExt)
1288 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1289 DAG.getValueType(VA.getValVT()));
1290 else if (VA.getLocInfo() == CCValAssign::ZExt)
1291 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1292 DAG.getValueType(VA.getValVT()));
1293
1294 if (VA.getLocInfo() != CCValAssign::Full)
1295 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1296
Gordon Henriksen18ace102008-01-05 16:56:59 +00001297 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001298 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001299 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001300 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1301 else if (RC == X86::VR128RegisterClass) {
1302 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1303 DAG.getConstant(0, MVT::i64));
1304 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1305 }
1306 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308 ArgValues.push_back(ArgValue);
1309 } else {
1310 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001311 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 }
1313 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001314
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001315 // The x86-64 ABI for returning structs by value requires that we copy
1316 // the sret argument into %rax for the return. Save the argument into
1317 // a virtual register so that we can access it from the return points.
1318 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1319 MachineFunction &MF = DAG.getMachineFunction();
1320 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1321 unsigned Reg = FuncInfo->getSRetReturnReg();
1322 if (!Reg) {
1323 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1324 FuncInfo->setSRetReturnReg(Reg);
1325 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001326 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001327 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1328 }
1329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001330 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001331 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001332 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001333 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334
1335 // If the function takes variable number of arguments, make a frame index for
1336 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001337 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001338 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1339 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1340 }
1341 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001342 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1343
1344 // FIXME: We should really autogenerate these arrays
1345 static const unsigned GPR64ArgRegsWin64[] = {
1346 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001348 static const unsigned XMMArgRegsWin64[] = {
1349 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1350 };
1351 static const unsigned GPR64ArgRegs64Bit[] = {
1352 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1353 };
1354 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001355 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1356 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1357 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001358 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1359
1360 if (IsWin64) {
1361 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1362 GPR64ArgRegs = GPR64ArgRegsWin64;
1363 XMMArgRegs = XMMArgRegsWin64;
1364 } else {
1365 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1366 GPR64ArgRegs = GPR64ArgRegs64Bit;
1367 XMMArgRegs = XMMArgRegs64Bit;
1368 }
1369 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1370 TotalNumIntRegs);
1371 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1372 TotalNumXMMRegs);
1373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 // For X86-64, if there are vararg parameters that are passed via
1375 // registers, then we must store them to their spots on the stack so they
1376 // may be loaded by deferencing the result of va_next.
1377 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001378 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1379 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1380 TotalNumXMMRegs * 16, 16);
1381
Gordon Henriksen18ace102008-01-05 16:56:59 +00001382 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001383 SmallVector<SDValue, 8> MemOps;
1384 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1385 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001386 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001387 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001388 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1389 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001390 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1391 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001392 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001393 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001394 MemOps.push_back(Store);
1395 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001396 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001397 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001398
Gordon Henriksen18ace102008-01-05 16:56:59 +00001399 // Now store the XMM (fp + vector) parameter registers.
1400 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001401 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001402 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001403 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1404 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001405 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1406 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001407 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001408 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001409 MemOps.push_back(Store);
1410 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001411 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001412 }
1413 if (!MemOps.empty())
1414 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1415 &MemOps[0], MemOps.size());
1416 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001417 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001418
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001419 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001420
Gordon Henriksen18ace102008-01-05 16:56:59 +00001421 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001422 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 BytesCallerReserves = 0;
1425 } else {
1426 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001427 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001428 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001429 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430 BytesCallerReserves = StackSize;
1431 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001432
Gordon Henriksen18ace102008-01-05 16:56:59 +00001433 if (!Is64Bit) {
1434 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1435 if (CC == CallingConv::X86_FastCall)
1436 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1437 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438
Anton Korobeynikove844e472007-08-15 17:12:32 +00001439 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001440
1441 // Return the new list of results.
Duncan Sands42d7bb82008-12-01 11:41:29 +00001442 return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
1443 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001444}
1445
Dan Gohman8181bd12008-07-27 21:46:04 +00001446SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001447X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001448 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001449 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001450 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001451 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001452 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001453 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001454 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001455 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001456 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001457 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001458 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001459 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001460}
1461
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001462/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1463/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001464SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001465X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001466 SDValue &OutRetAddr,
1467 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001468 bool IsTailCall,
1469 bool Is64Bit,
1470 int FPDiff) {
1471 if (!IsTailCall || FPDiff==0) return Chain;
1472
1473 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001474 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001475 OutRetAddr = getReturnAddressFrameIndex(DAG);
1476 // Load the "old" Return address.
1477 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001478 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001479}
1480
1481/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1482/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001483static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001484EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001485 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001486 bool Is64Bit, int FPDiff) {
1487 // Store the return address to the appropriate stack slot.
1488 if (!FPDiff) return Chain;
1489 // Calculate the new stack slot for the return address.
1490 int SlotSize = Is64Bit ? 8 : 4;
1491 int NewReturnAddrFI =
1492 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001493 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001494 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001495 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001496 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001497 return Chain;
1498}
1499
Dan Gohman8181bd12008-07-27 21:46:04 +00001500SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001501 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001502 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1503 SDValue Chain = TheCall->getChain();
1504 unsigned CC = TheCall->getCallingConv();
1505 bool isVarArg = TheCall->isVarArg();
1506 bool IsTailCall = TheCall->isTailCall() &&
1507 CC == CallingConv::Fast && PerformTailCallOpt;
1508 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001509 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001510 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001511
1512 assert(!(isVarArg && CC == CallingConv::Fast) &&
1513 "Var args not supported with calling convention fastcc");
1514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 // Analyze operands of the call, assigning locations to each operand.
1516 SmallVector<CCValAssign, 16> ArgLocs;
1517 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001518 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519
1520 // Get a count of how many bytes are to be pushed on the stack.
1521 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001522 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001523 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524
Gordon Henriksen18ace102008-01-05 16:56:59 +00001525 int FPDiff = 0;
1526 if (IsTailCall) {
1527 // Lower arguments at fp - stackoffset + fpdiff.
1528 unsigned NumBytesCallerPushed =
1529 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1530 FPDiff = NumBytesCallerPushed - NumBytes;
1531
1532 // Set the delta of movement of the returnaddr stackslot.
1533 // But only set if delta is greater than previous delta.
1534 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1535 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1536 }
1537
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001538 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539
Dan Gohman8181bd12008-07-27 21:46:04 +00001540 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001541 // Load return adress for tail calls.
1542 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1543 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001544
Dan Gohman8181bd12008-07-27 21:46:04 +00001545 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1546 SmallVector<SDValue, 8> MemOpChains;
1547 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001549 // Walk the register/memloc assignments, inserting copies/loads. In the case
1550 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001553 SDValue Arg = TheCall->getArg(i);
1554 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1555 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001556
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001557 // Promote the value if needed.
1558 switch (VA.getLocInfo()) {
1559 default: assert(0 && "Unknown loc info!");
1560 case CCValAssign::Full: break;
1561 case CCValAssign::SExt:
1562 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1563 break;
1564 case CCValAssign::ZExt:
1565 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1566 break;
1567 case CCValAssign::AExt:
1568 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1569 break;
1570 }
1571
1572 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001573 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001574 MVT RegVT = VA.getLocVT();
1575 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001576 switch (VA.getLocReg()) {
1577 default:
1578 break;
1579 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1580 case X86::R8: {
1581 // Special case: passing MMX values in GPR registers.
1582 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1583 break;
1584 }
1585 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1586 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1587 // Special case: passing MMX values in XMM registers.
1588 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1589 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1590 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1591 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1592 getMOVLMask(2, DAG));
1593 break;
1594 }
1595 }
1596 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001597 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1598 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001599 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001600 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001601 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001602 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1603
Dan Gohman705e3f72008-09-13 01:54:27 +00001604 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1605 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001606 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 }
1608 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609
1610 if (!MemOpChains.empty())
1611 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1612 &MemOpChains[0], MemOpChains.size());
1613
1614 // Build a sequence of copy-to-reg nodes chained together with token chain
1615 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001616 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001617 // Tail call byval lowering might overwrite argument registers so in case of
1618 // tail call optimization the copies to registers are lowered later.
1619 if (!IsTailCall)
1620 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1621 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1622 InFlag);
1623 InFlag = Chain.getValue(1);
1624 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001625
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001626 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001627 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001628 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1629 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1630 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1631 InFlag);
1632 InFlag = Chain.getValue(1);
1633 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001634 // If we are tail calling and generating PIC/GOT style code load the address
1635 // of the callee into ecx. The value in ecx is used as target of the tail
1636 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1637 // calls on PIC/GOT architectures. Normally we would just put the address of
1638 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1639 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001640 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001641 // Note: The actual moving to ecx is done further down.
1642 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001643 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001644 !G->getGlobal()->hasProtectedVisibility())
1645 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001646 else if (isa<ExternalSymbolSDNode>(Callee))
1647 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001648 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001649
Gordon Henriksen18ace102008-01-05 16:56:59 +00001650 if (Is64Bit && isVarArg) {
1651 // From AMD64 ABI document:
1652 // For calls that may call functions that use varargs or stdargs
1653 // (prototype-less calls or calls to functions containing ellipsis (...) in
1654 // the declaration) %al is used as hidden argument to specify the number
1655 // of SSE registers used. The contents of %al do not need to match exactly
1656 // the number of registers, but must be an ubound on the number of SSE
1657 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001658
1659 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 // Count the number of XMM registers allocated.
1661 static const unsigned XMMArgRegs[] = {
1662 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1663 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1664 };
1665 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1666
1667 Chain = DAG.getCopyToReg(Chain, X86::AL,
1668 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1669 InFlag = Chain.getValue(1);
1670 }
1671
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001672
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001673 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001675 SmallVector<SDValue, 8> MemOpChains2;
1676 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001677 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001678 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001679 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001680 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1681 CCValAssign &VA = ArgLocs[i];
1682 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001683 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001684 SDValue Arg = TheCall->getArg(i);
1685 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001686 // Create frame index.
1687 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001688 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001689 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001690 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001691
Duncan Sandsc93fae32008-03-21 09:14:45 +00001692 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001693 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001694 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001695 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001696 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1697 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1698
1699 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001700 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001701 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001702 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001703 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001704 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001705 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001706 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 }
1708 }
1709
1710 if (!MemOpChains2.empty())
1711 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001712 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001713
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001714 // Copy arguments to their registers.
1715 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1716 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1717 InFlag);
1718 InFlag = Chain.getValue(1);
1719 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001720 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001721
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001723 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1724 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 }
1726
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 // If the callee is a GlobalAddress node (quite common, every direct call is)
1728 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1729 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1730 // We should use extra load for direct calls to dllimported functions in
1731 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001732 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1733 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001734 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1735 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001736 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1737 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001738 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001739 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740
1741 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001742 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001743 Callee,InFlag);
1744 Callee = DAG.getRegister(Opc, getPointerTy());
1745 // Add register as live out.
1746 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001747 }
1748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 // Returns a chain & a flag for retval copy to use.
1750 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001751 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752
1753 if (IsTailCall) {
1754 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001755 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1756 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001757 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001758 Ops.push_back(InFlag);
1759 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1760 InFlag = Chain.getValue(1);
1761
1762 // Returns a chain & a flag for retval copy to use.
1763 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1764 Ops.clear();
1765 }
1766
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 Ops.push_back(Chain);
1768 Ops.push_back(Callee);
1769
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770 if (IsTailCall)
1771 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772
Gordon Henriksen18ace102008-01-05 16:56:59 +00001773 // Add argument registers to the end of the list so that they are known live
1774 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001775 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1776 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1777 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001778
Evan Cheng8ba45e62008-03-18 23:36:35 +00001779 // Add an implicit use GOT pointer in EBX.
1780 if (!IsTailCall && !Is64Bit &&
1781 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1782 Subtarget->isPICStyleGOT())
1783 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1784
1785 // Add an implicit use of AL for x86 vararg functions.
1786 if (Is64Bit && isVarArg)
1787 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1788
Gabor Greif1c80d112008-08-28 21:40:38 +00001789 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001790 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001791
Gordon Henriksen18ace102008-01-05 16:56:59 +00001792 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001793 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001794 "Flag must be set. Depend on flag being set in LowerRET");
1795 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001796 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001797
Gabor Greif1c80d112008-08-28 21:40:38 +00001798 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001799 }
1800
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001801 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802 InFlag = Chain.getValue(1);
1803
1804 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001805 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001806 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001807 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001808 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001809 // If this is is a call to a struct-return function, the callee
1810 // pops the hidden struct pointer, so we have to push it back.
1811 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001812 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001813 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001814 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001815
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001816 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001817 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001818 DAG.getIntPtrConstant(NumBytes, true),
1819 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1820 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001821 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001822 InFlag = Chain.getValue(1);
1823
1824 // Handle result values, copying them out of physregs into vregs that we
1825 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001826 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001827 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828}
1829
1830
1831//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001832// Fast Calling Convention (tail call) implementation
1833//===----------------------------------------------------------------------===//
1834
1835// Like std call, callee cleans arguments, convention except that ECX is
1836// reserved for storing the tail called function address. Only 2 registers are
1837// free for argument passing (inreg). Tail call optimization is performed
1838// provided:
1839// * tailcallopt is enabled
1840// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001841// On X86_64 architecture with GOT-style position independent code only local
1842// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001843// To keep the stack aligned according to platform abi the function
1844// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1845// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846// If a tail called function callee has more arguments than the caller the
1847// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001848// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849// original REtADDR, but before the saved framepointer or the spilled registers
1850// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1851// stack layout:
1852// arg1
1853// arg2
1854// RETADDR
1855// [ new RETADDR
1856// move area ]
1857// (possible EBP)
1858// ESI
1859// EDI
1860// local1 ..
1861
1862/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1863/// for a 16 byte align requirement.
1864unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1865 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001866 MachineFunction &MF = DAG.getMachineFunction();
1867 const TargetMachine &TM = MF.getTarget();
1868 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1869 unsigned StackAlignment = TFI.getStackAlignment();
1870 uint64_t AlignMask = StackAlignment - 1;
1871 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001872 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001873 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1874 // Number smaller than 12 so just add the difference.
1875 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1876 } else {
1877 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1878 Offset = ((~AlignMask) & Offset) + StackAlignment +
1879 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001880 }
Evan Chengded8f902008-09-07 09:07:23 +00001881 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001882}
1883
1884/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001885/// following the call is a return. A function is eligible if caller/callee
1886/// calling conventions match, currently only fastcc supports tail calls, and
1887/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001888bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001889 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001890 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001891 if (!PerformTailCallOpt)
1892 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001893
Dan Gohman705e3f72008-09-13 01:54:27 +00001894 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001895 MachineFunction &MF = DAG.getMachineFunction();
1896 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001897 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001898 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001899 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001900 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001901 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001902 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001903 return true;
1904
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001905 // Can only do local tail calls (in same module, hidden or protected) on
1906 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001907 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1908 return G->getGlobal()->hasHiddenVisibility()
1909 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001910 }
1911 }
Evan Chenge7a87392007-11-02 01:26:22 +00001912
1913 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001914}
1915
Dan Gohmanca4857a2008-09-03 23:12:08 +00001916FastISel *
1917X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001918 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001919 DenseMap<const Value *, unsigned> &vm,
1920 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001921 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001922 DenseMap<const AllocaInst *, int> &am
1923#ifndef NDEBUG
1924 , SmallSet<Instruction*, 8> &cil
1925#endif
1926 ) {
1927 return X86::createFastISel(mf, mmo, vm, bm, am
1928#ifndef NDEBUG
1929 , cil
1930#endif
1931 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001932}
1933
1934
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001935//===----------------------------------------------------------------------===//
1936// Other Lowering Hooks
1937//===----------------------------------------------------------------------===//
1938
1939
Dan Gohman8181bd12008-07-27 21:46:04 +00001940SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001941 MachineFunction &MF = DAG.getMachineFunction();
1942 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1943 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001944 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001945
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 if (ReturnAddrIndex == 0) {
1947 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001948 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001949 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001950 }
1951
1952 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1953}
1954
1955
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001956/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1957/// specific condition code. It returns a false if it cannot do a direct
1958/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1959/// needed.
1960static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001961 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 SelectionDAG &DAG) {
1963 X86CC = X86::COND_INVALID;
1964 if (!isFP) {
1965 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1966 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1967 // X > -1 -> X == 0, jump !sign.
1968 RHS = DAG.getConstant(0, RHS.getValueType());
1969 X86CC = X86::COND_NS;
1970 return true;
1971 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1972 // X < 0 -> X == 0, jump on sign.
1973 X86CC = X86::COND_S;
1974 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001975 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001976 // X < 1 -> X <= 0
1977 RHS = DAG.getConstant(0, RHS.getValueType());
1978 X86CC = X86::COND_LE;
1979 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001980 }
1981 }
1982
1983 switch (SetCCOpcode) {
1984 default: break;
1985 case ISD::SETEQ: X86CC = X86::COND_E; break;
1986 case ISD::SETGT: X86CC = X86::COND_G; break;
1987 case ISD::SETGE: X86CC = X86::COND_GE; break;
1988 case ISD::SETLT: X86CC = X86::COND_L; break;
1989 case ISD::SETLE: X86CC = X86::COND_LE; break;
1990 case ISD::SETNE: X86CC = X86::COND_NE; break;
1991 case ISD::SETULT: X86CC = X86::COND_B; break;
1992 case ISD::SETUGT: X86CC = X86::COND_A; break;
1993 case ISD::SETULE: X86CC = X86::COND_BE; break;
1994 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1995 }
1996 } else {
Duncan Sandsc2a04622008-10-24 13:03:10 +00001997 // First determine if it is required or is profitable to flip the operands.
1998
1999 // If LHS is a foldable load, but RHS is not, flip the condition.
2000 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2001 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2002 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2003 std::swap(LHS, RHS);
2004 }
2005
Evan Chengb488ca32008-08-29 23:22:12 +00002006 switch (SetCCOpcode) {
2007 default: break;
2008 case ISD::SETOLT:
2009 case ISD::SETOLE:
2010 case ISD::SETUGT:
2011 case ISD::SETUGE:
Duncan Sandsc2a04622008-10-24 13:03:10 +00002012 std::swap(LHS, RHS);
Evan Chengb488ca32008-08-29 23:22:12 +00002013 break;
2014 }
2015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 // On a floating point condition, the flags are set as follows:
2017 // ZF PF CF op
2018 // 0 | 0 | 0 | X > Y
2019 // 0 | 0 | 1 | X < Y
2020 // 1 | 0 | 0 | X == Y
2021 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 switch (SetCCOpcode) {
2023 default: break;
2024 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002025 case ISD::SETEQ:
2026 X86CC = X86::COND_E;
2027 break;
2028 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002029 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002030 case ISD::SETGT:
2031 X86CC = X86::COND_A;
2032 break;
2033 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002034 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002035 case ISD::SETGE:
2036 X86CC = X86::COND_AE;
2037 break;
2038 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002039 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002040 case ISD::SETLT:
2041 X86CC = X86::COND_B;
2042 break;
2043 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002045 case ISD::SETLE:
2046 X86CC = X86::COND_BE;
2047 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002049 case ISD::SETNE:
2050 X86CC = X86::COND_NE;
2051 break;
2052 case ISD::SETUO:
2053 X86CC = X86::COND_P;
2054 break;
2055 case ISD::SETO:
2056 X86CC = X86::COND_NP;
2057 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 }
Evan Chengfc937c92008-08-28 23:48:31 +00002059 }
2060
Evan Chengc6162692008-08-29 22:13:21 +00002061 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062}
2063
2064/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2065/// code. Current x86 isa includes the following FP cmov instructions:
2066/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2067static bool hasFPCMov(unsigned X86CC) {
2068 switch (X86CC) {
2069 default:
2070 return false;
2071 case X86::COND_B:
2072 case X86::COND_BE:
2073 case X86::COND_E:
2074 case X86::COND_P:
2075 case X86::COND_A:
2076 case X86::COND_AE:
2077 case X86::COND_NE:
2078 case X86::COND_NP:
2079 return true;
2080 }
2081}
2082
2083/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2084/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002085static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 if (Op.getOpcode() == ISD::UNDEF)
2087 return true;
2088
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002089 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002090 return (Val >= Low && Val < Hi);
2091}
2092
2093/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2094/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002095static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 if (Op.getOpcode() == ISD::UNDEF)
2097 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002098 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099}
2100
2101/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2102/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2103bool X86::isPSHUFDMask(SDNode *N) {
2104 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2105
Dan Gohman7dc19012007-08-02 21:17:01 +00002106 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002107 return false;
2108
2109 // Check if the value doesn't reference the second vector.
2110 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002111 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 if (Arg.getOpcode() == ISD::UNDEF) continue;
2113 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002114 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 return false;
2116 }
2117
2118 return true;
2119}
2120
2121/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2122/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2123bool X86::isPSHUFHWMask(SDNode *N) {
2124 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2125
2126 if (N->getNumOperands() != 8)
2127 return false;
2128
2129 // Lower quadword copied in order.
2130 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002131 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 if (Arg.getOpcode() == ISD::UNDEF) continue;
2133 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002134 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002135 return false;
2136 }
2137
2138 // Upper quadword shuffled.
2139 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002140 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002141 if (Arg.getOpcode() == ISD::UNDEF) continue;
2142 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002143 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002144 if (Val < 4 || Val > 7)
2145 return false;
2146 }
2147
2148 return true;
2149}
2150
2151/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2152/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2153bool X86::isPSHUFLWMask(SDNode *N) {
2154 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2155
2156 if (N->getNumOperands() != 8)
2157 return false;
2158
2159 // Upper quadword copied in order.
2160 for (unsigned i = 4; i != 8; ++i)
2161 if (!isUndefOrEqual(N->getOperand(i), i))
2162 return false;
2163
2164 // Lower quadword shuffled.
2165 for (unsigned i = 0; i != 4; ++i)
2166 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2167 return false;
2168
2169 return true;
2170}
2171
2172/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2173/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002174static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 if (NumElems != 2 && NumElems != 4) return false;
2176
2177 unsigned Half = NumElems / 2;
2178 for (unsigned i = 0; i < Half; ++i)
2179 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2180 return false;
2181 for (unsigned i = Half; i < NumElems; ++i)
2182 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2183 return false;
2184
2185 return true;
2186}
2187
2188bool X86::isSHUFPMask(SDNode *N) {
2189 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2190 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2191}
2192
2193/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2194/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2195/// half elements to come from vector 1 (which would equal the dest.) and
2196/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002197static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 if (NumOps != 2 && NumOps != 4) return false;
2199
2200 unsigned Half = NumOps / 2;
2201 for (unsigned i = 0; i < Half; ++i)
2202 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2203 return false;
2204 for (unsigned i = Half; i < NumOps; ++i)
2205 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2206 return false;
2207 return true;
2208}
2209
2210static bool isCommutedSHUFP(SDNode *N) {
2211 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2212 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2213}
2214
2215/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2216/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2217bool X86::isMOVHLPSMask(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219
2220 if (N->getNumOperands() != 4)
2221 return false;
2222
2223 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2224 return isUndefOrEqual(N->getOperand(0), 6) &&
2225 isUndefOrEqual(N->getOperand(1), 7) &&
2226 isUndefOrEqual(N->getOperand(2), 2) &&
2227 isUndefOrEqual(N->getOperand(3), 3);
2228}
2229
2230/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2231/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2232/// <2, 3, 2, 3>
2233bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2234 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2235
2236 if (N->getNumOperands() != 4)
2237 return false;
2238
2239 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2240 return isUndefOrEqual(N->getOperand(0), 2) &&
2241 isUndefOrEqual(N->getOperand(1), 3) &&
2242 isUndefOrEqual(N->getOperand(2), 2) &&
2243 isUndefOrEqual(N->getOperand(3), 3);
2244}
2245
2246/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2247/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2248bool X86::isMOVLPMask(SDNode *N) {
2249 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2250
2251 unsigned NumElems = N->getNumOperands();
2252 if (NumElems != 2 && NumElems != 4)
2253 return false;
2254
2255 for (unsigned i = 0; i < NumElems/2; ++i)
2256 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2257 return false;
2258
2259 for (unsigned i = NumElems/2; i < NumElems; ++i)
2260 if (!isUndefOrEqual(N->getOperand(i), i))
2261 return false;
2262
2263 return true;
2264}
2265
2266/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2267/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2268/// and MOVLHPS.
2269bool X86::isMOVHPMask(SDNode *N) {
2270 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2271
2272 unsigned NumElems = N->getNumOperands();
2273 if (NumElems != 2 && NumElems != 4)
2274 return false;
2275
2276 for (unsigned i = 0; i < NumElems/2; ++i)
2277 if (!isUndefOrEqual(N->getOperand(i), i))
2278 return false;
2279
2280 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002281 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002282 if (!isUndefOrEqual(Arg, i + NumElems))
2283 return false;
2284 }
2285
2286 return true;
2287}
2288
2289/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2290/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002291bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002292 bool V2IsSplat = false) {
2293 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2294 return false;
2295
2296 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002297 SDValue BitI = Elts[i];
2298 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002299 if (!isUndefOrEqual(BitI, j))
2300 return false;
2301 if (V2IsSplat) {
2302 if (isUndefOrEqual(BitI1, NumElts))
2303 return false;
2304 } else {
2305 if (!isUndefOrEqual(BitI1, j + NumElts))
2306 return false;
2307 }
2308 }
2309
2310 return true;
2311}
2312
2313bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2314 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2315 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2316}
2317
2318/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2319/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002320bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002321 bool V2IsSplat = false) {
2322 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2323 return false;
2324
2325 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002326 SDValue BitI = Elts[i];
2327 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328 if (!isUndefOrEqual(BitI, j + NumElts/2))
2329 return false;
2330 if (V2IsSplat) {
2331 if (isUndefOrEqual(BitI1, NumElts))
2332 return false;
2333 } else {
2334 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2335 return false;
2336 }
2337 }
2338
2339 return true;
2340}
2341
2342bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2343 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2344 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2345}
2346
2347/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2348/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2349/// <0, 0, 1, 1>
2350bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352
2353 unsigned NumElems = N->getNumOperands();
2354 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2355 return false;
2356
2357 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002358 SDValue BitI = N->getOperand(i);
2359 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360
2361 if (!isUndefOrEqual(BitI, j))
2362 return false;
2363 if (!isUndefOrEqual(BitI1, j))
2364 return false;
2365 }
2366
2367 return true;
2368}
2369
2370/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2371/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2372/// <2, 2, 3, 3>
2373bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2374 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2375
2376 unsigned NumElems = N->getNumOperands();
2377 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2378 return false;
2379
2380 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002381 SDValue BitI = N->getOperand(i);
2382 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002383
2384 if (!isUndefOrEqual(BitI, j))
2385 return false;
2386 if (!isUndefOrEqual(BitI1, j))
2387 return false;
2388 }
2389
2390 return true;
2391}
2392
2393/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2394/// specifies a shuffle of elements that is suitable for input to MOVSS,
2395/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002396static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002397 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002398 return false;
2399
2400 if (!isUndefOrEqual(Elts[0], NumElts))
2401 return false;
2402
2403 for (unsigned i = 1; i < NumElts; ++i) {
2404 if (!isUndefOrEqual(Elts[i], i))
2405 return false;
2406 }
2407
2408 return true;
2409}
2410
2411bool X86::isMOVLMask(SDNode *N) {
2412 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2413 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2414}
2415
2416/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2417/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2418/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002419static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002420 bool V2IsSplat = false,
2421 bool V2IsUndef = false) {
2422 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2423 return false;
2424
2425 if (!isUndefOrEqual(Ops[0], 0))
2426 return false;
2427
2428 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002429 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002430 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2431 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2432 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2433 return false;
2434 }
2435
2436 return true;
2437}
2438
2439static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2440 bool V2IsUndef = false) {
2441 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2442 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2443 V2IsSplat, V2IsUndef);
2444}
2445
2446/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2447/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2448bool X86::isMOVSHDUPMask(SDNode *N) {
2449 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2450
2451 if (N->getNumOperands() != 4)
2452 return false;
2453
2454 // Expect 1, 1, 3, 3
2455 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002456 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 if (Arg.getOpcode() == ISD::UNDEF) continue;
2458 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002459 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002460 if (Val != 1) return false;
2461 }
2462
2463 bool HasHi = false;
2464 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002465 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002466 if (Arg.getOpcode() == ISD::UNDEF) continue;
2467 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002468 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002469 if (Val != 3) return false;
2470 HasHi = true;
2471 }
2472
2473 // Don't use movshdup if it can be done with a shufps.
2474 return HasHi;
2475}
2476
2477/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2478/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2479bool X86::isMOVSLDUPMask(SDNode *N) {
2480 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2481
2482 if (N->getNumOperands() != 4)
2483 return false;
2484
2485 // Expect 0, 0, 2, 2
2486 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002487 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 if (Arg.getOpcode() == ISD::UNDEF) continue;
2489 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002490 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002491 if (Val != 0) return false;
2492 }
2493
2494 bool HasHi = false;
2495 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002496 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002497 if (Arg.getOpcode() == ISD::UNDEF) continue;
2498 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002499 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002500 if (Val != 2) return false;
2501 HasHi = true;
2502 }
2503
2504 // Don't use movshdup if it can be done with a shufps.
2505 return HasHi;
2506}
2507
2508/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2509/// specifies a identity operation on the LHS or RHS.
2510static bool isIdentityMask(SDNode *N, bool RHS = false) {
2511 unsigned NumElems = N->getNumOperands();
2512 for (unsigned i = 0; i < NumElems; ++i)
2513 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2514 return false;
2515 return true;
2516}
2517
2518/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2519/// a splat of a single element.
2520static bool isSplatMask(SDNode *N) {
2521 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2522
2523 // This is a splat operation if each element of the permute is the same, and
2524 // if the value doesn't reference the second vector.
2525 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002526 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002527 unsigned i = 0;
2528 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002529 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530 if (isa<ConstantSDNode>(Elt)) {
2531 ElementBase = Elt;
2532 break;
2533 }
2534 }
2535
Gabor Greif1c80d112008-08-28 21:40:38 +00002536 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002537 return false;
2538
2539 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002540 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 if (Arg.getOpcode() == ISD::UNDEF) continue;
2542 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2543 if (Arg != ElementBase) return false;
2544 }
2545
2546 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002547 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002548}
2549
2550/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2551/// a splat of a single element and it's a 2 or 4 element mask.
2552bool X86::isSplatMask(SDNode *N) {
2553 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2554
2555 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2556 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2557 return false;
2558 return ::isSplatMask(N);
2559}
2560
2561/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2562/// specifies a splat of zero element.
2563bool X86::isSplatLoMask(SDNode *N) {
2564 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2565
2566 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2567 if (!isUndefOrEqual(N->getOperand(i), 0))
2568 return false;
2569 return true;
2570}
2571
Evan Chenga2497eb2008-09-25 20:50:48 +00002572/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2573/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2574bool X86::isMOVDDUPMask(SDNode *N) {
2575 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2576
2577 unsigned e = N->getNumOperands() / 2;
2578 for (unsigned i = 0; i < e; ++i)
2579 if (!isUndefOrEqual(N->getOperand(i), i))
2580 return false;
2581 for (unsigned i = 0; i < e; ++i)
2582 if (!isUndefOrEqual(N->getOperand(e+i), i))
2583 return false;
2584 return true;
2585}
2586
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002587/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2588/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2589/// instructions.
2590unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2591 unsigned NumOperands = N->getNumOperands();
2592 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2593 unsigned Mask = 0;
2594 for (unsigned i = 0; i < NumOperands; ++i) {
2595 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002596 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002597 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002598 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 if (Val >= NumOperands) Val -= NumOperands;
2600 Mask |= Val;
2601 if (i != NumOperands - 1)
2602 Mask <<= Shift;
2603 }
2604
2605 return Mask;
2606}
2607
2608/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2609/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2610/// instructions.
2611unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2612 unsigned Mask = 0;
2613 // 8 nodes, but we only care about the last 4.
2614 for (unsigned i = 7; i >= 4; --i) {
2615 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002616 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002618 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002619 Mask |= (Val - 4);
2620 if (i != 4)
2621 Mask <<= 2;
2622 }
2623
2624 return Mask;
2625}
2626
2627/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2628/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2629/// instructions.
2630unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2631 unsigned Mask = 0;
2632 // 8 nodes, but we only care about the first 4.
2633 for (int i = 3; i >= 0; --i) {
2634 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002635 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002637 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002638 Mask |= Val;
2639 if (i != 0)
2640 Mask <<= 2;
2641 }
2642
2643 return Mask;
2644}
2645
2646/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2647/// specifies a 8 element shuffle that can be broken into a pair of
2648/// PSHUFHW and PSHUFLW.
2649static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2650 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2651
2652 if (N->getNumOperands() != 8)
2653 return false;
2654
2655 // Lower quadword shuffled.
2656 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002657 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002658 if (Arg.getOpcode() == ISD::UNDEF) continue;
2659 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002660 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002661 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 return false;
2663 }
2664
2665 // Upper quadword shuffled.
2666 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002667 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 if (Arg.getOpcode() == ISD::UNDEF) continue;
2669 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002670 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002671 if (Val < 4 || Val > 7)
2672 return false;
2673 }
2674
2675 return true;
2676}
2677
Chris Lattnere6aa3862007-11-25 00:24:49 +00002678/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002679/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002680static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2681 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002683 MVT VT = Op.getValueType();
2684 MVT MaskVT = Mask.getValueType();
2685 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002686 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002687 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002688
2689 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002690 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002691 if (Arg.getOpcode() == ISD::UNDEF) {
2692 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2693 continue;
2694 }
2695 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002696 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 if (Val < NumElems)
2698 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2699 else
2700 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2701 }
2702
2703 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002704 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002705 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2706}
2707
Evan Chenga6769df2007-12-07 21:30:01 +00002708/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2709/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002710static
Dan Gohman8181bd12008-07-27 21:46:04 +00002711SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002712 MVT MaskVT = Mask.getValueType();
2713 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002714 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002715 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002716 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002717 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002718 if (Arg.getOpcode() == ISD::UNDEF) {
2719 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2720 continue;
2721 }
2722 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002723 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002724 if (Val < NumElems)
2725 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2726 else
2727 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2728 }
2729 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2730}
2731
2732
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002733/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2734/// match movhlps. The lower half elements should come from upper half of
2735/// V1 (and in order), and the upper half elements should come from the upper
2736/// half of V2 (and in order).
2737static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2738 unsigned NumElems = Mask->getNumOperands();
2739 if (NumElems != 4)
2740 return false;
2741 for (unsigned i = 0, e = 2; i != e; ++i)
2742 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2743 return false;
2744 for (unsigned i = 2; i != 4; ++i)
2745 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2746 return false;
2747 return true;
2748}
2749
2750/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002751/// is promoted to a vector. It also returns the LoadSDNode by reference if
2752/// required.
2753static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002754 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2755 return false;
2756 N = N->getOperand(0).getNode();
2757 if (!ISD::isNON_EXTLoad(N))
2758 return false;
2759 if (LD)
2760 *LD = cast<LoadSDNode>(N);
2761 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762}
2763
2764/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2765/// match movlp{s|d}. The lower half elements should come from lower half of
2766/// V1 (and in order), and the upper half elements should come from the upper
2767/// half of V2 (and in order). And since V1 will become the source of the
2768/// MOVLP, it must be either a vector load or a scalar load to vector.
2769static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2770 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2771 return false;
2772 // Is V2 is a vector load, don't do this transformation. We will try to use
2773 // load folding shufps op.
2774 if (ISD::isNON_EXTLoad(V2))
2775 return false;
2776
2777 unsigned NumElems = Mask->getNumOperands();
2778 if (NumElems != 2 && NumElems != 4)
2779 return false;
2780 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2781 if (!isUndefOrEqual(Mask->getOperand(i), i))
2782 return false;
2783 for (unsigned i = NumElems/2; i != NumElems; ++i)
2784 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2785 return false;
2786 return true;
2787}
2788
2789/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2790/// all the same.
2791static bool isSplatVector(SDNode *N) {
2792 if (N->getOpcode() != ISD::BUILD_VECTOR)
2793 return false;
2794
Dan Gohman8181bd12008-07-27 21:46:04 +00002795 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2797 if (N->getOperand(i) != SplatValue)
2798 return false;
2799 return true;
2800}
2801
2802/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2803/// to an undef.
2804static bool isUndefShuffle(SDNode *N) {
2805 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2806 return false;
2807
Dan Gohman8181bd12008-07-27 21:46:04 +00002808 SDValue V1 = N->getOperand(0);
2809 SDValue V2 = N->getOperand(1);
2810 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 unsigned NumElems = Mask.getNumOperands();
2812 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002813 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002815 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2817 return false;
2818 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2819 return false;
2820 }
2821 }
2822 return true;
2823}
2824
2825/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2826/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002827static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002828 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002829 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002831 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002832}
2833
2834/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2835/// to an zero vector.
2836static bool isZeroShuffle(SDNode *N) {
2837 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2838 return false;
2839
Dan Gohman8181bd12008-07-27 21:46:04 +00002840 SDValue V1 = N->getOperand(0);
2841 SDValue V2 = N->getOperand(1);
2842 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 unsigned NumElems = Mask.getNumOperands();
2844 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002845 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002846 if (Arg.getOpcode() == ISD::UNDEF)
2847 continue;
2848
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002849 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002850 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002851 unsigned Opc = V1.getNode()->getOpcode();
2852 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002853 continue;
2854 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002855 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002856 return false;
2857 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002858 unsigned Opc = V2.getNode()->getOpcode();
2859 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002860 continue;
2861 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002862 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002863 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 }
2865 }
2866 return true;
2867}
2868
2869/// getZeroVector - Returns a vector of specified type with all zero elements.
2870///
Dan Gohman8181bd12008-07-27 21:46:04 +00002871static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002872 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002873
2874 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2875 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002876 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002877 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002878 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002879 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002880 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002881 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002882 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002883 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002885 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2886 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002887 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888}
2889
Chris Lattnere6aa3862007-11-25 00:24:49 +00002890/// getOnesVector - Returns a vector of specified type with all bits set.
2891///
Dan Gohman8181bd12008-07-27 21:46:04 +00002892static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002893 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002894
2895 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2896 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002897 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2898 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002899 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002900 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2901 else // SSE
2902 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2903 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2904}
2905
2906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002907/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2908/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002909static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002910 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2911
2912 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002914 unsigned NumElems = Mask.getNumOperands();
2915 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002918 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002919 if (Val > NumElems) {
2920 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2921 Changed = true;
2922 }
2923 }
2924 MaskVec.push_back(Arg);
2925 }
2926
2927 if (Changed)
2928 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2929 &MaskVec[0], MaskVec.size());
2930 return Mask;
2931}
2932
2933/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2934/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002935static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002936 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2937 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938
Dan Gohman8181bd12008-07-27 21:46:04 +00002939 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002940 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2941 for (unsigned i = 1; i != NumElems; ++i)
2942 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2943 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2944}
2945
2946/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2947/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002948static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002949 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2950 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002951 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2953 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2954 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2955 }
2956 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2957}
2958
2959/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2960/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002961static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002962 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2963 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002964 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 for (unsigned i = 0; i != Half; ++i) {
2967 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2968 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2969 }
2970 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2971}
2972
Chris Lattner2d91b962008-03-09 01:05:04 +00002973/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2974/// element #0 of a vector with the specified index, leaving the rest of the
2975/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002976static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002977 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002978 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2979 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002980 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002981 // Element #0 of the result gets the elt we are replacing.
2982 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2983 for (unsigned i = 1; i != NumElems; ++i)
2984 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2985 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2986}
2987
Evan Chengbf8b2c52008-04-05 00:30:36 +00002988/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002989static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002990 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2991 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002992 if (PVT == VT)
2993 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002994 SDValue V1 = Op.getOperand(0);
2995 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002997 // Special handling of v4f32 -> v4i32.
2998 if (VT != MVT::v4f32) {
2999 Mask = getUnpacklMask(NumElems, DAG);
3000 while (NumElems > 4) {
3001 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
3002 NumElems >>= 1;
3003 }
Evan Cheng8c590372008-05-15 08:39:06 +00003004 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003006
Evan Chengbf8b2c52008-04-05 00:30:36 +00003007 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00003008 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00003009 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3011}
3012
Evan Chenga2497eb2008-09-25 20:50:48 +00003013/// isVectorLoad - Returns true if the node is a vector load, a scalar
3014/// load that's promoted to vector, or a load bitcasted.
3015static bool isVectorLoad(SDValue Op) {
3016 assert(Op.getValueType().isVector() && "Expected a vector type");
3017 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3018 Op.getOpcode() == ISD::BIT_CONVERT) {
3019 return isa<LoadSDNode>(Op.getOperand(0));
3020 }
3021 return isa<LoadSDNode>(Op);
3022}
3023
3024
3025/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3026///
3027static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3028 SelectionDAG &DAG, bool HasSSE3) {
3029 // If we have sse3 and shuffle has more than one use or input is a load, then
3030 // use movddup. Otherwise, use movlhps.
3031 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3032 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3033 MVT VT = Op.getValueType();
3034 if (VT == PVT)
3035 return Op;
3036 unsigned NumElems = PVT.getVectorNumElements();
3037 if (NumElems == 2) {
3038 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3039 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3040 } else {
3041 assert(NumElems == 4);
3042 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3043 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3044 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3045 }
3046
3047 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3048 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3049 DAG.getNode(ISD::UNDEF, PVT), Mask);
3050 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3051}
3052
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003053/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003054/// vector of zero or undef vector. This produces a shuffle where the low
3055/// element of V2 is swizzled into the zero/undef vector, landing at element
3056/// 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 +00003057static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003058 bool isZero, bool HasSSE2,
3059 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003060 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003061 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003062 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003063 unsigned NumElems = V2.getValueType().getVectorNumElements();
3064 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3065 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003066 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003067 for (unsigned i = 0; i != NumElems; ++i)
3068 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3069 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3070 else
3071 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003072 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 &MaskVec[0], MaskVec.size());
3074 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3075}
3076
Evan Chengdea99362008-05-29 08:22:04 +00003077/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3078/// a shuffle that is zero.
3079static
Dan Gohman8181bd12008-07-27 21:46:04 +00003080unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003081 unsigned NumElems, bool Low,
3082 SelectionDAG &DAG) {
3083 unsigned NumZeros = 0;
3084 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003085 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003086 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003087 if (Idx.getOpcode() == ISD::UNDEF) {
3088 ++NumZeros;
3089 continue;
3090 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003091 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3092 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003093 ++NumZeros;
3094 else
3095 break;
3096 }
3097 return NumZeros;
3098}
3099
3100/// isVectorShift - Returns true if the shuffle can be implemented as a
3101/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003102static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3103 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003104 unsigned NumElems = Mask.getNumOperands();
3105
3106 isLeft = true;
3107 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3108 if (!NumZeros) {
3109 isLeft = false;
3110 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3111 if (!NumZeros)
3112 return false;
3113 }
3114
3115 bool SeenV1 = false;
3116 bool SeenV2 = false;
3117 for (unsigned i = NumZeros; i < NumElems; ++i) {
3118 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003119 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003120 if (Idx.getOpcode() == ISD::UNDEF)
3121 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003122 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003123 if (Index < NumElems)
3124 SeenV1 = true;
3125 else {
3126 Index -= NumElems;
3127 SeenV2 = true;
3128 }
3129 if (Index != Val)
3130 return false;
3131 }
3132 if (SeenV1 && SeenV2)
3133 return false;
3134
3135 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3136 ShAmt = NumZeros;
3137 return true;
3138}
3139
3140
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3142///
Dan Gohman8181bd12008-07-27 21:46:04 +00003143static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003144 unsigned NumNonZero, unsigned NumZero,
3145 SelectionDAG &DAG, TargetLowering &TLI) {
3146 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003147 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003148
Dan Gohman8181bd12008-07-27 21:46:04 +00003149 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003150 bool First = true;
3151 for (unsigned i = 0; i < 16; ++i) {
3152 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3153 if (ThisIsNonZero && First) {
3154 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003155 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 else
3157 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3158 First = false;
3159 }
3160
3161 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003162 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003163 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3164 if (LastIsNonZero) {
3165 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3166 }
3167 if (ThisIsNonZero) {
3168 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3169 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3170 ThisElt, DAG.getConstant(8, MVT::i8));
3171 if (LastIsNonZero)
3172 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3173 } else
3174 ThisElt = LastElt;
3175
Gabor Greif1c80d112008-08-28 21:40:38 +00003176 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003178 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003179 }
3180 }
3181
3182 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3183}
3184
3185/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3186///
Dan Gohman8181bd12008-07-27 21:46:04 +00003187static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003188 unsigned NumNonZero, unsigned NumZero,
3189 SelectionDAG &DAG, TargetLowering &TLI) {
3190 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003191 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003192
Dan Gohman8181bd12008-07-27 21:46:04 +00003193 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003194 bool First = true;
3195 for (unsigned i = 0; i < 8; ++i) {
3196 bool isNonZero = (NonZeros & (1 << i)) != 0;
3197 if (isNonZero) {
3198 if (First) {
3199 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003200 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 else
3202 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3203 First = false;
3204 }
3205 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003206 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003207 }
3208 }
3209
3210 return V;
3211}
3212
Evan Chengdea99362008-05-29 08:22:04 +00003213/// getVShift - Return a vector logical shift node.
3214///
Dan Gohman8181bd12008-07-27 21:46:04 +00003215static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003216 unsigned NumBits, SelectionDAG &DAG,
3217 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003218 bool isMMX = VT.getSizeInBits() == 64;
3219 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003220 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3221 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3222 return DAG.getNode(ISD::BIT_CONVERT, VT,
3223 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003224 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003225}
3226
Dan Gohman8181bd12008-07-27 21:46:04 +00003227SDValue
3228X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003229 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003230 if (ISD::isBuildVectorAllZeros(Op.getNode())
3231 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003232 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3233 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3234 // eliminated on x86-32 hosts.
3235 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3236 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003237
Gabor Greif1c80d112008-08-28 21:40:38 +00003238 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003239 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003240 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003241 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003242
Duncan Sands92c43912008-06-06 12:08:01 +00003243 MVT VT = Op.getValueType();
3244 MVT EVT = VT.getVectorElementType();
3245 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003246
3247 unsigned NumElems = Op.getNumOperands();
3248 unsigned NumZero = 0;
3249 unsigned NumNonZero = 0;
3250 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003251 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003252 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003254 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003255 if (Elt.getOpcode() == ISD::UNDEF)
3256 continue;
3257 Values.insert(Elt);
3258 if (Elt.getOpcode() != ISD::Constant &&
3259 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003260 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003261 if (isZeroNode(Elt))
3262 NumZero++;
3263 else {
3264 NonZeros |= (1 << i);
3265 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 }
3267 }
3268
3269 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003270 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3271 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 }
3273
Chris Lattner66a4dda2008-03-09 05:42:06 +00003274 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003275 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003277 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003278
Chris Lattner2d91b962008-03-09 01:05:04 +00003279 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3280 // the value are obviously zero, truncate the value to i32 and do the
3281 // insertion that way. Only do this if the value is non-constant or if the
3282 // value is a constant being inserted into element 0. It is cheaper to do
3283 // a constant pool load than it is to do a movd + shuffle.
3284 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3285 (!IsAllConstants || Idx == 0)) {
3286 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3287 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003288 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3289 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003290
3291 // Truncate the value (which may itself be a constant) to i32, and
3292 // convert it to a vector with movd (S2V+shuffle to zero extend).
3293 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3294 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003295 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3296 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003297
3298 // Now we have our 32-bit value zero extended in the low element of
3299 // a vector. If Idx != 0, swizzle it into place.
3300 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003301 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003302 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3303 getSwapEltZeroMask(VecElts, Idx, DAG)
3304 };
3305 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3306 }
3307 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3308 }
3309 }
3310
Chris Lattnerac914892008-03-08 22:59:52 +00003311 // If we have a constant or non-constant insertion into the low element of
3312 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3313 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3314 // depending on what the source datatype is. Because we can only get here
3315 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3316 if (Idx == 0 &&
3317 // Don't do this for i64 values on x86-32.
3318 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003319 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003320 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003321 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3322 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003323 }
Evan Chengdea99362008-05-29 08:22:04 +00003324
3325 // Is it a vector logical left shift?
3326 if (NumElems == 2 && Idx == 1 &&
3327 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003328 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003329 return getVShift(true, VT,
3330 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3331 NumBits/2, DAG, *this);
3332 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003333
3334 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003335 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336
Chris Lattnerac914892008-03-08 22:59:52 +00003337 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3338 // is a non-constant being inserted into an element other than the low one,
3339 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3340 // movd/movss) to move this into the low element, then shuffle it into
3341 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003342 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003343 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003345 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003346 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3347 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003348 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3349 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003350 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003351 for (unsigned i = 0; i < NumElems; i++)
3352 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003353 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003354 &MaskVec[0], MaskVec.size());
3355 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3356 DAG.getNode(ISD::UNDEF, VT), Mask);
3357 }
3358 }
3359
Chris Lattner66a4dda2008-03-09 05:42:06 +00003360 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3361 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003362 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003363
Dan Gohman21463242007-07-24 22:55:08 +00003364 // A vector full of immediates; various special cases are already
3365 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003366 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003368
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003370 if (EVTBits == 64) {
3371 if (NumNonZero == 1) {
3372 // One half is zero or undef.
3373 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003374 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003375 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003376 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3377 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003378 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003379 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003380 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003381
3382 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3383 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003384 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003386 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 }
3388
3389 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003390 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003392 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003393 }
3394
3395 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003396 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 V.resize(NumElems);
3398 if (NumElems == 4 && NumZero > 0) {
3399 for (unsigned i = 0; i < 4; ++i) {
3400 bool isZero = !(NonZeros & (1 << i));
3401 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003402 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 else
3404 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3405 }
3406
3407 for (unsigned i = 0; i < 2; ++i) {
3408 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3409 default: break;
3410 case 0:
3411 V[i] = V[i*2]; // Must be a zero vector.
3412 break;
3413 case 1:
3414 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3415 getMOVLMask(NumElems, DAG));
3416 break;
3417 case 2:
3418 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3419 getMOVLMask(NumElems, DAG));
3420 break;
3421 case 3:
3422 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3423 getUnpacklMask(NumElems, DAG));
3424 break;
3425 }
3426 }
3427
Duncan Sands92c43912008-06-06 12:08:01 +00003428 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3429 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003430 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003431 bool Reverse = (NonZeros & 0x3) == 2;
3432 for (unsigned i = 0; i < 2; ++i)
3433 if (Reverse)
3434 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3435 else
3436 MaskVec.push_back(DAG.getConstant(i, EVT));
3437 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3438 for (unsigned i = 0; i < 2; ++i)
3439 if (Reverse)
3440 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3441 else
3442 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003443 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003444 &MaskVec[0], MaskVec.size());
3445 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3446 }
3447
3448 if (Values.size() > 2) {
3449 // Expand into a number of unpckl*.
3450 // e.g. for v4f32
3451 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3452 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3453 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003455 for (unsigned i = 0; i < NumElems; ++i)
3456 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3457 NumElems >>= 1;
3458 while (NumElems != 0) {
3459 for (unsigned i = 0; i < NumElems; ++i)
3460 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3461 UnpckMask);
3462 NumElems >>= 1;
3463 }
3464 return V[0];
3465 }
3466
Dan Gohman8181bd12008-07-27 21:46:04 +00003467 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003468}
3469
Evan Chengfca29242007-12-07 08:07:39 +00003470static
Dan Gohman8181bd12008-07-27 21:46:04 +00003471SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003472 SDValue PermMask, SelectionDAG &DAG,
3473 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003474 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003475 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3476 MVT MaskEVT = MaskVT.getVectorElementType();
3477 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003478 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3479 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003480
3481 // First record which half of which vector the low elements come from.
3482 SmallVector<unsigned, 4> LowQuad(4);
3483 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003484 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003485 if (Elt.getOpcode() == ISD::UNDEF)
3486 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003487 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003488 int QuadIdx = EltIdx / 4;
3489 ++LowQuad[QuadIdx];
3490 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003491
Evan Cheng75184a92007-12-11 01:46:18 +00003492 int BestLowQuad = -1;
3493 unsigned MaxQuad = 1;
3494 for (unsigned i = 0; i < 4; ++i) {
3495 if (LowQuad[i] > MaxQuad) {
3496 BestLowQuad = i;
3497 MaxQuad = LowQuad[i];
3498 }
Evan Chengfca29242007-12-07 08:07:39 +00003499 }
3500
Evan Cheng75184a92007-12-11 01:46:18 +00003501 // Record which half of which vector the high elements come from.
3502 SmallVector<unsigned, 4> HighQuad(4);
3503 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003504 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003505 if (Elt.getOpcode() == ISD::UNDEF)
3506 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003507 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003508 int QuadIdx = EltIdx / 4;
3509 ++HighQuad[QuadIdx];
3510 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003511
Evan Cheng75184a92007-12-11 01:46:18 +00003512 int BestHighQuad = -1;
3513 MaxQuad = 1;
3514 for (unsigned i = 0; i < 4; ++i) {
3515 if (HighQuad[i] > MaxQuad) {
3516 BestHighQuad = i;
3517 MaxQuad = HighQuad[i];
3518 }
3519 }
3520
3521 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3522 if (BestLowQuad != -1 || BestHighQuad != -1) {
3523 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003524 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003525
Evan Cheng75184a92007-12-11 01:46:18 +00003526 if (BestLowQuad != -1)
3527 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3528 else
3529 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003530
Evan Cheng75184a92007-12-11 01:46:18 +00003531 if (BestHighQuad != -1)
3532 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3533 else
3534 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003535
Dan Gohman8181bd12008-07-27 21:46:04 +00003536 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003537 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3538 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3539 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3540 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3541
3542 // Now sort high and low parts separately.
3543 BitVector InOrder(8);
3544 if (BestLowQuad != -1) {
3545 // Sort lower half in order using PSHUFLW.
3546 MaskVec.clear();
3547 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003548
Evan Cheng75184a92007-12-11 01:46:18 +00003549 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003550 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003551 if (Elt.getOpcode() == ISD::UNDEF) {
3552 MaskVec.push_back(Elt);
3553 InOrder.set(i);
3554 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003555 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003556 if (EltIdx != i)
3557 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003558
Evan Cheng75184a92007-12-11 01:46:18 +00003559 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003560
Evan Cheng75184a92007-12-11 01:46:18 +00003561 // If this element is in the right place after this shuffle, then
3562 // remember it.
3563 if ((int)(EltIdx / 4) == BestLowQuad)
3564 InOrder.set(i);
3565 }
3566 }
3567 if (AnyOutOrder) {
3568 for (unsigned i = 4; i != 8; ++i)
3569 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003570 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003571 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3572 }
3573 }
3574
3575 if (BestHighQuad != -1) {
3576 // Sort high half in order using PSHUFHW if possible.
3577 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 for (unsigned i = 0; i != 4; ++i)
3580 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003581
Evan Cheng75184a92007-12-11 01:46:18 +00003582 bool AnyOutOrder = false;
3583 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003584 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003585 if (Elt.getOpcode() == ISD::UNDEF) {
3586 MaskVec.push_back(Elt);
3587 InOrder.set(i);
3588 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003589 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003590 if (EltIdx != i)
3591 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003592
Evan Cheng75184a92007-12-11 01:46:18 +00003593 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003594
Evan Cheng75184a92007-12-11 01:46:18 +00003595 // If this element is in the right place after this shuffle, then
3596 // remember it.
3597 if ((int)(EltIdx / 4) == BestHighQuad)
3598 InOrder.set(i);
3599 }
3600 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003601
Evan Cheng75184a92007-12-11 01:46:18 +00003602 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003603 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003604 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3605 }
3606 }
3607
3608 // The other elements are put in the right place using pextrw and pinsrw.
3609 for (unsigned i = 0; i != 8; ++i) {
3610 if (InOrder[i])
3611 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003613 if (Elt.getOpcode() == ISD::UNDEF)
3614 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003615 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003616 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003617 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3618 DAG.getConstant(EltIdx, PtrVT))
3619 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3620 DAG.getConstant(EltIdx - 8, PtrVT));
3621 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3622 DAG.getConstant(i, PtrVT));
3623 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003624
Evan Cheng75184a92007-12-11 01:46:18 +00003625 return NewV;
3626 }
3627
Bill Wendling2c7cd592008-08-21 22:35:37 +00003628 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3629 // few as possible. First, let's find out how many elements are already in the
3630 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003631 unsigned V1InOrder = 0;
3632 unsigned V1FromV1 = 0;
3633 unsigned V2InOrder = 0;
3634 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003635 SmallVector<SDValue, 8> V1Elts;
3636 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003637 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003638 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003639 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003640 V1Elts.push_back(Elt);
3641 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003642 ++V1InOrder;
3643 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003644 continue;
3645 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003646 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003647 if (EltIdx == i) {
3648 V1Elts.push_back(Elt);
3649 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3650 ++V1InOrder;
3651 } else if (EltIdx == i+8) {
3652 V1Elts.push_back(Elt);
3653 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3654 ++V2InOrder;
3655 } else if (EltIdx < 8) {
3656 V1Elts.push_back(Elt);
3657 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003658 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003659 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3660 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003661 }
3662 }
3663
3664 if (V2InOrder > V1InOrder) {
3665 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3666 std::swap(V1, V2);
3667 std::swap(V1Elts, V2Elts);
3668 std::swap(V1FromV1, V2FromV2);
3669 }
3670
Evan Cheng75184a92007-12-11 01:46:18 +00003671 if ((V1FromV1 + V1InOrder) != 8) {
3672 // Some elements are from V2.
3673 if (V1FromV1) {
3674 // If there are elements that are from V1 but out of place,
3675 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003676 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003677 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003678 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003679 if (Elt.getOpcode() == ISD::UNDEF) {
3680 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3681 continue;
3682 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003683 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003684 if (EltIdx >= 8)
3685 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3686 else
3687 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3688 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003689 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003690 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003691 }
Evan Cheng75184a92007-12-11 01:46:18 +00003692
3693 NewV = V1;
3694 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003695 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003696 if (Elt.getOpcode() == ISD::UNDEF)
3697 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003698 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003699 if (EltIdx < 8)
3700 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003701 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003702 DAG.getConstant(EltIdx - 8, PtrVT));
3703 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3704 DAG.getConstant(i, PtrVT));
3705 }
3706 return NewV;
3707 } else {
3708 // All elements are from V1.
3709 NewV = V1;
3710 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003711 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003712 if (Elt.getOpcode() == ISD::UNDEF)
3713 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003714 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003716 DAG.getConstant(EltIdx, PtrVT));
3717 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3718 DAG.getConstant(i, PtrVT));
3719 }
3720 return NewV;
3721 }
3722}
3723
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003724/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3725/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3726/// done when every pair / quad of shuffle mask elements point to elements in
3727/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003728/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3729static
Dan Gohman8181bd12008-07-27 21:46:04 +00003730SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003731 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003732 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003733 TargetLowering &TLI) {
3734 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003735 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003736 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003737 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003738 MVT NewVT = MaskVT;
3739 switch (VT.getSimpleVT()) {
3740 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003741 case MVT::v4f32: NewVT = MVT::v2f64; break;
3742 case MVT::v4i32: NewVT = MVT::v2i64; break;
3743 case MVT::v8i16: NewVT = MVT::v4i32; break;
3744 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003745 }
3746
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003747 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003748 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003749 NewVT = MVT::v2i64;
3750 else
3751 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003752 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003753 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003754 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003755 for (unsigned i = 0; i < NumElems; i += Scale) {
3756 unsigned StartIdx = ~0U;
3757 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003758 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003759 if (Elt.getOpcode() == ISD::UNDEF)
3760 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003761 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003762 if (StartIdx == ~0U)
3763 StartIdx = EltIdx - (EltIdx % Scale);
3764 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003765 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003766 }
3767 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003768 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003769 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003770 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003771 }
3772
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003773 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3774 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3775 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3776 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3777 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003778}
3779
Evan Chenge9b9c672008-05-09 21:53:03 +00003780/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003781///
Dan Gohman8181bd12008-07-27 21:46:04 +00003782static SDValue getVZextMovL(MVT VT, MVT OpVT,
3783 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003784 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003785 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3786 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003787 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003788 LD = dyn_cast<LoadSDNode>(SrcOp);
3789 if (!LD) {
3790 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3791 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003792 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003793 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3794 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3795 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3796 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3797 // PR2108
3798 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3799 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003800 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003801 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003802 SrcOp.getOperand(0)
3803 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003804 }
3805 }
3806 }
3807
3808 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003809 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003810 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3811}
3812
Evan Chengf50554e2008-07-22 21:13:36 +00003813/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3814/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003815static SDValue
3816LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3817 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003818 MVT MaskVT = PermMask.getValueType();
3819 MVT MaskEVT = MaskVT.getVectorElementType();
3820 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003821 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003822 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003823 unsigned NumHi = 0;
3824 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003825 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003826 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003827 if (Elt.getOpcode() == ISD::UNDEF) {
3828 Locs[i] = std::make_pair(-1, -1);
3829 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003830 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003831 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003832 if (Val < 4) {
3833 Locs[i] = std::make_pair(0, NumLo);
3834 Mask1[NumLo] = Elt;
3835 NumLo++;
3836 } else {
3837 Locs[i] = std::make_pair(1, NumHi);
3838 if (2+NumHi < 4)
3839 Mask1[2+NumHi] = Elt;
3840 NumHi++;
3841 }
3842 }
3843 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003844
Evan Chengf50554e2008-07-22 21:13:36 +00003845 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003846 // If no more than two elements come from either vector. This can be
3847 // implemented with two shuffles. First shuffle gather the elements.
3848 // The second shuffle, which takes the first shuffle as both of its
3849 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003850 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3851 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3852 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003853
Dan Gohman8181bd12008-07-27 21:46:04 +00003854 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003855 for (unsigned i = 0; i != 4; ++i) {
3856 if (Locs[i].first == -1)
3857 continue;
3858 else {
3859 unsigned Idx = (i < 2) ? 0 : 4;
3860 Idx += Locs[i].first * 2 + Locs[i].second;
3861 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3862 }
3863 }
3864
3865 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3866 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3867 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003868 } else if (NumLo == 3 || NumHi == 3) {
3869 // Otherwise, we must have three elements from one vector, call it X, and
3870 // one element from the other, call it Y. First, use a shufps to build an
3871 // intermediate vector with the one element from Y and the element from X
3872 // that will be in the same half in the final destination (the indexes don't
3873 // matter). Then, use a shufps to build the final vector, taking the half
3874 // containing the element from Y from the intermediate, and the other half
3875 // from X.
3876 if (NumHi == 3) {
3877 // Normalize it so the 3 elements come from V1.
3878 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3879 std::swap(V1, V2);
3880 }
3881
3882 // Find the element from V2.
3883 unsigned HiIndex;
3884 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003885 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003886 if (Elt.getOpcode() == ISD::UNDEF)
3887 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003888 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003889 if (Val >= 4)
3890 break;
3891 }
3892
3893 Mask1[0] = PermMask.getOperand(HiIndex);
3894 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3895 Mask1[2] = PermMask.getOperand(HiIndex^1);
3896 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3897 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3898 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3899
3900 if (HiIndex >= 2) {
3901 Mask1[0] = PermMask.getOperand(0);
3902 Mask1[1] = PermMask.getOperand(1);
3903 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3904 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3905 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3906 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3907 } else {
3908 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3909 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3910 Mask1[2] = PermMask.getOperand(2);
3911 Mask1[3] = PermMask.getOperand(3);
3912 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003913 Mask1[2] =
3914 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3915 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003916 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003917 Mask1[3] =
3918 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3919 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003920 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3921 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3922 }
Evan Chengf50554e2008-07-22 21:13:36 +00003923 }
3924
3925 // Break it into (shuffle shuffle_hi, shuffle_lo).
3926 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003927 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3928 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3929 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003930 unsigned MaskIdx = 0;
3931 unsigned LoIdx = 0;
3932 unsigned HiIdx = 2;
3933 for (unsigned i = 0; i != 4; ++i) {
3934 if (i == 2) {
3935 MaskPtr = &HiMask;
3936 MaskIdx = 1;
3937 LoIdx = 0;
3938 HiIdx = 2;
3939 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003940 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003941 if (Elt.getOpcode() == ISD::UNDEF) {
3942 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003943 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003944 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3945 (*MaskPtr)[LoIdx] = Elt;
3946 LoIdx++;
3947 } else {
3948 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3949 (*MaskPtr)[HiIdx] = Elt;
3950 HiIdx++;
3951 }
3952 }
3953
Dan Gohman8181bd12008-07-27 21:46:04 +00003954 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003955 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3956 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003957 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003958 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3959 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003960 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003961 for (unsigned i = 0; i != 4; ++i) {
3962 if (Locs[i].first == -1) {
3963 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3964 } else {
3965 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3966 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3967 }
3968 }
3969 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3970 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3971 &MaskOps[0], MaskOps.size()));
3972}
3973
Dan Gohman8181bd12008-07-27 21:46:04 +00003974SDValue
3975X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3976 SDValue V1 = Op.getOperand(0);
3977 SDValue V2 = Op.getOperand(1);
3978 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003979 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003980 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003981 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003982 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3983 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3984 bool V1IsSplat = false;
3985 bool V2IsSplat = false;
3986
Gabor Greif1c80d112008-08-28 21:40:38 +00003987 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003988 return DAG.getNode(ISD::UNDEF, VT);
3989
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003991 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992
Gabor Greif1c80d112008-08-28 21:40:38 +00003993 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003994 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003995 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 return V2;
3997
Evan Chengae6c9212008-09-25 23:35:16 +00003998 // Canonicalize movddup shuffles.
3999 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00004000 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00004001 X86::isMOVDDUPMask(PermMask.getNode()))
4002 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
4003
Gabor Greif1c80d112008-08-28 21:40:38 +00004004 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004005 if (isMMX || NumElems < 4) return Op;
4006 // Promote it to a v4{if}32 splat.
4007 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004008 }
4009
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004010 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4011 // do it!
4012 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004013 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004014 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004015 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4016 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4017 // FIXME: Figure out a cleaner way to do this.
4018 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004019 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004020 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004021 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004022 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004023 SDValue NewV1 = NewOp.getOperand(0);
4024 SDValue NewV2 = NewOp.getOperand(1);
4025 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004026 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004027 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004028 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004029 }
4030 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004031 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004032 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004033 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004034 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004035 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004036 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004037 }
4038 }
4039
Evan Chengdea99362008-05-29 08:22:04 +00004040 // Check if this can be converted into a logical shift.
4041 bool isLeft = false;
4042 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004043 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004044 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4045 if (isShift && ShVal.hasOneUse()) {
4046 // If the shifted value has multiple uses, it may be cheaper to use
4047 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004048 MVT EVT = VT.getVectorElementType();
4049 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004050 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4051 }
4052
Gabor Greif1c80d112008-08-28 21:40:38 +00004053 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004054 if (V1IsUndef)
4055 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004056 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004057 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004058 if (!isMMX)
4059 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004060 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004061
Gabor Greif1c80d112008-08-28 21:40:38 +00004062 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4063 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4064 X86::isMOVHLPSMask(PermMask.getNode()) ||
4065 X86::isMOVHPMask(PermMask.getNode()) ||
4066 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067 return Op;
4068
Gabor Greif1c80d112008-08-28 21:40:38 +00004069 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4070 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004071 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4072
Evan Chengdea99362008-05-29 08:22:04 +00004073 if (isShift) {
4074 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004075 MVT EVT = VT.getVectorElementType();
4076 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004077 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4078 }
4079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004081 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4082 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004083 V1IsSplat = isSplatVector(V1.getNode());
4084 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004085
4086 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004087 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4088 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4089 std::swap(V1IsSplat, V2IsSplat);
4090 std::swap(V1IsUndef, V2IsUndef);
4091 Commuted = true;
4092 }
4093
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004094 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004095 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 if (V2IsUndef) return V1;
4097 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4098 if (V2IsSplat) {
4099 // V2 is a splat, so the mask may be malformed. That is, it may point
4100 // to any V2 element. The instruction selectior won't like this. Get
4101 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004104 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4105 }
4106 return Op;
4107 }
4108
Gabor Greif1c80d112008-08-28 21:40:38 +00004109 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4110 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4111 X86::isUNPCKLMask(PermMask.getNode()) ||
4112 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 return Op;
4114
4115 if (V2IsSplat) {
4116 // Normalize mask so all entries that point to V2 points to its first
4117 // element then try to match unpck{h|l} again. If match, return a
4118 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004119 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004120 if (NewMask.getNode() != PermMask.getNode()) {
4121 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004122 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004124 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004125 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004126 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4127 }
4128 }
4129 }
4130
4131 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004132 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4134
4135 if (Commuted) {
4136 // Commute is back and try unpck* again.
4137 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004138 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4139 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4140 X86::isUNPCKLMask(PermMask.getNode()) ||
4141 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004142 return Op;
4143 }
4144
Evan Chengbf8b2c52008-04-05 00:30:36 +00004145 // Try PSHUF* first, then SHUFP*.
4146 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4147 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004148 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004149 if (V2.getOpcode() != ISD::UNDEF)
4150 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4151 DAG.getNode(ISD::UNDEF, VT), PermMask);
4152 return Op;
4153 }
4154
4155 if (!isMMX) {
4156 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004157 (X86::isPSHUFDMask(PermMask.getNode()) ||
4158 X86::isPSHUFHWMask(PermMask.getNode()) ||
4159 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004160 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004161 if (VT == MVT::v4f32) {
4162 RVT = MVT::v4i32;
4163 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4164 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4165 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4166 } else if (V2.getOpcode() != ISD::UNDEF)
4167 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4168 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4169 if (RVT != VT)
4170 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004171 return Op;
4172 }
4173
Evan Chengbf8b2c52008-04-05 00:30:36 +00004174 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004175 if (X86::isSHUFPMask(PermMask.getNode()) ||
4176 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004177 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 }
4179
Evan Cheng75184a92007-12-11 01:46:18 +00004180 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4181 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004182 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004183 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004184 return NewOp;
4185 }
4186
Evan Chengf50554e2008-07-22 21:13:36 +00004187 // Handle all 4 wide cases with a number of shuffles except for MMX.
4188 if (NumElems == 4 && !isMMX)
4189 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190
Dan Gohman8181bd12008-07-27 21:46:04 +00004191 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004192}
4193
Dan Gohman8181bd12008-07-27 21:46:04 +00004194SDValue
4195X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004196 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004197 MVT VT = Op.getValueType();
4198 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004200 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004201 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004202 DAG.getValueType(VT));
4203 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004204 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004205 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004206 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004207 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004208 DAG.getValueType(VT));
4209 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004210 } else if (VT == MVT::f32) {
4211 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4212 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004213 // result has a single use which is a store or a bitcast to i32. And in
4214 // the case of a store, it's not worth it if the index is a constant 0,
4215 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004216 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004217 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004218 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004219 if ((User->getOpcode() != ISD::STORE ||
4220 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4221 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004222 (User->getOpcode() != ISD::BIT_CONVERT ||
4223 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004224 return SDValue();
4225 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004226 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4227 Op.getOperand(1));
4228 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004229 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004230 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004231}
4232
4233
Dan Gohman8181bd12008-07-27 21:46:04 +00004234SDValue
4235X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004237 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004238
Evan Cheng6c249332008-03-24 21:52:23 +00004239 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004240 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004241 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004242 return Res;
4243 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004244
Duncan Sands92c43912008-06-06 12:08:01 +00004245 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004247 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004248 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004249 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004250 if (Idx == 0)
4251 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4252 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4253 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4254 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004256 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004259 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 DAG.getValueType(VT));
4261 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004262 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004263 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004264 if (Idx == 0)
4265 return Op;
4266 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004267 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004268 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004269 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004270 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004271 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004272 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004273 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004274 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004275 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004276 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004277 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004278 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004279 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4281 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4282 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004283 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004284 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004285 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4286 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4287 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004288 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 if (Idx == 0)
4290 return Op;
4291
4292 // UNPCKHPD the element to the lowest double word, then movsd.
4293 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4294 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004295 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004296 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004297 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004298 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004299 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004300 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004301 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004302 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4304 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4305 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004306 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 }
4308
Dan Gohman8181bd12008-07-27 21:46:04 +00004309 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310}
4311
Dan Gohman8181bd12008-07-27 21:46:04 +00004312SDValue
4313X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004314 MVT VT = Op.getValueType();
4315 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004316
Dan Gohman8181bd12008-07-27 21:46:04 +00004317 SDValue N0 = Op.getOperand(0);
4318 SDValue N1 = Op.getOperand(1);
4319 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004320
Dan Gohman5a7af042008-08-14 22:53:18 +00004321 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4322 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004323 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004324 : X86ISD::PINSRW;
4325 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4326 // argument.
4327 if (N1.getValueType() != MVT::i32)
4328 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4329 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004330 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004331 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004332 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004333 // Bits [7:6] of the constant are the source select. This will always be
4334 // zero here. The DAG Combiner may combine an extract_elt index into these
4335 // bits. For example (insert (extract, 3), 2) could be matched by putting
4336 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4337 // Bits [5:4] of the constant are the destination select. This is the
4338 // value of the incoming immediate.
4339 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4340 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004341 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004342 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4343 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004345}
4346
Dan Gohman8181bd12008-07-27 21:46:04 +00004347SDValue
4348X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004349 MVT VT = Op.getValueType();
4350 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004351
4352 if (Subtarget->hasSSE41())
4353 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4354
Evan Chenge12a7eb2007-12-12 07:55:34 +00004355 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004356 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004357
Dan Gohman8181bd12008-07-27 21:46:04 +00004358 SDValue N0 = Op.getOperand(0);
4359 SDValue N1 = Op.getOperand(1);
4360 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004361
Duncan Sands92c43912008-06-06 12:08:01 +00004362 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004363 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4364 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004365 if (N1.getValueType() != MVT::i32)
4366 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4367 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004368 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004369 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004370 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004371 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004372}
4373
Dan Gohman8181bd12008-07-27 21:46:04 +00004374SDValue
4375X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004376 if (Op.getValueType() == MVT::v2f32)
4377 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4378 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4379 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4380 Op.getOperand(0))));
4381
Dan Gohman8181bd12008-07-27 21:46:04 +00004382 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004383 MVT VT = MVT::v2i32;
4384 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004385 default: break;
4386 case MVT::v16i8:
4387 case MVT::v8i16:
4388 VT = MVT::v4i32;
4389 break;
4390 }
4391 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4392 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004393}
4394
Bill Wendlingfef06052008-09-16 21:48:12 +00004395// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4396// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4397// one of the above mentioned nodes. It has to be wrapped because otherwise
4398// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4399// be used to form addressing mode. These wrapped nodes will be selected
4400// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004401SDValue
4402X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004404 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004405 getPointerTy(),
4406 CP->getAlignment());
4407 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4408 // With PIC, the address is actually $g + Offset.
4409 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4410 !Subtarget->isPICStyleRIPRel()) {
4411 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4412 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4413 Result);
4414 }
4415
4416 return Result;
4417}
4418
Dan Gohman8181bd12008-07-27 21:46:04 +00004419SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004420X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004421 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004422 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004423 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4424 bool ExtraLoadRequired =
4425 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4426
4427 // Create the TargetGlobalAddress node, folding in the constant
4428 // offset if it is legal.
4429 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004430 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004431 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4432 Offset = 0;
4433 } else
4434 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004436
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004438 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004439 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4440 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4441 Result);
4442 }
4443
4444 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4445 // load the value at address GV, not the value of GV itself. This means that
4446 // the GlobalAddress must be in the base or index register of the address, not
4447 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4448 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004449 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004450 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004451 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004452
Dan Gohman36322c72008-10-18 02:06:02 +00004453 // If there was a non-zero offset that we didn't fold, create an explicit
4454 // addition for it.
4455 if (Offset != 0)
4456 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4457 DAG.getConstant(Offset, getPointerTy()));
4458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459 return Result;
4460}
4461
Evan Cheng7f250d62008-09-24 00:05:32 +00004462SDValue
4463X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4464 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004465 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4466 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004467}
4468
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004469// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004470static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004471LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004472 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004473 SDValue InFlag;
4474 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004475 DAG.getNode(X86ISD::GlobalBaseReg,
4476 PtrVT), InFlag);
4477 InFlag = Chain.getValue(1);
4478
4479 // emit leal symbol@TLSGD(,%ebx,1), %eax
4480 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004481 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004482 GA->getValueType(0),
4483 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004484 SDValue Ops[] = { Chain, TGA, InFlag };
4485 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004486 InFlag = Result.getValue(2);
4487 Chain = Result.getValue(1);
4488
4489 // call ___tls_get_addr. This function receives its argument in
4490 // the register EAX.
4491 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4492 InFlag = Chain.getValue(1);
4493
4494 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004495 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004496 DAG.getTargetExternalSymbol("___tls_get_addr",
4497 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 DAG.getRegister(X86::EAX, PtrVT),
4499 DAG.getRegister(X86::EBX, PtrVT),
4500 InFlag };
4501 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4502 InFlag = Chain.getValue(1);
4503
4504 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4505}
4506
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004507// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004508static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004509LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004510 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004512
4513 // emit leaq symbol@TLSGD(%rip), %rdi
4514 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004515 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004516 GA->getValueType(0),
4517 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004518 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4519 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004520 Chain = Result.getValue(1);
4521 InFlag = Result.getValue(2);
4522
aslb204cd52008-08-16 12:58:29 +00004523 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004524 // the register RDI.
4525 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4526 InFlag = Chain.getValue(1);
4527
4528 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004529 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004530 DAG.getTargetExternalSymbol("__tls_get_addr",
4531 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004532 DAG.getRegister(X86::RDI, PtrVT),
4533 InFlag };
4534 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4535 InFlag = Chain.getValue(1);
4536
4537 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4538}
4539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004540// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4541// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004542static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004543 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004545 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4547 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549 GA->getValueType(0),
4550 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004551 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552
4553 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004554 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004555 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556
4557 // The address of the thread local variable is the add of the thread
4558 // pointer with the offset of the variable.
4559 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4560}
4561
Dan Gohman8181bd12008-07-27 21:46:04 +00004562SDValue
4563X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004564 // TODO: implement the "local dynamic" model
4565 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004566 assert(Subtarget->isTargetELF() &&
4567 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004568 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4569 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4570 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004571 if (Subtarget->is64Bit()) {
4572 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4573 } else {
4574 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4575 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4576 else
4577 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579}
4580
Dan Gohman8181bd12008-07-27 21:46:04 +00004581SDValue
4582X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004583 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4584 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004585 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4586 // With PIC, the address is actually $g + Offset.
4587 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4588 !Subtarget->isPICStyleRIPRel()) {
4589 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4590 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4591 Result);
4592 }
4593
4594 return Result;
4595}
4596
Dan Gohman8181bd12008-07-27 21:46:04 +00004597SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004599 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004600 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4601 // With PIC, the address is actually $g + Offset.
4602 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4603 !Subtarget->isPICStyleRIPRel()) {
4604 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4605 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4606 Result);
4607 }
4608
4609 return Result;
4610}
4611
Chris Lattner62814a32007-10-17 06:02:13 +00004612/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4613/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004614SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004615 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004616 MVT VT = Op.getValueType();
4617 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004618 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004619 SDValue ShOpLo = Op.getOperand(0);
4620 SDValue ShOpHi = Op.getOperand(1);
4621 SDValue ShAmt = Op.getOperand(2);
4622 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004623 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4624 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004625
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004627 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004628 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4629 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004630 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004631 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4632 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004633 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004634
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004636 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004637 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004638 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004639
Dan Gohman8181bd12008-07-27 21:46:04 +00004640 SDValue Hi, Lo;
4641 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4642 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4643 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004644
Chris Lattner62814a32007-10-17 06:02:13 +00004645 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004646 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4647 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004648 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004649 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4650 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004651 }
4652
Dan Gohman8181bd12008-07-27 21:46:04 +00004653 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004654 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004655}
4656
Dan Gohman8181bd12008-07-27 21:46:04 +00004657SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004658 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004659 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004660 "Unknown SINT_TO_FP to lower!");
4661
4662 // These are really Legal; caller falls through into that case.
4663 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004664 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004665 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4666 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004667 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004668
Duncan Sands92c43912008-06-06 12:08:01 +00004669 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004670 MachineFunction &MF = DAG.getMachineFunction();
4671 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004672 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4673 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004674 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004675 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676
4677 // Build the FILD
4678 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004679 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004680 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004681 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4682 else
4683 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004684 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 Ops.push_back(Chain);
4686 Ops.push_back(StackSlot);
4687 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004688 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004689 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690
Dale Johannesen2fc20782007-09-14 22:26:36 +00004691 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004693 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694
4695 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4696 // shouldn't be necessary except that RFP cannot be live across
4697 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4698 MachineFunction &MF = DAG.getMachineFunction();
4699 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004700 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004701 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004702 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004703 Ops.push_back(Chain);
4704 Ops.push_back(Result);
4705 Ops.push_back(StackSlot);
4706 Ops.push_back(DAG.getValueType(Op.getValueType()));
4707 Ops.push_back(InFlag);
4708 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004709 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004710 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004711 }
4712
4713 return Result;
4714}
4715
Dale Johannesena359b8b2008-10-21 20:50:01 +00004716SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4717 MVT SrcVT = Op.getOperand(0).getValueType();
4718 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4719
4720 // We only handle SSE2 f64 target here; caller can handle the rest.
4721 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4722 return SDValue();
4723
Dale Johannesenfb019af2008-10-21 23:07:49 +00004724 // This algorithm is not obvious. Here it is in C code, more or less:
4725/*
4726 double uint64_to_double( uint32_t hi, uint32_t lo )
4727 {
4728 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4729 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4730
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004731 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004732 __m128i xh = _mm_cvtsi32_si128( hi );
4733 __m128i xl = _mm_cvtsi32_si128( lo );
4734
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004735 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004736 __m128i x = _mm_unpacklo_epi32( xh, xl );
4737 __m128d d;
4738 double sd;
4739
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004740 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004741 // right magnitude
4742 x = _mm_unpacklo_epi32( x, exp );
4743
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004744 // subtract away the biases to deal with the IEEE-754 double precision
4745 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004746 d = _mm_sub_pd( (__m128d) x, bias );
4747
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004748 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004749 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004750 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004751 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4752 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004753 // store doesn't really need to be here (except maybe to zero the other
4754 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004755 return sd;
4756 }
4757*/
4758
Dale Johannesena359b8b2008-10-21 20:50:01 +00004759 // Build some magic constants.
4760 std::vector<Constant*>CV0;
4761 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4762 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4763 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4764 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4765 Constant *C0 = ConstantVector::get(CV0);
4766 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4767
4768 std::vector<Constant*>CV1;
4769 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4770 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4771 Constant *C1 = ConstantVector::get(CV1);
4772 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4773
4774 SmallVector<SDValue, 4> MaskVec;
4775 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4776 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4777 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4778 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4779 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4780 MaskVec.size());
4781 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004782 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4783 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4784 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004785 MaskVec2.size());
4786
4787 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004788 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4789 Op.getOperand(0),
4790 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004791 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004792 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4793 Op.getOperand(0),
4794 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004795 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4796 XR1, XR2, UnpcklMask);
4797 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4798 PseudoSourceValue::getConstantPool(), 0, false, 16);
4799 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4800 Unpck1, CLod0, UnpcklMask);
4801 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4802 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4803 PseudoSourceValue::getConstantPool(), 0, false, 16);
4804 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4805 // Add the halves; easiest way is to swap them into another reg first.
4806 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4807 Sub, Sub, ShufMask);
4808 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4809 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4810 DAG.getIntPtrConstant(0));
4811}
4812
Dan Gohman8181bd12008-07-27 21:46:04 +00004813std::pair<SDValue,SDValue> X86TargetLowering::
4814FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004815 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4816 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004817 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004818
Dale Johannesen2fc20782007-09-14 22:26:36 +00004819 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004820 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004821 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004822 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004823 if (Subtarget->is64Bit() &&
4824 Op.getValueType() == MVT::i64 &&
4825 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004826 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004827
Evan Cheng05441e62007-10-15 20:11:21 +00004828 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4829 // stack slot.
4830 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004831 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004832 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004833 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004835 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004836 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4837 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4838 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4839 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004840 }
4841
Dan Gohman8181bd12008-07-27 21:46:04 +00004842 SDValue Chain = DAG.getEntryNode();
4843 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004844 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004846 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004847 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004848 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004849 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004850 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4851 };
4852 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4853 Chain = Value.getValue(1);
4854 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4855 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4856 }
4857
4858 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004859 SDValue Ops[] = { Chain, Value, StackSlot };
4860 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004862 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863}
4864
Dan Gohman8181bd12008-07-27 21:46:04 +00004865SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4866 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4867 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004868 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004869
4870 // Load the result.
4871 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4872}
4873
Dan Gohman8181bd12008-07-27 21:46:04 +00004874SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004875 MVT VT = Op.getValueType();
4876 MVT EltVT = VT;
4877 if (VT.isVector())
4878 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004879 std::vector<Constant*> CV;
4880 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004881 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004882 CV.push_back(C);
4883 CV.push_back(C);
4884 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004885 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 CV.push_back(C);
4887 CV.push_back(C);
4888 CV.push_back(C);
4889 CV.push_back(C);
4890 }
Dan Gohman11821702007-07-27 17:16:43 +00004891 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004892 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4893 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004894 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004895 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4897}
4898
Dan Gohman8181bd12008-07-27 21:46:04 +00004899SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004900 MVT VT = Op.getValueType();
4901 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004902 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004903 if (VT.isVector()) {
4904 EltVT = VT.getVectorElementType();
4905 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004906 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004907 std::vector<Constant*> CV;
4908 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004909 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004910 CV.push_back(C);
4911 CV.push_back(C);
4912 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004913 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004914 CV.push_back(C);
4915 CV.push_back(C);
4916 CV.push_back(C);
4917 CV.push_back(C);
4918 }
Dan Gohman11821702007-07-27 17:16:43 +00004919 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004920 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4921 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004922 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004923 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004924 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004925 return DAG.getNode(ISD::BIT_CONVERT, VT,
4926 DAG.getNode(ISD::XOR, MVT::v2i64,
4927 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4928 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4929 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004930 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4931 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004932}
4933
Dan Gohman8181bd12008-07-27 21:46:04 +00004934SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4935 SDValue Op0 = Op.getOperand(0);
4936 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004937 MVT VT = Op.getValueType();
4938 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004939
4940 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004941 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004942 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4943 SrcVT = VT;
4944 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004945 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004946 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004947 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004948 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004949 }
4950
4951 // At this point the operands and the result should have the same
4952 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004953
4954 // First get the sign bit of second operand.
4955 std::vector<Constant*> CV;
4956 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004957 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4958 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004959 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004960 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4961 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4962 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4963 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004964 }
Dan Gohman11821702007-07-27 17:16:43 +00004965 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004966 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4967 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004968 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004969 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004970 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971
4972 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004973 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004974 // Op0 is MVT::f32, Op1 is MVT::f64.
4975 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4976 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4977 DAG.getConstant(32, MVT::i32));
4978 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4979 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004980 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004981 }
4982
4983 // Clear first operand sign bit.
4984 CV.clear();
4985 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004986 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4987 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004988 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004989 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993 }
Dan Gohman11821702007-07-27 17:16:43 +00004994 C = ConstantVector::get(CV);
4995 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004996 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004997 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004998 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000
5001 // Or the value with the sign bit.
5002 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5003}
5004
Dan Gohman8181bd12008-07-27 21:46:04 +00005005SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005006 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005007 SDValue Cond;
5008 SDValue Op0 = Op.getOperand(0);
5009 SDValue Op1 = Op.getOperand(1);
5010 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00005011 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00005012 unsigned X86CC;
5013
Evan Cheng950aac02007-09-25 01:57:46 +00005014 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00005015 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00005016 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5017 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00005018 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00005019 }
Evan Cheng950aac02007-09-25 01:57:46 +00005020
Evan Cheng71343822008-10-15 02:05:31 +00005021 assert(0 && "Illegal SetCC!");
5022 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00005023}
5024
Dan Gohman8181bd12008-07-27 21:46:04 +00005025SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5026 SDValue Cond;
5027 SDValue Op0 = Op.getOperand(0);
5028 SDValue Op1 = Op.getOperand(1);
5029 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005030 MVT VT = Op.getValueType();
5031 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5032 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5033
5034 if (isFP) {
5035 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005036 MVT VT0 = Op0.getValueType();
5037 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5038 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005039 bool Swap = false;
5040
5041 switch (SetCCOpcode) {
5042 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005043 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005044 case ISD::SETEQ: SSECC = 0; break;
5045 case ISD::SETOGT:
5046 case ISD::SETGT: Swap = true; // Fallthrough
5047 case ISD::SETLT:
5048 case ISD::SETOLT: SSECC = 1; break;
5049 case ISD::SETOGE:
5050 case ISD::SETGE: Swap = true; // Fallthrough
5051 case ISD::SETLE:
5052 case ISD::SETOLE: SSECC = 2; break;
5053 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005054 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005055 case ISD::SETNE: SSECC = 4; break;
5056 case ISD::SETULE: Swap = true;
5057 case ISD::SETUGE: SSECC = 5; break;
5058 case ISD::SETULT: Swap = true;
5059 case ISD::SETUGT: SSECC = 6; break;
5060 case ISD::SETO: SSECC = 7; break;
5061 }
5062 if (Swap)
5063 std::swap(Op0, Op1);
5064
Nate Begeman6357f9d2008-07-25 19:05:58 +00005065 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005066 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005067 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005068 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005069 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5070 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5071 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5072 }
5073 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005074 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005075 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5076 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5077 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5078 }
5079 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005080 }
5081 // Handle all other FP comparisons here.
5082 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5083 }
5084
5085 // We are handling one of the integer comparisons here. Since SSE only has
5086 // GT and EQ comparisons for integer, swapping operands and multiple
5087 // operations may be required for some comparisons.
5088 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5089 bool Swap = false, Invert = false, FlipSigns = false;
5090
5091 switch (VT.getSimpleVT()) {
5092 default: break;
5093 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5094 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5095 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5096 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5097 }
5098
5099 switch (SetCCOpcode) {
5100 default: break;
5101 case ISD::SETNE: Invert = true;
5102 case ISD::SETEQ: Opc = EQOpc; break;
5103 case ISD::SETLT: Swap = true;
5104 case ISD::SETGT: Opc = GTOpc; break;
5105 case ISD::SETGE: Swap = true;
5106 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5107 case ISD::SETULT: Swap = true;
5108 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5109 case ISD::SETUGE: Swap = true;
5110 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5111 }
5112 if (Swap)
5113 std::swap(Op0, Op1);
5114
5115 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5116 // bits of the inputs before performing those operations.
5117 if (FlipSigns) {
5118 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005119 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5120 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5121 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005122 SignBits.size());
5123 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5124 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5125 }
5126
Dan Gohman8181bd12008-07-27 21:46:04 +00005127 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005128
5129 // If the logical-not of the result is required, perform that now.
5130 if (Invert) {
5131 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005132 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5133 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5134 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005135 NegOnes.size());
5136 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5137 }
5138 return Result;
5139}
Evan Cheng950aac02007-09-25 01:57:46 +00005140
Evan Chengd580f022008-12-03 08:38:43 +00005141// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
5142static bool isX86LogicalCmp(unsigned Opc) {
5143 return Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI;
5144}
5145
Dan Gohman8181bd12008-07-27 21:46:04 +00005146SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005147 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005148 SDValue Cond = Op.getOperand(0);
5149 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005150
5151 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005152 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153
Evan Cheng50d37ab2007-10-08 22:16:29 +00005154 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5155 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005156 if (Cond.getOpcode() == X86ISD::SETCC) {
5157 CC = Cond.getOperand(0);
5158
Dan Gohman8181bd12008-07-27 21:46:04 +00005159 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005161 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005162
Evan Cheng50d37ab2007-10-08 22:16:29 +00005163 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005164 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005165 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005166 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005167
Evan Chengd580f022008-12-03 08:38:43 +00005168 if (isX86LogicalCmp(Opc) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005169 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005170 addTest = false;
5171 }
5172 }
5173
5174 if (addTest) {
5175 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005176 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005177 }
5178
Duncan Sands92c43912008-06-06 12:08:01 +00005179 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005180 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005181 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005182 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5183 // condition is true.
5184 Ops.push_back(Op.getOperand(2));
5185 Ops.push_back(Op.getOperand(1));
5186 Ops.push_back(CC);
5187 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005188 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005189}
5190
Evan Chengd580f022008-12-03 08:38:43 +00005191// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5192// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5193// from the AND / OR.
5194static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5195 Opc = Op.getOpcode();
5196 if (Opc != ISD::OR && Opc != ISD::AND)
5197 return false;
5198 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5199 Op.getOperand(0).hasOneUse() &&
5200 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5201 Op.getOperand(1).hasOneUse());
5202}
5203
Dan Gohman8181bd12008-07-27 21:46:04 +00005204SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005205 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005206 SDValue Chain = Op.getOperand(0);
5207 SDValue Cond = Op.getOperand(1);
5208 SDValue Dest = Op.getOperand(2);
5209 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210
5211 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005212 Cond = LowerSETCC(Cond, DAG);
Bill Wendlingae034ed2008-12-12 00:56:36 +00005213 else if (Cond.getOpcode() == X86ISD::ADD ||
5214 Cond.getOpcode() == X86ISD::SUB ||
5215 Cond.getOpcode() == X86ISD::MUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00005216 Cond = LowerXALUO(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217
Evan Cheng50d37ab2007-10-08 22:16:29 +00005218 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5219 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005220 if (Cond.getOpcode() == X86ISD::SETCC) {
5221 CC = Cond.getOperand(0);
5222
Dan Gohman8181bd12008-07-27 21:46:04 +00005223 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005224 unsigned Opc = Cmp.getOpcode();
Evan Chengd580f022008-12-03 08:38:43 +00005225 if (isX86LogicalCmp(Opc)) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005226 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005227 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00005228 } else {
Evan Chengd580f022008-12-03 08:38:43 +00005229 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00005230 default: break;
5231 case X86::COND_O:
5232 case X86::COND_C:
Evan Chengd580f022008-12-03 08:38:43 +00005233 // These can only come from an arithmetic instruction with overflow, e.g.
5234 // SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00005235 Cond = Cond.getNode()->getOperand(1);
5236 addTest = false;
5237 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00005238 }
Evan Cheng950aac02007-09-25 01:57:46 +00005239 }
Evan Chengd580f022008-12-03 08:38:43 +00005240 } else {
5241 unsigned CondOpc;
5242 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5243 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5244 unsigned Opc = Cmp.getOpcode();
5245 if (CondOpc == ISD::OR) {
5246 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5247 // two branches instead of an explicit OR instruction with a
5248 // separate test.
5249 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5250 isX86LogicalCmp(Opc)) {
5251 CC = Cond.getOperand(0).getOperand(0);
5252 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5253 Chain, Dest, CC, Cmp);
5254 CC = Cond.getOperand(1).getOperand(0);
5255 Cond = Cmp;
5256 addTest = false;
5257 }
5258 } else { // ISD::AND
5259 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5260 // two branches instead of an explicit AND instruction with a
5261 // separate test. However, we only do this if this block doesn't
5262 // have a fall-through edge, because this requires an explicit
5263 // jmp when the condition is false.
5264 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5265 isX86LogicalCmp(Opc) &&
5266 Op.getNode()->hasOneUse()) {
5267 X86::CondCode CCode =
5268 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5269 CCode = X86::GetOppositeBranchCondition(CCode);
5270 CC = DAG.getConstant(CCode, MVT::i8);
5271 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5272 // Look for an unconditional branch following this conditional branch.
5273 // We need this because we need to reverse the successors in order
5274 // to implement FCMP_OEQ.
5275 if (User.getOpcode() == ISD::BR) {
5276 SDValue FalseBB = User.getOperand(1);
5277 SDValue NewBR =
5278 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5279 assert(NewBR == User);
5280 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005281
Evan Chengd580f022008-12-03 08:38:43 +00005282 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5283 Chain, Dest, CC, Cmp);
5284 X86::CondCode CCode =
5285 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5286 CCode = X86::GetOppositeBranchCondition(CCode);
5287 CC = DAG.getConstant(CCode, MVT::i8);
5288 Cond = Cmp;
5289 addTest = false;
5290 }
5291 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005292 }
5293 }
Evan Cheng950aac02007-09-25 01:57:46 +00005294 }
5295
5296 if (addTest) {
5297 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005298 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005299 }
Evan Cheng621216e2007-09-29 00:00:36 +00005300 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005301 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005302}
5303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005304
5305// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5306// Calls to _alloca is needed to probe the stack when allocating more than 4k
5307// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5308// that the guard pages used by the OS virtual memory manager are allocated in
5309// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005310SDValue
5311X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 SelectionDAG &DAG) {
5313 assert(Subtarget->isTargetCygMing() &&
5314 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005315
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005316 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005317 SDValue Chain = Op.getOperand(0);
5318 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 // FIXME: Ensure alignment here
5320
Dan Gohman8181bd12008-07-27 21:46:04 +00005321 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005322
Duncan Sands92c43912008-06-06 12:08:01 +00005323 MVT IntPtr = getPointerTy();
5324 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005326 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005328 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5329 Flag = Chain.getValue(1);
5330
5331 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005332 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005333 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005334 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005335 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005337 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 Flag = Chain.getValue(1);
5339
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005340 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005341 DAG.getIntPtrConstant(0, true),
5342 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005343 Flag);
5344
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005346
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005348 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005349}
5350
Dan Gohman8181bd12008-07-27 21:46:04 +00005351SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005352X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005353 SDValue Chain,
5354 SDValue Dst, SDValue Src,
5355 SDValue Size, unsigned Align,
5356 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005357 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005358 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005359
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005360 // If not DWORD aligned or size is more than the threshold, call the library.
5361 // The libc version is likely to be faster for these cases. It can use the
5362 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005363 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005364 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005365 ConstantSize->getZExtValue() >
5366 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005367 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005368
5369 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005370 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005371
Bill Wendling4b2e3782008-10-01 00:59:58 +00005372 if (const char *bzeroEntry = V &&
5373 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5374 MVT IntPtr = getPointerTy();
5375 const Type *IntPtrTy = TD->getIntPtrType();
5376 TargetLowering::ArgListTy Args;
5377 TargetLowering::ArgListEntry Entry;
5378 Entry.Node = Dst;
5379 Entry.Ty = IntPtrTy;
5380 Args.push_back(Entry);
5381 Entry.Node = Size;
5382 Args.push_back(Entry);
5383 std::pair<SDValue,SDValue> CallResult =
5384 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5385 CallingConv::C, false,
5386 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5387 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005388 }
5389
Dan Gohmane8b391e2008-04-12 04:36:06 +00005390 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005391 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005392 }
5393
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005394 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005395 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005396 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005397 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005398 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 unsigned BytesLeft = 0;
5400 bool TwoRepStos = false;
5401 if (ValC) {
5402 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005403 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005404
5405 // If the value is a constant, then we can potentially use larger sets.
5406 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005407 case 2: // WORD aligned
5408 AVT = MVT::i16;
5409 ValReg = X86::AX;
5410 Val = (Val << 8) | Val;
5411 break;
5412 case 0: // DWORD aligned
5413 AVT = MVT::i32;
5414 ValReg = X86::EAX;
5415 Val = (Val << 8) | Val;
5416 Val = (Val << 16) | Val;
5417 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5418 AVT = MVT::i64;
5419 ValReg = X86::RAX;
5420 Val = (Val << 32) | Val;
5421 }
5422 break;
5423 default: // Byte aligned
5424 AVT = MVT::i8;
5425 ValReg = X86::AL;
5426 Count = DAG.getIntPtrConstant(SizeVal);
5427 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 }
5429
Duncan Sandsec142ee2008-06-08 20:54:56 +00005430 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005431 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005432 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5433 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005434 }
5435
5436 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5437 InFlag);
5438 InFlag = Chain.getValue(1);
5439 } else {
5440 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005441 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005442 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443 InFlag = Chain.getValue(1);
5444 }
5445
5446 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5447 Count, InFlag);
5448 InFlag = Chain.getValue(1);
5449 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005450 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 InFlag = Chain.getValue(1);
5452
5453 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005454 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 Ops.push_back(Chain);
5456 Ops.push_back(DAG.getValueType(AVT));
5457 Ops.push_back(InFlag);
5458 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5459
5460 if (TwoRepStos) {
5461 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005462 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005463 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005464 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005465 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5466 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5467 Left, InFlag);
5468 InFlag = Chain.getValue(1);
5469 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5470 Ops.clear();
5471 Ops.push_back(Chain);
5472 Ops.push_back(DAG.getValueType(MVT::i8));
5473 Ops.push_back(InFlag);
5474 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5475 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005476 // Handle the last 1 - 7 bytes.
5477 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005478 MVT AddrVT = Dst.getValueType();
5479 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005480
5481 Chain = DAG.getMemset(Chain,
5482 DAG.getNode(ISD::ADD, AddrVT, Dst,
5483 DAG.getConstant(Offset, AddrVT)),
5484 Src,
5485 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005486 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005487 }
5488
Dan Gohmane8b391e2008-04-12 04:36:06 +00005489 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005490 return Chain;
5491}
5492
Dan Gohman8181bd12008-07-27 21:46:04 +00005493SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005494X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005495 SDValue Chain, SDValue Dst, SDValue Src,
5496 SDValue Size, unsigned Align,
5497 bool AlwaysInline,
5498 const Value *DstSV, uint64_t DstSVOff,
5499 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005500 // This requires the copy size to be a constant, preferrably
5501 // within a subtarget-specific limit.
5502 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5503 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005504 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005505 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005506 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005508
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005509 /// If not DWORD aligned, call the library.
5510 if ((Align & 3) != 0)
5511 return SDValue();
5512
5513 // DWORD aligned
5514 MVT AVT = MVT::i32;
5515 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005516 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005517
Duncan Sands92c43912008-06-06 12:08:01 +00005518 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005519 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005520 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005521 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005522
Dan Gohman8181bd12008-07-27 21:46:04 +00005523 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5525 Count, InFlag);
5526 InFlag = Chain.getValue(1);
5527 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005528 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005529 InFlag = Chain.getValue(1);
5530 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005531 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005532 InFlag = Chain.getValue(1);
5533
5534 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005535 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 Ops.push_back(Chain);
5537 Ops.push_back(DAG.getValueType(AVT));
5538 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005539 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540
Dan Gohman8181bd12008-07-27 21:46:04 +00005541 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005542 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005543 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005544 // Handle the last 1 - 7 bytes.
5545 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005546 MVT DstVT = Dst.getValueType();
5547 MVT SrcVT = Src.getValueType();
5548 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005549 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005550 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005551 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005552 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005553 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005554 DAG.getConstant(BytesLeft, SizeVT),
5555 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005556 DstSV, DstSVOff + Offset,
5557 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 }
5559
Dan Gohmane8b391e2008-04-12 04:36:06 +00005560 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005561}
5562
Dan Gohman8181bd12008-07-27 21:46:04 +00005563SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005564 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005565
5566 if (!Subtarget->is64Bit()) {
5567 // vastart just stores the address of the VarArgsFrameIndex slot into the
5568 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005569 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005570 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 }
5572
5573 // __va_list_tag:
5574 // gp_offset (0 - 6 * 8)
5575 // fp_offset (48 - 48 + 8 * 16)
5576 // overflow_arg_area (point to parameters coming in memory).
5577 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005578 SmallVector<SDValue, 8> MemOps;
5579 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005581 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005582 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005583 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005584 MemOps.push_back(Store);
5585
5586 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005587 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 Store = DAG.getStore(Op.getOperand(0),
5589 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005590 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591 MemOps.push_back(Store);
5592
5593 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005594 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005596 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005597 MemOps.push_back(Store);
5598
5599 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005600 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005601 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005602 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603 MemOps.push_back(Store);
5604 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5605}
5606
Dan Gohman8181bd12008-07-27 21:46:04 +00005607SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005608 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5609 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005610 SDValue Chain = Op.getOperand(0);
5611 SDValue SrcPtr = Op.getOperand(1);
5612 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005613
5614 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5615 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005616 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005617}
5618
Dan Gohman8181bd12008-07-27 21:46:04 +00005619SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005621 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue Chain = Op.getOperand(0);
5623 SDValue DstPtr = Op.getOperand(1);
5624 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005625 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5626 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627
Dan Gohman840ff5c2008-04-18 20:55:41 +00005628 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5629 DAG.getIntPtrConstant(24), 8, false,
5630 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005631}
5632
Dan Gohman8181bd12008-07-27 21:46:04 +00005633SDValue
5634X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005635 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005637 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005638 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639 case Intrinsic::x86_sse_comieq_ss:
5640 case Intrinsic::x86_sse_comilt_ss:
5641 case Intrinsic::x86_sse_comile_ss:
5642 case Intrinsic::x86_sse_comigt_ss:
5643 case Intrinsic::x86_sse_comige_ss:
5644 case Intrinsic::x86_sse_comineq_ss:
5645 case Intrinsic::x86_sse_ucomieq_ss:
5646 case Intrinsic::x86_sse_ucomilt_ss:
5647 case Intrinsic::x86_sse_ucomile_ss:
5648 case Intrinsic::x86_sse_ucomigt_ss:
5649 case Intrinsic::x86_sse_ucomige_ss:
5650 case Intrinsic::x86_sse_ucomineq_ss:
5651 case Intrinsic::x86_sse2_comieq_sd:
5652 case Intrinsic::x86_sse2_comilt_sd:
5653 case Intrinsic::x86_sse2_comile_sd:
5654 case Intrinsic::x86_sse2_comigt_sd:
5655 case Intrinsic::x86_sse2_comige_sd:
5656 case Intrinsic::x86_sse2_comineq_sd:
5657 case Intrinsic::x86_sse2_ucomieq_sd:
5658 case Intrinsic::x86_sse2_ucomilt_sd:
5659 case Intrinsic::x86_sse2_ucomile_sd:
5660 case Intrinsic::x86_sse2_ucomigt_sd:
5661 case Intrinsic::x86_sse2_ucomige_sd:
5662 case Intrinsic::x86_sse2_ucomineq_sd: {
5663 unsigned Opc = 0;
5664 ISD::CondCode CC = ISD::SETCC_INVALID;
5665 switch (IntNo) {
5666 default: break;
5667 case Intrinsic::x86_sse_comieq_ss:
5668 case Intrinsic::x86_sse2_comieq_sd:
5669 Opc = X86ISD::COMI;
5670 CC = ISD::SETEQ;
5671 break;
5672 case Intrinsic::x86_sse_comilt_ss:
5673 case Intrinsic::x86_sse2_comilt_sd:
5674 Opc = X86ISD::COMI;
5675 CC = ISD::SETLT;
5676 break;
5677 case Intrinsic::x86_sse_comile_ss:
5678 case Intrinsic::x86_sse2_comile_sd:
5679 Opc = X86ISD::COMI;
5680 CC = ISD::SETLE;
5681 break;
5682 case Intrinsic::x86_sse_comigt_ss:
5683 case Intrinsic::x86_sse2_comigt_sd:
5684 Opc = X86ISD::COMI;
5685 CC = ISD::SETGT;
5686 break;
5687 case Intrinsic::x86_sse_comige_ss:
5688 case Intrinsic::x86_sse2_comige_sd:
5689 Opc = X86ISD::COMI;
5690 CC = ISD::SETGE;
5691 break;
5692 case Intrinsic::x86_sse_comineq_ss:
5693 case Intrinsic::x86_sse2_comineq_sd:
5694 Opc = X86ISD::COMI;
5695 CC = ISD::SETNE;
5696 break;
5697 case Intrinsic::x86_sse_ucomieq_ss:
5698 case Intrinsic::x86_sse2_ucomieq_sd:
5699 Opc = X86ISD::UCOMI;
5700 CC = ISD::SETEQ;
5701 break;
5702 case Intrinsic::x86_sse_ucomilt_ss:
5703 case Intrinsic::x86_sse2_ucomilt_sd:
5704 Opc = X86ISD::UCOMI;
5705 CC = ISD::SETLT;
5706 break;
5707 case Intrinsic::x86_sse_ucomile_ss:
5708 case Intrinsic::x86_sse2_ucomile_sd:
5709 Opc = X86ISD::UCOMI;
5710 CC = ISD::SETLE;
5711 break;
5712 case Intrinsic::x86_sse_ucomigt_ss:
5713 case Intrinsic::x86_sse2_ucomigt_sd:
5714 Opc = X86ISD::UCOMI;
5715 CC = ISD::SETGT;
5716 break;
5717 case Intrinsic::x86_sse_ucomige_ss:
5718 case Intrinsic::x86_sse2_ucomige_sd:
5719 Opc = X86ISD::UCOMI;
5720 CC = ISD::SETGE;
5721 break;
5722 case Intrinsic::x86_sse_ucomineq_ss:
5723 case Intrinsic::x86_sse2_ucomineq_sd:
5724 Opc = X86ISD::UCOMI;
5725 CC = ISD::SETNE;
5726 break;
5727 }
5728
5729 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005730 SDValue LHS = Op.getOperand(1);
5731 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5733
Dan Gohman8181bd12008-07-27 21:46:04 +00005734 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5735 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005736 DAG.getConstant(X86CC, MVT::i8), Cond);
5737 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005738 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005739
5740 // Fix vector shift instructions where the last operand is a non-immediate
5741 // i32 value.
5742 case Intrinsic::x86_sse2_pslli_w:
5743 case Intrinsic::x86_sse2_pslli_d:
5744 case Intrinsic::x86_sse2_pslli_q:
5745 case Intrinsic::x86_sse2_psrli_w:
5746 case Intrinsic::x86_sse2_psrli_d:
5747 case Intrinsic::x86_sse2_psrli_q:
5748 case Intrinsic::x86_sse2_psrai_w:
5749 case Intrinsic::x86_sse2_psrai_d:
5750 case Intrinsic::x86_mmx_pslli_w:
5751 case Intrinsic::x86_mmx_pslli_d:
5752 case Intrinsic::x86_mmx_pslli_q:
5753 case Intrinsic::x86_mmx_psrli_w:
5754 case Intrinsic::x86_mmx_psrli_d:
5755 case Intrinsic::x86_mmx_psrli_q:
5756 case Intrinsic::x86_mmx_psrai_w:
5757 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005758 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005759 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005760 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005761
5762 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005763 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005764 switch (IntNo) {
5765 case Intrinsic::x86_sse2_pslli_w:
5766 NewIntNo = Intrinsic::x86_sse2_psll_w;
5767 break;
5768 case Intrinsic::x86_sse2_pslli_d:
5769 NewIntNo = Intrinsic::x86_sse2_psll_d;
5770 break;
5771 case Intrinsic::x86_sse2_pslli_q:
5772 NewIntNo = Intrinsic::x86_sse2_psll_q;
5773 break;
5774 case Intrinsic::x86_sse2_psrli_w:
5775 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5776 break;
5777 case Intrinsic::x86_sse2_psrli_d:
5778 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5779 break;
5780 case Intrinsic::x86_sse2_psrli_q:
5781 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5782 break;
5783 case Intrinsic::x86_sse2_psrai_w:
5784 NewIntNo = Intrinsic::x86_sse2_psra_w;
5785 break;
5786 case Intrinsic::x86_sse2_psrai_d:
5787 NewIntNo = Intrinsic::x86_sse2_psra_d;
5788 break;
5789 default: {
5790 ShAmtVT = MVT::v2i32;
5791 switch (IntNo) {
5792 case Intrinsic::x86_mmx_pslli_w:
5793 NewIntNo = Intrinsic::x86_mmx_psll_w;
5794 break;
5795 case Intrinsic::x86_mmx_pslli_d:
5796 NewIntNo = Intrinsic::x86_mmx_psll_d;
5797 break;
5798 case Intrinsic::x86_mmx_pslli_q:
5799 NewIntNo = Intrinsic::x86_mmx_psll_q;
5800 break;
5801 case Intrinsic::x86_mmx_psrli_w:
5802 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5803 break;
5804 case Intrinsic::x86_mmx_psrli_d:
5805 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5806 break;
5807 case Intrinsic::x86_mmx_psrli_q:
5808 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5809 break;
5810 case Intrinsic::x86_mmx_psrai_w:
5811 NewIntNo = Intrinsic::x86_mmx_psra_w;
5812 break;
5813 case Intrinsic::x86_mmx_psrai_d:
5814 NewIntNo = Intrinsic::x86_mmx_psra_d;
5815 break;
5816 default: abort(); // Can't reach here.
5817 }
5818 break;
5819 }
5820 }
Duncan Sands92c43912008-06-06 12:08:01 +00005821 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005822 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5823 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5824 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5825 DAG.getConstant(NewIntNo, MVT::i32),
5826 Op.getOperand(1), ShAmt);
5827 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005828 }
5829}
5830
Dan Gohman8181bd12008-07-27 21:46:04 +00005831SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005833 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005834 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005835
5836 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005837 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5839}
5840
Dan Gohman8181bd12008-07-27 21:46:04 +00005841SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005842 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5843 MFI->setFrameAddressIsTaken(true);
5844 MVT VT = Op.getValueType();
5845 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5846 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5847 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5848 while (Depth--)
5849 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5850 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005851}
5852
Dan Gohman8181bd12008-07-27 21:46:04 +00005853SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005854 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005855 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856}
5857
Dan Gohman8181bd12008-07-27 21:46:04 +00005858SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005860 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005861 SDValue Chain = Op.getOperand(0);
5862 SDValue Offset = Op.getOperand(1);
5863 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005864
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005865 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5866 getPointerTy());
5867 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005868
Dan Gohman8181bd12008-07-27 21:46:04 +00005869 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005870 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005871 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5872 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005873 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5874 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005875
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005876 return DAG.getNode(X86ISD::EH_RETURN,
5877 MVT::Other,
5878 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005879}
5880
Dan Gohman8181bd12008-07-27 21:46:04 +00005881SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005882 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005883 SDValue Root = Op.getOperand(0);
5884 SDValue Trmp = Op.getOperand(1); // trampoline
5885 SDValue FPtr = Op.getOperand(2); // nested function
5886 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005887
Dan Gohman12a9c082008-02-06 22:27:42 +00005888 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005889
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005890 const X86InstrInfo *TII =
5891 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5892
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005893 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005894 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005895
5896 // Large code-model.
5897
5898 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5899 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5900
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005901 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5902 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005903
5904 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5905
5906 // Load the pointer to the nested function into R11.
5907 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005908 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005909 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005910 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005911
5912 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005913 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005914
5915 // Load the 'nest' parameter value into R10.
5916 // R10 is specified in X86CallingConv.td
5917 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5918 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5919 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005920 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005921
5922 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005923 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005924
5925 // Jump to the nested function.
5926 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5927 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5928 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005929 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005930
5931 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5932 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5933 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005934 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005935
Dan Gohman8181bd12008-07-27 21:46:04 +00005936 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005937 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005938 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005939 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005940 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005941 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5942 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005943 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005944
5945 switch (CC) {
5946 default:
5947 assert(0 && "Unsupported calling convention");
5948 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005949 case CallingConv::X86_StdCall: {
5950 // Pass 'nest' parameter in ECX.
5951 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005952 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005953
5954 // Check that ECX wasn't needed by an 'inreg' parameter.
5955 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005956 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005957
Chris Lattner1c8733e2008-03-12 17:45:29 +00005958 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005959 unsigned InRegCount = 0;
5960 unsigned Idx = 1;
5961
5962 for (FunctionType::param_iterator I = FTy->param_begin(),
5963 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005964 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005965 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005966 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005967
5968 if (InRegCount > 2) {
5969 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5970 abort();
5971 }
5972 }
5973 break;
5974 }
5975 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005976 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005977 // Pass 'nest' parameter in EAX.
5978 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005979 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005980 break;
5981 }
5982
Dan Gohman8181bd12008-07-27 21:46:04 +00005983 SDValue OutChains[4];
5984 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005985
5986 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5987 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5988
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005989 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005990 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005991 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005992 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005993
5994 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005995 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005996
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005997 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005998 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5999 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006000 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006001
6002 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006003 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006004
Dan Gohman8181bd12008-07-27 21:46:04 +00006005 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006006 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006007 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006008 }
6009}
6010
Dan Gohman8181bd12008-07-27 21:46:04 +00006011SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006012 /*
6013 The rounding mode is in bits 11:10 of FPSR, and has the following
6014 settings:
6015 00 Round to nearest
6016 01 Round to -inf
6017 10 Round to +inf
6018 11 Round to 0
6019
6020 FLT_ROUNDS, on the other hand, expects the following:
6021 -1 Undefined
6022 0 Round to 0
6023 1 Round to nearest
6024 2 Round to +inf
6025 3 Round to -inf
6026
6027 To perform the conversion, we do:
6028 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6029 */
6030
6031 MachineFunction &MF = DAG.getMachineFunction();
6032 const TargetMachine &TM = MF.getTarget();
6033 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6034 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006035 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006036
6037 // Save FP Control Word to stack slot
6038 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006039 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006040
Dan Gohman8181bd12008-07-27 21:46:04 +00006041 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006042 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006043
6044 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006045 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006046
6047 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006048 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006049 DAG.getNode(ISD::SRL, MVT::i16,
6050 DAG.getNode(ISD::AND, MVT::i16,
6051 CWD, DAG.getConstant(0x800, MVT::i16)),
6052 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006053 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006054 DAG.getNode(ISD::SRL, MVT::i16,
6055 DAG.getNode(ISD::AND, MVT::i16,
6056 CWD, DAG.getConstant(0x400, MVT::i16)),
6057 DAG.getConstant(9, MVT::i8));
6058
Dan Gohman8181bd12008-07-27 21:46:04 +00006059 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006060 DAG.getNode(ISD::AND, MVT::i16,
6061 DAG.getNode(ISD::ADD, MVT::i16,
6062 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6063 DAG.getConstant(1, MVT::i16)),
6064 DAG.getConstant(3, MVT::i16));
6065
6066
Duncan Sands92c43912008-06-06 12:08:01 +00006067 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006068 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6069}
6070
Dan Gohman8181bd12008-07-27 21:46:04 +00006071SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006072 MVT VT = Op.getValueType();
6073 MVT OpVT = VT;
6074 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006075
6076 Op = Op.getOperand(0);
6077 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006078 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006079 OpVT = MVT::i32;
6080 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6081 }
Evan Cheng48679f42007-12-14 02:13:44 +00006082
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006083 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6084 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6085 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6086
6087 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006088 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006089 Ops.push_back(Op);
6090 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6091 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6092 Ops.push_back(Op.getValue(1));
6093 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6094
6095 // Finally xor with NumBits-1.
6096 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6097
Evan Cheng48679f42007-12-14 02:13:44 +00006098 if (VT == MVT::i8)
6099 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6100 return Op;
6101}
6102
Dan Gohman8181bd12008-07-27 21:46:04 +00006103SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006104 MVT VT = Op.getValueType();
6105 MVT OpVT = VT;
6106 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006107
6108 Op = Op.getOperand(0);
6109 if (VT == MVT::i8) {
6110 OpVT = MVT::i32;
6111 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6112 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006113
6114 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6115 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6116 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6117
6118 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006119 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006120 Ops.push_back(Op);
6121 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6122 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6123 Ops.push_back(Op.getValue(1));
6124 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6125
Evan Cheng48679f42007-12-14 02:13:44 +00006126 if (VT == MVT::i8)
6127 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6128 return Op;
6129}
6130
Bill Wendling7e04be62008-12-09 22:08:41 +00006131SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6132 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6133 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00006134 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6135 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00006136 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00006137 SDValue LHS = N->getOperand(0);
6138 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00006139 unsigned BaseOp = 0;
6140 unsigned Cond = 0;
6141
6142 switch (Op.getOpcode()) {
6143 default: assert(0 && "Unknown ovf instruction!");
6144 case ISD::SADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006145 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006146 Cond = X86::COND_O;
6147 break;
6148 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006149 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00006150 Cond = X86::COND_C;
6151 break;
6152 case ISD::SSUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006153 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006154 Cond = X86::COND_O;
6155 break;
6156 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006157 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00006158 Cond = X86::COND_C;
6159 break;
6160 case ISD::SMULO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006161 BaseOp = X86ISD::MUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006162 Cond = X86::COND_O;
6163 break;
6164 case ISD::UMULO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00006165 BaseOp = X86ISD::MUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00006166 Cond = X86::COND_C;
6167 break;
6168 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00006169
Bill Wendlingd3511522008-12-02 01:06:39 +00006170 // Also sets EFLAGS.
6171 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Bill Wendling7e04be62008-12-09 22:08:41 +00006172 SDValue Sum = DAG.getNode(BaseOp, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00006173
Bill Wendlingd3511522008-12-02 01:06:39 +00006174 SDValue SetCC =
6175 DAG.getNode(X86ISD::SETCC, N->getValueType(1),
Bill Wendling35f1a9d2008-12-10 02:01:32 +00006176 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00006177
Bill Wendlingd3511522008-12-02 01:06:39 +00006178 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6179 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00006180}
6181
Dan Gohman8181bd12008-07-27 21:46:04 +00006182SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006183 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006184 unsigned Reg = 0;
6185 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006186 switch(T.getSimpleVT()) {
6187 default:
6188 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006189 case MVT::i8: Reg = X86::AL; size = 1; break;
6190 case MVT::i16: Reg = X86::AX; size = 2; break;
6191 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006192 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006193 assert(Subtarget->is64Bit() && "Node not type legal!");
6194 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00006195 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006196 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006197 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006198 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006199 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006200 Op.getOperand(1),
6201 Op.getOperand(3),
6202 DAG.getTargetConstant(size, MVT::i8),
6203 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006204 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006205 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6206 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006207 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6208 return cpOut;
6209}
6210
Duncan Sands7d9834b2008-12-01 11:39:25 +00006211SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00006212 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00006213 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharth81580822008-03-05 01:15:49 +00006214 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006215 SDValue TheChain = Op.getOperand(0);
6216 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6217 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
6218 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX, MVT::i64,
6219 rax.getValue(2));
6220 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
6221 DAG.getConstant(32, MVT::i8));
6222 SDValue Ops[] = {
6223 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp),
6224 rdx.getValue(1)
6225 };
6226 return DAG.getMergeValues(Ops, 2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006227}
6228
Dale Johannesen9011d872008-09-29 22:25:26 +00006229SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6230 SDNode *Node = Op.getNode();
6231 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006232 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006233 DAG.getConstant(0, T), Node->getOperand(2));
6234 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6235 ISD::ATOMIC_LOAD_ADD_8 :
6236 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6237 ISD::ATOMIC_LOAD_ADD_16 :
6238 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6239 ISD::ATOMIC_LOAD_ADD_32 :
6240 ISD::ATOMIC_LOAD_ADD_64),
6241 Node->getOperand(0),
6242 Node->getOperand(1), negOp,
6243 cast<AtomicSDNode>(Node)->getSrcValue(),
6244 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006245}
6246
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006247/// LowerOperation - Provide custom lowering hooks for some operations.
6248///
Dan Gohman8181bd12008-07-27 21:46:04 +00006249SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 switch (Op.getOpcode()) {
6251 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006252 case ISD::ATOMIC_CMP_SWAP_8:
6253 case ISD::ATOMIC_CMP_SWAP_16:
6254 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006255 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006256 case ISD::ATOMIC_LOAD_SUB_8:
6257 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006258 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006259 case ISD::ATOMIC_LOAD_SUB_64: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006260 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6261 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6262 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6263 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6264 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6265 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6266 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6267 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006268 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006269 case ISD::SHL_PARTS:
6270 case ISD::SRA_PARTS:
6271 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6272 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006273 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006274 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6275 case ISD::FABS: return LowerFABS(Op, DAG);
6276 case ISD::FNEG: return LowerFNEG(Op, DAG);
6277 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006278 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006279 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006280 case ISD::SELECT: return LowerSELECT(Op, DAG);
6281 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006282 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6283 case ISD::CALL: return LowerCALL(Op, DAG);
6284 case ISD::RET: return LowerRET(Op, DAG);
6285 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006286 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006287 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006288 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6289 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6290 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6291 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6292 case ISD::FRAME_TO_ARGS_OFFSET:
6293 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6294 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6295 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006296 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006297 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006298 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6299 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00006300 case ISD::SADDO:
6301 case ISD::UADDO:
6302 case ISD::SSUBO:
6303 case ISD::USUBO:
6304 case ISD::SMULO:
6305 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00006306 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006307 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006308}
6309
Duncan Sands7d9834b2008-12-01 11:39:25 +00006310void X86TargetLowering::
6311ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6312 SelectionDAG &DAG, unsigned NewOp) {
6313 MVT T = Node->getValueType(0);
6314 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6315
6316 SDValue Chain = Node->getOperand(0);
6317 SDValue In1 = Node->getOperand(1);
6318 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6319 Node->getOperand(2), DAG.getIntPtrConstant(0));
6320 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6321 Node->getOperand(2), DAG.getIntPtrConstant(1));
6322 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6323 // have a MemOperand. Pass the info through as a normal operand.
6324 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6325 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6326 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
6327 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
6328 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6329 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6330 Results.push_back(Result.getValue(2));
6331}
6332
Duncan Sandsac496a12008-07-04 11:47:58 +00006333/// ReplaceNodeResults - Replace a node with an illegal result type
6334/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00006335void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6336 SmallVectorImpl<SDValue>&Results,
6337 SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006338 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006339 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00006340 assert(false && "Do not know how to custom type legalize this operation!");
6341 return;
6342 case ISD::FP_TO_SINT: {
6343 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
6344 SDValue FIST = Vals.first, StackSlot = Vals.second;
6345 if (FIST.getNode() != 0) {
6346 MVT VT = N->getValueType(0);
6347 // Return a load from the stack slot.
6348 Results.push_back(DAG.getLoad(VT, FIST, StackSlot, NULL, 0));
6349 }
6350 return;
6351 }
6352 case ISD::READCYCLECOUNTER: {
6353 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6354 SDValue TheChain = N->getOperand(0);
6355 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
6356 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
6357 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX, MVT::i32,
6358 eax.getValue(2));
6359 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6360 SDValue Ops[] = { eax, edx };
6361 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2));
6362 Results.push_back(edx.getValue(1));
6363 return;
6364 }
6365 case ISD::ATOMIC_CMP_SWAP_64: {
6366 MVT T = N->getValueType(0);
6367 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6368 SDValue cpInL, cpInH;
6369 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6370 DAG.getConstant(0, MVT::i32));
6371 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(2),
6372 DAG.getConstant(1, MVT::i32));
6373 cpInL = DAG.getCopyToReg(N->getOperand(0), X86::EAX, cpInL, SDValue());
6374 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX, cpInH,
6375 cpInL.getValue(1));
6376 SDValue swapInL, swapInH;
6377 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6378 DAG.getConstant(0, MVT::i32));
6379 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, N->getOperand(3),
6380 DAG.getConstant(1, MVT::i32));
6381 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX, swapInL,
6382 cpInH.getValue(1));
6383 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX, swapInH,
6384 swapInL.getValue(1));
6385 SDValue Ops[] = { swapInH.getValue(0),
6386 N->getOperand(1),
6387 swapInH.getValue(1) };
6388 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6389 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6390 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
6391 Result.getValue(1));
6392 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
6393 cpOutL.getValue(2));
6394 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6395 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2));
6396 Results.push_back(cpOutH.getValue(1));
6397 return;
6398 }
6399 case ISD::ATOMIC_LOAD_ADD_64:
6400 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6401 return;
6402 case ISD::ATOMIC_LOAD_AND_64:
6403 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6404 return;
6405 case ISD::ATOMIC_LOAD_NAND_64:
6406 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6407 return;
6408 case ISD::ATOMIC_LOAD_OR_64:
6409 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6410 return;
6411 case ISD::ATOMIC_LOAD_SUB_64:
6412 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6413 return;
6414 case ISD::ATOMIC_LOAD_XOR_64:
6415 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6416 return;
6417 case ISD::ATOMIC_SWAP_64:
6418 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6419 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006420 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006421}
6422
6423const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6424 switch (Opcode) {
6425 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006426 case X86ISD::BSF: return "X86ISD::BSF";
6427 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 case X86ISD::SHLD: return "X86ISD::SHLD";
6429 case X86ISD::SHRD: return "X86ISD::SHRD";
6430 case X86ISD::FAND: return "X86ISD::FAND";
6431 case X86ISD::FOR: return "X86ISD::FOR";
6432 case X86ISD::FXOR: return "X86ISD::FXOR";
6433 case X86ISD::FSRL: return "X86ISD::FSRL";
6434 case X86ISD::FILD: return "X86ISD::FILD";
6435 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6436 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6437 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6438 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6439 case X86ISD::FLD: return "X86ISD::FLD";
6440 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006441 case X86ISD::CALL: return "X86ISD::CALL";
6442 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6443 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6444 case X86ISD::CMP: return "X86ISD::CMP";
6445 case X86ISD::COMI: return "X86ISD::COMI";
6446 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6447 case X86ISD::SETCC: return "X86ISD::SETCC";
6448 case X86ISD::CMOV: return "X86ISD::CMOV";
6449 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6450 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6451 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6452 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006453 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6454 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006455 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006456 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006457 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6458 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006459 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6460 case X86ISD::FMAX: return "X86ISD::FMAX";
6461 case X86ISD::FMIN: return "X86ISD::FMIN";
6462 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6463 case X86ISD::FRCP: return "X86ISD::FRCP";
6464 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6465 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6466 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006467 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006468 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006469 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6470 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006471 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6472 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6473 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6474 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6475 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6476 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006477 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6478 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006479 case X86ISD::VSHL: return "X86ISD::VSHL";
6480 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006481 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6482 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6483 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6484 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6485 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6486 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6487 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6488 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6489 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6490 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00006491 case X86ISD::ADD: return "X86ISD::ADD";
6492 case X86ISD::SUB: return "X86ISD::SUB";
6493 case X86ISD::MUL: return "X86ISD::MUL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006494 }
6495}
6496
6497// isLegalAddressingMode - Return true if the addressing mode represented
6498// by AM is legal for this target, for a load/store of the specified type.
6499bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6500 const Type *Ty) const {
6501 // X86 supports extremely general addressing modes.
6502
6503 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6504 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6505 return false;
6506
6507 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006508 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006509 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6510 return false;
Dale Johannesen64660e92008-12-05 21:47:27 +00006511 // If BaseGV requires a register, we cannot also have a BaseReg.
6512 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6513 AM.HasBaseReg)
6514 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006515
6516 // X86-64 only supports addr of globals in small code model.
6517 if (Subtarget->is64Bit()) {
6518 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6519 return false;
6520 // If lower 4G is not available, then we must use rip-relative addressing.
6521 if (AM.BaseOffs || AM.Scale > 1)
6522 return false;
6523 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006524 }
6525
6526 switch (AM.Scale) {
6527 case 0:
6528 case 1:
6529 case 2:
6530 case 4:
6531 case 8:
6532 // These scales always work.
6533 break;
6534 case 3:
6535 case 5:
6536 case 9:
6537 // These scales are formed with basereg+scalereg. Only accept if there is
6538 // no basereg yet.
6539 if (AM.HasBaseReg)
6540 return false;
6541 break;
6542 default: // Other stuff never works.
6543 return false;
6544 }
6545
6546 return true;
6547}
6548
6549
Evan Cheng27a820a2007-10-26 01:56:11 +00006550bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6551 if (!Ty1->isInteger() || !Ty2->isInteger())
6552 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006553 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6554 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006555 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006556 return false;
6557 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006558}
6559
Duncan Sands92c43912008-06-06 12:08:01 +00006560bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6561 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006562 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006563 unsigned NumBits1 = VT1.getSizeInBits();
6564 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006565 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006566 return false;
6567 return Subtarget->is64Bit() || NumBits1 < 64;
6568}
Evan Cheng27a820a2007-10-26 01:56:11 +00006569
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006570/// isShuffleMaskLegal - Targets can use this to indicate that they only
6571/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6572/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6573/// are assumed to be legal.
6574bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006575X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006576 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006577 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006578 return (Mask.getNode()->getNumOperands() <= 4 ||
6579 isIdentityMask(Mask.getNode()) ||
6580 isIdentityMask(Mask.getNode(), true) ||
6581 isSplatMask(Mask.getNode()) ||
6582 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6583 X86::isUNPCKLMask(Mask.getNode()) ||
6584 X86::isUNPCKHMask(Mask.getNode()) ||
6585 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6586 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006587}
6588
Dan Gohman48d5f062008-04-09 20:09:42 +00006589bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006590X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006591 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006592 unsigned NumElts = BVOps.size();
6593 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006594 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006595 if (NumElts == 2) return true;
6596 if (NumElts == 4) {
6597 return (isMOVLMask(&BVOps[0], 4) ||
6598 isCommutedMOVL(&BVOps[0], 4, true) ||
6599 isSHUFPMask(&BVOps[0], 4) ||
6600 isCommutedSHUFP(&BVOps[0], 4));
6601 }
6602 return false;
6603}
6604
6605//===----------------------------------------------------------------------===//
6606// X86 Scheduler Hooks
6607//===----------------------------------------------------------------------===//
6608
Mon P Wang078a62d2008-05-05 19:05:59 +00006609// private utility function
6610MachineBasicBlock *
6611X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6612 MachineBasicBlock *MBB,
6613 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006614 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006615 unsigned LoadOpc,
6616 unsigned CXchgOpc,
6617 unsigned copyOpc,
6618 unsigned notOpc,
6619 unsigned EAXreg,
6620 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006621 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006622 // For the atomic bitwise operator, we generate
6623 // thisMBB:
6624 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006625 // ld t1 = [bitinstr.addr]
6626 // op t2 = t1, [bitinstr.val]
6627 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006628 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6629 // bz newMBB
6630 // fallthrough -->nextMBB
6631 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6632 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006633 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006634 ++MBBIter;
6635
6636 /// First build the CFG
6637 MachineFunction *F = MBB->getParent();
6638 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006639 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6640 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6641 F->insert(MBBIter, newMBB);
6642 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006643
6644 // Move all successors to thisMBB to nextMBB
6645 nextMBB->transferSuccessors(thisMBB);
6646
6647 // Update thisMBB to fall through to newMBB
6648 thisMBB->addSuccessor(newMBB);
6649
6650 // newMBB jumps to itself and fall through to nextMBB
6651 newMBB->addSuccessor(nextMBB);
6652 newMBB->addSuccessor(newMBB);
6653
6654 // Insert instructions into newMBB based on incoming instruction
6655 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6656 MachineOperand& destOper = bInstr->getOperand(0);
6657 MachineOperand* argOpers[6];
6658 int numArgs = bInstr->getNumOperands() - 1;
6659 for (int i=0; i < numArgs; ++i)
6660 argOpers[i] = &bInstr->getOperand(i+1);
6661
6662 // x86 address has 4 operands: base, index, scale, and displacement
6663 int lastAddrIndx = 3; // [0,3]
6664 int valArgIndx = 4;
6665
Dale Johannesend20e4452008-08-19 18:47:28 +00006666 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6667 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006668 for (int i=0; i <= lastAddrIndx; ++i)
6669 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006670
Dale Johannesend20e4452008-08-19 18:47:28 +00006671 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006672 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006673 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006674 }
6675 else
6676 tt = t1;
6677
Dale Johannesend20e4452008-08-19 18:47:28 +00006678 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006679 assert((argOpers[valArgIndx]->isReg() ||
6680 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006681 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006682 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006683 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6684 else
6685 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006686 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006687 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006688
Dale Johannesend20e4452008-08-19 18:47:28 +00006689 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006690 MIB.addReg(t1);
6691
Dale Johannesend20e4452008-08-19 18:47:28 +00006692 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006693 for (int i=0; i <= lastAddrIndx; ++i)
6694 (*MIB).addOperand(*argOpers[i]);
6695 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006696 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6697 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6698
Dale Johannesend20e4452008-08-19 18:47:28 +00006699 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6700 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006701
6702 // insert branch
6703 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6704
Dan Gohman221a4372008-07-07 23:14:23 +00006705 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006706 return nextMBB;
6707}
6708
Dale Johannesen44eb5372008-10-03 19:41:08 +00006709// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006710MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006711X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6712 MachineBasicBlock *MBB,
6713 unsigned regOpcL,
6714 unsigned regOpcH,
6715 unsigned immOpcL,
6716 unsigned immOpcH,
6717 bool invSrc) {
6718 // For the atomic bitwise operator, we generate
6719 // thisMBB (instructions are in pairs, except cmpxchg8b)
6720 // ld t1,t2 = [bitinstr.addr]
6721 // newMBB:
6722 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6723 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006724 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006725 // mov ECX, EBX <- t5, t6
6726 // mov EAX, EDX <- t1, t2
6727 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6728 // mov t3, t4 <- EAX, EDX
6729 // bz newMBB
6730 // result in out1, out2
6731 // fallthrough -->nextMBB
6732
6733 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6734 const unsigned LoadOpc = X86::MOV32rm;
6735 const unsigned copyOpc = X86::MOV32rr;
6736 const unsigned NotOpc = X86::NOT32r;
6737 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6738 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6739 MachineFunction::iterator MBBIter = MBB;
6740 ++MBBIter;
6741
6742 /// First build the CFG
6743 MachineFunction *F = MBB->getParent();
6744 MachineBasicBlock *thisMBB = MBB;
6745 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6746 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6747 F->insert(MBBIter, newMBB);
6748 F->insert(MBBIter, nextMBB);
6749
6750 // Move all successors to thisMBB to nextMBB
6751 nextMBB->transferSuccessors(thisMBB);
6752
6753 // Update thisMBB to fall through to newMBB
6754 thisMBB->addSuccessor(newMBB);
6755
6756 // newMBB jumps to itself and fall through to nextMBB
6757 newMBB->addSuccessor(nextMBB);
6758 newMBB->addSuccessor(newMBB);
6759
6760 // Insert instructions into newMBB based on incoming instruction
6761 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6762 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6763 MachineOperand& dest1Oper = bInstr->getOperand(0);
6764 MachineOperand& dest2Oper = bInstr->getOperand(1);
6765 MachineOperand* argOpers[6];
6766 for (int i=0; i < 6; ++i)
6767 argOpers[i] = &bInstr->getOperand(i+2);
6768
6769 // x86 address has 4 operands: base, index, scale, and displacement
6770 int lastAddrIndx = 3; // [0,3]
6771
6772 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6773 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6774 for (int i=0; i <= lastAddrIndx; ++i)
6775 (*MIB).addOperand(*argOpers[i]);
6776 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6777 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006778 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006779 for (int i=0; i <= lastAddrIndx-1; ++i)
6780 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006781 MachineOperand newOp3 = *(argOpers[3]);
6782 if (newOp3.isImm())
6783 newOp3.setImm(newOp3.getImm()+4);
6784 else
6785 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006786 (*MIB).addOperand(newOp3);
6787
6788 // t3/4 are defined later, at the bottom of the loop
6789 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6790 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6791 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6792 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6793 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6794 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6795
6796 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6797 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6798 if (invSrc) {
6799 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6800 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6801 } else {
6802 tt1 = t1;
6803 tt2 = t2;
6804 }
6805
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006806 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006807 "invalid operand");
6808 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6809 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006810 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006811 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6812 else
6813 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006814 if (regOpcL != X86::MOV32rr)
6815 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006816 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006817 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6818 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6819 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006820 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6821 else
6822 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006823 if (regOpcH != X86::MOV32rr)
6824 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006825 (*MIB).addOperand(*argOpers[5]);
6826
6827 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6828 MIB.addReg(t1);
6829 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6830 MIB.addReg(t2);
6831
6832 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6833 MIB.addReg(t5);
6834 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6835 MIB.addReg(t6);
6836
6837 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6838 for (int i=0; i <= lastAddrIndx; ++i)
6839 (*MIB).addOperand(*argOpers[i]);
6840
6841 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6842 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6843
6844 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6845 MIB.addReg(X86::EAX);
6846 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6847 MIB.addReg(X86::EDX);
6848
6849 // insert branch
6850 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6851
6852 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6853 return nextMBB;
6854}
6855
6856// private utility function
6857MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006858X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6859 MachineBasicBlock *MBB,
6860 unsigned cmovOpc) {
6861 // For the atomic min/max operator, we generate
6862 // thisMBB:
6863 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006864 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006865 // mov t2 = [min/max.val]
6866 // cmp t1, t2
6867 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006868 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006869 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6870 // bz newMBB
6871 // fallthrough -->nextMBB
6872 //
6873 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6874 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006875 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006876 ++MBBIter;
6877
6878 /// First build the CFG
6879 MachineFunction *F = MBB->getParent();
6880 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006881 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6882 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6883 F->insert(MBBIter, newMBB);
6884 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006885
6886 // Move all successors to thisMBB to nextMBB
6887 nextMBB->transferSuccessors(thisMBB);
6888
6889 // Update thisMBB to fall through to newMBB
6890 thisMBB->addSuccessor(newMBB);
6891
6892 // newMBB jumps to newMBB and fall through to nextMBB
6893 newMBB->addSuccessor(nextMBB);
6894 newMBB->addSuccessor(newMBB);
6895
6896 // Insert instructions into newMBB based on incoming instruction
6897 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6898 MachineOperand& destOper = mInstr->getOperand(0);
6899 MachineOperand* argOpers[6];
6900 int numArgs = mInstr->getNumOperands() - 1;
6901 for (int i=0; i < numArgs; ++i)
6902 argOpers[i] = &mInstr->getOperand(i+1);
6903
6904 // x86 address has 4 operands: base, index, scale, and displacement
6905 int lastAddrIndx = 3; // [0,3]
6906 int valArgIndx = 4;
6907
Mon P Wang318b0372008-05-05 22:56:23 +00006908 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6909 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006910 for (int i=0; i <= lastAddrIndx; ++i)
6911 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006912
Mon P Wang078a62d2008-05-05 19:05:59 +00006913 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006914 assert((argOpers[valArgIndx]->isReg() ||
6915 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006916 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006917
6918 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006919 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006920 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6921 else
6922 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6923 (*MIB).addOperand(*argOpers[valArgIndx]);
6924
Mon P Wang318b0372008-05-05 22:56:23 +00006925 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6926 MIB.addReg(t1);
6927
Mon P Wang078a62d2008-05-05 19:05:59 +00006928 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6929 MIB.addReg(t1);
6930 MIB.addReg(t2);
6931
6932 // Generate movc
6933 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6934 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6935 MIB.addReg(t2);
6936 MIB.addReg(t1);
6937
6938 // Cmp and exchange if none has modified the memory location
6939 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6940 for (int i=0; i <= lastAddrIndx; ++i)
6941 (*MIB).addOperand(*argOpers[i]);
6942 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006943 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6944 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006945
6946 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6947 MIB.addReg(X86::EAX);
6948
6949 // insert branch
6950 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6951
Dan Gohman221a4372008-07-07 23:14:23 +00006952 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006953 return nextMBB;
6954}
6955
6956
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006957MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006958X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6959 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006960 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6961 switch (MI->getOpcode()) {
6962 default: assert(false && "Unexpected instr type to insert");
6963 case X86::CMOV_FR32:
6964 case X86::CMOV_FR64:
6965 case X86::CMOV_V4F32:
6966 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006967 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006968 // To "insert" a SELECT_CC instruction, we actually have to insert the
6969 // diamond control-flow pattern. The incoming instruction knows the
6970 // destination vreg to set, the condition code register to branch on, the
6971 // true/false values to select between, and a branch opcode to use.
6972 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006973 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006974 ++It;
6975
6976 // thisMBB:
6977 // ...
6978 // TrueVal = ...
6979 // cmpTY ccX, r1, r2
6980 // bCC copy1MBB
6981 // fallthrough --> copy0MBB
6982 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006983 MachineFunction *F = BB->getParent();
6984 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6985 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006986 unsigned Opc =
6987 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6988 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006989 F->insert(It, copy0MBB);
6990 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006991 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006992 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006993 sinkMBB->transferSuccessors(BB);
6994
6995 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006996 BB->addSuccessor(copy0MBB);
6997 BB->addSuccessor(sinkMBB);
6998
6999 // copy0MBB:
7000 // %FalseValue = ...
7001 // # fallthrough to sinkMBB
7002 BB = copy0MBB;
7003
7004 // Update machine-CFG edges
7005 BB->addSuccessor(sinkMBB);
7006
7007 // sinkMBB:
7008 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7009 // ...
7010 BB = sinkMBB;
7011 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
7012 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7013 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7014
Dan Gohman221a4372008-07-07 23:14:23 +00007015 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007016 return BB;
7017 }
7018
7019 case X86::FP32_TO_INT16_IN_MEM:
7020 case X86::FP32_TO_INT32_IN_MEM:
7021 case X86::FP32_TO_INT64_IN_MEM:
7022 case X86::FP64_TO_INT16_IN_MEM:
7023 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007024 case X86::FP64_TO_INT64_IN_MEM:
7025 case X86::FP80_TO_INT16_IN_MEM:
7026 case X86::FP80_TO_INT32_IN_MEM:
7027 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007028 // Change the floating point control register to use "round towards zero"
7029 // mode when truncating to an integer value.
7030 MachineFunction *F = BB->getParent();
7031 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
7032 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
7033
7034 // Load the old value of the high byte of the control word...
7035 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00007036 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007037 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
7038
7039 // Set the high part to be round to zero...
7040 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
7041 .addImm(0xC7F);
7042
7043 // Reload the modified control word now...
7044 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7045
7046 // Restore the memory image of control word to original value
7047 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
7048 .addReg(OldCW);
7049
7050 // Get the X86 opcode to use.
7051 unsigned Opc;
7052 switch (MI->getOpcode()) {
7053 default: assert(0 && "illegal opcode!");
7054 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7055 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7056 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7057 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7058 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7059 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00007060 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7061 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7062 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007063 }
7064
7065 X86AddressMode AM;
7066 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007067 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007068 AM.BaseType = X86AddressMode::RegBase;
7069 AM.Base.Reg = Op.getReg();
7070 } else {
7071 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00007072 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007073 }
7074 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007075 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007076 AM.Scale = Op.getImm();
7077 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007078 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007079 AM.IndexReg = Op.getImm();
7080 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007081 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007082 AM.GV = Op.getGlobal();
7083 } else {
7084 AM.Disp = Op.getImm();
7085 }
7086 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
7087 .addReg(MI->getOperand(4).getReg());
7088
7089 // Reload the original control word now.
7090 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7091
Dan Gohman221a4372008-07-07 23:14:23 +00007092 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007093 return BB;
7094 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007095 case X86::ATOMAND32:
7096 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007097 X86::AND32ri, X86::MOV32rm,
7098 X86::LCMPXCHG32, X86::MOV32rr,
7099 X86::NOT32r, X86::EAX,
7100 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007101 case X86::ATOMOR32:
7102 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007103 X86::OR32ri, X86::MOV32rm,
7104 X86::LCMPXCHG32, X86::MOV32rr,
7105 X86::NOT32r, X86::EAX,
7106 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007107 case X86::ATOMXOR32:
7108 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007109 X86::XOR32ri, X86::MOV32rm,
7110 X86::LCMPXCHG32, X86::MOV32rr,
7111 X86::NOT32r, X86::EAX,
7112 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007113 case X86::ATOMNAND32:
7114 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007115 X86::AND32ri, X86::MOV32rm,
7116 X86::LCMPXCHG32, X86::MOV32rr,
7117 X86::NOT32r, X86::EAX,
7118 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007119 case X86::ATOMMIN32:
7120 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7121 case X86::ATOMMAX32:
7122 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7123 case X86::ATOMUMIN32:
7124 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7125 case X86::ATOMUMAX32:
7126 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007127
7128 case X86::ATOMAND16:
7129 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7130 X86::AND16ri, X86::MOV16rm,
7131 X86::LCMPXCHG16, X86::MOV16rr,
7132 X86::NOT16r, X86::AX,
7133 X86::GR16RegisterClass);
7134 case X86::ATOMOR16:
7135 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7136 X86::OR16ri, X86::MOV16rm,
7137 X86::LCMPXCHG16, X86::MOV16rr,
7138 X86::NOT16r, X86::AX,
7139 X86::GR16RegisterClass);
7140 case X86::ATOMXOR16:
7141 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7142 X86::XOR16ri, X86::MOV16rm,
7143 X86::LCMPXCHG16, X86::MOV16rr,
7144 X86::NOT16r, X86::AX,
7145 X86::GR16RegisterClass);
7146 case X86::ATOMNAND16:
7147 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7148 X86::AND16ri, X86::MOV16rm,
7149 X86::LCMPXCHG16, X86::MOV16rr,
7150 X86::NOT16r, X86::AX,
7151 X86::GR16RegisterClass, true);
7152 case X86::ATOMMIN16:
7153 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7154 case X86::ATOMMAX16:
7155 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7156 case X86::ATOMUMIN16:
7157 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7158 case X86::ATOMUMAX16:
7159 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7160
7161 case X86::ATOMAND8:
7162 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7163 X86::AND8ri, X86::MOV8rm,
7164 X86::LCMPXCHG8, X86::MOV8rr,
7165 X86::NOT8r, X86::AL,
7166 X86::GR8RegisterClass);
7167 case X86::ATOMOR8:
7168 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7169 X86::OR8ri, X86::MOV8rm,
7170 X86::LCMPXCHG8, X86::MOV8rr,
7171 X86::NOT8r, X86::AL,
7172 X86::GR8RegisterClass);
7173 case X86::ATOMXOR8:
7174 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7175 X86::XOR8ri, X86::MOV8rm,
7176 X86::LCMPXCHG8, X86::MOV8rr,
7177 X86::NOT8r, X86::AL,
7178 X86::GR8RegisterClass);
7179 case X86::ATOMNAND8:
7180 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7181 X86::AND8ri, X86::MOV8rm,
7182 X86::LCMPXCHG8, X86::MOV8rr,
7183 X86::NOT8r, X86::AL,
7184 X86::GR8RegisterClass, true);
7185 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007186 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007187 case X86::ATOMAND64:
7188 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7189 X86::AND64ri32, X86::MOV64rm,
7190 X86::LCMPXCHG64, X86::MOV64rr,
7191 X86::NOT64r, X86::RAX,
7192 X86::GR64RegisterClass);
7193 case X86::ATOMOR64:
7194 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7195 X86::OR64ri32, X86::MOV64rm,
7196 X86::LCMPXCHG64, X86::MOV64rr,
7197 X86::NOT64r, X86::RAX,
7198 X86::GR64RegisterClass);
7199 case X86::ATOMXOR64:
7200 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7201 X86::XOR64ri32, X86::MOV64rm,
7202 X86::LCMPXCHG64, X86::MOV64rr,
7203 X86::NOT64r, X86::RAX,
7204 X86::GR64RegisterClass);
7205 case X86::ATOMNAND64:
7206 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7207 X86::AND64ri32, X86::MOV64rm,
7208 X86::LCMPXCHG64, X86::MOV64rr,
7209 X86::NOT64r, X86::RAX,
7210 X86::GR64RegisterClass, true);
7211 case X86::ATOMMIN64:
7212 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7213 case X86::ATOMMAX64:
7214 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7215 case X86::ATOMUMIN64:
7216 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7217 case X86::ATOMUMAX64:
7218 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007219
7220 // This group does 64-bit operations on a 32-bit host.
7221 case X86::ATOMAND6432:
7222 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7223 X86::AND32rr, X86::AND32rr,
7224 X86::AND32ri, X86::AND32ri,
7225 false);
7226 case X86::ATOMOR6432:
7227 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7228 X86::OR32rr, X86::OR32rr,
7229 X86::OR32ri, X86::OR32ri,
7230 false);
7231 case X86::ATOMXOR6432:
7232 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7233 X86::XOR32rr, X86::XOR32rr,
7234 X86::XOR32ri, X86::XOR32ri,
7235 false);
7236 case X86::ATOMNAND6432:
7237 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7238 X86::AND32rr, X86::AND32rr,
7239 X86::AND32ri, X86::AND32ri,
7240 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007241 case X86::ATOMADD6432:
7242 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7243 X86::ADD32rr, X86::ADC32rr,
7244 X86::ADD32ri, X86::ADC32ri,
7245 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007246 case X86::ATOMSUB6432:
7247 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7248 X86::SUB32rr, X86::SBB32rr,
7249 X86::SUB32ri, X86::SBB32ri,
7250 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007251 case X86::ATOMSWAP6432:
7252 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7253 X86::MOV32rr, X86::MOV32rr,
7254 X86::MOV32ri, X86::MOV32ri,
7255 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007256 }
7257}
7258
7259//===----------------------------------------------------------------------===//
7260// X86 Optimization Hooks
7261//===----------------------------------------------------------------------===//
7262
Dan Gohman8181bd12008-07-27 21:46:04 +00007263void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007264 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007265 APInt &KnownZero,
7266 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007267 const SelectionDAG &DAG,
7268 unsigned Depth) const {
7269 unsigned Opc = Op.getOpcode();
7270 assert((Opc >= ISD::BUILTIN_OP_END ||
7271 Opc == ISD::INTRINSIC_WO_CHAIN ||
7272 Opc == ISD::INTRINSIC_W_CHAIN ||
7273 Opc == ISD::INTRINSIC_VOID) &&
7274 "Should use MaskedValueIsZero if you don't know whether Op"
7275 " is a target node!");
7276
Dan Gohman1d79e432008-02-13 23:07:24 +00007277 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007278 switch (Opc) {
7279 default: break;
7280 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007281 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7282 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007283 break;
7284 }
7285}
7286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007287/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007288/// node is a GlobalAddress + offset.
7289bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7290 GlobalValue* &GA, int64_t &Offset) const{
7291 if (N->getOpcode() == X86ISD::Wrapper) {
7292 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007293 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007294 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007295 return true;
7296 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007297 }
Evan Chengef7be082008-05-12 19:56:52 +00007298 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007299}
7300
Evan Chengef7be082008-05-12 19:56:52 +00007301static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7302 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007303 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007304 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007305 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007306 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007307 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007308 return false;
7309}
7310
Dan Gohman8181bd12008-07-27 21:46:04 +00007311static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007312 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007313 SDNode *&Base,
7314 SelectionDAG &DAG, MachineFrameInfo *MFI,
7315 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007316 Base = NULL;
7317 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007318 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007319 if (Idx.getOpcode() == ISD::UNDEF) {
7320 if (!Base)
7321 return false;
7322 continue;
7323 }
7324
Dan Gohman8181bd12008-07-27 21:46:04 +00007325 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007326 if (!Elt.getNode() ||
7327 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007328 return false;
7329 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007330 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007331 if (Base->getOpcode() == ISD::UNDEF)
7332 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007333 continue;
7334 }
7335 if (Elt.getOpcode() == ISD::UNDEF)
7336 continue;
7337
Gabor Greif1c80d112008-08-28 21:40:38 +00007338 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007339 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007340 return false;
7341 }
7342 return true;
7343}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007344
7345/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7346/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7347/// if the load addresses are consecutive, non-overlapping, and in the right
7348/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007349static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007350 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007351 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007352 MVT VT = N->getValueType(0);
7353 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007354 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007355 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007356 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007357 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7358 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007359 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007360
Dan Gohman11821702007-07-27 17:16:43 +00007361 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007362 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007363 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007364 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007365 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7366 LD->getSrcValueOffset(), LD->isVolatile(),
7367 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007368}
7369
Evan Chengb6290462008-05-12 23:04:07 +00007370/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007371static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007372 const X86Subtarget *Subtarget,
7373 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007374 unsigned NumOps = N->getNumOperands();
7375
Evan Chenge9b9c672008-05-09 21:53:03 +00007376 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007377 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007378 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007379
Duncan Sands92c43912008-06-06 12:08:01 +00007380 MVT VT = N->getValueType(0);
7381 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007382 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7383 // We are looking for load i64 and zero extend. We want to transform
7384 // it before legalizer has a chance to expand it. Also look for i64
7385 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007386 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007387 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007388 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007389 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007390 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007391
7392 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007393 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007394 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007395 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007396 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007397 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007398 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007399 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007400 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007401
7402 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007403 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007404
7405 // Load must not be an extload.
7406 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007407 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007408
Evan Cheng6617eed2008-09-24 23:26:36 +00007409 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7410 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7411 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7412 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7413 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007414}
7415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007416/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007417static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007418 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007419 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007420
7421 // If we have SSE[12] support, try to form min/max nodes.
7422 if (Subtarget->hasSSE2() &&
7423 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7424 if (Cond.getOpcode() == ISD::SETCC) {
7425 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007426 SDValue LHS = N->getOperand(1);
7427 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007428 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7429
7430 unsigned Opcode = 0;
7431 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7432 switch (CC) {
7433 default: break;
7434 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7435 case ISD::SETULE:
7436 case ISD::SETLE:
7437 if (!UnsafeFPMath) break;
7438 // FALL THROUGH.
7439 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7440 case ISD::SETLT:
7441 Opcode = X86ISD::FMIN;
7442 break;
7443
7444 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7445 case ISD::SETUGT:
7446 case ISD::SETGT:
7447 if (!UnsafeFPMath) break;
7448 // FALL THROUGH.
7449 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7450 case ISD::SETGE:
7451 Opcode = X86ISD::FMAX;
7452 break;
7453 }
7454 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7455 switch (CC) {
7456 default: break;
7457 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7458 case ISD::SETUGT:
7459 case ISD::SETGT:
7460 if (!UnsafeFPMath) break;
7461 // FALL THROUGH.
7462 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7463 case ISD::SETGE:
7464 Opcode = X86ISD::FMIN;
7465 break;
7466
7467 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7468 case ISD::SETULE:
7469 case ISD::SETLE:
7470 if (!UnsafeFPMath) break;
7471 // FALL THROUGH.
7472 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7473 case ISD::SETLT:
7474 Opcode = X86ISD::FMAX;
7475 break;
7476 }
7477 }
7478
7479 if (Opcode)
7480 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7481 }
7482
7483 }
7484
Dan Gohman8181bd12008-07-27 21:46:04 +00007485 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007486}
7487
Chris Lattnerce84ae42008-02-22 02:09:43 +00007488/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007489static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007490 const X86Subtarget *Subtarget) {
7491 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7492 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007493 // A preferable solution to the general problem is to figure out the right
7494 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007495 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007496 if (St->getValue().getValueType().isVector() &&
7497 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007498 isa<LoadSDNode>(St->getValue()) &&
7499 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7500 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007501 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007502 LoadSDNode *Ld = 0;
7503 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007504 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007505 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007506 // Must be a store of a load. We currently handle two cases: the load
7507 // is a direct child, and it's under an intervening TokenFactor. It is
7508 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007509 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007510 Ld = cast<LoadSDNode>(St->getChain());
7511 else if (St->getValue().hasOneUse() &&
7512 ChainVal->getOpcode() == ISD::TokenFactor) {
7513 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007514 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007515 TokenFactorIndex = i;
7516 Ld = cast<LoadSDNode>(St->getValue());
7517 } else
7518 Ops.push_back(ChainVal->getOperand(i));
7519 }
7520 }
7521 if (Ld) {
7522 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7523 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007524 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007525 Ld->getBasePtr(), Ld->getSrcValue(),
7526 Ld->getSrcValueOffset(), Ld->isVolatile(),
7527 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007528 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007529 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007530 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007531 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7532 Ops.size());
7533 }
7534 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7535 St->getSrcValue(), St->getSrcValueOffset(),
7536 St->isVolatile(), St->getAlignment());
7537 }
7538
7539 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007540 SDValue LoAddr = Ld->getBasePtr();
7541 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007542 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007543
Dan Gohman8181bd12008-07-27 21:46:04 +00007544 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007545 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7546 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007547 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007548 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7549 Ld->isVolatile(),
7550 MinAlign(Ld->getAlignment(), 4));
7551
Dan Gohman8181bd12008-07-27 21:46:04 +00007552 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007553 if (TokenFactorIndex != -1) {
7554 Ops.push_back(LoLd);
7555 Ops.push_back(HiLd);
7556 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7557 Ops.size());
7558 }
7559
7560 LoAddr = St->getBasePtr();
7561 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007562 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007563
Dan Gohman8181bd12008-07-27 21:46:04 +00007564 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007565 St->getSrcValue(), St->getSrcValueOffset(),
7566 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007567 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007568 St->getSrcValue(),
7569 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007570 St->isVolatile(),
7571 MinAlign(St->getAlignment(), 4));
7572 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007573 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007574 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007575 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007576}
7577
Chris Lattner470d5dc2008-01-25 06:14:17 +00007578/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7579/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007580static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007581 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7582 // F[X]OR(0.0, x) -> x
7583 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007584 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7585 if (C->getValueAPF().isPosZero())
7586 return N->getOperand(1);
7587 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7588 if (C->getValueAPF().isPosZero())
7589 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007590 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007591}
7592
7593/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007594static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007595 // FAND(0.0, x) -> 0.0
7596 // FAND(x, 0.0) -> 0.0
7597 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7598 if (C->getValueAPF().isPosZero())
7599 return N->getOperand(0);
7600 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7601 if (C->getValueAPF().isPosZero())
7602 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007603 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007604}
7605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007606
Dan Gohman8181bd12008-07-27 21:46:04 +00007607SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007608 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007609 SelectionDAG &DAG = DCI.DAG;
7610 switch (N->getOpcode()) {
7611 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007612 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7613 case ISD::BUILD_VECTOR:
7614 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007615 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007616 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007617 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007618 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7619 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007620 }
7621
Dan Gohman8181bd12008-07-27 21:46:04 +00007622 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007623}
7624
7625//===----------------------------------------------------------------------===//
7626// X86 Inline Assembly Support
7627//===----------------------------------------------------------------------===//
7628
7629/// getConstraintType - Given a constraint letter, return the type of
7630/// constraint it is for this target.
7631X86TargetLowering::ConstraintType
7632X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7633 if (Constraint.size() == 1) {
7634 switch (Constraint[0]) {
7635 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00007636 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00007637 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007638 case 'r':
7639 case 'R':
7640 case 'l':
7641 case 'q':
7642 case 'Q':
7643 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007644 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007645 case 'Y':
7646 return C_RegisterClass;
7647 default:
7648 break;
7649 }
7650 }
7651 return TargetLowering::getConstraintType(Constraint);
7652}
7653
Dale Johannesene99fc902008-01-29 02:21:21 +00007654/// LowerXConstraint - try to replace an X constraint, which matches anything,
7655/// with another that has more specific requirements based on the type of the
7656/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007657const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007658LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007659 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7660 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007661 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007662 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007663 return "Y";
7664 if (Subtarget->hasSSE1())
7665 return "x";
7666 }
7667
7668 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007669}
7670
Chris Lattnera531abc2007-08-25 00:47:38 +00007671/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7672/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007673void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007674 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007675 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007676 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007677 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007678 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007680 switch (Constraint) {
7681 default: break;
7682 case 'I':
7683 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007684 if (C->getZExtValue() <= 31) {
7685 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007686 break;
7687 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007688 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007689 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007690 case 'J':
7691 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7692 if (C->getZExtValue() <= 63) {
7693 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7694 break;
7695 }
7696 }
7697 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007698 case 'N':
7699 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007700 if (C->getZExtValue() <= 255) {
7701 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007702 break;
7703 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007704 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007705 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007706 case 'i': {
7707 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007708 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007709 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007710 break;
7711 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007712
7713 // If we are in non-pic codegen mode, we allow the address of a global (with
7714 // an optional displacement) to be used with 'i'.
7715 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7716 int64_t Offset = 0;
7717
7718 // Match either (GA) or (GA+C)
7719 if (GA) {
7720 Offset = GA->getOffset();
7721 } else if (Op.getOpcode() == ISD::ADD) {
7722 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7723 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7724 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007725 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007726 } else {
7727 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7728 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7729 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007730 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007731 else
7732 C = 0, GA = 0;
7733 }
7734 }
7735
7736 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007737 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007738 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007739 else
7740 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7741 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007742 Result = Op;
7743 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007744 }
7745
7746 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007747 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007748 }
7749 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007750
Gabor Greif1c80d112008-08-28 21:40:38 +00007751 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007752 Ops.push_back(Result);
7753 return;
7754 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007755 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7756 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007757}
7758
7759std::vector<unsigned> X86TargetLowering::
7760getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007761 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007762 if (Constraint.size() == 1) {
7763 // FIXME: not handling fp-stack yet!
7764 switch (Constraint[0]) { // GCC X86 Constraint Letters
7765 default: break; // Unknown constraint letter
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007766 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7767 case 'Q': // Q_REGS
7768 if (VT == MVT::i32)
7769 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7770 else if (VT == MVT::i16)
7771 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7772 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007773 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007774 else if (VT == MVT::i64)
7775 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7776 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007777 }
7778 }
7779
7780 return std::vector<unsigned>();
7781}
7782
7783std::pair<unsigned, const TargetRegisterClass*>
7784X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007785 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007786 // First, see if this is a constraint that directly corresponds to an LLVM
7787 // register class.
7788 if (Constraint.size() == 1) {
7789 // GCC Constraint Letters
7790 switch (Constraint[0]) {
7791 default: break;
7792 case 'r': // GENERAL_REGS
7793 case 'R': // LEGACY_REGS
7794 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007795 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007796 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007797 if (VT == MVT::i16)
7798 return std::make_pair(0U, X86::GR16RegisterClass);
7799 if (VT == MVT::i32 || !Subtarget->is64Bit())
7800 return std::make_pair(0U, X86::GR32RegisterClass);
7801 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007802 case 'f': // FP Stack registers.
7803 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7804 // value to the correct fpstack register class.
7805 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7806 return std::make_pair(0U, X86::RFP32RegisterClass);
7807 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7808 return std::make_pair(0U, X86::RFP64RegisterClass);
7809 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007810 case 'y': // MMX_REGS if MMX allowed.
7811 if (!Subtarget->hasMMX()) break;
7812 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007813 case 'Y': // SSE_REGS if SSE2 allowed
7814 if (!Subtarget->hasSSE2()) break;
7815 // FALL THROUGH.
7816 case 'x': // SSE_REGS if SSE1 allowed
7817 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007818
7819 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007820 default: break;
7821 // Scalar SSE types.
7822 case MVT::f32:
7823 case MVT::i32:
7824 return std::make_pair(0U, X86::FR32RegisterClass);
7825 case MVT::f64:
7826 case MVT::i64:
7827 return std::make_pair(0U, X86::FR64RegisterClass);
7828 // Vector types.
7829 case MVT::v16i8:
7830 case MVT::v8i16:
7831 case MVT::v4i32:
7832 case MVT::v2i64:
7833 case MVT::v4f32:
7834 case MVT::v2f64:
7835 return std::make_pair(0U, X86::VR128RegisterClass);
7836 }
7837 break;
7838 }
7839 }
7840
7841 // Use the default implementation in TargetLowering to convert the register
7842 // constraint into a member of a register class.
7843 std::pair<unsigned, const TargetRegisterClass*> Res;
7844 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7845
7846 // Not found as a standard register?
7847 if (Res.second == 0) {
7848 // GCC calls "st(0)" just plain "st".
7849 if (StringsEqualNoCase("{st}", Constraint)) {
7850 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007851 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007852 }
Dale Johannesen73920c02008-11-13 21:52:36 +00007853 // 'A' means EAX + EDX.
7854 if (Constraint == "A") {
7855 Res.first = X86::EAX;
7856 Res.second = X86::GRADRegisterClass;
7857 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007858 return Res;
7859 }
7860
7861 // Otherwise, check to see if this is a register class of the wrong value
7862 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7863 // turn into {ax},{dx}.
7864 if (Res.second->hasType(VT))
7865 return Res; // Correct type already, nothing to do.
7866
7867 // All of the single-register GCC register classes map their values onto
7868 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7869 // really want an 8-bit or 32-bit register, map to the appropriate register
7870 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007871 if (Res.second == X86::GR16RegisterClass) {
7872 if (VT == MVT::i8) {
7873 unsigned DestReg = 0;
7874 switch (Res.first) {
7875 default: break;
7876 case X86::AX: DestReg = X86::AL; break;
7877 case X86::DX: DestReg = X86::DL; break;
7878 case X86::CX: DestReg = X86::CL; break;
7879 case X86::BX: DestReg = X86::BL; break;
7880 }
7881 if (DestReg) {
7882 Res.first = DestReg;
7883 Res.second = Res.second = X86::GR8RegisterClass;
7884 }
7885 } else if (VT == MVT::i32) {
7886 unsigned DestReg = 0;
7887 switch (Res.first) {
7888 default: break;
7889 case X86::AX: DestReg = X86::EAX; break;
7890 case X86::DX: DestReg = X86::EDX; break;
7891 case X86::CX: DestReg = X86::ECX; break;
7892 case X86::BX: DestReg = X86::EBX; break;
7893 case X86::SI: DestReg = X86::ESI; break;
7894 case X86::DI: DestReg = X86::EDI; break;
7895 case X86::BP: DestReg = X86::EBP; break;
7896 case X86::SP: DestReg = X86::ESP; break;
7897 }
7898 if (DestReg) {
7899 Res.first = DestReg;
7900 Res.second = Res.second = X86::GR32RegisterClass;
7901 }
7902 } else if (VT == MVT::i64) {
7903 unsigned DestReg = 0;
7904 switch (Res.first) {
7905 default: break;
7906 case X86::AX: DestReg = X86::RAX; break;
7907 case X86::DX: DestReg = X86::RDX; break;
7908 case X86::CX: DestReg = X86::RCX; break;
7909 case X86::BX: DestReg = X86::RBX; break;
7910 case X86::SI: DestReg = X86::RSI; break;
7911 case X86::DI: DestReg = X86::RDI; break;
7912 case X86::BP: DestReg = X86::RBP; break;
7913 case X86::SP: DestReg = X86::RSP; break;
7914 }
7915 if (DestReg) {
7916 Res.first = DestReg;
7917 Res.second = Res.second = X86::GR64RegisterClass;
7918 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007919 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007920 } else if (Res.second == X86::FR32RegisterClass ||
7921 Res.second == X86::FR64RegisterClass ||
7922 Res.second == X86::VR128RegisterClass) {
7923 // Handle references to XMM physical registers that got mapped into the
7924 // wrong class. This can happen with constraints like {xmm0} where the
7925 // target independent register mapper will just pick the first match it can
7926 // find, ignoring the required type.
7927 if (VT == MVT::f32)
7928 Res.second = X86::FR32RegisterClass;
7929 else if (VT == MVT::f64)
7930 Res.second = X86::FR64RegisterClass;
7931 else if (X86::VR128RegisterClass->hasType(VT))
7932 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007933 }
7934
7935 return Res;
7936}
Mon P Wang1448aad2008-10-30 08:01:45 +00007937
7938//===----------------------------------------------------------------------===//
7939// X86 Widen vector type
7940//===----------------------------------------------------------------------===//
7941
7942/// getWidenVectorType: given a vector type, returns the type to widen
7943/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7944/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00007945/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00007946/// scalarizing vs using the wider vector type.
7947
7948MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7949 assert(VT.isVector());
7950 if (isTypeLegal(VT))
7951 return VT;
7952
7953 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7954 // type based on element type. This would speed up our search (though
7955 // it may not be worth it since the size of the list is relatively
7956 // small).
7957 MVT EltVT = VT.getVectorElementType();
7958 unsigned NElts = VT.getVectorNumElements();
7959
7960 // On X86, it make sense to widen any vector wider than 1
7961 if (NElts <= 1)
7962 return MVT::Other;
7963
7964 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7965 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7966 MVT SVT = (MVT::SimpleValueType)nVT;
7967
7968 if (isTypeLegal(SVT) &&
7969 SVT.getVectorElementType() == EltVT &&
7970 SVT.getVectorNumElements() > NElts)
7971 return SVT;
7972 }
7973 return MVT::Other;
7974}