blob: 18d0f6c78cab79ac92351a0a3b3b25c33dfc3317 [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"
41using namespace llvm;
42
Evan Cheng2aea0b42008-04-25 19:11:04 +000043// Forward declarations.
Dan Gohman8181bd12008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng2aea0b42008-04-25 19:11:04 +000045
Dan Gohmanb41dfba2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 : TargetLowering(TM) {
48 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000052
Chris Lattnerdec9cb52008-01-24 08:07:48 +000053 bool Fast = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000056 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
63 setSchedulingPreference(SchedulingForRegPressure);
64 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
65 setStackPointerRegisterToSaveRestore(X86StackPtr);
66
67 if (Subtarget->isTargetDarwin()) {
68 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
69 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
71 } else if (Subtarget->isTargetMingw()) {
72 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
80 // Set up the register classes.
81 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
84 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
86
Duncan Sands082524c2008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Chris Lattner3bc08502008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
102
103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
106 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
118 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
127
Dale Johannesen958b08b2007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132
133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 } else {
143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
156 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
166
167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
172
Dan Gohman8450d862008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 }
237
238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
240
241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
244 // X86 wants to expand cmov itself.
245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
260 // X86 ret instruction may pop stack.
261 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
264 // Darwin ABI issue.
265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 }
278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Evan Cheng8d51ab32008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000290
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang078a62d2008-05-05 19:05:59 +0000294 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000299
Dale Johannesenbc187662008-08-28 02:44:49 +0000300 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000304
Dan Gohman472d12c2008-06-30 20:59:49 +0000305 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
306 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 // FIXME - use subtarget debug flags
308 if (!Subtarget->isTargetDarwin() &&
309 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000310 !Subtarget->isTargetCygMing()) {
311 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
312 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
313 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
317 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
318 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
319 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320 setExceptionPointerRegister(X86::RAX);
321 setExceptionSelectorRegister(X86::RDX);
322 } else {
323 setExceptionPointerRegister(X86::EAX);
324 setExceptionSelectorRegister(X86::EDX);
325 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000326 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000327 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
328
Duncan Sands7407a9f2007-09-11 14:10:23 +0000329 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000330
Chris Lattner56b941f2008-01-15 21:58:22 +0000331 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000332
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
334 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000335 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000336 if (Subtarget->is64Bit()) {
337 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000339 } else {
340 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000342 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
346 if (Subtarget->is64Bit())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
348 if (Subtarget->isTargetCygMing())
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
350 else
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
352
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000353 if (X86ScalarSSEf64) {
354 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 // Set up the FP register classes.
356 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
357 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
358
359 // Use ANDPD to simulate FABS.
360 setOperationAction(ISD::FABS , MVT::f64, Custom);
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
362
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f64, Custom);
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
367 // Use ANDPD and ORPD to simulate FCOPYSIGN.
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
369 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
370
371 // We don't support sin/cos/fmod
372 setOperationAction(ISD::FSIN , MVT::f64, Expand);
373 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376
377 // Expand FP immediates into loads from the stack, except for the special
378 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000379 addLegalFPImmediate(APFloat(+0.0)); // xorpd
380 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000381
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000382 // Floating truncations from f80 and extensions to f80 go through memory.
383 // If optimizing, we lie about this though and handle it in
384 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
385 if (Fast) {
386 setConvertAction(MVT::f32, MVT::f80, Expand);
387 setConvertAction(MVT::f64, MVT::f80, Expand);
388 setConvertAction(MVT::f80, MVT::f32, Expand);
389 setConvertAction(MVT::f80, MVT::f64, Expand);
390 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000391 } else if (X86ScalarSSEf32) {
392 // Use SSE for f32, x87 for f64.
393 // Set up the FP register classes.
394 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
395 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
396
397 // Use ANDPS to simulate FABS.
398 setOperationAction(ISD::FABS , MVT::f32, Custom);
399
400 // Use XORP to simulate FNEG.
401 setOperationAction(ISD::FNEG , MVT::f32, Custom);
402
403 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
404
405 // Use ANDPS and ORPS to simulate FCOPYSIGN.
406 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
407 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
408
409 // We don't support sin/cos/fmod
410 setOperationAction(ISD::FSIN , MVT::f32, Expand);
411 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000412
Nate Begemane2ba64f2008-02-14 08:57:00 +0000413 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000414 addLegalFPImmediate(APFloat(+0.0f)); // xorps
415 addLegalFPImmediate(APFloat(+0.0)); // FLD0
416 addLegalFPImmediate(APFloat(+1.0)); // FLD1
417 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
418 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
419
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000420 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
421 // this though and handle it in InstructionSelectPreprocess so that
422 // dagcombine2 can hack on these.
423 if (Fast) {
424 setConvertAction(MVT::f32, MVT::f64, Expand);
425 setConvertAction(MVT::f32, MVT::f80, Expand);
426 setConvertAction(MVT::f80, MVT::f32, Expand);
427 setConvertAction(MVT::f64, MVT::f32, Expand);
428 // And x87->x87 truncations also.
429 setConvertAction(MVT::f80, MVT::f64, Expand);
430 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000431
432 if (!UnsafeFPMath) {
433 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
434 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
435 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000437 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 // Set up the FP register classes.
439 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
440 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
441
442 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
443 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000446
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000447 // Floating truncations go through memory. If optimizing, we lie about
448 // this though and handle it in InstructionSelectPreprocess so that
449 // dagcombine2 can hack on these.
450 if (Fast) {
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000460 addLegalFPImmediate(APFloat(+0.0)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000464 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
465 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
466 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
467 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 }
469
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000470 // Long double always uses X87.
471 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000472 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000474 {
Chris Lattnerdd867392008-01-27 06:19:31 +0000475 APFloat TmpFlt(+0.0);
476 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
477 addLegalFPImmediate(TmpFlt); // FLD0
478 TmpFlt.changeSign();
479 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
480 APFloat TmpFlt2(+1.0);
481 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
486
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000491
Dan Gohman2f7b1982007-10-11 23:21:31 +0000492 // Always use a library call for pow.
493 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
494 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
496
Dale Johannesen92b33082008-09-04 00:47:13 +0000497 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000500 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
502
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 // First set operation action for all vector types to expand. Then we
504 // will selectively turn on ones that can be effectively codegen'd.
505 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
506 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000507 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000520 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
522 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000523 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000545 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 }
551
552 if (Subtarget->hasMMX()) {
553 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
554 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
555 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000556 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
558
559 // FIXME: add MMX packed arithmetics
560
561 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
562 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
563 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
564 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
565
566 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
567 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
568 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000569 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570
571 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
572 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
573
574 setOperationAction(ISD::AND, MVT::v8i8, Promote);
575 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
576 setOperationAction(ISD::AND, MVT::v4i16, Promote);
577 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
578 setOperationAction(ISD::AND, MVT::v2i32, Promote);
579 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
580 setOperationAction(ISD::AND, MVT::v1i64, Legal);
581
582 setOperationAction(ISD::OR, MVT::v8i8, Promote);
583 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
584 setOperationAction(ISD::OR, MVT::v4i16, Promote);
585 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
586 setOperationAction(ISD::OR, MVT::v2i32, Promote);
587 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
588 setOperationAction(ISD::OR, MVT::v1i64, Legal);
589
590 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
591 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
592 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
593 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
594 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
595 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
596 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
597
598 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
599 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
600 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
601 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
602 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
603 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000604 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
605 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
607
608 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
609 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
610 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000611 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000612 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
613
614 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
616 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
617 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
618
Evan Cheng759fe022008-07-22 18:39:19 +0000619 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
621 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000622 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000623
624 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 }
626
627 if (Subtarget->hasSSE1()) {
628 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
629
630 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
631 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
632 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
633 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
634 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
635 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
639 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
640 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000641 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 }
643
644 if (Subtarget->hasSSE2()) {
645 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
646 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
647 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
648 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
649 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
650
651 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
652 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
653 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
654 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
655 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
656 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
657 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
658 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
659 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
660 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
661 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
662 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
663 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
664 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
665 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666
Nate Begeman03605a02008-07-17 16:51:19 +0000667 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
668 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
669 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
670 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
673 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
674 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
675 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
677
678 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000679 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
680 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000681 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000682 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000683 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000684 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
685 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
686 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687 }
688 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
689 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
691 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000694 if (Subtarget->is64Bit()) {
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000697 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698
699 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
700 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000701 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
702 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
703 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
704 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
705 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
706 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
707 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
708 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
709 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
710 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712
Chris Lattner3bc08502008-01-17 19:59:44 +0000713 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715 // Custom lower v2i64 and v2f64 selects.
716 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
717 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
718 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
719 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000720
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000722
723 if (Subtarget->hasSSE41()) {
724 // FIXME: Do we need to handle scalar-to-vector here?
725 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000726 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000727
728 // i8 and i16 vectors are custom , because the source register and source
729 // source memory operand types are not the same width. f32 vectors are
730 // custom since the immediate controlling the insert encodes additional
731 // information.
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
734 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
736
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
738 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
739 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000741
742 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000745 }
746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747
Nate Begeman03605a02008-07-17 16:51:19 +0000748 if (Subtarget->hasSSE42()) {
749 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
750 }
751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 // We want to custom lower some of our intrinsics.
753 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
754
755 // We have target-specific dag combine patterns for the following nodes:
756 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000757 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000759 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000760
761 computeRegisterProperties();
762
763 // FIXME: These should be based on subtarget info. Plus, the values should
764 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000765 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
766 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
767 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000769 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770}
771
Scott Michel502151f2008-03-10 15:42:14 +0000772
Dan Gohman8181bd12008-07-27 21:46:04 +0000773MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000774 return MVT::i8;
775}
776
777
Evan Cheng5a67b812008-01-23 23:17:41 +0000778/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
779/// the desired ByVal argument alignment.
780static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
781 if (MaxAlign == 16)
782 return;
783 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
784 if (VTy->getBitWidth() == 128)
785 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000786 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
787 unsigned EltAlign = 0;
788 getMaxByValAlign(ATy->getElementType(), EltAlign);
789 if (EltAlign > MaxAlign)
790 MaxAlign = EltAlign;
791 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
792 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
793 unsigned EltAlign = 0;
794 getMaxByValAlign(STy->getElementType(i), EltAlign);
795 if (EltAlign > MaxAlign)
796 MaxAlign = EltAlign;
797 if (MaxAlign == 16)
798 break;
799 }
800 }
801 return;
802}
803
804/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
805/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000806/// that contain SSE vectors are placed at 16-byte boundaries while the rest
807/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000808unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000809 if (Subtarget->is64Bit()) {
810 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000811 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000812 if (TyAlign > 8)
813 return TyAlign;
814 return 8;
815 }
816
Evan Cheng5a67b812008-01-23 23:17:41 +0000817 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000818 if (Subtarget->hasSSE1())
819 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000820 return Align;
821}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000822
Evan Cheng8c590372008-05-15 08:39:06 +0000823/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000824/// and store operations as a result of memset, memcpy, and memmove
825/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000826/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000827MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000828X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
829 bool isSrcConst, bool isSrcStr) const {
830 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
831 return MVT::v4i32;
832 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
833 return MVT::v4f32;
834 if (Subtarget->is64Bit() && Size >= 8)
835 return MVT::i64;
836 return MVT::i32;
837}
838
839
Evan Cheng6fb06762007-11-09 01:32:10 +0000840/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
841/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000842SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000843 SelectionDAG &DAG) const {
844 if (usesGlobalOffsetTable())
845 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
846 if (!Subtarget->isPICStyleRIPRel())
847 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
848 return Table;
849}
850
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851//===----------------------------------------------------------------------===//
852// Return Value Calling Convention Implementation
853//===----------------------------------------------------------------------===//
854
855#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000856
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000858SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000859 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
860
861 SmallVector<CCValAssign, 16> RVLocs;
862 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
863 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
864 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000865 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 // If this is the first return lowered for this function, add the regs to the
868 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000869 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 for (unsigned i = 0; i != RVLocs.size(); ++i)
871 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000872 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000874 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000876 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000877 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000878 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000879 SDValue TailCall = Chain;
880 SDValue TargetAddress = TailCall.getOperand(1);
881 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000882 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000883 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000884 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000885 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000886 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
887 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000888 assert(StackAdjustment.getOpcode() == ISD::Constant &&
889 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000890
Dan Gohman8181bd12008-07-27 21:46:04 +0000891 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000892 Operands.push_back(Chain.getOperand(0));
893 Operands.push_back(TargetAddress);
894 Operands.push_back(StackAdjustment);
895 // Copy registers used by the call. Last operand is a flag so it is not
896 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000897 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000898 Operands.push_back(Chain.getOperand(i));
899 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000900 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
901 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000902 }
903
904 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000905 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000906
Dan Gohman8181bd12008-07-27 21:46:04 +0000907 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000908 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
909 // Operand #1 = Bytes To Pop
910 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
911
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000913 for (unsigned i = 0; i != RVLocs.size(); ++i) {
914 CCValAssign &VA = RVLocs[i];
915 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000916 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000917
Chris Lattnerb56cc342008-03-11 03:23:40 +0000918 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
919 // the RET instruction and handled by the FP Stackifier.
920 if (RVLocs[i].getLocReg() == X86::ST0 ||
921 RVLocs[i].getLocReg() == X86::ST1) {
922 // If this is a copy from an xmm register to ST(0), use an FPExtend to
923 // change the value to the FP stack register class.
924 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
925 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
926 RetOps.push_back(ValToCopy);
927 // Don't emit a copytoreg.
928 continue;
929 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000930
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000931 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000932 Flag = Chain.getValue(1);
933 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000934
935 // The x86-64 ABI for returning structs by value requires that we copy
936 // the sret argument into %rax for the return. We saved the argument into
937 // a virtual register in the entry block, so now we copy the value out
938 // and into %rax.
939 if (Subtarget->is64Bit() &&
940 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
941 MachineFunction &MF = DAG.getMachineFunction();
942 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
943 unsigned Reg = FuncInfo->getSRetReturnReg();
944 if (!Reg) {
945 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
946 FuncInfo->setSRetReturnReg(Reg);
947 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000948 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000949
950 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
951 Flag = Chain.getValue(1);
952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953
Chris Lattnerb56cc342008-03-11 03:23:40 +0000954 RetOps[0] = Chain; // Update chain.
955
956 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000957 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000958 RetOps.push_back(Flag);
959
960 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961}
962
963
964/// LowerCallResult - Lower the result values of an ISD::CALL into the
965/// appropriate copies out of appropriate physical registers. This assumes that
966/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
967/// being lowered. The returns a SDNode with the same number of values as the
968/// ISD::CALL.
969SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000970LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000971 unsigned CallingConv, SelectionDAG &DAG) {
972
973 // Assign locations to each value returned by this call.
974 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +0000975 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
977 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
978
Dan Gohman8181bd12008-07-27 21:46:04 +0000979 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980
981 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000982 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000983 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000984
985 // If this is a call to a function that returns an fp value on the floating
986 // point stack, but where we prefer to use the value in xmm registers, copy
987 // 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 +0000988 if ((RVLocs[i].getLocReg() == X86::ST0 ||
989 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000990 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
991 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000994 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
995 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000996 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000997 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +0000998
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000999 if (CopyVT != RVLocs[i].getValVT()) {
1000 // Round the F80 the right size, which also moves to the appropriate xmm
1001 // register.
1002 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1003 // This truncation won't change the value.
1004 DAG.getIntPtrConstant(1));
1005 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001006
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001007 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001008 }
Duncan Sands698842f2008-07-02 17:40:58 +00001009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 // Merge everything together with a MERGE_VALUES node.
1011 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001012 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001013 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014}
1015
1016
1017//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001018// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001019//===----------------------------------------------------------------------===//
1020// StdCall calling convention seems to be standard for many Windows' API
1021// routines and around. It differs from C calling convention just a little:
1022// callee should clean up the stack, not caller. Symbols should be also
1023// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001024// For info on fast calling convention see Fast Calling Convention (tail call)
1025// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026
1027/// AddLiveIn - This helper function adds the specified physical register to the
1028/// MachineFunction as a live in value. It also creates a corresponding virtual
1029/// register for it.
1030static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1031 const TargetRegisterClass *RC) {
1032 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001033 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1034 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035 return VReg;
1036}
1037
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001038/// CallIsStructReturn - Determines whether a CALL node uses struct return
1039/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001040static bool CallIsStructReturn(CallSDNode *TheCall) {
1041 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001042 if (!NumOps)
1043 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001044
Dan Gohman705e3f72008-09-13 01:54:27 +00001045 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001046}
1047
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001048/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1049/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001050static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001051 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001052 if (!NumArgs)
1053 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001054
1055 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001056}
1057
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001058/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1059/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001060/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001061bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001062 if (IsVarArg)
1063 return false;
1064
Dan Gohman705e3f72008-09-13 01:54:27 +00001065 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001066 default:
1067 return false;
1068 case CallingConv::X86_StdCall:
1069 return !Subtarget->is64Bit();
1070 case CallingConv::X86_FastCall:
1071 return !Subtarget->is64Bit();
1072 case CallingConv::Fast:
1073 return PerformTailCallOpt;
1074 }
1075}
1076
Dan Gohman705e3f72008-09-13 01:54:27 +00001077/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1078/// given CallingConvention value.
1079CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001080 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001081 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001082 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001083 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1084 return CC_X86_64_TailCall;
1085 else
1086 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001087 }
1088
Gordon Henriksen18ace102008-01-05 16:56:59 +00001089 if (CC == CallingConv::X86_FastCall)
1090 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001091 else if (CC == CallingConv::Fast)
1092 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001093 else
1094 return CC_X86_32_C;
1095}
1096
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001097/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1098/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001099NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001100X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001101 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001102 if (CC == CallingConv::X86_FastCall)
1103 return FastCall;
1104 else if (CC == CallingConv::X86_StdCall)
1105 return StdCall;
1106 return None;
1107}
1108
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001109
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001110/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1111/// in a register before calling.
1112bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1113 return !IsTailCall && !Is64Bit &&
1114 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT();
1116}
1117
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001118/// CallRequiresFnAddressInReg - Check whether the call requires the function
1119/// address to be loaded in a register.
1120bool
1121X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1122 return !Is64Bit && IsTailCall &&
1123 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1124 Subtarget->isPICStyleGOT();
1125}
1126
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001127/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1128/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001129/// the specific parameter attribute. The copy will be passed as a byval
1130/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001131static SDValue
1132CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001133 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001134 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001135 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001136 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001137}
1138
Dan Gohman8181bd12008-07-27 21:46:04 +00001139SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001140 const CCValAssign &VA,
1141 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001142 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001143 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001144 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001145 ISD::ArgFlagsTy Flags =
1146 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001147 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001148 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001149
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001150 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1151 // changed with more analysis.
1152 // In case of tail call optimization mark all arguments mutable. Since they
1153 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001154 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001155 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001156 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001157 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001158 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001159 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001160 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001161}
1162
Dan Gohman8181bd12008-07-27 21:46:04 +00001163SDValue
1164X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001165 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001166 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1167
1168 const Function* Fn = MF.getFunction();
1169 if (Fn->hasExternalLinkage() &&
1170 Subtarget->isTargetCygMing() &&
1171 Fn->getName() == "main")
1172 FuncInfo->setForceFramePointer(true);
1173
1174 // Decorate the function name.
1175 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001177 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001178 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001179 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001180 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001181 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001182 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001183
1184 assert(!(isVarArg && CC == CallingConv::Fast) &&
1185 "Var args not supported with calling convention fastcc");
1186
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 // Assign locations to all of the incoming arguments.
1188 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001189 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001190 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001191
Dan Gohman8181bd12008-07-27 21:46:04 +00001192 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 unsigned LastVal = ~0U;
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 CCValAssign &VA = ArgLocs[i];
1196 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1197 // places.
1198 assert(VA.getValNo() != LastVal &&
1199 "Don't support value assigned to multiple locs yet");
1200 LastVal = VA.getValNo();
1201
1202 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001203 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 TargetRegisterClass *RC;
1205 if (RegVT == MVT::i32)
1206 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001207 else if (Is64Bit && RegVT == MVT::i64)
1208 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001209 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001211 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001212 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001213 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001214 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001215 else if (RegVT.isVector()) {
1216 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001217 if (!Is64Bit)
1218 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1219 else {
1220 // Darwin calling convention passes MMX values in either GPRs or
1221 // XMMs in x86-64. Other targets pass them in memory.
1222 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1223 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1224 RegVT = MVT::v2i64;
1225 } else {
1226 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1227 RegVT = MVT::i64;
1228 }
1229 }
1230 } else {
1231 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001233
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001234 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001235 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236
1237 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1238 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1239 // right size.
1240 if (VA.getLocInfo() == CCValAssign::SExt)
1241 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243 else if (VA.getLocInfo() == CCValAssign::ZExt)
1244 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1245 DAG.getValueType(VA.getValVT()));
1246
1247 if (VA.getLocInfo() != CCValAssign::Full)
1248 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1249
Gordon Henriksen18ace102008-01-05 16:56:59 +00001250 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001251 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001252 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001253 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1254 else if (RC == X86::VR128RegisterClass) {
1255 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1256 DAG.getConstant(0, MVT::i64));
1257 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1258 }
1259 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001260
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 ArgValues.push_back(ArgValue);
1262 } else {
1263 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001264 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 }
1266 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001267
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001268 // The x86-64 ABI for returning structs by value requires that we copy
1269 // the sret argument into %rax for the return. Save the argument into
1270 // a virtual register so that we can access it from the return points.
1271 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1272 MachineFunction &MF = DAG.getMachineFunction();
1273 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1274 unsigned Reg = FuncInfo->getSRetReturnReg();
1275 if (!Reg) {
1276 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1277 FuncInfo->setSRetReturnReg(Reg);
1278 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001279 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001280 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1281 }
1282
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001283 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001284 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001285 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001286 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001287
1288 // If the function takes variable number of arguments, make a frame index for
1289 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001290 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001291 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1292 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1293 }
1294 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001295 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1296
1297 // FIXME: We should really autogenerate these arrays
1298 static const unsigned GPR64ArgRegsWin64[] = {
1299 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001300 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001301 static const unsigned XMMArgRegsWin64[] = {
1302 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1303 };
1304 static const unsigned GPR64ArgRegs64Bit[] = {
1305 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1306 };
1307 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001308 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1309 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1310 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001311 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1312
1313 if (IsWin64) {
1314 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1315 GPR64ArgRegs = GPR64ArgRegsWin64;
1316 XMMArgRegs = XMMArgRegsWin64;
1317 } else {
1318 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1319 GPR64ArgRegs = GPR64ArgRegs64Bit;
1320 XMMArgRegs = XMMArgRegs64Bit;
1321 }
1322 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1323 TotalNumIntRegs);
1324 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1325 TotalNumXMMRegs);
1326
Gordon Henriksen18ace102008-01-05 16:56:59 +00001327 // For X86-64, if there are vararg parameters that are passed via
1328 // registers, then we must store them to their spots on the stack so they
1329 // may be loaded by deferencing the result of va_next.
1330 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001331 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1332 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1333 TotalNumXMMRegs * 16, 16);
1334
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001336 SmallVector<SDValue, 8> MemOps;
1337 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1338 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001339 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001341 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1342 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001343 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1344 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001345 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001346 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001347 MemOps.push_back(Store);
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001350 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001351
Gordon Henriksen18ace102008-01-05 16:56:59 +00001352 // Now store the XMM (fp + vector) parameter registers.
1353 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001354 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001355 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1357 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001358 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1359 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001360 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001361 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 MemOps.push_back(Store);
1363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001364 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 }
1366 if (!MemOps.empty())
1367 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1368 &MemOps[0], MemOps.size());
1369 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001370 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001372 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001373
Gordon Henriksen18ace102008-01-05 16:56:59 +00001374 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001375 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001376 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001377 BytesCallerReserves = 0;
1378 } else {
1379 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001380 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001381 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 BytesCallerReserves = StackSize;
1384 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001385
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 if (!Is64Bit) {
1387 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1388 if (CC == CallingConv::X86_FastCall)
1389 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001391
Anton Korobeynikove844e472007-08-15 17:12:32 +00001392 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001393
1394 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001395 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001396 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001397}
1398
Dan Gohman8181bd12008-07-27 21:46:04 +00001399SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001400X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001401 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001402 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001403 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001404 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001405 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001406 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001407 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001408 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001409 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001410 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001411 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001412 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001413}
1414
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001415/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1416/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001417SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001418X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001419 SDValue &OutRetAddr,
1420 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001421 bool IsTailCall,
1422 bool Is64Bit,
1423 int FPDiff) {
1424 if (!IsTailCall || FPDiff==0) return Chain;
1425
1426 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001427 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001428 OutRetAddr = getReturnAddressFrameIndex(DAG);
1429 // Load the "old" Return address.
1430 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001431 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432}
1433
1434/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1435/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001436static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001437EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001438 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001439 bool Is64Bit, int FPDiff) {
1440 // Store the return address to the appropriate stack slot.
1441 if (!FPDiff) return Chain;
1442 // Calculate the new stack slot for the return address.
1443 int SlotSize = Is64Bit ? 8 : 4;
1444 int NewReturnAddrFI =
1445 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001446 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001447 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001448 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001449 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450 return Chain;
1451}
1452
Dan Gohman8181bd12008-07-27 21:46:04 +00001453SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001454 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001455 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1456 SDValue Chain = TheCall->getChain();
1457 unsigned CC = TheCall->getCallingConv();
1458 bool isVarArg = TheCall->isVarArg();
1459 bool IsTailCall = TheCall->isTailCall() &&
1460 CC == CallingConv::Fast && PerformTailCallOpt;
1461 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001462 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001463 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001464
1465 assert(!(isVarArg && CC == CallingConv::Fast) &&
1466 "Var args not supported with calling convention fastcc");
1467
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001468 // Analyze operands of the call, assigning locations to each operand.
1469 SmallVector<CCValAssign, 16> ArgLocs;
1470 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001471 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472
1473 // Get a count of how many bytes are to be pushed on the stack.
1474 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001475 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001476 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477
Gordon Henriksen18ace102008-01-05 16:56:59 +00001478 int FPDiff = 0;
1479 if (IsTailCall) {
1480 // Lower arguments at fp - stackoffset + fpdiff.
1481 unsigned NumBytesCallerPushed =
1482 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1483 FPDiff = NumBytesCallerPushed - NumBytes;
1484
1485 // Set the delta of movement of the returnaddr stackslot.
1486 // But only set if delta is greater than previous delta.
1487 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1488 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1489 }
1490
Chris Lattner5872a362008-01-17 07:00:52 +00001491 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492
Dan Gohman8181bd12008-07-27 21:46:04 +00001493 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001494 // Load return adress for tail calls.
1495 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1496 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001497
Dan Gohman8181bd12008-07-27 21:46:04 +00001498 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1499 SmallVector<SDValue, 8> MemOpChains;
1500 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001502 // Walk the register/memloc assignments, inserting copies/loads. In the case
1503 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1505 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001506 SDValue Arg = TheCall->getArg(i);
1507 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1508 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001509
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001510 // Promote the value if needed.
1511 switch (VA.getLocInfo()) {
1512 default: assert(0 && "Unknown loc info!");
1513 case CCValAssign::Full: break;
1514 case CCValAssign::SExt:
1515 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1516 break;
1517 case CCValAssign::ZExt:
1518 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1519 break;
1520 case CCValAssign::AExt:
1521 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1522 break;
1523 }
1524
1525 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001526 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001527 MVT RegVT = VA.getLocVT();
1528 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001529 switch (VA.getLocReg()) {
1530 default:
1531 break;
1532 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1533 case X86::R8: {
1534 // Special case: passing MMX values in GPR registers.
1535 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1536 break;
1537 }
1538 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1539 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1540 // Special case: passing MMX values in XMM registers.
1541 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1542 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1543 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1544 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1545 getMOVLMask(2, DAG));
1546 break;
1547 }
1548 }
1549 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1551 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001552 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001553 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001554 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001555 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1556
Dan Gohman705e3f72008-09-13 01:54:27 +00001557 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1558 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 }
1561 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001562
1563 if (!MemOpChains.empty())
1564 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1565 &MemOpChains[0], MemOpChains.size());
1566
1567 // Build a sequence of copy-to-reg nodes chained together with token chain
1568 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001569 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001570 // Tail call byval lowering might overwrite argument registers so in case of
1571 // tail call optimization the copies to registers are lowered later.
1572 if (!IsTailCall)
1573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1574 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1575 InFlag);
1576 InFlag = Chain.getValue(1);
1577 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001578
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001580 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001581 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1582 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1583 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1584 InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001587 // If we are tail calling and generating PIC/GOT style code load the address
1588 // of the callee into ecx. The value in ecx is used as target of the tail
1589 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1590 // calls on PIC/GOT architectures. Normally we would just put the address of
1591 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1592 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001593 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001594 // Note: The actual moving to ecx is done further down.
1595 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1596 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1597 !G->getGlobal()->hasProtectedVisibility())
1598 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001599 else if (isa<ExternalSymbolSDNode>(Callee))
1600 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001602
Gordon Henriksen18ace102008-01-05 16:56:59 +00001603 if (Is64Bit && isVarArg) {
1604 // From AMD64 ABI document:
1605 // For calls that may call functions that use varargs or stdargs
1606 // (prototype-less calls or calls to functions containing ellipsis (...) in
1607 // the declaration) %al is used as hidden argument to specify the number
1608 // of SSE registers used. The contents of %al do not need to match exactly
1609 // the number of registers, but must be an ubound on the number of SSE
1610 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001611
1612 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001613 // Count the number of XMM registers allocated.
1614 static const unsigned XMMArgRegs[] = {
1615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1616 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1617 };
1618 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1619
1620 Chain = DAG.getCopyToReg(Chain, X86::AL,
1621 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
1624
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001625
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001626 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001627 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001628 SmallVector<SDValue, 8> MemOpChains2;
1629 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001630 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001631 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001632 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1634 CCValAssign &VA = ArgLocs[i];
1635 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001636 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001637 SDValue Arg = TheCall->getArg(i);
1638 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001639 // Create frame index.
1640 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001641 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001643 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001644
Duncan Sandsc93fae32008-03-21 09:14:45 +00001645 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001646 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001648 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001649 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1650 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1651
1652 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001653 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001655 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001656 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001657 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001658 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001659 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 }
1661 }
1662
1663 if (!MemOpChains2.empty())
1664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001665 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001666
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001667 // Copy arguments to their registers.
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1669 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1670 InFlag);
1671 InFlag = Chain.getValue(1);
1672 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001673 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001674
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001676 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1677 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001678 }
1679
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001680 // If the callee is a GlobalAddress node (quite common, every direct call is)
1681 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1683 // We should use extra load for direct calls to dllimported functions in
1684 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001685 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1686 getTargetMachine(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001687 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendlingfef06052008-09-16 21:48:12 +00001688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1689 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001690 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001691 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001692
1693 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001694 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695 Callee,InFlag);
1696 Callee = DAG.getRegister(Opc, getPointerTy());
1697 // Add register as live out.
1698 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001699 }
1700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 // Returns a chain & a flag for retval copy to use.
1702 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704
1705 if (IsTailCall) {
1706 Ops.push_back(Chain);
Chris Lattner5872a362008-01-17 07:00:52 +00001707 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1708 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001709 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001710 Ops.push_back(InFlag);
1711 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1713
1714 // Returns a chain & a flag for retval copy to use.
1715 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1716 Ops.clear();
1717 }
1718
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Callee);
1721
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722 if (IsTailCall)
1723 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 // Add argument registers to the end of the list so that they are known live
1726 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001727 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1728 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1729 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001730
Evan Cheng8ba45e62008-03-18 23:36:35 +00001731 // Add an implicit use GOT pointer in EBX.
1732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1736
1737 // Add an implicit use of AL for x86 vararg functions.
1738 if (Is64Bit && isVarArg)
1739 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1740
Gabor Greif1c80d112008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001742 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001743
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001745 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001746 "Flag must be set. Depend on flag being set in LowerRET");
1747 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001748 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001749
Gabor Greif1c80d112008-08-28 21:40:38 +00001750 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001751 }
1752
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001753 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754 InFlag = Chain.getValue(1);
1755
1756 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001757 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001758 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001759 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001760 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 // If this is is a call to a struct-return function, the callee
1762 // pops the hidden struct pointer, so we have to push it back.
1763 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001765 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001767
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001768 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001769 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner5872a362008-01-17 07:00:52 +00001770 DAG.getIntPtrConstant(NumBytes),
1771 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001772 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001773 InFlag = Chain.getValue(1);
1774
1775 // Handle result values, copying them out of physregs into vregs that we
1776 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001777 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001778 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001779}
1780
1781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001817 MachineFunction &MF = DAG.getMachineFunction();
1818 const TargetMachine &TM = MF.getTarget();
1819 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1820 unsigned StackAlignment = TFI.getStackAlignment();
1821 uint64_t AlignMask = StackAlignment - 1;
1822 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001823 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001824 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1825 // Number smaller than 12 so just add the difference.
1826 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1827 } else {
1828 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1829 Offset = ((~AlignMask) & Offset) + StackAlignment +
1830 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001831 }
Evan Chengded8f902008-09-07 09:07:23 +00001832 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001833}
1834
1835/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001836/// following the call is a return. A function is eligible if caller/callee
1837/// calling conventions match, currently only fastcc supports tail calls, and
1838/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001839bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001840 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001841 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001842 if (!PerformTailCallOpt)
1843 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001844
Dan Gohman705e3f72008-09-13 01:54:27 +00001845 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001846 MachineFunction &MF = DAG.getMachineFunction();
1847 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001848 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001849 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001850 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001851 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001852 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001853 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001854 return true;
1855
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001856 // Can only do local tail calls (in same module, hidden or protected) on
1857 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001858 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1859 return G->getGlobal()->hasHiddenVisibility()
1860 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001861 }
1862 }
Evan Chenge7a87392007-11-02 01:26:22 +00001863
1864 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001865}
1866
Dan Gohmanca4857a2008-09-03 23:12:08 +00001867FastISel *
1868X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001869 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001870 DenseMap<const Value *, unsigned> &vm,
1871 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001872 MachineBasicBlock *> &bm,
1873 DenseMap<const AllocaInst *, int> &am) {
1874
Dan Gohman76dd96e2008-09-23 21:53:34 +00001875 return X86::createFastISel(mf, mmo, vm, bm, am);
Dan Gohman97805ee2008-08-19 21:32:53 +00001876}
1877
1878
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879//===----------------------------------------------------------------------===//
1880// Other Lowering Hooks
1881//===----------------------------------------------------------------------===//
1882
1883
Dan Gohman8181bd12008-07-27 21:46:04 +00001884SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001885 MachineFunction &MF = DAG.getMachineFunction();
1886 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1887 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001888 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001889
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 if (ReturnAddrIndex == 0) {
1891 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001892 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001893 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 }
1895
1896 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1897}
1898
1899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001900/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1901/// specific condition code. It returns a false if it cannot do a direct
1902/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1903/// needed.
1904static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001905 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001906 SelectionDAG &DAG) {
1907 X86CC = X86::COND_INVALID;
1908 if (!isFP) {
1909 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1910 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1911 // X > -1 -> X == 0, jump !sign.
1912 RHS = DAG.getConstant(0, RHS.getValueType());
1913 X86CC = X86::COND_NS;
1914 return true;
1915 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1916 // X < 0 -> X == 0, jump on sign.
1917 X86CC = X86::COND_S;
1918 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001919 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001920 // X < 1 -> X <= 0
1921 RHS = DAG.getConstant(0, RHS.getValueType());
1922 X86CC = X86::COND_LE;
1923 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 }
1925 }
1926
1927 switch (SetCCOpcode) {
1928 default: break;
1929 case ISD::SETEQ: X86CC = X86::COND_E; break;
1930 case ISD::SETGT: X86CC = X86::COND_G; break;
1931 case ISD::SETGE: X86CC = X86::COND_GE; break;
1932 case ISD::SETLT: X86CC = X86::COND_L; break;
1933 case ISD::SETLE: X86CC = X86::COND_LE; break;
1934 case ISD::SETNE: X86CC = X86::COND_NE; break;
1935 case ISD::SETULT: X86CC = X86::COND_B; break;
1936 case ISD::SETUGT: X86CC = X86::COND_A; break;
1937 case ISD::SETULE: X86CC = X86::COND_BE; break;
1938 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1939 }
1940 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001941 // First determine if it requires or is profitable to flip the operands.
1942 bool Flip = false;
1943 switch (SetCCOpcode) {
1944 default: break;
1945 case ISD::SETOLT:
1946 case ISD::SETOLE:
1947 case ISD::SETUGT:
1948 case ISD::SETUGE:
1949 Flip = true;
1950 break;
1951 }
1952
1953 // If LHS is a foldable load, but RHS is not, flip the condition.
1954 if (!Flip &&
1955 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1956 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1957 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1958 Flip = true;
1959 }
1960 if (Flip)
1961 std::swap(LHS, RHS);
1962
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001963 // On a floating point condition, the flags are set as follows:
1964 // ZF PF CF op
1965 // 0 | 0 | 0 | X > Y
1966 // 0 | 0 | 1 | X < Y
1967 // 1 | 0 | 0 | X == Y
1968 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 switch (SetCCOpcode) {
1970 default: break;
1971 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00001972 case ISD::SETEQ:
1973 X86CC = X86::COND_E;
1974 break;
1975 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001976 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00001977 case ISD::SETGT:
1978 X86CC = X86::COND_A;
1979 break;
1980 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001981 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00001982 case ISD::SETGE:
1983 X86CC = X86::COND_AE;
1984 break;
1985 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001986 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00001987 case ISD::SETLT:
1988 X86CC = X86::COND_B;
1989 break;
1990 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001991 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00001992 case ISD::SETLE:
1993 X86CC = X86::COND_BE;
1994 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00001996 case ISD::SETNE:
1997 X86CC = X86::COND_NE;
1998 break;
1999 case ISD::SETUO:
2000 X86CC = X86::COND_P;
2001 break;
2002 case ISD::SETO:
2003 X86CC = X86::COND_NP;
2004 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 }
Evan Chengfc937c92008-08-28 23:48:31 +00002006 }
2007
Evan Chengc6162692008-08-29 22:13:21 +00002008 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002009}
2010
2011/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2012/// code. Current x86 isa includes the following FP cmov instructions:
2013/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2014static bool hasFPCMov(unsigned X86CC) {
2015 switch (X86CC) {
2016 default:
2017 return false;
2018 case X86::COND_B:
2019 case X86::COND_BE:
2020 case X86::COND_E:
2021 case X86::COND_P:
2022 case X86::COND_A:
2023 case X86::COND_AE:
2024 case X86::COND_NE:
2025 case X86::COND_NP:
2026 return true;
2027 }
2028}
2029
2030/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2031/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002032static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 if (Op.getOpcode() == ISD::UNDEF)
2034 return true;
2035
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002036 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 return (Val >= Low && Val < Hi);
2038}
2039
2040/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2041/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002042static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 if (Op.getOpcode() == ISD::UNDEF)
2044 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002045 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046}
2047
2048/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2049/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2050bool X86::isPSHUFDMask(SDNode *N) {
2051 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2052
Dan Gohman7dc19012007-08-02 21:17:01 +00002053 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054 return false;
2055
2056 // Check if the value doesn't reference the second vector.
2057 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002058 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002059 if (Arg.getOpcode() == ISD::UNDEF) continue;
2060 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002061 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062 return false;
2063 }
2064
2065 return true;
2066}
2067
2068/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2069/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2070bool X86::isPSHUFHWMask(SDNode *N) {
2071 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2072
2073 if (N->getNumOperands() != 8)
2074 return false;
2075
2076 // Lower quadword copied in order.
2077 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002078 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002079 if (Arg.getOpcode() == ISD::UNDEF) continue;
2080 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002081 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002082 return false;
2083 }
2084
2085 // Upper quadword shuffled.
2086 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002087 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 if (Arg.getOpcode() == ISD::UNDEF) continue;
2089 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002090 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 if (Val < 4 || Val > 7)
2092 return false;
2093 }
2094
2095 return true;
2096}
2097
2098/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2099/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2100bool X86::isPSHUFLWMask(SDNode *N) {
2101 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2102
2103 if (N->getNumOperands() != 8)
2104 return false;
2105
2106 // Upper quadword copied in order.
2107 for (unsigned i = 4; i != 8; ++i)
2108 if (!isUndefOrEqual(N->getOperand(i), i))
2109 return false;
2110
2111 // Lower quadword shuffled.
2112 for (unsigned i = 0; i != 4; ++i)
2113 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2114 return false;
2115
2116 return true;
2117}
2118
2119/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2120/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002121static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002122 if (NumElems != 2 && NumElems != 4) return false;
2123
2124 unsigned Half = NumElems / 2;
2125 for (unsigned i = 0; i < Half; ++i)
2126 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2127 return false;
2128 for (unsigned i = Half; i < NumElems; ++i)
2129 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2130 return false;
2131
2132 return true;
2133}
2134
2135bool X86::isSHUFPMask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2138}
2139
2140/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2141/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2142/// half elements to come from vector 1 (which would equal the dest.) and
2143/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002144static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 if (NumOps != 2 && NumOps != 4) return false;
2146
2147 unsigned Half = NumOps / 2;
2148 for (unsigned i = 0; i < Half; ++i)
2149 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2150 return false;
2151 for (unsigned i = Half; i < NumOps; ++i)
2152 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2153 return false;
2154 return true;
2155}
2156
2157static bool isCommutedSHUFP(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2160}
2161
2162/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2163/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2164bool X86::isMOVHLPSMask(SDNode *N) {
2165 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2166
2167 if (N->getNumOperands() != 4)
2168 return false;
2169
2170 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2171 return isUndefOrEqual(N->getOperand(0), 6) &&
2172 isUndefOrEqual(N->getOperand(1), 7) &&
2173 isUndefOrEqual(N->getOperand(2), 2) &&
2174 isUndefOrEqual(N->getOperand(3), 3);
2175}
2176
2177/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2178/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2179/// <2, 3, 2, 3>
2180bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2181 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2182
2183 if (N->getNumOperands() != 4)
2184 return false;
2185
2186 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2187 return isUndefOrEqual(N->getOperand(0), 2) &&
2188 isUndefOrEqual(N->getOperand(1), 3) &&
2189 isUndefOrEqual(N->getOperand(2), 2) &&
2190 isUndefOrEqual(N->getOperand(3), 3);
2191}
2192
2193/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2194/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2195bool X86::isMOVLPMask(SDNode *N) {
2196 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2197
2198 unsigned NumElems = N->getNumOperands();
2199 if (NumElems != 2 && NumElems != 4)
2200 return false;
2201
2202 for (unsigned i = 0; i < NumElems/2; ++i)
2203 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2204 return false;
2205
2206 for (unsigned i = NumElems/2; i < NumElems; ++i)
2207 if (!isUndefOrEqual(N->getOperand(i), i))
2208 return false;
2209
2210 return true;
2211}
2212
2213/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2214/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2215/// and MOVLHPS.
2216bool X86::isMOVHPMask(SDNode *N) {
2217 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2218
2219 unsigned NumElems = N->getNumOperands();
2220 if (NumElems != 2 && NumElems != 4)
2221 return false;
2222
2223 for (unsigned i = 0; i < NumElems/2; ++i)
2224 if (!isUndefOrEqual(N->getOperand(i), i))
2225 return false;
2226
2227 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002228 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 if (!isUndefOrEqual(Arg, i + NumElems))
2230 return false;
2231 }
2232
2233 return true;
2234}
2235
2236/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2237/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002238bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 bool V2IsSplat = false) {
2240 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2241 return false;
2242
2243 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002244 SDValue BitI = Elts[i];
2245 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002246 if (!isUndefOrEqual(BitI, j))
2247 return false;
2248 if (V2IsSplat) {
2249 if (isUndefOrEqual(BitI1, NumElts))
2250 return false;
2251 } else {
2252 if (!isUndefOrEqual(BitI1, j + NumElts))
2253 return false;
2254 }
2255 }
2256
2257 return true;
2258}
2259
2260bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2261 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2262 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2263}
2264
2265/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2266/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002267bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002268 bool V2IsSplat = false) {
2269 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2270 return false;
2271
2272 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002273 SDValue BitI = Elts[i];
2274 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002275 if (!isUndefOrEqual(BitI, j + NumElts/2))
2276 return false;
2277 if (V2IsSplat) {
2278 if (isUndefOrEqual(BitI1, NumElts))
2279 return false;
2280 } else {
2281 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2282 return false;
2283 }
2284 }
2285
2286 return true;
2287}
2288
2289bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2290 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2291 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2292}
2293
2294/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2295/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2296/// <0, 0, 1, 1>
2297bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299
2300 unsigned NumElems = N->getNumOperands();
2301 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2302 return false;
2303
2304 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002305 SDValue BitI = N->getOperand(i);
2306 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002307
2308 if (!isUndefOrEqual(BitI, j))
2309 return false;
2310 if (!isUndefOrEqual(BitI1, j))
2311 return false;
2312 }
2313
2314 return true;
2315}
2316
2317/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2318/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2319/// <2, 2, 3, 3>
2320bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2322
2323 unsigned NumElems = N->getNumOperands();
2324 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2325 return false;
2326
2327 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002328 SDValue BitI = N->getOperand(i);
2329 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330
2331 if (!isUndefOrEqual(BitI, j))
2332 return false;
2333 if (!isUndefOrEqual(BitI1, j))
2334 return false;
2335 }
2336
2337 return true;
2338}
2339
2340/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2341/// specifies a shuffle of elements that is suitable for input to MOVSS,
2342/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002343static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002344 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002345 return false;
2346
2347 if (!isUndefOrEqual(Elts[0], NumElts))
2348 return false;
2349
2350 for (unsigned i = 1; i < NumElts; ++i) {
2351 if (!isUndefOrEqual(Elts[i], i))
2352 return false;
2353 }
2354
2355 return true;
2356}
2357
2358bool X86::isMOVLMask(SDNode *N) {
2359 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2360 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2361}
2362
2363/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2364/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2365/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002366static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002367 bool V2IsSplat = false,
2368 bool V2IsUndef = false) {
2369 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2370 return false;
2371
2372 if (!isUndefOrEqual(Ops[0], 0))
2373 return false;
2374
2375 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002376 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002377 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2378 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2379 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2380 return false;
2381 }
2382
2383 return true;
2384}
2385
2386static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2387 bool V2IsUndef = false) {
2388 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2389 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2390 V2IsSplat, V2IsUndef);
2391}
2392
2393/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2394/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2395bool X86::isMOVSHDUPMask(SDNode *N) {
2396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2397
2398 if (N->getNumOperands() != 4)
2399 return false;
2400
2401 // Expect 1, 1, 3, 3
2402 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002403 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 if (Arg.getOpcode() == ISD::UNDEF) continue;
2405 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002406 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 if (Val != 1) return false;
2408 }
2409
2410 bool HasHi = false;
2411 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002412 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002413 if (Arg.getOpcode() == ISD::UNDEF) continue;
2414 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002415 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 if (Val != 3) return false;
2417 HasHi = true;
2418 }
2419
2420 // Don't use movshdup if it can be done with a shufps.
2421 return HasHi;
2422}
2423
2424/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2425/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2426bool X86::isMOVSLDUPMask(SDNode *N) {
2427 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2428
2429 if (N->getNumOperands() != 4)
2430 return false;
2431
2432 // Expect 0, 0, 2, 2
2433 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002434 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002435 if (Arg.getOpcode() == ISD::UNDEF) continue;
2436 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002437 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 if (Val != 0) return false;
2439 }
2440
2441 bool HasHi = false;
2442 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002443 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002444 if (Arg.getOpcode() == ISD::UNDEF) continue;
2445 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002446 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002447 if (Val != 2) return false;
2448 HasHi = true;
2449 }
2450
2451 // Don't use movshdup if it can be done with a shufps.
2452 return HasHi;
2453}
2454
2455/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2456/// specifies a identity operation on the LHS or RHS.
2457static bool isIdentityMask(SDNode *N, bool RHS = false) {
2458 unsigned NumElems = N->getNumOperands();
2459 for (unsigned i = 0; i < NumElems; ++i)
2460 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2461 return false;
2462 return true;
2463}
2464
2465/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2466/// a splat of a single element.
2467static bool isSplatMask(SDNode *N) {
2468 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2469
2470 // This is a splat operation if each element of the permute is the same, and
2471 // if the value doesn't reference the second vector.
2472 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002473 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 unsigned i = 0;
2475 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002476 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 if (isa<ConstantSDNode>(Elt)) {
2478 ElementBase = Elt;
2479 break;
2480 }
2481 }
2482
Gabor Greif1c80d112008-08-28 21:40:38 +00002483 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002484 return false;
2485
2486 for (; i != NumElems; ++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!");
2490 if (Arg != ElementBase) return false;
2491 }
2492
2493 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002494 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002495}
2496
2497/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2498/// a splat of a single element and it's a 2 or 4 element mask.
2499bool X86::isSplatMask(SDNode *N) {
2500 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2501
2502 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2503 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2504 return false;
2505 return ::isSplatMask(N);
2506}
2507
2508/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2509/// specifies a splat of zero element.
2510bool X86::isSplatLoMask(SDNode *N) {
2511 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2512
2513 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2514 if (!isUndefOrEqual(N->getOperand(i), 0))
2515 return false;
2516 return true;
2517}
2518
2519/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2520/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2521/// instructions.
2522unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2523 unsigned NumOperands = N->getNumOperands();
2524 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2525 unsigned Mask = 0;
2526 for (unsigned i = 0; i < NumOperands; ++i) {
2527 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002528 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002530 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 if (Val >= NumOperands) Val -= NumOperands;
2532 Mask |= Val;
2533 if (i != NumOperands - 1)
2534 Mask <<= Shift;
2535 }
2536
2537 return Mask;
2538}
2539
2540/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2541/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2542/// instructions.
2543unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2544 unsigned Mask = 0;
2545 // 8 nodes, but we only care about the last 4.
2546 for (unsigned i = 7; i >= 4; --i) {
2547 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002548 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002549 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002550 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551 Mask |= (Val - 4);
2552 if (i != 4)
2553 Mask <<= 2;
2554 }
2555
2556 return Mask;
2557}
2558
2559/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2560/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2561/// instructions.
2562unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2563 unsigned Mask = 0;
2564 // 8 nodes, but we only care about the first 4.
2565 for (int i = 3; i >= 0; --i) {
2566 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002567 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002569 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 Mask |= Val;
2571 if (i != 0)
2572 Mask <<= 2;
2573 }
2574
2575 return Mask;
2576}
2577
2578/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2579/// specifies a 8 element shuffle that can be broken into a pair of
2580/// PSHUFHW and PSHUFLW.
2581static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2582 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2583
2584 if (N->getNumOperands() != 8)
2585 return false;
2586
2587 // Lower quadword shuffled.
2588 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002589 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002590 if (Arg.getOpcode() == ISD::UNDEF) continue;
2591 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002592 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002593 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 return false;
2595 }
2596
2597 // Upper quadword shuffled.
2598 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002599 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002600 if (Arg.getOpcode() == ISD::UNDEF) continue;
2601 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002602 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603 if (Val < 4 || Val > 7)
2604 return false;
2605 }
2606
2607 return true;
2608}
2609
Chris Lattnere6aa3862007-11-25 00:24:49 +00002610/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002611/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002612static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2613 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002614 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002615 MVT VT = Op.getValueType();
2616 MVT MaskVT = Mask.getValueType();
2617 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002619 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620
2621 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002622 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 if (Arg.getOpcode() == ISD::UNDEF) {
2624 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2625 continue;
2626 }
2627 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002628 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 if (Val < NumElems)
2630 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2631 else
2632 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2633 }
2634
2635 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002636 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2638}
2639
Evan Chenga6769df2007-12-07 21:30:01 +00002640/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2641/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002642static
Dan Gohman8181bd12008-07-27 21:46:04 +00002643SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002644 MVT MaskVT = Mask.getValueType();
2645 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002646 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002647 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002648 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002649 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002650 if (Arg.getOpcode() == ISD::UNDEF) {
2651 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2652 continue;
2653 }
2654 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002655 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002656 if (Val < NumElems)
2657 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2658 else
2659 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2660 }
2661 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2662}
2663
2664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2666/// match movhlps. The lower half elements should come from upper half of
2667/// V1 (and in order), and the upper half elements should come from the upper
2668/// half of V2 (and in order).
2669static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2670 unsigned NumElems = Mask->getNumOperands();
2671 if (NumElems != 4)
2672 return false;
2673 for (unsigned i = 0, e = 2; i != e; ++i)
2674 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2675 return false;
2676 for (unsigned i = 2; i != 4; ++i)
2677 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2678 return false;
2679 return true;
2680}
2681
2682/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002683/// is promoted to a vector. It also returns the LoadSDNode by reference if
2684/// required.
2685static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002686 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002687 N = N->getOperand(0).getNode();
Evan Cheng40ee6e52008-05-08 00:57:18 +00002688 if (ISD::isNON_EXTLoad(N)) {
2689 if (LD)
2690 *LD = cast<LoadSDNode>(N);
2691 return true;
2692 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002693 }
2694 return false;
2695}
2696
2697/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2698/// match movlp{s|d}. The lower half elements should come from lower half of
2699/// V1 (and in order), and the upper half elements should come from the upper
2700/// half of V2 (and in order). And since V1 will become the source of the
2701/// MOVLP, it must be either a vector load or a scalar load to vector.
2702static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2703 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2704 return false;
2705 // Is V2 is a vector load, don't do this transformation. We will try to use
2706 // load folding shufps op.
2707 if (ISD::isNON_EXTLoad(V2))
2708 return false;
2709
2710 unsigned NumElems = Mask->getNumOperands();
2711 if (NumElems != 2 && NumElems != 4)
2712 return false;
2713 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2714 if (!isUndefOrEqual(Mask->getOperand(i), i))
2715 return false;
2716 for (unsigned i = NumElems/2; i != NumElems; ++i)
2717 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2718 return false;
2719 return true;
2720}
2721
2722/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2723/// all the same.
2724static bool isSplatVector(SDNode *N) {
2725 if (N->getOpcode() != ISD::BUILD_VECTOR)
2726 return false;
2727
Dan Gohman8181bd12008-07-27 21:46:04 +00002728 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2730 if (N->getOperand(i) != SplatValue)
2731 return false;
2732 return true;
2733}
2734
2735/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2736/// to an undef.
2737static bool isUndefShuffle(SDNode *N) {
2738 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2739 return false;
2740
Dan Gohman8181bd12008-07-27 21:46:04 +00002741 SDValue V1 = N->getOperand(0);
2742 SDValue V2 = N->getOperand(1);
2743 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 unsigned NumElems = Mask.getNumOperands();
2745 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002746 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002747 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002748 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002749 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2750 return false;
2751 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2752 return false;
2753 }
2754 }
2755 return true;
2756}
2757
2758/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2759/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002760static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002762 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002764 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765}
2766
2767/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2768/// to an zero vector.
2769static bool isZeroShuffle(SDNode *N) {
2770 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2771 return false;
2772
Dan Gohman8181bd12008-07-27 21:46:04 +00002773 SDValue V1 = N->getOperand(0);
2774 SDValue V2 = N->getOperand(1);
2775 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002776 unsigned NumElems = Mask.getNumOperands();
2777 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002778 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002779 if (Arg.getOpcode() == ISD::UNDEF)
2780 continue;
2781
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002782 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002783 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002784 unsigned Opc = V1.getNode()->getOpcode();
2785 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002786 continue;
2787 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002788 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002789 return false;
2790 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002791 unsigned Opc = V2.getNode()->getOpcode();
2792 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002793 continue;
2794 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002795 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002796 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 }
2798 }
2799 return true;
2800}
2801
2802/// getZeroVector - Returns a vector of specified type with all zero elements.
2803///
Dan Gohman8181bd12008-07-27 21:46:04 +00002804static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002805 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002806
2807 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2808 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002809 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002810 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002811 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002812 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002813 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002814 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002815 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002816 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002817 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002818 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2819 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002820 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002821}
2822
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823/// getOnesVector - Returns a vector of specified type with all bits set.
2824///
Dan Gohman8181bd12008-07-27 21:46:04 +00002825static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002826 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827
2828 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2829 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002830 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2831 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002832 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002833 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2834 else // SSE
2835 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2836 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2837}
2838
2839
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2841/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002842static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002843 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2844
2845 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002846 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002847 unsigned NumElems = Mask.getNumOperands();
2848 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002849 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002851 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852 if (Val > NumElems) {
2853 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2854 Changed = true;
2855 }
2856 }
2857 MaskVec.push_back(Arg);
2858 }
2859
2860 if (Changed)
2861 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2862 &MaskVec[0], MaskVec.size());
2863 return Mask;
2864}
2865
2866/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2867/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002868static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002869 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2870 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002871
Dan Gohman8181bd12008-07-27 21:46:04 +00002872 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2874 for (unsigned i = 1; i != NumElems; ++i)
2875 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2876 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2877}
2878
2879/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2880/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002881static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002882 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2883 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002884 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002885 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2886 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2887 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2888 }
2889 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2890}
2891
2892/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2893/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002894static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002895 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2896 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002897 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 for (unsigned i = 0; i != Half; ++i) {
2900 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2901 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2902 }
2903 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2904}
2905
Chris Lattner2d91b962008-03-09 01:05:04 +00002906/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2907/// element #0 of a vector with the specified index, leaving the rest of the
2908/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002909static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002910 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002911 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2912 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002913 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002914 // Element #0 of the result gets the elt we are replacing.
2915 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2916 for (unsigned i = 1; i != NumElems; ++i)
2917 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2918 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2919}
2920
Evan Chengbf8b2c52008-04-05 00:30:36 +00002921/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002922static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002923 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2924 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002925 if (PVT == VT)
2926 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002927 SDValue V1 = Op.getOperand(0);
2928 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002930 // Special handling of v4f32 -> v4i32.
2931 if (VT != MVT::v4f32) {
2932 Mask = getUnpacklMask(NumElems, DAG);
2933 while (NumElems > 4) {
2934 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2935 NumElems >>= 1;
2936 }
Evan Cheng8c590372008-05-15 08:39:06 +00002937 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002938 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002939
Evan Chengbf8b2c52008-04-05 00:30:36 +00002940 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002941 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002942 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2944}
2945
2946/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00002947/// vector of zero or undef vector. This produces a shuffle where the low
2948/// element of V2 is swizzled into the zero/undef vector, landing at element
2949/// 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 +00002950static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00002951 bool isZero, bool HasSSE2,
2952 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002953 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002954 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00002955 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00002956 unsigned NumElems = V2.getValueType().getVectorNumElements();
2957 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2958 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002959 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00002960 for (unsigned i = 0; i != NumElems; ++i)
2961 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2962 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2963 else
2964 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966 &MaskVec[0], MaskVec.size());
2967 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2968}
2969
Evan Chengdea99362008-05-29 08:22:04 +00002970/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2971/// a shuffle that is zero.
2972static
Dan Gohman8181bd12008-07-27 21:46:04 +00002973unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00002974 unsigned NumElems, bool Low,
2975 SelectionDAG &DAG) {
2976 unsigned NumZeros = 0;
2977 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00002978 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00002979 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00002980 if (Idx.getOpcode() == ISD::UNDEF) {
2981 ++NumZeros;
2982 continue;
2983 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002984 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2985 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00002986 ++NumZeros;
2987 else
2988 break;
2989 }
2990 return NumZeros;
2991}
2992
2993/// isVectorShift - Returns true if the shuffle can be implemented as a
2994/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00002995static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2996 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00002997 unsigned NumElems = Mask.getNumOperands();
2998
2999 isLeft = true;
3000 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3001 if (!NumZeros) {
3002 isLeft = false;
3003 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3004 if (!NumZeros)
3005 return false;
3006 }
3007
3008 bool SeenV1 = false;
3009 bool SeenV2 = false;
3010 for (unsigned i = NumZeros; i < NumElems; ++i) {
3011 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003012 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003013 if (Idx.getOpcode() == ISD::UNDEF)
3014 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003015 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003016 if (Index < NumElems)
3017 SeenV1 = true;
3018 else {
3019 Index -= NumElems;
3020 SeenV2 = true;
3021 }
3022 if (Index != Val)
3023 return false;
3024 }
3025 if (SeenV1 && SeenV2)
3026 return false;
3027
3028 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3029 ShAmt = NumZeros;
3030 return true;
3031}
3032
3033
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003034/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3035///
Dan Gohman8181bd12008-07-27 21:46:04 +00003036static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003037 unsigned NumNonZero, unsigned NumZero,
3038 SelectionDAG &DAG, TargetLowering &TLI) {
3039 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003040 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003041
Dan Gohman8181bd12008-07-27 21:46:04 +00003042 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003043 bool First = true;
3044 for (unsigned i = 0; i < 16; ++i) {
3045 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3046 if (ThisIsNonZero && First) {
3047 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003048 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003049 else
3050 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3051 First = false;
3052 }
3053
3054 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003055 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003056 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3057 if (LastIsNonZero) {
3058 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3059 }
3060 if (ThisIsNonZero) {
3061 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3062 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3063 ThisElt, DAG.getConstant(8, MVT::i8));
3064 if (LastIsNonZero)
3065 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3066 } else
3067 ThisElt = LastElt;
3068
Gabor Greif1c80d112008-08-28 21:40:38 +00003069 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003070 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003071 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003072 }
3073 }
3074
3075 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3076}
3077
3078/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3079///
Dan Gohman8181bd12008-07-27 21:46:04 +00003080static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003081 unsigned NumNonZero, unsigned NumZero,
3082 SelectionDAG &DAG, TargetLowering &TLI) {
3083 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003084 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085
Dan Gohman8181bd12008-07-27 21:46:04 +00003086 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 bool First = true;
3088 for (unsigned i = 0; i < 8; ++i) {
3089 bool isNonZero = (NonZeros & (1 << i)) != 0;
3090 if (isNonZero) {
3091 if (First) {
3092 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003093 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003094 else
3095 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3096 First = false;
3097 }
3098 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003099 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003100 }
3101 }
3102
3103 return V;
3104}
3105
Evan Chengdea99362008-05-29 08:22:04 +00003106/// getVShift - Return a vector logical shift node.
3107///
Dan Gohman8181bd12008-07-27 21:46:04 +00003108static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003109 unsigned NumBits, SelectionDAG &DAG,
3110 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003111 bool isMMX = VT.getSizeInBits() == 64;
3112 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003113 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3114 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3115 return DAG.getNode(ISD::BIT_CONVERT, VT,
3116 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003117 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003118}
3119
Dan Gohman8181bd12008-07-27 21:46:04 +00003120SDValue
3121X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003122 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003123 if (ISD::isBuildVectorAllZeros(Op.getNode())
3124 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003125 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3126 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3127 // eliminated on x86-32 hosts.
3128 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3129 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130
Gabor Greif1c80d112008-08-28 21:40:38 +00003131 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003132 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003133 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003134 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135
Duncan Sands92c43912008-06-06 12:08:01 +00003136 MVT VT = Op.getValueType();
3137 MVT EVT = VT.getVectorElementType();
3138 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003139
3140 unsigned NumElems = Op.getNumOperands();
3141 unsigned NumZero = 0;
3142 unsigned NumNonZero = 0;
3143 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003144 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003145 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003147 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003148 if (Elt.getOpcode() == ISD::UNDEF)
3149 continue;
3150 Values.insert(Elt);
3151 if (Elt.getOpcode() != ISD::Constant &&
3152 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003153 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003154 if (isZeroNode(Elt))
3155 NumZero++;
3156 else {
3157 NonZeros |= (1 << i);
3158 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 }
3160 }
3161
3162 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003163 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3164 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 }
3166
Chris Lattner66a4dda2008-03-09 05:42:06 +00003167 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003168 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003170 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003171
Chris Lattner2d91b962008-03-09 01:05:04 +00003172 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3173 // the value are obviously zero, truncate the value to i32 and do the
3174 // insertion that way. Only do this if the value is non-constant or if the
3175 // value is a constant being inserted into element 0. It is cheaper to do
3176 // a constant pool load than it is to do a movd + shuffle.
3177 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3178 (!IsAllConstants || Idx == 0)) {
3179 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3180 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003181 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3182 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003183
3184 // Truncate the value (which may itself be a constant) to i32, and
3185 // convert it to a vector with movd (S2V+shuffle to zero extend).
3186 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3187 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003188 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3189 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003190
3191 // Now we have our 32-bit value zero extended in the low element of
3192 // a vector. If Idx != 0, swizzle it into place.
3193 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003194 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003195 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3196 getSwapEltZeroMask(VecElts, Idx, DAG)
3197 };
3198 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3199 }
3200 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3201 }
3202 }
3203
Chris Lattnerac914892008-03-08 22:59:52 +00003204 // If we have a constant or non-constant insertion into the low element of
3205 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3206 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3207 // depending on what the source datatype is. Because we can only get here
3208 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3209 if (Idx == 0 &&
3210 // Don't do this for i64 values on x86-32.
3211 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003212 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003213 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003214 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3215 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003216 }
Evan Chengdea99362008-05-29 08:22:04 +00003217
3218 // Is it a vector logical left shift?
3219 if (NumElems == 2 && Idx == 1 &&
3220 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003221 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003222 return getVShift(true, VT,
3223 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3224 NumBits/2, DAG, *this);
3225 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003226
3227 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003228 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229
Chris Lattnerac914892008-03-08 22:59:52 +00003230 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3231 // is a non-constant being inserted into an element other than the low one,
3232 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3233 // movd/movss) to move this into the low element, then shuffle it into
3234 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003236 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003238 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003239 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3240 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003241 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3242 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003243 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244 for (unsigned i = 0; i < NumElems; i++)
3245 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003246 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003247 &MaskVec[0], MaskVec.size());
3248 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3249 DAG.getNode(ISD::UNDEF, VT), Mask);
3250 }
3251 }
3252
Chris Lattner66a4dda2008-03-09 05:42:06 +00003253 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3254 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003255 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003256
Dan Gohman21463242007-07-24 22:55:08 +00003257 // A vector full of immediates; various special cases are already
3258 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003259 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003260 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003262 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003263 if (EVTBits == 64) {
3264 if (NumNonZero == 1) {
3265 // One half is zero or undef.
3266 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003267 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003268 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003269 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3270 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003271 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003272 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003274
3275 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3276 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003277 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003278 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003279 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003280 }
3281
3282 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003283 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003284 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003285 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 }
3287
3288 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003289 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 V.resize(NumElems);
3291 if (NumElems == 4 && NumZero > 0) {
3292 for (unsigned i = 0; i < 4; ++i) {
3293 bool isZero = !(NonZeros & (1 << i));
3294 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003295 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 else
3297 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3298 }
3299
3300 for (unsigned i = 0; i < 2; ++i) {
3301 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3302 default: break;
3303 case 0:
3304 V[i] = V[i*2]; // Must be a zero vector.
3305 break;
3306 case 1:
3307 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3308 getMOVLMask(NumElems, DAG));
3309 break;
3310 case 2:
3311 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3312 getMOVLMask(NumElems, DAG));
3313 break;
3314 case 3:
3315 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3316 getUnpacklMask(NumElems, DAG));
3317 break;
3318 }
3319 }
3320
Duncan Sands92c43912008-06-06 12:08:01 +00003321 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3322 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003323 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 bool Reverse = (NonZeros & 0x3) == 2;
3325 for (unsigned i = 0; i < 2; ++i)
3326 if (Reverse)
3327 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3328 else
3329 MaskVec.push_back(DAG.getConstant(i, EVT));
3330 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3331 for (unsigned i = 0; i < 2; ++i)
3332 if (Reverse)
3333 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3334 else
3335 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003337 &MaskVec[0], MaskVec.size());
3338 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3339 }
3340
3341 if (Values.size() > 2) {
3342 // Expand into a number of unpckl*.
3343 // e.g. for v4f32
3344 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3345 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3346 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003347 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003348 for (unsigned i = 0; i < NumElems; ++i)
3349 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3350 NumElems >>= 1;
3351 while (NumElems != 0) {
3352 for (unsigned i = 0; i < NumElems; ++i)
3353 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3354 UnpckMask);
3355 NumElems >>= 1;
3356 }
3357 return V[0];
3358 }
3359
Dan Gohman8181bd12008-07-27 21:46:04 +00003360 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361}
3362
Evan Chengfca29242007-12-07 08:07:39 +00003363static
Dan Gohman8181bd12008-07-27 21:46:04 +00003364SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003365 SDValue PermMask, SelectionDAG &DAG,
3366 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003368 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3369 MVT MaskEVT = MaskVT.getVectorElementType();
3370 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003371 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3372 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003373
3374 // First record which half of which vector the low elements come from.
3375 SmallVector<unsigned, 4> LowQuad(4);
3376 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003377 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003378 if (Elt.getOpcode() == ISD::UNDEF)
3379 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003380 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003381 int QuadIdx = EltIdx / 4;
3382 ++LowQuad[QuadIdx];
3383 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003384
Evan Cheng75184a92007-12-11 01:46:18 +00003385 int BestLowQuad = -1;
3386 unsigned MaxQuad = 1;
3387 for (unsigned i = 0; i < 4; ++i) {
3388 if (LowQuad[i] > MaxQuad) {
3389 BestLowQuad = i;
3390 MaxQuad = LowQuad[i];
3391 }
Evan Chengfca29242007-12-07 08:07:39 +00003392 }
3393
Evan Cheng75184a92007-12-11 01:46:18 +00003394 // Record which half of which vector the high elements come from.
3395 SmallVector<unsigned, 4> HighQuad(4);
3396 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003397 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003398 if (Elt.getOpcode() == ISD::UNDEF)
3399 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003400 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003401 int QuadIdx = EltIdx / 4;
3402 ++HighQuad[QuadIdx];
3403 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003404
Evan Cheng75184a92007-12-11 01:46:18 +00003405 int BestHighQuad = -1;
3406 MaxQuad = 1;
3407 for (unsigned i = 0; i < 4; ++i) {
3408 if (HighQuad[i] > MaxQuad) {
3409 BestHighQuad = i;
3410 MaxQuad = HighQuad[i];
3411 }
3412 }
3413
3414 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3415 if (BestLowQuad != -1 || BestHighQuad != -1) {
3416 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003417 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003418
Evan Cheng75184a92007-12-11 01:46:18 +00003419 if (BestLowQuad != -1)
3420 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3421 else
3422 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003423
Evan Cheng75184a92007-12-11 01:46:18 +00003424 if (BestHighQuad != -1)
3425 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3426 else
3427 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003428
Dan Gohman8181bd12008-07-27 21:46:04 +00003429 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003430 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3431 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3432 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3433 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3434
3435 // Now sort high and low parts separately.
3436 BitVector InOrder(8);
3437 if (BestLowQuad != -1) {
3438 // Sort lower half in order using PSHUFLW.
3439 MaskVec.clear();
3440 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003441
Evan Cheng75184a92007-12-11 01:46:18 +00003442 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003443 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003444 if (Elt.getOpcode() == ISD::UNDEF) {
3445 MaskVec.push_back(Elt);
3446 InOrder.set(i);
3447 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003448 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003449 if (EltIdx != i)
3450 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003451
Evan Cheng75184a92007-12-11 01:46:18 +00003452 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003453
Evan Cheng75184a92007-12-11 01:46:18 +00003454 // If this element is in the right place after this shuffle, then
3455 // remember it.
3456 if ((int)(EltIdx / 4) == BestLowQuad)
3457 InOrder.set(i);
3458 }
3459 }
3460 if (AnyOutOrder) {
3461 for (unsigned i = 4; i != 8; ++i)
3462 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003463 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003464 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3465 }
3466 }
3467
3468 if (BestHighQuad != -1) {
3469 // Sort high half in order using PSHUFHW if possible.
3470 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003471
Evan Cheng75184a92007-12-11 01:46:18 +00003472 for (unsigned i = 0; i != 4; ++i)
3473 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003474
Evan Cheng75184a92007-12-11 01:46:18 +00003475 bool AnyOutOrder = false;
3476 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003477 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003478 if (Elt.getOpcode() == ISD::UNDEF) {
3479 MaskVec.push_back(Elt);
3480 InOrder.set(i);
3481 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003482 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003483 if (EltIdx != i)
3484 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003485
Evan Cheng75184a92007-12-11 01:46:18 +00003486 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003487
Evan Cheng75184a92007-12-11 01:46:18 +00003488 // If this element is in the right place after this shuffle, then
3489 // remember it.
3490 if ((int)(EltIdx / 4) == BestHighQuad)
3491 InOrder.set(i);
3492 }
3493 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003494
Evan Cheng75184a92007-12-11 01:46:18 +00003495 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003496 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003497 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3498 }
3499 }
3500
3501 // The other elements are put in the right place using pextrw and pinsrw.
3502 for (unsigned i = 0; i != 8; ++i) {
3503 if (InOrder[i])
3504 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003505 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003506 if (Elt.getOpcode() == ISD::UNDEF)
3507 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003508 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003509 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003510 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3511 DAG.getConstant(EltIdx, PtrVT))
3512 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3513 DAG.getConstant(EltIdx - 8, PtrVT));
3514 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3515 DAG.getConstant(i, PtrVT));
3516 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003517
Evan Cheng75184a92007-12-11 01:46:18 +00003518 return NewV;
3519 }
3520
Bill Wendling2c7cd592008-08-21 22:35:37 +00003521 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3522 // few as possible. First, let's find out how many elements are already in the
3523 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003524 unsigned V1InOrder = 0;
3525 unsigned V1FromV1 = 0;
3526 unsigned V2InOrder = 0;
3527 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003528 SmallVector<SDValue, 8> V1Elts;
3529 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003530 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003531 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003532 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003533 V1Elts.push_back(Elt);
3534 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003535 ++V1InOrder;
3536 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003537 continue;
3538 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003539 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003540 if (EltIdx == i) {
3541 V1Elts.push_back(Elt);
3542 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3543 ++V1InOrder;
3544 } else if (EltIdx == i+8) {
3545 V1Elts.push_back(Elt);
3546 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3547 ++V2InOrder;
3548 } else if (EltIdx < 8) {
3549 V1Elts.push_back(Elt);
3550 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003551 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003552 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3553 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003554 }
3555 }
3556
3557 if (V2InOrder > V1InOrder) {
3558 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3559 std::swap(V1, V2);
3560 std::swap(V1Elts, V2Elts);
3561 std::swap(V1FromV1, V2FromV2);
3562 }
3563
Evan Cheng75184a92007-12-11 01:46:18 +00003564 if ((V1FromV1 + V1InOrder) != 8) {
3565 // Some elements are from V2.
3566 if (V1FromV1) {
3567 // If there are elements that are from V1 but out of place,
3568 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003569 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003570 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003571 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003572 if (Elt.getOpcode() == ISD::UNDEF) {
3573 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3574 continue;
3575 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003576 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003577 if (EltIdx >= 8)
3578 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3579 else
3580 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3581 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003582 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003583 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003584 }
Evan Cheng75184a92007-12-11 01:46:18 +00003585
3586 NewV = V1;
3587 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003588 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003589 if (Elt.getOpcode() == ISD::UNDEF)
3590 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003591 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003592 if (EltIdx < 8)
3593 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003595 DAG.getConstant(EltIdx - 8, PtrVT));
3596 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3597 DAG.getConstant(i, PtrVT));
3598 }
3599 return NewV;
3600 } else {
3601 // All elements are from V1.
3602 NewV = V1;
3603 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003604 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003605 if (Elt.getOpcode() == ISD::UNDEF)
3606 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003607 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003608 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003609 DAG.getConstant(EltIdx, PtrVT));
3610 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3611 DAG.getConstant(i, PtrVT));
3612 }
3613 return NewV;
3614 }
3615}
3616
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003617/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3618/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3619/// done when every pair / quad of shuffle mask elements point to elements in
3620/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003621/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3622static
Dan Gohman8181bd12008-07-27 21:46:04 +00003623SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003624 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003625 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003626 TargetLowering &TLI) {
3627 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003628 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003629 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003630 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003631 MVT NewVT = MaskVT;
3632 switch (VT.getSimpleVT()) {
3633 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003634 case MVT::v4f32: NewVT = MVT::v2f64; break;
3635 case MVT::v4i32: NewVT = MVT::v2i64; break;
3636 case MVT::v8i16: NewVT = MVT::v4i32; break;
3637 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003638 }
3639
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003640 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003641 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003642 NewVT = MVT::v2i64;
3643 else
3644 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003645 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003646 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003647 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003648 for (unsigned i = 0; i < NumElems; i += Scale) {
3649 unsigned StartIdx = ~0U;
3650 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003651 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003652 if (Elt.getOpcode() == ISD::UNDEF)
3653 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003654 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003655 if (StartIdx == ~0U)
3656 StartIdx = EltIdx - (EltIdx % Scale);
3657 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003658 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003659 }
3660 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003661 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003662 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003663 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003664 }
3665
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003666 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3667 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3668 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3669 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3670 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003671}
3672
Evan Chenge9b9c672008-05-09 21:53:03 +00003673/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003674///
Dan Gohman8181bd12008-07-27 21:46:04 +00003675static SDValue getVZextMovL(MVT VT, MVT OpVT,
3676 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003677 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003678 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3679 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003680 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003681 LD = dyn_cast<LoadSDNode>(SrcOp);
3682 if (!LD) {
3683 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3684 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003685 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003686 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3687 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3688 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3689 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3690 // PR2108
3691 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3692 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003693 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003694 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003695 SrcOp.getOperand(0)
3696 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003697 }
3698 }
3699 }
3700
3701 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003702 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003703 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3704}
3705
Evan Chengf50554e2008-07-22 21:13:36 +00003706/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3707/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003708static SDValue
3709LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3710 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003711 MVT MaskVT = PermMask.getValueType();
3712 MVT MaskEVT = MaskVT.getVectorElementType();
3713 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003714 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003715 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003716 unsigned NumHi = 0;
3717 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003718 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003719 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003720 if (Elt.getOpcode() == ISD::UNDEF) {
3721 Locs[i] = std::make_pair(-1, -1);
3722 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003723 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003724 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003725 if (Val < 4) {
3726 Locs[i] = std::make_pair(0, NumLo);
3727 Mask1[NumLo] = Elt;
3728 NumLo++;
3729 } else {
3730 Locs[i] = std::make_pair(1, NumHi);
3731 if (2+NumHi < 4)
3732 Mask1[2+NumHi] = Elt;
3733 NumHi++;
3734 }
3735 }
3736 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003737
Evan Chengf50554e2008-07-22 21:13:36 +00003738 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003739 // If no more than two elements come from either vector. This can be
3740 // implemented with two shuffles. First shuffle gather the elements.
3741 // The second shuffle, which takes the first shuffle as both of its
3742 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003743 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3744 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3745 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003746
Dan Gohman8181bd12008-07-27 21:46:04 +00003747 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003748 for (unsigned i = 0; i != 4; ++i) {
3749 if (Locs[i].first == -1)
3750 continue;
3751 else {
3752 unsigned Idx = (i < 2) ? 0 : 4;
3753 Idx += Locs[i].first * 2 + Locs[i].second;
3754 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3755 }
3756 }
3757
3758 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3759 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3760 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003761 } else if (NumLo == 3 || NumHi == 3) {
3762 // Otherwise, we must have three elements from one vector, call it X, and
3763 // one element from the other, call it Y. First, use a shufps to build an
3764 // intermediate vector with the one element from Y and the element from X
3765 // that will be in the same half in the final destination (the indexes don't
3766 // matter). Then, use a shufps to build the final vector, taking the half
3767 // containing the element from Y from the intermediate, and the other half
3768 // from X.
3769 if (NumHi == 3) {
3770 // Normalize it so the 3 elements come from V1.
3771 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3772 std::swap(V1, V2);
3773 }
3774
3775 // Find the element from V2.
3776 unsigned HiIndex;
3777 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003778 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003779 if (Elt.getOpcode() == ISD::UNDEF)
3780 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003781 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003782 if (Val >= 4)
3783 break;
3784 }
3785
3786 Mask1[0] = PermMask.getOperand(HiIndex);
3787 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3788 Mask1[2] = PermMask.getOperand(HiIndex^1);
3789 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3790 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3791 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3792
3793 if (HiIndex >= 2) {
3794 Mask1[0] = PermMask.getOperand(0);
3795 Mask1[1] = PermMask.getOperand(1);
3796 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3797 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3798 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3799 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3800 } else {
3801 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3802 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3803 Mask1[2] = PermMask.getOperand(2);
3804 Mask1[3] = PermMask.getOperand(3);
3805 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003806 Mask1[2] =
3807 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3808 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003809 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003810 Mask1[3] =
3811 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3812 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003813 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3814 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3815 }
Evan Chengf50554e2008-07-22 21:13:36 +00003816 }
3817
3818 // Break it into (shuffle shuffle_hi, shuffle_lo).
3819 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003820 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3821 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3822 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003823 unsigned MaskIdx = 0;
3824 unsigned LoIdx = 0;
3825 unsigned HiIdx = 2;
3826 for (unsigned i = 0; i != 4; ++i) {
3827 if (i == 2) {
3828 MaskPtr = &HiMask;
3829 MaskIdx = 1;
3830 LoIdx = 0;
3831 HiIdx = 2;
3832 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003833 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003834 if (Elt.getOpcode() == ISD::UNDEF) {
3835 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003836 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003837 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3838 (*MaskPtr)[LoIdx] = Elt;
3839 LoIdx++;
3840 } else {
3841 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3842 (*MaskPtr)[HiIdx] = Elt;
3843 HiIdx++;
3844 }
3845 }
3846
Dan Gohman8181bd12008-07-27 21:46:04 +00003847 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003848 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3849 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003850 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003851 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3852 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003853 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003854 for (unsigned i = 0; i != 4; ++i) {
3855 if (Locs[i].first == -1) {
3856 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3857 } else {
3858 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3859 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3860 }
3861 }
3862 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3863 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3864 &MaskOps[0], MaskOps.size()));
3865}
3866
Dan Gohman8181bd12008-07-27 21:46:04 +00003867SDValue
3868X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3869 SDValue V1 = Op.getOperand(0);
3870 SDValue V2 = Op.getOperand(1);
3871 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003872 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003873 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003874 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3876 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3877 bool V1IsSplat = false;
3878 bool V2IsSplat = false;
3879
Gabor Greif1c80d112008-08-28 21:40:38 +00003880 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003881 return DAG.getNode(ISD::UNDEF, VT);
3882
Gabor Greif1c80d112008-08-28 21:40:38 +00003883 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003884 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003885
Gabor Greif1c80d112008-08-28 21:40:38 +00003886 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003887 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003888 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003889 return V2;
3890
Gabor Greif1c80d112008-08-28 21:40:38 +00003891 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003892 if (isMMX || NumElems < 4) return Op;
3893 // Promote it to a v4{if}32 splat.
3894 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 }
3896
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003897 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3898 // do it!
3899 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003900 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003902 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3903 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3904 // FIXME: Figure out a cleaner way to do this.
3905 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003906 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003907 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003908 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003909 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003910 SDValue NewV1 = NewOp.getOperand(0);
3911 SDValue NewV2 = NewOp.getOperand(1);
3912 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00003913 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003914 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00003915 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003916 }
3917 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003918 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003919 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003920 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003921 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003922 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00003923 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003924 }
3925 }
3926
Evan Chengdea99362008-05-29 08:22:04 +00003927 // Check if this can be converted into a logical shift.
3928 bool isLeft = false;
3929 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003930 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00003931 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3932 if (isShift && ShVal.hasOneUse()) {
3933 // If the shifted value has multiple uses, it may be cheaper to use
3934 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00003935 MVT EVT = VT.getVectorElementType();
3936 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003937 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3938 }
3939
Gabor Greif1c80d112008-08-28 21:40:38 +00003940 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003941 if (V1IsUndef)
3942 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00003943 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00003944 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00003945 if (!isMMX)
3946 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003947 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003948
Gabor Greif1c80d112008-08-28 21:40:38 +00003949 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3950 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3951 X86::isMOVHLPSMask(PermMask.getNode()) ||
3952 X86::isMOVHPMask(PermMask.getNode()) ||
3953 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003954 return Op;
3955
Gabor Greif1c80d112008-08-28 21:40:38 +00003956 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3957 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003958 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3959
Evan Chengdea99362008-05-29 08:22:04 +00003960 if (isShift) {
3961 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00003962 MVT EVT = VT.getVectorElementType();
3963 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003964 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3965 }
3966
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003967 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003968 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3969 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00003970 V1IsSplat = isSplatVector(V1.getNode());
3971 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00003972
3973 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3975 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3976 std::swap(V1IsSplat, V2IsSplat);
3977 std::swap(V1IsUndef, V2IsUndef);
3978 Commuted = true;
3979 }
3980
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003981 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00003982 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003983 if (V2IsUndef) return V1;
3984 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3985 if (V2IsSplat) {
3986 // V2 is a splat, so the mask may be malformed. That is, it may point
3987 // to any V2 element. The instruction selectior won't like this. Get
3988 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00003989 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003991 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3992 }
3993 return Op;
3994 }
3995
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
3997 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
3998 X86::isUNPCKLMask(PermMask.getNode()) ||
3999 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 return Op;
4001
4002 if (V2IsSplat) {
4003 // Normalize mask so all entries that point to V2 points to its first
4004 // element then try to match unpck{h|l} again. If match, return a
4005 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004006 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004007 if (NewMask.getNode() != PermMask.getNode()) {
4008 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004009 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004012 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004013 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4014 }
4015 }
4016 }
4017
4018 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004019 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4021
4022 if (Commuted) {
4023 // Commute is back and try unpck* again.
4024 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004025 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4026 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4027 X86::isUNPCKLMask(PermMask.getNode()) ||
4028 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 return Op;
4030 }
4031
Evan Chengbf8b2c52008-04-05 00:30:36 +00004032 // Try PSHUF* first, then SHUFP*.
4033 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4034 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004036 if (V2.getOpcode() != ISD::UNDEF)
4037 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4038 DAG.getNode(ISD::UNDEF, VT), PermMask);
4039 return Op;
4040 }
4041
4042 if (!isMMX) {
4043 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004044 (X86::isPSHUFDMask(PermMask.getNode()) ||
4045 X86::isPSHUFHWMask(PermMask.getNode()) ||
4046 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004047 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004048 if (VT == MVT::v4f32) {
4049 RVT = MVT::v4i32;
4050 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4051 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4052 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4053 } else if (V2.getOpcode() != ISD::UNDEF)
4054 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4055 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4056 if (RVT != VT)
4057 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004058 return Op;
4059 }
4060
Evan Chengbf8b2c52008-04-05 00:30:36 +00004061 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004062 if (X86::isSHUFPMask(PermMask.getNode()) ||
4063 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004065 }
4066
Evan Cheng75184a92007-12-11 01:46:18 +00004067 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4068 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004069 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004070 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004071 return NewOp;
4072 }
4073
Evan Chengf50554e2008-07-22 21:13:36 +00004074 // Handle all 4 wide cases with a number of shuffles except for MMX.
4075 if (NumElems == 4 && !isMMX)
4076 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077
Dan Gohman8181bd12008-07-27 21:46:04 +00004078 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004079}
4080
Dan Gohman8181bd12008-07-27 21:46:04 +00004081SDValue
4082X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004083 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004084 MVT VT = Op.getValueType();
4085 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004086 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004087 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004088 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004089 DAG.getValueType(VT));
4090 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004091 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004092 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004093 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004094 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004095 DAG.getValueType(VT));
4096 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004097 } else if (VT == MVT::f32) {
4098 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4099 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004100 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004101 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004103 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004104 if (User->getOpcode() != ISD::STORE &&
4105 (User->getOpcode() != ISD::BIT_CONVERT ||
4106 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 return SDValue();
4108 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004109 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4110 Op.getOperand(1));
4111 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004112 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004113 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004114}
4115
4116
Dan Gohman8181bd12008-07-27 21:46:04 +00004117SDValue
4118X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004120 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004121
Evan Cheng6c249332008-03-24 21:52:23 +00004122 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004123 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004124 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004125 return Res;
4126 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004127
Duncan Sands92c43912008-06-06 12:08:01 +00004128 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004129 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004130 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004131 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004132 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004133 if (Idx == 0)
4134 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4135 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4136 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4137 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004138 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004139 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004140 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004142 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004143 DAG.getValueType(VT));
4144 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004145 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004146 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004147 if (Idx == 0)
4148 return Op;
4149 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004150 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004151 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004152 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004153 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004154 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004155 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004156 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004157 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004158 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004159 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004160 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004162 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004163 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4164 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4165 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004166 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004167 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004168 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4169 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4170 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004171 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172 if (Idx == 0)
4173 return Op;
4174
4175 // UNPCKHPD the element to the lowest double word, then movsd.
4176 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4177 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004178 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004179 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004180 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004181 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004182 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004185 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004186 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4187 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4188 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004189 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 }
4191
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004193}
4194
Dan Gohman8181bd12008-07-27 21:46:04 +00004195SDValue
4196X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004197 MVT VT = Op.getValueType();
4198 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004199
Dan Gohman8181bd12008-07-27 21:46:04 +00004200 SDValue N0 = Op.getOperand(0);
4201 SDValue N1 = Op.getOperand(1);
4202 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004203
Dan Gohman5a7af042008-08-14 22:53:18 +00004204 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4205 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004206 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004207 : X86ISD::PINSRW;
4208 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4209 // argument.
4210 if (N1.getValueType() != MVT::i32)
4211 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4212 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004213 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004214 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004215 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004216 // Bits [7:6] of the constant are the source select. This will always be
4217 // zero here. The DAG Combiner may combine an extract_elt index into these
4218 // bits. For example (insert (extract, 3), 2) could be matched by putting
4219 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4220 // Bits [5:4] of the constant are the destination select. This is the
4221 // value of the incoming immediate.
4222 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4223 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004224 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004225 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4226 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004227 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004228}
4229
Dan Gohman8181bd12008-07-27 21:46:04 +00004230SDValue
4231X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004232 MVT VT = Op.getValueType();
4233 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004234
4235 if (Subtarget->hasSSE41())
4236 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4237
Evan Chenge12a7eb2007-12-12 07:55:34 +00004238 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004239 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004240
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue N0 = Op.getOperand(0);
4242 SDValue N1 = Op.getOperand(1);
4243 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004244
Duncan Sands92c43912008-06-06 12:08:01 +00004245 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004246 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4247 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004248 if (N1.getValueType() != MVT::i32)
4249 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4250 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004251 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004252 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004254 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004255}
4256
Dan Gohman8181bd12008-07-27 21:46:04 +00004257SDValue
4258X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004259 if (Op.getValueType() == MVT::v2f32)
4260 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4261 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4262 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4263 Op.getOperand(0))));
4264
Dan Gohman8181bd12008-07-27 21:46:04 +00004265 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004266 MVT VT = MVT::v2i32;
4267 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004268 default: break;
4269 case MVT::v16i8:
4270 case MVT::v8i16:
4271 VT = MVT::v4i32;
4272 break;
4273 }
4274 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4275 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276}
4277
Bill Wendlingfef06052008-09-16 21:48:12 +00004278// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4279// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4280// one of the above mentioned nodes. It has to be wrapped because otherwise
4281// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4282// be used to form addressing mode. These wrapped nodes will be selected
4283// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004284SDValue
4285X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004286 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004287 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004288 getPointerTy(),
4289 CP->getAlignment());
4290 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4291 // With PIC, the address is actually $g + Offset.
4292 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4293 !Subtarget->isPICStyleRIPRel()) {
4294 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4295 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4296 Result);
4297 }
4298
4299 return Result;
4300}
4301
Dan Gohman8181bd12008-07-27 21:46:04 +00004302SDValue
4303X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004304 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +00004305 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004306 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4307 // With PIC, the address is actually $g + Offset.
4308 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4309 !Subtarget->isPICStyleRIPRel()) {
4310 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4311 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4312 Result);
4313 }
4314
4315 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4316 // load the value at address GV, not the value of GV itself. This means that
4317 // the GlobalAddress must be in the base or index register of the address, not
4318 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4319 // The same applies for external symbols during PIC codegen
4320 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman12a9c082008-02-06 22:27:42 +00004321 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004322 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323
4324 return Result;
4325}
4326
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004327// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004328static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004329LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004330 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004331 SDValue InFlag;
4332 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004333 DAG.getNode(X86ISD::GlobalBaseReg,
4334 PtrVT), InFlag);
4335 InFlag = Chain.getValue(1);
4336
4337 // emit leal symbol@TLSGD(,%ebx,1), %eax
4338 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004339 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004340 GA->getValueType(0),
4341 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004342 SDValue Ops[] = { Chain, TGA, InFlag };
4343 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 InFlag = Result.getValue(2);
4345 Chain = Result.getValue(1);
4346
4347 // call ___tls_get_addr. This function receives its argument in
4348 // the register EAX.
4349 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4350 InFlag = Chain.getValue(1);
4351
4352 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004353 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004354 DAG.getTargetExternalSymbol("___tls_get_addr",
4355 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004356 DAG.getRegister(X86::EAX, PtrVT),
4357 DAG.getRegister(X86::EBX, PtrVT),
4358 InFlag };
4359 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4360 InFlag = Chain.getValue(1);
4361
4362 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4363}
4364
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004365// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004366static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004367LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004368 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004369 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004370
4371 // emit leaq symbol@TLSGD(%rip), %rdi
4372 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004373 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004374 GA->getValueType(0),
4375 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4377 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004378 Chain = Result.getValue(1);
4379 InFlag = Result.getValue(2);
4380
aslb204cd52008-08-16 12:58:29 +00004381 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004382 // the register RDI.
4383 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4384 InFlag = Chain.getValue(1);
4385
4386 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004387 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004388 DAG.getTargetExternalSymbol("__tls_get_addr",
4389 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004390 DAG.getRegister(X86::RDI, PtrVT),
4391 InFlag };
4392 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4393 InFlag = Chain.getValue(1);
4394
4395 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4396}
4397
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004398// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4399// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004400static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004401 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004402 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004403 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004404 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4405 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004406 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407 GA->getValueType(0),
4408 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004409 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410
4411 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004412 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004413 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414
4415 // The address of the thread local variable is the add of the thread
4416 // pointer with the offset of the variable.
4417 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4418}
4419
Dan Gohman8181bd12008-07-27 21:46:04 +00004420SDValue
4421X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004422 // TODO: implement the "local dynamic" model
4423 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004424 assert(Subtarget->isTargetELF() &&
4425 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004426 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4427 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4428 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004429 if (Subtarget->is64Bit()) {
4430 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4431 } else {
4432 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4433 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4434 else
4435 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4436 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004437}
4438
Dan Gohman8181bd12008-07-27 21:46:04 +00004439SDValue
4440X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004441 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4442 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4444 // With PIC, the address is actually $g + Offset.
4445 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4446 !Subtarget->isPICStyleRIPRel()) {
4447 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4448 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4449 Result);
4450 }
4451
4452 return Result;
4453}
4454
Dan Gohman8181bd12008-07-27 21:46:04 +00004455SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004457 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4459 // With PIC, the address is actually $g + Offset.
4460 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4461 !Subtarget->isPICStyleRIPRel()) {
4462 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4463 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4464 Result);
4465 }
4466
4467 return Result;
4468}
4469
Chris Lattner62814a32007-10-17 06:02:13 +00004470/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4471/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004472SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004473 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004474 MVT VT = Op.getValueType();
4475 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004476 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004477 SDValue ShOpLo = Op.getOperand(0);
4478 SDValue ShOpHi = Op.getOperand(1);
4479 SDValue ShAmt = Op.getOperand(2);
4480 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004481 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4482 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004483
Dan Gohman8181bd12008-07-27 21:46:04 +00004484 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004485 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004486 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4487 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004488 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004489 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4490 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004491 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004492
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004494 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004495 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004496 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004497
Dan Gohman8181bd12008-07-27 21:46:04 +00004498 SDValue Hi, Lo;
4499 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4500 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4501 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004502
Chris Lattner62814a32007-10-17 06:02:13 +00004503 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004504 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4505 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004506 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004507 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4508 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004509 }
4510
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004512 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513}
4514
Dan Gohman8181bd12008-07-27 21:46:04 +00004515SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004516 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004517 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004518 "Unknown SINT_TO_FP to lower!");
4519
4520 // These are really Legal; caller falls through into that case.
4521 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004522 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004523 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4524 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004525 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004526
Duncan Sands92c43912008-06-06 12:08:01 +00004527 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 MachineFunction &MF = DAG.getMachineFunction();
4529 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004530 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4531 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004532 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004533 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534
4535 // Build the FILD
4536 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004537 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004538 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004539 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4540 else
4541 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004542 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004543 Ops.push_back(Chain);
4544 Ops.push_back(StackSlot);
4545 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004546 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004547 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004548
Dale Johannesen2fc20782007-09-14 22:26:36 +00004549 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004551 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552
4553 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4554 // shouldn't be necessary except that RFP cannot be live across
4555 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4556 MachineFunction &MF = DAG.getMachineFunction();
4557 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004558 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004560 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 Ops.push_back(Chain);
4562 Ops.push_back(Result);
4563 Ops.push_back(StackSlot);
4564 Ops.push_back(DAG.getValueType(Op.getValueType()));
4565 Ops.push_back(InFlag);
4566 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004567 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004568 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004569 }
4570
4571 return Result;
4572}
4573
Dan Gohman8181bd12008-07-27 21:46:04 +00004574std::pair<SDValue,SDValue> X86TargetLowering::
4575FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004576 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4577 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004578 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579
Dale Johannesen2fc20782007-09-14 22:26:36 +00004580 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004581 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004582 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004583 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004584 if (Subtarget->is64Bit() &&
4585 Op.getValueType() == MVT::i64 &&
4586 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004587 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004588
Evan Cheng05441e62007-10-15 20:11:21 +00004589 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4590 // stack slot.
4591 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004592 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004593 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004594 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004596 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004597 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4598 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4599 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4600 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004601 }
4602
Dan Gohman8181bd12008-07-27 21:46:04 +00004603 SDValue Chain = DAG.getEntryNode();
4604 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004605 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004606 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004607 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004608 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004609 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004610 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004611 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4612 };
4613 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4614 Chain = Value.getValue(1);
4615 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4616 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4617 }
4618
4619 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004620 SDValue Ops[] = { Chain, Value, StackSlot };
4621 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004622
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004623 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624}
4625
Dan Gohman8181bd12008-07-27 21:46:04 +00004626SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4627 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4628 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004629 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004630
4631 // Load the result.
4632 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4633}
4634
4635SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004636 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4637 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004638 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004639
4640 MVT VT = N->getValueType(0);
4641
4642 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004643 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004644
Duncan Sands698842f2008-07-02 17:40:58 +00004645 // Use MERGE_VALUES to drop the chain result value and get a node with one
4646 // result. This requires turning off getMergeValues simplification, since
4647 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004648 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004649}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004650
Dan Gohman8181bd12008-07-27 21:46:04 +00004651SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004652 MVT VT = Op.getValueType();
4653 MVT EltVT = VT;
4654 if (VT.isVector())
4655 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004656 std::vector<Constant*> CV;
4657 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004658 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 CV.push_back(C);
4660 CV.push_back(C);
4661 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004662 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663 CV.push_back(C);
4664 CV.push_back(C);
4665 CV.push_back(C);
4666 CV.push_back(C);
4667 }
Dan Gohman11821702007-07-27 17:16:43 +00004668 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004669 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4670 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004671 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004672 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4674}
4675
Dan Gohman8181bd12008-07-27 21:46:04 +00004676SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004677 MVT VT = Op.getValueType();
4678 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004679 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004680 if (VT.isVector()) {
4681 EltVT = VT.getVectorElementType();
4682 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004683 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004684 std::vector<Constant*> CV;
4685 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004686 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004687 CV.push_back(C);
4688 CV.push_back(C);
4689 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004690 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004691 CV.push_back(C);
4692 CV.push_back(C);
4693 CV.push_back(C);
4694 CV.push_back(C);
4695 }
Dan Gohman11821702007-07-27 17:16:43 +00004696 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004697 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4698 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004699 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004700 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004701 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004702 return DAG.getNode(ISD::BIT_CONVERT, VT,
4703 DAG.getNode(ISD::XOR, MVT::v2i64,
4704 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4705 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4706 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004707 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004709}
4710
Dan Gohman8181bd12008-07-27 21:46:04 +00004711SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4712 SDValue Op0 = Op.getOperand(0);
4713 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004714 MVT VT = Op.getValueType();
4715 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716
4717 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004718 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004719 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4720 SrcVT = VT;
4721 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004722 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004723 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004724 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004725 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004726 }
4727
4728 // At this point the operands and the result should have the same
4729 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004730
4731 // First get the sign bit of second operand.
4732 std::vector<Constant*> CV;
4733 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004734 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4735 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004736 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004737 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4738 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4739 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4740 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 }
Dan Gohman11821702007-07-27 17:16:43 +00004742 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004743 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4744 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004745 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004746 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004747 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004748
4749 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004750 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004751 // Op0 is MVT::f32, Op1 is MVT::f64.
4752 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4753 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4754 DAG.getConstant(32, MVT::i32));
4755 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4756 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004757 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 }
4759
4760 // Clear first operand sign bit.
4761 CV.clear();
4762 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004763 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4764 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004766 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4767 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4768 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4769 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 }
Dan Gohman11821702007-07-27 17:16:43 +00004771 C = ConstantVector::get(CV);
4772 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004773 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004774 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004775 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004776 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777
4778 // Or the value with the sign bit.
4779 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4780}
4781
Dan Gohman8181bd12008-07-27 21:46:04 +00004782SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004783 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004784 SDValue Cond;
4785 SDValue Op0 = Op.getOperand(0);
4786 SDValue Op1 = Op.getOperand(1);
4787 SDValue CC = Op.getOperand(2);
Evan Cheng950aac02007-09-25 01:57:46 +00004788 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands92c43912008-06-06 12:08:01 +00004789 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004790 unsigned X86CC;
4791
Evan Cheng950aac02007-09-25 01:57:46 +00004792 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004793 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004794 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4795 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004796 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004797 }
Evan Cheng950aac02007-09-25 01:57:46 +00004798
4799 assert(isFP && "Illegal integer SetCC!");
4800
Evan Cheng621216e2007-09-29 00:00:36 +00004801 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng950aac02007-09-25 01:57:46 +00004802 switch (SetCCOpcode) {
4803 default: assert(false && "Illegal floating point SetCC!");
4804 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004805 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004806 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004807 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004808 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4809 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4810 }
4811 case ISD::SETUNE: { // PF | !ZF
Dan Gohman8181bd12008-07-27 21:46:04 +00004812 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004813 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman8181bd12008-07-27 21:46:04 +00004814 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004815 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4816 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4817 }
4818 }
4819}
4820
Dan Gohman8181bd12008-07-27 21:46:04 +00004821SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4822 SDValue Cond;
4823 SDValue Op0 = Op.getOperand(0);
4824 SDValue Op1 = Op.getOperand(1);
4825 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004826 MVT VT = Op.getValueType();
4827 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4828 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4829
4830 if (isFP) {
4831 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004832 MVT VT0 = Op0.getValueType();
4833 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4834 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004835 bool Swap = false;
4836
4837 switch (SetCCOpcode) {
4838 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004839 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004840 case ISD::SETEQ: SSECC = 0; break;
4841 case ISD::SETOGT:
4842 case ISD::SETGT: Swap = true; // Fallthrough
4843 case ISD::SETLT:
4844 case ISD::SETOLT: SSECC = 1; break;
4845 case ISD::SETOGE:
4846 case ISD::SETGE: Swap = true; // Fallthrough
4847 case ISD::SETLE:
4848 case ISD::SETOLE: SSECC = 2; break;
4849 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004850 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004851 case ISD::SETNE: SSECC = 4; break;
4852 case ISD::SETULE: Swap = true;
4853 case ISD::SETUGE: SSECC = 5; break;
4854 case ISD::SETULT: Swap = true;
4855 case ISD::SETUGT: SSECC = 6; break;
4856 case ISD::SETO: SSECC = 7; break;
4857 }
4858 if (Swap)
4859 std::swap(Op0, Op1);
4860
Nate Begeman6357f9d2008-07-25 19:05:58 +00004861 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004862 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004863 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004864 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004865 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4866 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4867 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4868 }
4869 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004870 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004871 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4872 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4873 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4874 }
4875 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004876 }
4877 // Handle all other FP comparisons here.
4878 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4879 }
4880
4881 // We are handling one of the integer comparisons here. Since SSE only has
4882 // GT and EQ comparisons for integer, swapping operands and multiple
4883 // operations may be required for some comparisons.
4884 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4885 bool Swap = false, Invert = false, FlipSigns = false;
4886
4887 switch (VT.getSimpleVT()) {
4888 default: break;
4889 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4890 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4891 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4892 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4893 }
4894
4895 switch (SetCCOpcode) {
4896 default: break;
4897 case ISD::SETNE: Invert = true;
4898 case ISD::SETEQ: Opc = EQOpc; break;
4899 case ISD::SETLT: Swap = true;
4900 case ISD::SETGT: Opc = GTOpc; break;
4901 case ISD::SETGE: Swap = true;
4902 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4903 case ISD::SETULT: Swap = true;
4904 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4905 case ISD::SETUGE: Swap = true;
4906 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4907 }
4908 if (Swap)
4909 std::swap(Op0, Op1);
4910
4911 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4912 // bits of the inputs before performing those operations.
4913 if (FlipSigns) {
4914 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004915 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4916 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4917 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004918 SignBits.size());
4919 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4920 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4921 }
4922
Dan Gohman8181bd12008-07-27 21:46:04 +00004923 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00004924
4925 // If the logical-not of the result is required, perform that now.
4926 if (Invert) {
4927 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00004928 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4929 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4930 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00004931 NegOnes.size());
4932 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4933 }
4934 return Result;
4935}
Evan Cheng950aac02007-09-25 01:57:46 +00004936
Dan Gohman8181bd12008-07-27 21:46:04 +00004937SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004938 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004939 SDValue Cond = Op.getOperand(0);
4940 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004941
4942 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004943 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944
Evan Cheng50d37ab2007-10-08 22:16:29 +00004945 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4946 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947 if (Cond.getOpcode() == X86ISD::SETCC) {
4948 CC = Cond.getOperand(0);
4949
Dan Gohman8181bd12008-07-27 21:46:04 +00004950 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00004952 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00004953
Evan Cheng50d37ab2007-10-08 22:16:29 +00004954 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00004955 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004956 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng50d37ab2007-10-08 22:16:29 +00004957 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattnerfca7f222008-01-16 06:19:45 +00004958
Evan Cheng621216e2007-09-29 00:00:36 +00004959 if ((Opc == X86ISD::CMP ||
4960 Opc == X86ISD::COMI ||
4961 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00004962 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00004963 addTest = false;
4964 }
4965 }
4966
4967 if (addTest) {
4968 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00004969 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00004970 }
4971
Duncan Sands92c43912008-06-06 12:08:01 +00004972 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00004973 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004974 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00004975 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4976 // condition is true.
4977 Ops.push_back(Op.getOperand(2));
4978 Ops.push_back(Op.getOperand(1));
4979 Ops.push_back(CC);
4980 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00004981 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00004982}
4983
Dan Gohman8181bd12008-07-27 21:46:04 +00004984SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004986 SDValue Chain = Op.getOperand(0);
4987 SDValue Cond = Op.getOperand(1);
4988 SDValue Dest = Op.getOperand(2);
4989 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004990
4991 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00004992 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004993
Evan Cheng50d37ab2007-10-08 22:16:29 +00004994 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4995 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 if (Cond.getOpcode() == X86ISD::SETCC) {
4997 CC = Cond.getOperand(0);
4998
Dan Gohman8181bd12008-07-27 21:46:04 +00004999 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005000 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005001 if (Opc == X86ISD::CMP ||
5002 Opc == X86ISD::COMI ||
5003 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005004 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005005 addTest = false;
5006 }
5007 }
5008
5009 if (addTest) {
5010 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005011 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005012 }
Evan Cheng621216e2007-09-29 00:00:36 +00005013 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005014 Chain, Op.getOperand(2), CC, Cond);
5015}
5016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005017
5018// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5019// Calls to _alloca is needed to probe the stack when allocating more than 4k
5020// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5021// that the guard pages used by the OS virtual memory manager are allocated in
5022// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005023SDValue
5024X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005025 SelectionDAG &DAG) {
5026 assert(Subtarget->isTargetCygMing() &&
5027 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005029 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005030 SDValue Chain = Op.getOperand(0);
5031 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005032 // FIXME: Ensure alignment here
5033
Dan Gohman8181bd12008-07-27 21:46:04 +00005034 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005035
Duncan Sands92c43912008-06-06 12:08:01 +00005036 MVT IntPtr = getPointerTy();
5037 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005038
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005039 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5040
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005041 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5042 Flag = Chain.getValue(1);
5043
5044 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005045 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005046 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005048 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005049 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005050 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005051 Flag = Chain.getValue(1);
5052
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005053 Chain = DAG.getCALLSEQ_END(Chain,
5054 DAG.getIntPtrConstant(0),
5055 DAG.getIntPtrConstant(0),
5056 Flag);
5057
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005058 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005059
Dan Gohman8181bd12008-07-27 21:46:04 +00005060 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005061 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062}
5063
Dan Gohman8181bd12008-07-27 21:46:04 +00005064SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005065X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005066 SDValue Chain,
5067 SDValue Dst, SDValue Src,
5068 SDValue Size, unsigned Align,
Dan Gohman65118f42008-04-28 17:15:20 +00005069 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005070 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005071
Dan Gohmane8b391e2008-04-12 04:36:06 +00005072 /// If not DWORD aligned or size is more than the threshold, call the library.
5073 /// The libc version is likely to be faster for these cases. It can use the
5074 /// address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005075 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005076 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005077 ConstantSize->getZExtValue() >
5078 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005079 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005080
5081 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005082 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5083 if (const char *bzeroEntry =
5084 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands92c43912008-06-06 12:08:01 +00005085 MVT IntPtr = getPointerTy();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005086 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005087 TargetLowering::ArgListTy Args;
5088 TargetLowering::ArgListEntry Entry;
5089 Entry.Node = Dst;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005090 Entry.Ty = IntPtrTy;
5091 Args.push_back(Entry);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005092 Entry.Node = Size;
5093 Args.push_back(Entry);
Dan Gohman8181bd12008-07-27 21:46:04 +00005094 std::pair<SDValue,SDValue> CallResult =
Dan Gohmane8b391e2008-04-12 04:36:06 +00005095 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
Bill Wendlingfef06052008-09-16 21:48:12 +00005096 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005097 Args, DAG);
5098 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005099 }
5100
Dan Gohmane8b391e2008-04-12 04:36:06 +00005101 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005102 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005103 }
5104
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005105 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005106 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005107 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005108 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005109 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005110 unsigned BytesLeft = 0;
5111 bool TwoRepStos = false;
5112 if (ValC) {
5113 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005114 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115
5116 // If the value is a constant, then we can potentially use larger sets.
5117 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005118 case 2: // WORD aligned
5119 AVT = MVT::i16;
5120 ValReg = X86::AX;
5121 Val = (Val << 8) | Val;
5122 break;
5123 case 0: // DWORD aligned
5124 AVT = MVT::i32;
5125 ValReg = X86::EAX;
5126 Val = (Val << 8) | Val;
5127 Val = (Val << 16) | Val;
5128 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5129 AVT = MVT::i64;
5130 ValReg = X86::RAX;
5131 Val = (Val << 32) | Val;
5132 }
5133 break;
5134 default: // Byte aligned
5135 AVT = MVT::i8;
5136 ValReg = X86::AL;
5137 Count = DAG.getIntPtrConstant(SizeVal);
5138 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005139 }
5140
Duncan Sandsec142ee2008-06-08 20:54:56 +00005141 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005142 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005143 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5144 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005145 }
5146
5147 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5148 InFlag);
5149 InFlag = Chain.getValue(1);
5150 } else {
5151 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005152 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005153 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 InFlag = Chain.getValue(1);
5155 }
5156
5157 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5158 Count, InFlag);
5159 InFlag = Chain.getValue(1);
5160 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005161 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005162 InFlag = Chain.getValue(1);
5163
5164 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005165 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005166 Ops.push_back(Chain);
5167 Ops.push_back(DAG.getValueType(AVT));
5168 Ops.push_back(InFlag);
5169 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5170
5171 if (TwoRepStos) {
5172 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005173 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005174 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005175 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005176 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5177 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5178 Left, InFlag);
5179 InFlag = Chain.getValue(1);
5180 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5181 Ops.clear();
5182 Ops.push_back(Chain);
5183 Ops.push_back(DAG.getValueType(MVT::i8));
5184 Ops.push_back(InFlag);
5185 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5186 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005187 // Handle the last 1 - 7 bytes.
5188 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005189 MVT AddrVT = Dst.getValueType();
5190 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005191
5192 Chain = DAG.getMemset(Chain,
5193 DAG.getNode(ISD::ADD, AddrVT, Dst,
5194 DAG.getConstant(Offset, AddrVT)),
5195 Src,
5196 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005197 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005198 }
5199
Dan Gohmane8b391e2008-04-12 04:36:06 +00005200 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005201 return Chain;
5202}
5203
Dan Gohman8181bd12008-07-27 21:46:04 +00005204SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005205X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005206 SDValue Chain, SDValue Dst, SDValue Src,
5207 SDValue Size, unsigned Align,
5208 bool AlwaysInline,
5209 const Value *DstSV, uint64_t DstSVOff,
5210 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005211 // This requires the copy size to be a constant, preferrably
5212 // within a subtarget-specific limit.
5213 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5214 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005215 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005216 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005217 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005218 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005219
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005220 /// If not DWORD aligned, call the library.
5221 if ((Align & 3) != 0)
5222 return SDValue();
5223
5224 // DWORD aligned
5225 MVT AVT = MVT::i32;
5226 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005227 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005228
Duncan Sands92c43912008-06-06 12:08:01 +00005229 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005230 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005231 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005232 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233
Dan Gohman8181bd12008-07-27 21:46:04 +00005234 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005235 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5236 Count, InFlag);
5237 InFlag = Chain.getValue(1);
5238 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005239 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005240 InFlag = Chain.getValue(1);
5241 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005242 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005243 InFlag = Chain.getValue(1);
5244
5245 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005246 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005247 Ops.push_back(Chain);
5248 Ops.push_back(DAG.getValueType(AVT));
5249 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005251
Dan Gohman8181bd12008-07-27 21:46:04 +00005252 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005253 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005254 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005255 // Handle the last 1 - 7 bytes.
5256 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005257 MVT DstVT = Dst.getValueType();
5258 MVT SrcVT = Src.getValueType();
5259 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005260 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005261 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005262 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005263 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005264 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005265 DAG.getConstant(BytesLeft, SizeVT),
5266 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005267 DstSV, DstSVOff + Offset,
5268 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005269 }
5270
Dan Gohmane8b391e2008-04-12 04:36:06 +00005271 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005272}
5273
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005274/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5275SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005276 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005277 SDValue TheChain = N->getOperand(0);
5278 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005280 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5281 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005282 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005283 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005285 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005286 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005287 };
5288
Gabor Greif1c80d112008-08-28 21:40:38 +00005289 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 }
5291
Dan Gohman8181bd12008-07-27 21:46:04 +00005292 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5293 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005294 MVT::i32, eax.getValue(2));
5295 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005296 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005297 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5298
5299 // Use a MERGE_VALUES to return the value and chain.
5300 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005301 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302}
5303
Dan Gohman8181bd12008-07-27 21:46:04 +00005304SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005305 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005306
5307 if (!Subtarget->is64Bit()) {
5308 // vastart just stores the address of the VarArgsFrameIndex slot into the
5309 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005310 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005311 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005312 }
5313
5314 // __va_list_tag:
5315 // gp_offset (0 - 6 * 8)
5316 // fp_offset (48 - 48 + 8 * 16)
5317 // overflow_arg_area (point to parameters coming in memory).
5318 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005319 SmallVector<SDValue, 8> MemOps;
5320 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005321 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005322 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005323 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005324 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 MemOps.push_back(Store);
5326
5327 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005328 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 Store = DAG.getStore(Op.getOperand(0),
5330 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005331 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005332 MemOps.push_back(Store);
5333
5334 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005335 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005336 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005337 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005338 MemOps.push_back(Store);
5339
5340 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005341 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005342 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005343 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005344 MemOps.push_back(Store);
5345 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5346}
5347
Dan Gohman8181bd12008-07-27 21:46:04 +00005348SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005349 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5350 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005351 SDValue Chain = Op.getOperand(0);
5352 SDValue SrcPtr = Op.getOperand(1);
5353 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005354
5355 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5356 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005357 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005358}
5359
Dan Gohman8181bd12008-07-27 21:46:04 +00005360SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005362 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005363 SDValue Chain = Op.getOperand(0);
5364 SDValue DstPtr = Op.getOperand(1);
5365 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005366 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5367 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005368
Dan Gohman840ff5c2008-04-18 20:55:41 +00005369 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5370 DAG.getIntPtrConstant(24), 8, false,
5371 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372}
5373
Dan Gohman8181bd12008-07-27 21:46:04 +00005374SDValue
5375X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005376 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005377 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005379 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 case Intrinsic::x86_sse_comieq_ss:
5381 case Intrinsic::x86_sse_comilt_ss:
5382 case Intrinsic::x86_sse_comile_ss:
5383 case Intrinsic::x86_sse_comigt_ss:
5384 case Intrinsic::x86_sse_comige_ss:
5385 case Intrinsic::x86_sse_comineq_ss:
5386 case Intrinsic::x86_sse_ucomieq_ss:
5387 case Intrinsic::x86_sse_ucomilt_ss:
5388 case Intrinsic::x86_sse_ucomile_ss:
5389 case Intrinsic::x86_sse_ucomigt_ss:
5390 case Intrinsic::x86_sse_ucomige_ss:
5391 case Intrinsic::x86_sse_ucomineq_ss:
5392 case Intrinsic::x86_sse2_comieq_sd:
5393 case Intrinsic::x86_sse2_comilt_sd:
5394 case Intrinsic::x86_sse2_comile_sd:
5395 case Intrinsic::x86_sse2_comigt_sd:
5396 case Intrinsic::x86_sse2_comige_sd:
5397 case Intrinsic::x86_sse2_comineq_sd:
5398 case Intrinsic::x86_sse2_ucomieq_sd:
5399 case Intrinsic::x86_sse2_ucomilt_sd:
5400 case Intrinsic::x86_sse2_ucomile_sd:
5401 case Intrinsic::x86_sse2_ucomigt_sd:
5402 case Intrinsic::x86_sse2_ucomige_sd:
5403 case Intrinsic::x86_sse2_ucomineq_sd: {
5404 unsigned Opc = 0;
5405 ISD::CondCode CC = ISD::SETCC_INVALID;
5406 switch (IntNo) {
5407 default: break;
5408 case Intrinsic::x86_sse_comieq_ss:
5409 case Intrinsic::x86_sse2_comieq_sd:
5410 Opc = X86ISD::COMI;
5411 CC = ISD::SETEQ;
5412 break;
5413 case Intrinsic::x86_sse_comilt_ss:
5414 case Intrinsic::x86_sse2_comilt_sd:
5415 Opc = X86ISD::COMI;
5416 CC = ISD::SETLT;
5417 break;
5418 case Intrinsic::x86_sse_comile_ss:
5419 case Intrinsic::x86_sse2_comile_sd:
5420 Opc = X86ISD::COMI;
5421 CC = ISD::SETLE;
5422 break;
5423 case Intrinsic::x86_sse_comigt_ss:
5424 case Intrinsic::x86_sse2_comigt_sd:
5425 Opc = X86ISD::COMI;
5426 CC = ISD::SETGT;
5427 break;
5428 case Intrinsic::x86_sse_comige_ss:
5429 case Intrinsic::x86_sse2_comige_sd:
5430 Opc = X86ISD::COMI;
5431 CC = ISD::SETGE;
5432 break;
5433 case Intrinsic::x86_sse_comineq_ss:
5434 case Intrinsic::x86_sse2_comineq_sd:
5435 Opc = X86ISD::COMI;
5436 CC = ISD::SETNE;
5437 break;
5438 case Intrinsic::x86_sse_ucomieq_ss:
5439 case Intrinsic::x86_sse2_ucomieq_sd:
5440 Opc = X86ISD::UCOMI;
5441 CC = ISD::SETEQ;
5442 break;
5443 case Intrinsic::x86_sse_ucomilt_ss:
5444 case Intrinsic::x86_sse2_ucomilt_sd:
5445 Opc = X86ISD::UCOMI;
5446 CC = ISD::SETLT;
5447 break;
5448 case Intrinsic::x86_sse_ucomile_ss:
5449 case Intrinsic::x86_sse2_ucomile_sd:
5450 Opc = X86ISD::UCOMI;
5451 CC = ISD::SETLE;
5452 break;
5453 case Intrinsic::x86_sse_ucomigt_ss:
5454 case Intrinsic::x86_sse2_ucomigt_sd:
5455 Opc = X86ISD::UCOMI;
5456 CC = ISD::SETGT;
5457 break;
5458 case Intrinsic::x86_sse_ucomige_ss:
5459 case Intrinsic::x86_sse2_ucomige_sd:
5460 Opc = X86ISD::UCOMI;
5461 CC = ISD::SETGE;
5462 break;
5463 case Intrinsic::x86_sse_ucomineq_ss:
5464 case Intrinsic::x86_sse2_ucomineq_sd:
5465 Opc = X86ISD::UCOMI;
5466 CC = ISD::SETNE;
5467 break;
5468 }
5469
5470 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005471 SDValue LHS = Op.getOperand(1);
5472 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005473 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5474
Dan Gohman8181bd12008-07-27 21:46:04 +00005475 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5476 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005477 DAG.getConstant(X86CC, MVT::i8), Cond);
5478 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005479 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005480
5481 // Fix vector shift instructions where the last operand is a non-immediate
5482 // i32 value.
5483 case Intrinsic::x86_sse2_pslli_w:
5484 case Intrinsic::x86_sse2_pslli_d:
5485 case Intrinsic::x86_sse2_pslli_q:
5486 case Intrinsic::x86_sse2_psrli_w:
5487 case Intrinsic::x86_sse2_psrli_d:
5488 case Intrinsic::x86_sse2_psrli_q:
5489 case Intrinsic::x86_sse2_psrai_w:
5490 case Intrinsic::x86_sse2_psrai_d:
5491 case Intrinsic::x86_mmx_pslli_w:
5492 case Intrinsic::x86_mmx_pslli_d:
5493 case Intrinsic::x86_mmx_pslli_q:
5494 case Intrinsic::x86_mmx_psrli_w:
5495 case Intrinsic::x86_mmx_psrli_d:
5496 case Intrinsic::x86_mmx_psrli_q:
5497 case Intrinsic::x86_mmx_psrai_w:
5498 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005499 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005500 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005501 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005502
5503 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005504 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005505 switch (IntNo) {
5506 case Intrinsic::x86_sse2_pslli_w:
5507 NewIntNo = Intrinsic::x86_sse2_psll_w;
5508 break;
5509 case Intrinsic::x86_sse2_pslli_d:
5510 NewIntNo = Intrinsic::x86_sse2_psll_d;
5511 break;
5512 case Intrinsic::x86_sse2_pslli_q:
5513 NewIntNo = Intrinsic::x86_sse2_psll_q;
5514 break;
5515 case Intrinsic::x86_sse2_psrli_w:
5516 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5517 break;
5518 case Intrinsic::x86_sse2_psrli_d:
5519 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5520 break;
5521 case Intrinsic::x86_sse2_psrli_q:
5522 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5523 break;
5524 case Intrinsic::x86_sse2_psrai_w:
5525 NewIntNo = Intrinsic::x86_sse2_psra_w;
5526 break;
5527 case Intrinsic::x86_sse2_psrai_d:
5528 NewIntNo = Intrinsic::x86_sse2_psra_d;
5529 break;
5530 default: {
5531 ShAmtVT = MVT::v2i32;
5532 switch (IntNo) {
5533 case Intrinsic::x86_mmx_pslli_w:
5534 NewIntNo = Intrinsic::x86_mmx_psll_w;
5535 break;
5536 case Intrinsic::x86_mmx_pslli_d:
5537 NewIntNo = Intrinsic::x86_mmx_psll_d;
5538 break;
5539 case Intrinsic::x86_mmx_pslli_q:
5540 NewIntNo = Intrinsic::x86_mmx_psll_q;
5541 break;
5542 case Intrinsic::x86_mmx_psrli_w:
5543 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5544 break;
5545 case Intrinsic::x86_mmx_psrli_d:
5546 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5547 break;
5548 case Intrinsic::x86_mmx_psrli_q:
5549 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5550 break;
5551 case Intrinsic::x86_mmx_psrai_w:
5552 NewIntNo = Intrinsic::x86_mmx_psra_w;
5553 break;
5554 case Intrinsic::x86_mmx_psrai_d:
5555 NewIntNo = Intrinsic::x86_mmx_psra_d;
5556 break;
5557 default: abort(); // Can't reach here.
5558 }
5559 break;
5560 }
5561 }
Duncan Sands92c43912008-06-06 12:08:01 +00005562 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005563 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5564 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5565 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5566 DAG.getConstant(NewIntNo, MVT::i32),
5567 Op.getOperand(1), ShAmt);
5568 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569 }
5570}
5571
Dan Gohman8181bd12008-07-27 21:46:04 +00005572SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005573 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005574 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005575 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576
5577 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005578 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005579 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5580}
5581
Dan Gohman8181bd12008-07-27 21:46:04 +00005582SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005583 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005584 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005585 return SDValue();
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005588 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005589 DAG.getIntPtrConstant(TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590}
5591
Dan Gohman8181bd12008-07-27 21:46:04 +00005592SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005593 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005594 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005595}
5596
Dan Gohman8181bd12008-07-27 21:46:04 +00005597SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005599 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005600 SDValue Chain = Op.getOperand(0);
5601 SDValue Offset = Op.getOperand(1);
5602 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005603
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005604 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5605 getPointerTy());
5606 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607
Dan Gohman8181bd12008-07-27 21:46:04 +00005608 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005609 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5611 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005612 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5613 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005615 return DAG.getNode(X86ISD::EH_RETURN,
5616 MVT::Other,
5617 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618}
5619
Dan Gohman8181bd12008-07-27 21:46:04 +00005620SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005621 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005622 SDValue Root = Op.getOperand(0);
5623 SDValue Trmp = Op.getOperand(1); // trampoline
5624 SDValue FPtr = Op.getOperand(2); // nested function
5625 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005626
Dan Gohman12a9c082008-02-06 22:27:42 +00005627 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005628
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005629 const X86InstrInfo *TII =
5630 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5631
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005632 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005633 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005634
5635 // Large code-model.
5636
5637 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5638 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5639
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005640 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5641 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005642
5643 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5644
5645 // Load the pointer to the nested function into R11.
5646 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005647 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005648 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005649 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005650
5651 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005652 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005653
5654 // Load the 'nest' parameter value into R10.
5655 // R10 is specified in X86CallingConv.td
5656 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5657 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5658 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005659 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005660
5661 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005662 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005663
5664 // Jump to the nested function.
5665 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5666 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5667 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005668 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005669
5670 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5671 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5672 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005673 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005674
Dan Gohman8181bd12008-07-27 21:46:04 +00005675 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005676 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005677 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005678 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005679 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005680 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5681 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005682 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005683
5684 switch (CC) {
5685 default:
5686 assert(0 && "Unsupported calling convention");
5687 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005688 case CallingConv::X86_StdCall: {
5689 // Pass 'nest' parameter in ECX.
5690 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005691 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005692
5693 // Check that ECX wasn't needed by an 'inreg' parameter.
5694 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner1c8733e2008-03-12 17:45:29 +00005695 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005696
Chris Lattner1c8733e2008-03-12 17:45:29 +00005697 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005698 unsigned InRegCount = 0;
5699 unsigned Idx = 1;
5700
5701 for (FunctionType::param_iterator I = FTy->param_begin(),
5702 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner1c8733e2008-03-12 17:45:29 +00005703 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005704 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005705 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005706
5707 if (InRegCount > 2) {
5708 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5709 abort();
5710 }
5711 }
5712 break;
5713 }
5714 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005715 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005716 // Pass 'nest' parameter in EAX.
5717 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005718 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005719 break;
5720 }
5721
Dan Gohman8181bd12008-07-27 21:46:04 +00005722 SDValue OutChains[4];
5723 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005724
5725 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5726 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5727
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005728 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005729 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005730 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005731 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005732
5733 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005734 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005735
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005736 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005737 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5738 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005739 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005740
5741 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005742 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005743
Dan Gohman8181bd12008-07-27 21:46:04 +00005744 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005745 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005746 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005747 }
5748}
5749
Dan Gohman8181bd12008-07-27 21:46:04 +00005750SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005751 /*
5752 The rounding mode is in bits 11:10 of FPSR, and has the following
5753 settings:
5754 00 Round to nearest
5755 01 Round to -inf
5756 10 Round to +inf
5757 11 Round to 0
5758
5759 FLT_ROUNDS, on the other hand, expects the following:
5760 -1 Undefined
5761 0 Round to 0
5762 1 Round to nearest
5763 2 Round to +inf
5764 3 Round to -inf
5765
5766 To perform the conversion, we do:
5767 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5768 */
5769
5770 MachineFunction &MF = DAG.getMachineFunction();
5771 const TargetMachine &TM = MF.getTarget();
5772 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5773 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005774 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005775
5776 // Save FP Control Word to stack slot
5777 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005778 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005779
Dan Gohman8181bd12008-07-27 21:46:04 +00005780 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005781 DAG.getEntryNode(), StackSlot);
5782
5783 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005784 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005785
5786 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005788 DAG.getNode(ISD::SRL, MVT::i16,
5789 DAG.getNode(ISD::AND, MVT::i16,
5790 CWD, DAG.getConstant(0x800, MVT::i16)),
5791 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005792 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005793 DAG.getNode(ISD::SRL, MVT::i16,
5794 DAG.getNode(ISD::AND, MVT::i16,
5795 CWD, DAG.getConstant(0x400, MVT::i16)),
5796 DAG.getConstant(9, MVT::i8));
5797
Dan Gohman8181bd12008-07-27 21:46:04 +00005798 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005799 DAG.getNode(ISD::AND, MVT::i16,
5800 DAG.getNode(ISD::ADD, MVT::i16,
5801 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5802 DAG.getConstant(1, MVT::i16)),
5803 DAG.getConstant(3, MVT::i16));
5804
5805
Duncan Sands92c43912008-06-06 12:08:01 +00005806 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005807 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5808}
5809
Dan Gohman8181bd12008-07-27 21:46:04 +00005810SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005811 MVT VT = Op.getValueType();
5812 MVT OpVT = VT;
5813 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005814
5815 Op = Op.getOperand(0);
5816 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005817 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005818 OpVT = MVT::i32;
5819 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5820 }
Evan Cheng48679f42007-12-14 02:13:44 +00005821
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005822 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5823 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5824 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5825
5826 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005827 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005828 Ops.push_back(Op);
5829 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5830 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5831 Ops.push_back(Op.getValue(1));
5832 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5833
5834 // Finally xor with NumBits-1.
5835 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5836
Evan Cheng48679f42007-12-14 02:13:44 +00005837 if (VT == MVT::i8)
5838 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5839 return Op;
5840}
5841
Dan Gohman8181bd12008-07-27 21:46:04 +00005842SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005843 MVT VT = Op.getValueType();
5844 MVT OpVT = VT;
5845 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005846
5847 Op = Op.getOperand(0);
5848 if (VT == MVT::i8) {
5849 OpVT = MVT::i32;
5850 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5851 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005852
5853 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5854 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5855 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5856
5857 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005858 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005859 Ops.push_back(Op);
5860 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5861 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5862 Ops.push_back(Op.getValue(1));
5863 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5864
Evan Cheng48679f42007-12-14 02:13:44 +00005865 if (VT == MVT::i8)
5866 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5867 return Op;
5868}
5869
Dan Gohman8181bd12008-07-27 21:46:04 +00005870SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005871 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00005872 unsigned Reg = 0;
5873 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005874 switch(T.getSimpleVT()) {
5875 default:
5876 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005877 case MVT::i8: Reg = X86::AL; size = 1; break;
5878 case MVT::i16: Reg = X86::AX; size = 2; break;
5879 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00005880 case MVT::i64:
5881 if (Subtarget->is64Bit()) {
5882 Reg = X86::RAX; size = 8;
5883 } else //Should go away when LowerType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00005884 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00005885 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005886 };
Dan Gohman8181bd12008-07-27 21:46:04 +00005887 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00005888 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00005889 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005890 Op.getOperand(1),
Dale Johannesenddb761b2008-09-11 03:12:59 +00005891 Op.getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005892 DAG.getTargetConstant(size, MVT::i8),
5893 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005894 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005895 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5896 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00005897 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5898 return cpOut;
5899}
5900
Gabor Greif825aa892008-08-28 23:19:51 +00005901SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5902 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005903 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005904 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00005905 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005906 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005907 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005908 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00005909 DAG.getConstant(1, MVT::i32));
5910 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00005911 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00005912 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5913 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005914 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00005915 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005916 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00005917 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00005918 DAG.getConstant(1, MVT::i32));
5919 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5920 swapInL, cpInH.getValue(1));
5921 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5922 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharth81580822008-03-05 01:15:49 +00005924 Op->getOperand(1),
5925 swapInH.getValue(1)};
5926 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005927 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5928 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005929 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00005930 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00005931 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005932 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5933 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5934 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00005935 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00005936}
5937
Gabor Greif825aa892008-08-28 23:19:51 +00005938SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5939 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00005940 MVT T = Op->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00005941 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang078a62d2008-05-05 19:05:59 +00005942 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesenbc187662008-08-28 02:44:49 +00005943 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5944 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5945 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5946 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5947 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang6bde9ec2008-06-25 08:15:39 +00005948 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greif1c80d112008-08-28 21:40:38 +00005949 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang078a62d2008-05-05 19:05:59 +00005950}
5951
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005952/// LowerOperation - Provide custom lowering hooks for some operations.
5953///
Dan Gohman8181bd12008-07-27 21:46:04 +00005954SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005955 switch (Op.getOpcode()) {
5956 default: assert(0 && "Should not custom lower this!");
Dale Johannesenbc187662008-08-28 02:44:49 +00005957 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5958 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5959 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5960 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5962 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5963 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5964 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5965 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5966 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5967 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
5968 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00005969 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 case ISD::SHL_PARTS:
5971 case ISD::SRA_PARTS:
5972 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5973 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5974 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5975 case ISD::FABS: return LowerFABS(Op, DAG);
5976 case ISD::FNEG: return LowerFNEG(Op, DAG);
5977 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005978 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00005979 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00005980 case ISD::SELECT: return LowerSELECT(Op, DAG);
5981 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005982 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
5983 case ISD::CALL: return LowerCALL(Op, DAG);
5984 case ISD::RET: return LowerRET(Op, DAG);
5985 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005986 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005987 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005988 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
5989 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5990 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5991 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
5992 case ISD::FRAME_TO_ARGS_OFFSET:
5993 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5994 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5995 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005996 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00005997 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00005998 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5999 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006000
6001 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6002 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006003 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006004 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006005}
6006
Duncan Sandsac496a12008-07-04 11:47:58 +00006007/// ReplaceNodeResults - Replace a node with an illegal result type
6008/// with a new node built out of custom code.
6009SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006010 switch (N->getOpcode()) {
6011 default: assert(0 && "Should not custom lower this!");
6012 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6013 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006014 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6015 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6016 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6017 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6018 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006019 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006020}
6021
6022const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6023 switch (Opcode) {
6024 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006025 case X86ISD::BSF: return "X86ISD::BSF";
6026 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006027 case X86ISD::SHLD: return "X86ISD::SHLD";
6028 case X86ISD::SHRD: return "X86ISD::SHRD";
6029 case X86ISD::FAND: return "X86ISD::FAND";
6030 case X86ISD::FOR: return "X86ISD::FOR";
6031 case X86ISD::FXOR: return "X86ISD::FXOR";
6032 case X86ISD::FSRL: return "X86ISD::FSRL";
6033 case X86ISD::FILD: return "X86ISD::FILD";
6034 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6035 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6036 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6037 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6038 case X86ISD::FLD: return "X86ISD::FLD";
6039 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006040 case X86ISD::CALL: return "X86ISD::CALL";
6041 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6042 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6043 case X86ISD::CMP: return "X86ISD::CMP";
6044 case X86ISD::COMI: return "X86ISD::COMI";
6045 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6046 case X86ISD::SETCC: return "X86ISD::SETCC";
6047 case X86ISD::CMOV: return "X86ISD::CMOV";
6048 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6049 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6050 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6051 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006052 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6053 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006054 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006055 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006056 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6057 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006058 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6059 case X86ISD::FMAX: return "X86ISD::FMAX";
6060 case X86ISD::FMIN: return "X86ISD::FMIN";
6061 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6062 case X86ISD::FRCP: return "X86ISD::FRCP";
6063 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6064 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6065 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006066 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006067 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006068 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6069 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006070 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6071 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006072 case X86ISD::VSHL: return "X86ISD::VSHL";
6073 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006074 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6075 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6076 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6077 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6078 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6079 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6080 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6081 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6082 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6083 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 }
6085}
6086
6087// isLegalAddressingMode - Return true if the addressing mode represented
6088// by AM is legal for this target, for a load/store of the specified type.
6089bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6090 const Type *Ty) const {
6091 // X86 supports extremely general addressing modes.
6092
6093 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6094 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6095 return false;
6096
6097 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006098 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006099 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6100 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006101
6102 // X86-64 only supports addr of globals in small code model.
6103 if (Subtarget->is64Bit()) {
6104 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6105 return false;
6106 // If lower 4G is not available, then we must use rip-relative addressing.
6107 if (AM.BaseOffs || AM.Scale > 1)
6108 return false;
6109 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 }
6111
6112 switch (AM.Scale) {
6113 case 0:
6114 case 1:
6115 case 2:
6116 case 4:
6117 case 8:
6118 // These scales always work.
6119 break;
6120 case 3:
6121 case 5:
6122 case 9:
6123 // These scales are formed with basereg+scalereg. Only accept if there is
6124 // no basereg yet.
6125 if (AM.HasBaseReg)
6126 return false;
6127 break;
6128 default: // Other stuff never works.
6129 return false;
6130 }
6131
6132 return true;
6133}
6134
6135
Evan Cheng27a820a2007-10-26 01:56:11 +00006136bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6137 if (!Ty1->isInteger() || !Ty2->isInteger())
6138 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006139 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6140 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006141 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006142 return false;
6143 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006144}
6145
Duncan Sands92c43912008-06-06 12:08:01 +00006146bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6147 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006148 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006149 unsigned NumBits1 = VT1.getSizeInBits();
6150 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006151 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006152 return false;
6153 return Subtarget->is64Bit() || NumBits1 < 64;
6154}
Evan Cheng27a820a2007-10-26 01:56:11 +00006155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006156/// isShuffleMaskLegal - Targets can use this to indicate that they only
6157/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6158/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6159/// are assumed to be legal.
6160bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006161X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006162 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006163 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006164 return (Mask.getNode()->getNumOperands() <= 4 ||
6165 isIdentityMask(Mask.getNode()) ||
6166 isIdentityMask(Mask.getNode(), true) ||
6167 isSplatMask(Mask.getNode()) ||
6168 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6169 X86::isUNPCKLMask(Mask.getNode()) ||
6170 X86::isUNPCKHMask(Mask.getNode()) ||
6171 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6172 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006173}
6174
Dan Gohman48d5f062008-04-09 20:09:42 +00006175bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006176X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006177 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006178 unsigned NumElts = BVOps.size();
6179 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006180 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 if (NumElts == 2) return true;
6182 if (NumElts == 4) {
6183 return (isMOVLMask(&BVOps[0], 4) ||
6184 isCommutedMOVL(&BVOps[0], 4, true) ||
6185 isSHUFPMask(&BVOps[0], 4) ||
6186 isCommutedSHUFP(&BVOps[0], 4));
6187 }
6188 return false;
6189}
6190
6191//===----------------------------------------------------------------------===//
6192// X86 Scheduler Hooks
6193//===----------------------------------------------------------------------===//
6194
Mon P Wang078a62d2008-05-05 19:05:59 +00006195// private utility function
6196MachineBasicBlock *
6197X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6198 MachineBasicBlock *MBB,
6199 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006200 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006201 unsigned LoadOpc,
6202 unsigned CXchgOpc,
6203 unsigned copyOpc,
6204 unsigned notOpc,
6205 unsigned EAXreg,
6206 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006207 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006208 // For the atomic bitwise operator, we generate
6209 // thisMBB:
6210 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006211 // ld t1 = [bitinstr.addr]
6212 // op t2 = t1, [bitinstr.val]
6213 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006214 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6215 // bz newMBB
6216 // fallthrough -->nextMBB
6217 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6218 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006219 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006220 ++MBBIter;
6221
6222 /// First build the CFG
6223 MachineFunction *F = MBB->getParent();
6224 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006225 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6226 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6227 F->insert(MBBIter, newMBB);
6228 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006229
6230 // Move all successors to thisMBB to nextMBB
6231 nextMBB->transferSuccessors(thisMBB);
6232
6233 // Update thisMBB to fall through to newMBB
6234 thisMBB->addSuccessor(newMBB);
6235
6236 // newMBB jumps to itself and fall through to nextMBB
6237 newMBB->addSuccessor(nextMBB);
6238 newMBB->addSuccessor(newMBB);
6239
6240 // Insert instructions into newMBB based on incoming instruction
6241 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6242 MachineOperand& destOper = bInstr->getOperand(0);
6243 MachineOperand* argOpers[6];
6244 int numArgs = bInstr->getNumOperands() - 1;
6245 for (int i=0; i < numArgs; ++i)
6246 argOpers[i] = &bInstr->getOperand(i+1);
6247
6248 // x86 address has 4 operands: base, index, scale, and displacement
6249 int lastAddrIndx = 3; // [0,3]
6250 int valArgIndx = 4;
6251
Dale Johannesend20e4452008-08-19 18:47:28 +00006252 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6253 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006254 for (int i=0; i <= lastAddrIndx; ++i)
6255 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006256
Dale Johannesend20e4452008-08-19 18:47:28 +00006257 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006258 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006259 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006260 }
6261 else
6262 tt = t1;
6263
Dale Johannesend20e4452008-08-19 18:47:28 +00006264 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006265 assert((argOpers[valArgIndx]->isRegister() ||
6266 argOpers[valArgIndx]->isImmediate()) &&
6267 "invalid operand");
6268 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006269 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6270 else
6271 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006272 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006273 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006274
Dale Johannesend20e4452008-08-19 18:47:28 +00006275 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006276 MIB.addReg(t1);
6277
Dale Johannesend20e4452008-08-19 18:47:28 +00006278 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006279 for (int i=0; i <= lastAddrIndx; ++i)
6280 (*MIB).addOperand(*argOpers[i]);
6281 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006282 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6283 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6284
Dale Johannesend20e4452008-08-19 18:47:28 +00006285 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6286 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006287
6288 // insert branch
6289 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6290
Dan Gohman221a4372008-07-07 23:14:23 +00006291 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006292 return nextMBB;
6293}
6294
6295// private utility function
6296MachineBasicBlock *
6297X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6298 MachineBasicBlock *MBB,
6299 unsigned cmovOpc) {
6300 // For the atomic min/max operator, we generate
6301 // thisMBB:
6302 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006303 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006304 // mov t2 = [min/max.val]
6305 // cmp t1, t2
6306 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006307 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006308 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6309 // bz newMBB
6310 // fallthrough -->nextMBB
6311 //
6312 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6313 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006314 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006315 ++MBBIter;
6316
6317 /// First build the CFG
6318 MachineFunction *F = MBB->getParent();
6319 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006320 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6321 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6322 F->insert(MBBIter, newMBB);
6323 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006324
6325 // Move all successors to thisMBB to nextMBB
6326 nextMBB->transferSuccessors(thisMBB);
6327
6328 // Update thisMBB to fall through to newMBB
6329 thisMBB->addSuccessor(newMBB);
6330
6331 // newMBB jumps to newMBB and fall through to nextMBB
6332 newMBB->addSuccessor(nextMBB);
6333 newMBB->addSuccessor(newMBB);
6334
6335 // Insert instructions into newMBB based on incoming instruction
6336 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6337 MachineOperand& destOper = mInstr->getOperand(0);
6338 MachineOperand* argOpers[6];
6339 int numArgs = mInstr->getNumOperands() - 1;
6340 for (int i=0; i < numArgs; ++i)
6341 argOpers[i] = &mInstr->getOperand(i+1);
6342
6343 // x86 address has 4 operands: base, index, scale, and displacement
6344 int lastAddrIndx = 3; // [0,3]
6345 int valArgIndx = 4;
6346
Mon P Wang318b0372008-05-05 22:56:23 +00006347 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6348 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006349 for (int i=0; i <= lastAddrIndx; ++i)
6350 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006351
Mon P Wang078a62d2008-05-05 19:05:59 +00006352 // We only support register and immediate values
Dan Gohman7f7f3652008-09-13 17:58:21 +00006353 assert((argOpers[valArgIndx]->isRegister() ||
6354 argOpers[valArgIndx]->isImmediate()) &&
6355 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006356
6357 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman7f7f3652008-09-13 17:58:21 +00006358 if (argOpers[valArgIndx]->isRegister())
Mon P Wang078a62d2008-05-05 19:05:59 +00006359 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6360 else
6361 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6362 (*MIB).addOperand(*argOpers[valArgIndx]);
6363
Mon P Wang318b0372008-05-05 22:56:23 +00006364 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6365 MIB.addReg(t1);
6366
Mon P Wang078a62d2008-05-05 19:05:59 +00006367 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6368 MIB.addReg(t1);
6369 MIB.addReg(t2);
6370
6371 // Generate movc
6372 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6373 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6374 MIB.addReg(t2);
6375 MIB.addReg(t1);
6376
6377 // Cmp and exchange if none has modified the memory location
6378 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6379 for (int i=0; i <= lastAddrIndx; ++i)
6380 (*MIB).addOperand(*argOpers[i]);
6381 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006382 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6383 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006384
6385 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6386 MIB.addReg(X86::EAX);
6387
6388 // insert branch
6389 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6390
Dan Gohman221a4372008-07-07 23:14:23 +00006391 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006392 return nextMBB;
6393}
6394
6395
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006396MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006397X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6398 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006399 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6400 switch (MI->getOpcode()) {
6401 default: assert(false && "Unexpected instr type to insert");
6402 case X86::CMOV_FR32:
6403 case X86::CMOV_FR64:
6404 case X86::CMOV_V4F32:
6405 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006406 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006407 // To "insert" a SELECT_CC instruction, we actually have to insert the
6408 // diamond control-flow pattern. The incoming instruction knows the
6409 // destination vreg to set, the condition code register to branch on, the
6410 // true/false values to select between, and a branch opcode to use.
6411 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006412 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006413 ++It;
6414
6415 // thisMBB:
6416 // ...
6417 // TrueVal = ...
6418 // cmpTY ccX, r1, r2
6419 // bCC copy1MBB
6420 // fallthrough --> copy0MBB
6421 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006422 MachineFunction *F = BB->getParent();
6423 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6424 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006425 unsigned Opc =
6426 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6427 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006428 F->insert(It, copy0MBB);
6429 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006430 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006432 sinkMBB->transferSuccessors(BB);
6433
6434 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006435 BB->addSuccessor(copy0MBB);
6436 BB->addSuccessor(sinkMBB);
6437
6438 // copy0MBB:
6439 // %FalseValue = ...
6440 // # fallthrough to sinkMBB
6441 BB = copy0MBB;
6442
6443 // Update machine-CFG edges
6444 BB->addSuccessor(sinkMBB);
6445
6446 // sinkMBB:
6447 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6448 // ...
6449 BB = sinkMBB;
6450 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6451 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6452 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6453
Dan Gohman221a4372008-07-07 23:14:23 +00006454 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006455 return BB;
6456 }
6457
6458 case X86::FP32_TO_INT16_IN_MEM:
6459 case X86::FP32_TO_INT32_IN_MEM:
6460 case X86::FP32_TO_INT64_IN_MEM:
6461 case X86::FP64_TO_INT16_IN_MEM:
6462 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006463 case X86::FP64_TO_INT64_IN_MEM:
6464 case X86::FP80_TO_INT16_IN_MEM:
6465 case X86::FP80_TO_INT32_IN_MEM:
6466 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006467 // Change the floating point control register to use "round towards zero"
6468 // mode when truncating to an integer value.
6469 MachineFunction *F = BB->getParent();
6470 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6471 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6472
6473 // Load the old value of the high byte of the control word...
6474 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006475 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006476 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6477
6478 // Set the high part to be round to zero...
6479 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6480 .addImm(0xC7F);
6481
6482 // Reload the modified control word now...
6483 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6484
6485 // Restore the memory image of control word to original value
6486 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6487 .addReg(OldCW);
6488
6489 // Get the X86 opcode to use.
6490 unsigned Opc;
6491 switch (MI->getOpcode()) {
6492 default: assert(0 && "illegal opcode!");
6493 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6494 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6495 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6496 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6497 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6498 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006499 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6500 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6501 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006502 }
6503
6504 X86AddressMode AM;
6505 MachineOperand &Op = MI->getOperand(0);
6506 if (Op.isRegister()) {
6507 AM.BaseType = X86AddressMode::RegBase;
6508 AM.Base.Reg = Op.getReg();
6509 } else {
6510 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006511 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006512 }
6513 Op = MI->getOperand(1);
6514 if (Op.isImmediate())
6515 AM.Scale = Op.getImm();
6516 Op = MI->getOperand(2);
6517 if (Op.isImmediate())
6518 AM.IndexReg = Op.getImm();
6519 Op = MI->getOperand(3);
6520 if (Op.isGlobalAddress()) {
6521 AM.GV = Op.getGlobal();
6522 } else {
6523 AM.Disp = Op.getImm();
6524 }
6525 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6526 .addReg(MI->getOperand(4).getReg());
6527
6528 // Reload the original control word now.
6529 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6530
Dan Gohman221a4372008-07-07 23:14:23 +00006531 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006532 return BB;
6533 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006534 case X86::ATOMAND32:
6535 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006536 X86::AND32ri, X86::MOV32rm,
6537 X86::LCMPXCHG32, X86::MOV32rr,
6538 X86::NOT32r, X86::EAX,
6539 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006540 case X86::ATOMOR32:
6541 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006542 X86::OR32ri, X86::MOV32rm,
6543 X86::LCMPXCHG32, X86::MOV32rr,
6544 X86::NOT32r, X86::EAX,
6545 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006546 case X86::ATOMXOR32:
6547 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006548 X86::XOR32ri, X86::MOV32rm,
6549 X86::LCMPXCHG32, X86::MOV32rr,
6550 X86::NOT32r, X86::EAX,
6551 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006552 case X86::ATOMNAND32:
6553 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006554 X86::AND32ri, X86::MOV32rm,
6555 X86::LCMPXCHG32, X86::MOV32rr,
6556 X86::NOT32r, X86::EAX,
6557 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006558 case X86::ATOMMIN32:
6559 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6560 case X86::ATOMMAX32:
6561 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6562 case X86::ATOMUMIN32:
6563 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6564 case X86::ATOMUMAX32:
6565 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006566
6567 case X86::ATOMAND16:
6568 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6569 X86::AND16ri, X86::MOV16rm,
6570 X86::LCMPXCHG16, X86::MOV16rr,
6571 X86::NOT16r, X86::AX,
6572 X86::GR16RegisterClass);
6573 case X86::ATOMOR16:
6574 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6575 X86::OR16ri, X86::MOV16rm,
6576 X86::LCMPXCHG16, X86::MOV16rr,
6577 X86::NOT16r, X86::AX,
6578 X86::GR16RegisterClass);
6579 case X86::ATOMXOR16:
6580 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6581 X86::XOR16ri, X86::MOV16rm,
6582 X86::LCMPXCHG16, X86::MOV16rr,
6583 X86::NOT16r, X86::AX,
6584 X86::GR16RegisterClass);
6585 case X86::ATOMNAND16:
6586 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6587 X86::AND16ri, X86::MOV16rm,
6588 X86::LCMPXCHG16, X86::MOV16rr,
6589 X86::NOT16r, X86::AX,
6590 X86::GR16RegisterClass, true);
6591 case X86::ATOMMIN16:
6592 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6593 case X86::ATOMMAX16:
6594 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6595 case X86::ATOMUMIN16:
6596 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6597 case X86::ATOMUMAX16:
6598 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6599
6600 case X86::ATOMAND8:
6601 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6602 X86::AND8ri, X86::MOV8rm,
6603 X86::LCMPXCHG8, X86::MOV8rr,
6604 X86::NOT8r, X86::AL,
6605 X86::GR8RegisterClass);
6606 case X86::ATOMOR8:
6607 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6608 X86::OR8ri, X86::MOV8rm,
6609 X86::LCMPXCHG8, X86::MOV8rr,
6610 X86::NOT8r, X86::AL,
6611 X86::GR8RegisterClass);
6612 case X86::ATOMXOR8:
6613 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6614 X86::XOR8ri, X86::MOV8rm,
6615 X86::LCMPXCHG8, X86::MOV8rr,
6616 X86::NOT8r, X86::AL,
6617 X86::GR8RegisterClass);
6618 case X86::ATOMNAND8:
6619 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6620 X86::AND8ri, X86::MOV8rm,
6621 X86::LCMPXCHG8, X86::MOV8rr,
6622 X86::NOT8r, X86::AL,
6623 X86::GR8RegisterClass, true);
6624 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006625 case X86::ATOMAND64:
6626 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6627 X86::AND64ri32, X86::MOV64rm,
6628 X86::LCMPXCHG64, X86::MOV64rr,
6629 X86::NOT64r, X86::RAX,
6630 X86::GR64RegisterClass);
6631 case X86::ATOMOR64:
6632 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6633 X86::OR64ri32, X86::MOV64rm,
6634 X86::LCMPXCHG64, X86::MOV64rr,
6635 X86::NOT64r, X86::RAX,
6636 X86::GR64RegisterClass);
6637 case X86::ATOMXOR64:
6638 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6639 X86::XOR64ri32, X86::MOV64rm,
6640 X86::LCMPXCHG64, X86::MOV64rr,
6641 X86::NOT64r, X86::RAX,
6642 X86::GR64RegisterClass);
6643 case X86::ATOMNAND64:
6644 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6645 X86::AND64ri32, X86::MOV64rm,
6646 X86::LCMPXCHG64, X86::MOV64rr,
6647 X86::NOT64r, X86::RAX,
6648 X86::GR64RegisterClass, true);
6649 case X86::ATOMMIN64:
6650 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6651 case X86::ATOMMAX64:
6652 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6653 case X86::ATOMUMIN64:
6654 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6655 case X86::ATOMUMAX64:
6656 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006657 }
6658}
6659
6660//===----------------------------------------------------------------------===//
6661// X86 Optimization Hooks
6662//===----------------------------------------------------------------------===//
6663
Dan Gohman8181bd12008-07-27 21:46:04 +00006664void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00006665 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00006666 APInt &KnownZero,
6667 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006668 const SelectionDAG &DAG,
6669 unsigned Depth) const {
6670 unsigned Opc = Op.getOpcode();
6671 assert((Opc >= ISD::BUILTIN_OP_END ||
6672 Opc == ISD::INTRINSIC_WO_CHAIN ||
6673 Opc == ISD::INTRINSIC_W_CHAIN ||
6674 Opc == ISD::INTRINSIC_VOID) &&
6675 "Should use MaskedValueIsZero if you don't know whether Op"
6676 " is a target node!");
6677
Dan Gohman1d79e432008-02-13 23:07:24 +00006678 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006679 switch (Opc) {
6680 default: break;
6681 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00006682 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6683 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006684 break;
6685 }
6686}
6687
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006688/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00006689/// node is a GlobalAddress + offset.
6690bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6691 GlobalValue* &GA, int64_t &Offset) const{
6692 if (N->getOpcode() == X86ISD::Wrapper) {
6693 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006694 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6695 return true;
6696 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006697 }
Evan Chengef7be082008-05-12 19:56:52 +00006698 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006699}
6700
Evan Chengef7be082008-05-12 19:56:52 +00006701static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6702 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006703 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00006704 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00006705 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006706 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00006707 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006708 return false;
6709}
6710
Dan Gohman8181bd12008-07-27 21:46:04 +00006711static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00006712 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00006713 SDNode *&Base,
6714 SelectionDAG &DAG, MachineFrameInfo *MFI,
6715 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006716 Base = NULL;
6717 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006718 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00006719 if (Idx.getOpcode() == ISD::UNDEF) {
6720 if (!Base)
6721 return false;
6722 continue;
6723 }
6724
Dan Gohman8181bd12008-07-27 21:46:04 +00006725 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00006726 if (!Elt.getNode() ||
6727 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006728 return false;
6729 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006730 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00006731 if (Base->getOpcode() == ISD::UNDEF)
6732 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00006733 continue;
6734 }
6735 if (Elt.getOpcode() == ISD::UNDEF)
6736 continue;
6737
Gabor Greif1c80d112008-08-28 21:40:38 +00006738 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00006739 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00006740 return false;
6741 }
6742 return true;
6743}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006744
6745/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6746/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6747/// if the load addresses are consecutive, non-overlapping, and in the right
6748/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00006749static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006750 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00006751 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00006752 MVT VT = N->getValueType(0);
6753 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00006754 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00006755 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006756 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00006757 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6758 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00006759 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006760
Dan Gohman11821702007-07-27 17:16:43 +00006761 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00006762 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006763 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00006764 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00006765 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6766 LD->getSrcValueOffset(), LD->isVolatile(),
6767 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006768}
6769
Evan Chengb6290462008-05-12 23:04:07 +00006770/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00006771static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00006772 const X86Subtarget *Subtarget,
6773 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00006774 unsigned NumOps = N->getNumOperands();
6775
Evan Chenge9b9c672008-05-09 21:53:03 +00006776 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00006777 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006778 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006779
Duncan Sands92c43912008-06-06 12:08:01 +00006780 MVT VT = N->getValueType(0);
6781 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00006782 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6783 // We are looking for load i64 and zero extend. We want to transform
6784 // it before legalizer has a chance to expand it. Also look for i64
6785 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00006786 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006787 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006788 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00006789 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006790 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006791
6792 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00006793 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00006794 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00006795 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00006796 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00006797 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00006798 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00006799 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00006800 }
Evan Chenge9b9c672008-05-09 21:53:03 +00006801
6802 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00006803 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00006804
6805 // Load must not be an extload.
6806 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00006807 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00006808
Evan Chenge9b9c672008-05-09 21:53:03 +00006809 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6810}
6811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006812/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006813static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006814 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006815 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006816
6817 // If we have SSE[12] support, try to form min/max nodes.
6818 if (Subtarget->hasSSE2() &&
6819 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6820 if (Cond.getOpcode() == ISD::SETCC) {
6821 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00006822 SDValue LHS = N->getOperand(1);
6823 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006824 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
6825
6826 unsigned Opcode = 0;
6827 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
6828 switch (CC) {
6829 default: break;
6830 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6831 case ISD::SETULE:
6832 case ISD::SETLE:
6833 if (!UnsafeFPMath) break;
6834 // FALL THROUGH.
6835 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6836 case ISD::SETLT:
6837 Opcode = X86ISD::FMIN;
6838 break;
6839
6840 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6841 case ISD::SETUGT:
6842 case ISD::SETGT:
6843 if (!UnsafeFPMath) break;
6844 // FALL THROUGH.
6845 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6846 case ISD::SETGE:
6847 Opcode = X86ISD::FMAX;
6848 break;
6849 }
6850 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6851 switch (CC) {
6852 default: break;
6853 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6854 case ISD::SETUGT:
6855 case ISD::SETGT:
6856 if (!UnsafeFPMath) break;
6857 // FALL THROUGH.
6858 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6859 case ISD::SETGE:
6860 Opcode = X86ISD::FMIN;
6861 break;
6862
6863 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6864 case ISD::SETULE:
6865 case ISD::SETLE:
6866 if (!UnsafeFPMath) break;
6867 // FALL THROUGH.
6868 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6869 case ISD::SETLT:
6870 Opcode = X86ISD::FMAX;
6871 break;
6872 }
6873 }
6874
6875 if (Opcode)
6876 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6877 }
6878
6879 }
6880
Dan Gohman8181bd12008-07-27 21:46:04 +00006881 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006882}
6883
Chris Lattnerce84ae42008-02-22 02:09:43 +00006884/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006885static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006886 const X86Subtarget *Subtarget) {
6887 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6888 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00006889 // A preferable solution to the general problem is to figure out the right
6890 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00006891 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00006892 if (St->getValue().getValueType().isVector() &&
6893 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00006894 isa<LoadSDNode>(St->getValue()) &&
6895 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6896 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006897 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006898 LoadSDNode *Ld = 0;
6899 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00006900 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00006901 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00006902 // Must be a store of a load. We currently handle two cases: the load
6903 // is a direct child, and it's under an intervening TokenFactor. It is
6904 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00006905 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00006906 Ld = cast<LoadSDNode>(St->getChain());
6907 else if (St->getValue().hasOneUse() &&
6908 ChainVal->getOpcode() == ISD::TokenFactor) {
6909 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00006910 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00006911 TokenFactorIndex = i;
6912 Ld = cast<LoadSDNode>(St->getValue());
6913 } else
6914 Ops.push_back(ChainVal->getOperand(i));
6915 }
6916 }
6917 if (Ld) {
6918 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6919 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006920 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00006921 Ld->getBasePtr(), Ld->getSrcValue(),
6922 Ld->getSrcValueOffset(), Ld->isVolatile(),
6923 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006924 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006925 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00006926 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00006927 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6928 Ops.size());
6929 }
6930 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6931 St->getSrcValue(), St->getSrcValueOffset(),
6932 St->isVolatile(), St->getAlignment());
6933 }
6934
6935 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00006936 SDValue LoAddr = Ld->getBasePtr();
6937 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006938 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006939
Dan Gohman8181bd12008-07-27 21:46:04 +00006940 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006941 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6942 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006943 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00006944 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6945 Ld->isVolatile(),
6946 MinAlign(Ld->getAlignment(), 4));
6947
Dan Gohman8181bd12008-07-27 21:46:04 +00006948 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00006949 if (TokenFactorIndex != -1) {
6950 Ops.push_back(LoLd);
6951 Ops.push_back(HiLd);
6952 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6953 Ops.size());
6954 }
6955
6956 LoAddr = St->getBasePtr();
6957 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00006958 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00006959
Dan Gohman8181bd12008-07-27 21:46:04 +00006960 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00006961 St->getSrcValue(), St->getSrcValueOffset(),
6962 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00006963 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00006964 St->getSrcValue(),
6965 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00006966 St->isVolatile(),
6967 MinAlign(St->getAlignment(), 4));
6968 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00006969 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00006970 }
Dan Gohman8181bd12008-07-27 21:46:04 +00006971 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00006972}
6973
Chris Lattner470d5dc2008-01-25 06:14:17 +00006974/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6975/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006976static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00006977 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6978 // F[X]OR(0.0, x) -> x
6979 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00006980 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6981 if (C->getValueAPF().isPosZero())
6982 return N->getOperand(1);
6983 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6984 if (C->getValueAPF().isPosZero())
6985 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006986 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00006987}
6988
6989/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00006990static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00006991 // FAND(0.0, x) -> 0.0
6992 // FAND(x, 0.0) -> 0.0
6993 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6994 if (C->getValueAPF().isPosZero())
6995 return N->getOperand(0);
6996 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6997 if (C->getValueAPF().isPosZero())
6998 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00006999 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007000}
7001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007002
Dan Gohman8181bd12008-07-27 21:46:04 +00007003SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007004 DAGCombinerInfo &DCI) const {
7005 SelectionDAG &DAG = DCI.DAG;
7006 switch (N->getOpcode()) {
7007 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007008 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7009 case ISD::BUILD_VECTOR:
7010 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007011 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007012 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007013 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007014 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7015 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007016 }
7017
Dan Gohman8181bd12008-07-27 21:46:04 +00007018 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007019}
7020
7021//===----------------------------------------------------------------------===//
7022// X86 Inline Assembly Support
7023//===----------------------------------------------------------------------===//
7024
7025/// getConstraintType - Given a constraint letter, return the type of
7026/// constraint it is for this target.
7027X86TargetLowering::ConstraintType
7028X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7029 if (Constraint.size() == 1) {
7030 switch (Constraint[0]) {
7031 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007032 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007033 case 'r':
7034 case 'R':
7035 case 'l':
7036 case 'q':
7037 case 'Q':
7038 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007039 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007040 case 'Y':
7041 return C_RegisterClass;
7042 default:
7043 break;
7044 }
7045 }
7046 return TargetLowering::getConstraintType(Constraint);
7047}
7048
Dale Johannesene99fc902008-01-29 02:21:21 +00007049/// LowerXConstraint - try to replace an X constraint, which matches anything,
7050/// with another that has more specific requirements based on the type of the
7051/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007052const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007053LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007054 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7055 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007056 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007057 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007058 return "Y";
7059 if (Subtarget->hasSSE1())
7060 return "x";
7061 }
7062
7063 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007064}
7065
Chris Lattnera531abc2007-08-25 00:47:38 +00007066/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7067/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007068void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007069 char Constraint,
Dan Gohman8181bd12008-07-27 21:46:04 +00007070 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007071 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007072 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007074 switch (Constraint) {
7075 default: break;
7076 case 'I':
7077 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007078 if (C->getZExtValue() <= 31) {
7079 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007080 break;
7081 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007082 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007083 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007084 case 'J':
7085 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7086 if (C->getZExtValue() <= 63) {
7087 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7088 break;
7089 }
7090 }
7091 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007092 case 'N':
7093 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007094 if (C->getZExtValue() <= 255) {
7095 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007096 break;
7097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007098 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007099 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007100 case 'i': {
7101 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007102 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007103 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007104 break;
7105 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007106
7107 // If we are in non-pic codegen mode, we allow the address of a global (with
7108 // an optional displacement) to be used with 'i'.
7109 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7110 int64_t Offset = 0;
7111
7112 // Match either (GA) or (GA+C)
7113 if (GA) {
7114 Offset = GA->getOffset();
7115 } else if (Op.getOpcode() == ISD::ADD) {
7116 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7117 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7118 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007119 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007120 } else {
7121 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7122 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7123 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007124 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007125 else
7126 C = 0, GA = 0;
7127 }
7128 }
7129
7130 if (GA) {
7131 // If addressing this global requires a load (e.g. in PIC mode), we can't
7132 // match.
7133 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7134 false))
Chris Lattnera531abc2007-08-25 00:47:38 +00007135 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007136
7137 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7138 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007139 Result = Op;
7140 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007141 }
7142
7143 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007144 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007145 }
7146 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007147
Gabor Greif1c80d112008-08-28 21:40:38 +00007148 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007149 Ops.push_back(Result);
7150 return;
7151 }
7152 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007153}
7154
7155std::vector<unsigned> X86TargetLowering::
7156getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007157 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007158 if (Constraint.size() == 1) {
7159 // FIXME: not handling fp-stack yet!
7160 switch (Constraint[0]) { // GCC X86 Constraint Letters
7161 default: break; // Unknown constraint letter
7162 case 'A': // EAX/EDX
7163 if (VT == MVT::i32 || VT == MVT::i64)
7164 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7165 break;
7166 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7167 case 'Q': // Q_REGS
7168 if (VT == MVT::i32)
7169 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7170 else if (VT == MVT::i16)
7171 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7172 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007173 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007174 else if (VT == MVT::i64)
7175 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7176 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007177 }
7178 }
7179
7180 return std::vector<unsigned>();
7181}
7182
7183std::pair<unsigned, const TargetRegisterClass*>
7184X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007185 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007186 // First, see if this is a constraint that directly corresponds to an LLVM
7187 // register class.
7188 if (Constraint.size() == 1) {
7189 // GCC Constraint Letters
7190 switch (Constraint[0]) {
7191 default: break;
7192 case 'r': // GENERAL_REGS
7193 case 'R': // LEGACY_REGS
7194 case 'l': // INDEX_REGS
7195 if (VT == MVT::i64 && Subtarget->is64Bit())
7196 return std::make_pair(0U, X86::GR64RegisterClass);
7197 if (VT == MVT::i32)
7198 return std::make_pair(0U, X86::GR32RegisterClass);
7199 else if (VT == MVT::i16)
7200 return std::make_pair(0U, X86::GR16RegisterClass);
7201 else if (VT == MVT::i8)
7202 return std::make_pair(0U, X86::GR8RegisterClass);
7203 break;
Chris Lattner267805f2008-03-11 19:06:29 +00007204 case 'f': // FP Stack registers.
7205 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7206 // value to the correct fpstack register class.
7207 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7208 return std::make_pair(0U, X86::RFP32RegisterClass);
7209 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7210 return std::make_pair(0U, X86::RFP64RegisterClass);
7211 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007212 case 'y': // MMX_REGS if MMX allowed.
7213 if (!Subtarget->hasMMX()) break;
7214 return std::make_pair(0U, X86::VR64RegisterClass);
7215 break;
7216 case 'Y': // SSE_REGS if SSE2 allowed
7217 if (!Subtarget->hasSSE2()) break;
7218 // FALL THROUGH.
7219 case 'x': // SSE_REGS if SSE1 allowed
7220 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007221
7222 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007223 default: break;
7224 // Scalar SSE types.
7225 case MVT::f32:
7226 case MVT::i32:
7227 return std::make_pair(0U, X86::FR32RegisterClass);
7228 case MVT::f64:
7229 case MVT::i64:
7230 return std::make_pair(0U, X86::FR64RegisterClass);
7231 // Vector types.
7232 case MVT::v16i8:
7233 case MVT::v8i16:
7234 case MVT::v4i32:
7235 case MVT::v2i64:
7236 case MVT::v4f32:
7237 case MVT::v2f64:
7238 return std::make_pair(0U, X86::VR128RegisterClass);
7239 }
7240 break;
7241 }
7242 }
7243
7244 // Use the default implementation in TargetLowering to convert the register
7245 // constraint into a member of a register class.
7246 std::pair<unsigned, const TargetRegisterClass*> Res;
7247 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7248
7249 // Not found as a standard register?
7250 if (Res.second == 0) {
7251 // GCC calls "st(0)" just plain "st".
7252 if (StringsEqualNoCase("{st}", Constraint)) {
7253 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007254 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007255 }
7256
7257 return Res;
7258 }
7259
7260 // Otherwise, check to see if this is a register class of the wrong value
7261 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7262 // turn into {ax},{dx}.
7263 if (Res.second->hasType(VT))
7264 return Res; // Correct type already, nothing to do.
7265
7266 // All of the single-register GCC register classes map their values onto
7267 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7268 // really want an 8-bit or 32-bit register, map to the appropriate register
7269 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007270 if (Res.second == X86::GR16RegisterClass) {
7271 if (VT == MVT::i8) {
7272 unsigned DestReg = 0;
7273 switch (Res.first) {
7274 default: break;
7275 case X86::AX: DestReg = X86::AL; break;
7276 case X86::DX: DestReg = X86::DL; break;
7277 case X86::CX: DestReg = X86::CL; break;
7278 case X86::BX: DestReg = X86::BL; break;
7279 }
7280 if (DestReg) {
7281 Res.first = DestReg;
7282 Res.second = Res.second = X86::GR8RegisterClass;
7283 }
7284 } else if (VT == MVT::i32) {
7285 unsigned DestReg = 0;
7286 switch (Res.first) {
7287 default: break;
7288 case X86::AX: DestReg = X86::EAX; break;
7289 case X86::DX: DestReg = X86::EDX; break;
7290 case X86::CX: DestReg = X86::ECX; break;
7291 case X86::BX: DestReg = X86::EBX; break;
7292 case X86::SI: DestReg = X86::ESI; break;
7293 case X86::DI: DestReg = X86::EDI; break;
7294 case X86::BP: DestReg = X86::EBP; break;
7295 case X86::SP: DestReg = X86::ESP; break;
7296 }
7297 if (DestReg) {
7298 Res.first = DestReg;
7299 Res.second = Res.second = X86::GR32RegisterClass;
7300 }
7301 } else if (VT == MVT::i64) {
7302 unsigned DestReg = 0;
7303 switch (Res.first) {
7304 default: break;
7305 case X86::AX: DestReg = X86::RAX; break;
7306 case X86::DX: DestReg = X86::RDX; break;
7307 case X86::CX: DestReg = X86::RCX; break;
7308 case X86::BX: DestReg = X86::RBX; break;
7309 case X86::SI: DestReg = X86::RSI; break;
7310 case X86::DI: DestReg = X86::RDI; break;
7311 case X86::BP: DestReg = X86::RBP; break;
7312 case X86::SP: DestReg = X86::RSP; break;
7313 }
7314 if (DestReg) {
7315 Res.first = DestReg;
7316 Res.second = Res.second = X86::GR64RegisterClass;
7317 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007318 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007319 } else if (Res.second == X86::FR32RegisterClass ||
7320 Res.second == X86::FR64RegisterClass ||
7321 Res.second == X86::VR128RegisterClass) {
7322 // Handle references to XMM physical registers that got mapped into the
7323 // wrong class. This can happen with constraints like {xmm0} where the
7324 // target independent register mapper will just pick the first match it can
7325 // find, ignoring the required type.
7326 if (VT == MVT::f32)
7327 Res.second = X86::FR32RegisterClass;
7328 else if (VT == MVT::f64)
7329 Res.second = X86::FR64RegisterClass;
7330 else if (X86::VR128RegisterClass->hasType(VT))
7331 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007332 }
7333
7334 return Res;
7335}