blob: eee38406505566d48e408fb9a510a0ed4526c581 [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
Evan Cheng08c171a2008-10-14 21:26:46 +000087 setLoadExtAction(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);
Evan Cheng71343822008-10-15 02:05:31 +000095 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
97 // SETOEQ and SETUNE require checking two conditions.
98 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
99 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
100 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
101 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
106 // operation.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
108 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
110
111 if (Subtarget->is64Bit()) {
112 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
113 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
114 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000115 if (X86ScalarSSEf64)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
118 else
119 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
120 }
121
122 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
123 // this operation.
124 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
126 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000127 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000129 // f32 and f64 cases are Legal, f80 case is not
130 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
131 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
133 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
134 }
135
Dale Johannesen958b08b2007-09-19 23:55:34 +0000136 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
137 // are Legal, f80 is custom lowered.
138 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
139 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
141 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
142 // this operation.
143 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
144 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
145
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000146 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000148 // f32 and f64 cases are Legal, f80 case is not
149 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 } else {
151 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
152 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
153 }
154
155 // Handle FP_TO_UINT by promoting the destination to a larger signed
156 // conversion.
157 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
158 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
159 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
160
161 if (Subtarget->is64Bit()) {
162 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000165 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 // Expand FP_TO_UINT into a select.
167 // FIXME: We would like to use a Custom expander here eventually to do
168 // the optimal thing for SSE vs. the default expansion in the legalizer.
169 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
170 else
171 // With SSE3 we can use fisttpll to convert to a signed i64.
172 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
173 }
174
175 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000176 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
178 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
179 }
180
Dan Gohman8450d862008-02-18 19:34:53 +0000181 // Scalar integer divide and remainder are lowered to use operations that
182 // produce two results, to match the available instructions. This exposes
183 // the two-result form to trivial CSE, which is able to combine x/y and x%y
184 // into a single instruction.
185 //
186 // Scalar integer multiply-high is also lowered to use two-result
187 // operations, to match the available instructions. However, plain multiply
188 // (low) operations are left as Legal, as there are single-result
189 // instructions for this in x86. Using the two-result multiply instructions
190 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000191 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
192 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
193 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
194 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
195 setOperationAction(ISD::SREM , MVT::i8 , Expand);
196 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000197 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
198 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
199 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
200 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
201 setOperationAction(ISD::SREM , MVT::i16 , Expand);
202 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000203 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
204 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
205 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
206 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
207 setOperationAction(ISD::SREM , MVT::i32 , Expand);
208 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000209 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
210 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
211 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
212 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
213 setOperationAction(ISD::SREM , MVT::i64 , Expand);
214 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000215
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
217 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
218 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
219 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000221 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
222 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
223 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
225 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000226 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000228 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000229 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000232 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
233 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000235 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
236 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000238 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
239 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 if (Subtarget->is64Bit()) {
241 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000242 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
243 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 }
245
246 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
247 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
248
249 // These should be promoted to a larger select which is supported.
250 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
251 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
252 // X86 wants to expand cmov itself.
253 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
254 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
255 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
256 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000257 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
259 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
260 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
261 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
262 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000263 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 if (Subtarget->is64Bit()) {
265 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
266 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
267 }
268 // X86 ret instruction may pop stack.
269 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000270 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271
272 // Darwin ABI issue.
273 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
276 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000277 if (Subtarget->is64Bit())
278 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000279 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 if (Subtarget->is64Bit()) {
281 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
282 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
283 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000284 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 }
286 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
287 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
288 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
289 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000290 if (Subtarget->is64Bit()) {
291 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
292 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
293 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
294 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295
Evan Cheng8d51ab32008-03-10 19:38:10 +0000296 if (Subtarget->hasSSE1())
297 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000298
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000299 if (!Subtarget->hasSSE2())
300 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
301
Mon P Wang078a62d2008-05-05 19:05:59 +0000302 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000303 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
304 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
305 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
306 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000307
Dale Johannesen9011d872008-09-29 22:25:26 +0000308 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
309 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
310 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
311 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000312
Dale Johannesenf160d802008-10-02 18:53:47 +0000313 if (!Subtarget->is64Bit()) {
314 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
315 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
316 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
320 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
321 }
322
Dan Gohman472d12c2008-06-30 20:59:49 +0000323 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
324 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 // FIXME - use subtarget debug flags
326 if (!Subtarget->isTargetDarwin() &&
327 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000328 !Subtarget->isTargetCygMing()) {
329 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
330 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
331 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
333 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
334 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
335 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
336 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
337 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 setExceptionPointerRegister(X86::RAX);
339 setExceptionSelectorRegister(X86::RDX);
340 } else {
341 setExceptionPointerRegister(X86::EAX);
342 setExceptionSelectorRegister(X86::EDX);
343 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000344 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000345 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
346
Duncan Sands7407a9f2007-09-11 14:10:23 +0000347 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000348
Chris Lattner56b941f2008-01-15 21:58:22 +0000349 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
352 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000354 if (Subtarget->is64Bit()) {
355 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000357 } else {
358 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000360 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361
362 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
363 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
364 if (Subtarget->is64Bit())
365 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
366 if (Subtarget->isTargetCygMing())
367 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
368 else
369 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
370
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000371 if (X86ScalarSSEf64) {
372 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 // Set up the FP register classes.
374 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
375 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
376
377 // Use ANDPD to simulate FABS.
378 setOperationAction(ISD::FABS , MVT::f64, Custom);
379 setOperationAction(ISD::FABS , MVT::f32, Custom);
380
381 // Use XORP to simulate FNEG.
382 setOperationAction(ISD::FNEG , MVT::f64, Custom);
383 setOperationAction(ISD::FNEG , MVT::f32, Custom);
384
385 // Use ANDPD and ORPD to simulate FCOPYSIGN.
386 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
387 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
388
389 // We don't support sin/cos/fmod
390 setOperationAction(ISD::FSIN , MVT::f64, Expand);
391 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 setOperationAction(ISD::FSIN , MVT::f32, Expand);
393 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394
395 // Expand FP immediates into loads from the stack, except for the special
396 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000397 addLegalFPImmediate(APFloat(+0.0)); // xorpd
398 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000399
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000400 // Floating truncations from f80 and extensions to f80 go through memory.
401 // If optimizing, we lie about this though and handle it in
402 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
403 if (Fast) {
404 setConvertAction(MVT::f32, MVT::f80, Expand);
405 setConvertAction(MVT::f64, MVT::f80, Expand);
406 setConvertAction(MVT::f80, MVT::f32, Expand);
407 setConvertAction(MVT::f80, MVT::f64, Expand);
408 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000409 } else if (X86ScalarSSEf32) {
410 // Use SSE for f32, x87 for f64.
411 // Set up the FP register classes.
412 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
413 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
414
415 // Use ANDPS to simulate FABS.
416 setOperationAction(ISD::FABS , MVT::f32, Custom);
417
418 // Use XORP to simulate FNEG.
419 setOperationAction(ISD::FNEG , MVT::f32, Custom);
420
421 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
422
423 // Use ANDPS and ORPS to simulate FCOPYSIGN.
424 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
425 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
426
427 // We don't support sin/cos/fmod
428 setOperationAction(ISD::FSIN , MVT::f32, Expand);
429 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000430
Nate Begemane2ba64f2008-02-14 08:57:00 +0000431 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000432 addLegalFPImmediate(APFloat(+0.0f)); // xorps
433 addLegalFPImmediate(APFloat(+0.0)); // FLD0
434 addLegalFPImmediate(APFloat(+1.0)); // FLD1
435 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
436 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
437
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000438 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
439 // this though and handle it in InstructionSelectPreprocess so that
440 // dagcombine2 can hack on these.
441 if (Fast) {
442 setConvertAction(MVT::f32, MVT::f64, Expand);
443 setConvertAction(MVT::f32, MVT::f80, Expand);
444 setConvertAction(MVT::f80, MVT::f32, Expand);
445 setConvertAction(MVT::f64, MVT::f32, Expand);
446 // And x87->x87 truncations also.
447 setConvertAction(MVT::f80, MVT::f64, Expand);
448 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000449
450 if (!UnsafeFPMath) {
451 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
452 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
453 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 // Set up the FP register classes.
457 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
458 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
459
460 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
461 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
462 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
463 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000464
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000465 // Floating truncations go through memory. If optimizing, we lie about
466 // this though and handle it in InstructionSelectPreprocess so that
467 // dagcombine2 can hack on these.
468 if (Fast) {
469 setConvertAction(MVT::f80, MVT::f32, Expand);
470 setConvertAction(MVT::f64, MVT::f32, Expand);
471 setConvertAction(MVT::f80, MVT::f64, Expand);
472 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473
474 if (!UnsafeFPMath) {
475 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
476 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
477 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000478 addLegalFPImmediate(APFloat(+0.0)); // FLD0
479 addLegalFPImmediate(APFloat(+1.0)); // FLD1
480 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
481 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000482 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
483 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
484 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
485 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 }
487
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000488 // Long double always uses X87.
489 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000490 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000492 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000493 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000494 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000495 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
496 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000497 addLegalFPImmediate(TmpFlt); // FLD0
498 TmpFlt.changeSign();
499 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
500 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000501 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
502 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000503 addLegalFPImmediate(TmpFlt2); // FLD1
504 TmpFlt2.changeSign();
505 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
506 }
507
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000508 if (!UnsafeFPMath) {
509 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
510 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
511 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000512
Dan Gohman2f7b1982007-10-11 23:21:31 +0000513 // Always use a library call for pow.
514 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
515 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
516 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
517
Dale Johannesen92b33082008-09-04 00:47:13 +0000518 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000519 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000520 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000521 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000522 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 // First set operation action for all vector types to expand. Then we
525 // will selectively turn on ones that can be effectively codegen'd.
526 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
527 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000528 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000541 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
543 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000544 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000566 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 }
572
573 if (Subtarget->hasMMX()) {
574 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
575 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
576 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000577 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
579
580 // FIXME: add MMX packed arithmetics
581
582 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
583 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
584 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
585 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
586
587 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
588 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
589 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000590 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591
592 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
593 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
594
595 setOperationAction(ISD::AND, MVT::v8i8, Promote);
596 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
597 setOperationAction(ISD::AND, MVT::v4i16, Promote);
598 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
599 setOperationAction(ISD::AND, MVT::v2i32, Promote);
600 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
601 setOperationAction(ISD::AND, MVT::v1i64, Legal);
602
603 setOperationAction(ISD::OR, MVT::v8i8, Promote);
604 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
605 setOperationAction(ISD::OR, MVT::v4i16, Promote);
606 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
607 setOperationAction(ISD::OR, MVT::v2i32, Promote);
608 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
609 setOperationAction(ISD::OR, MVT::v1i64, Legal);
610
611 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
612 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
613 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
614 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
615 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
616 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
617 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
618
619 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
620 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
621 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
622 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
623 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
624 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000625 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
626 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000627 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
628
629 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
630 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
631 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000632 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
634
635 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
636 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
637 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
639
Evan Cheng759fe022008-07-22 18:39:19 +0000640 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
642 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000643 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000644
645 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 }
647
648 if (Subtarget->hasSSE1()) {
649 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
650
651 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
652 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
653 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
654 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
655 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
656 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
658 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
659 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
660 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
661 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000662 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663 }
664
665 if (Subtarget->hasSSE2()) {
666 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
667 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
668 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
669 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
670 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
671
672 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
673 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
674 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
675 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
676 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
677 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
678 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
679 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
680 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
681 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
682 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
683 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
684 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
685 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
686 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687
Nate Begeman03605a02008-07-17 16:51:19 +0000688 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
689 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
690 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
691 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000692
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
694 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
696 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
698
699 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000700 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
701 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000702 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000703 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000704 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000705 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
706 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
707 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 }
709 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
710 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
711 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
712 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000715 if (Subtarget->is64Bit()) {
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000717 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000718 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000719
720 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
721 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000722 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
723 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
724 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
725 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
726 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
727 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
728 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
729 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
730 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
731 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 }
733
Chris Lattner3bc08502008-01-17 19:59:44 +0000734 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000735
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 // Custom lower v2i64 and v2f64 selects.
737 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
738 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
739 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
740 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000741
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000742 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000743
744 if (Subtarget->hasSSE41()) {
745 // FIXME: Do we need to handle scalar-to-vector here?
746 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000747 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000748
749 // i8 and i16 vectors are custom , because the source register and source
750 // source memory operand types are not the same width. f32 vectors are
751 // custom since the immediate controlling the insert encodes additional
752 // information.
753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
754 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
755 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
756 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
757
758 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
759 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
760 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000761 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000762
763 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000764 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000766 }
767 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000768
Nate Begeman03605a02008-07-17 16:51:19 +0000769 if (Subtarget->hasSSE42()) {
770 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
771 }
772
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 // We want to custom lower some of our intrinsics.
774 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
775
776 // We have target-specific dag combine patterns for the following nodes:
777 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000778 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000780 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000781
782 computeRegisterProperties();
783
784 // FIXME: These should be based on subtarget info. Plus, the values should
785 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000786 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
787 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
788 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000789 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000790 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000791}
792
Scott Michel502151f2008-03-10 15:42:14 +0000793
Dan Gohman8181bd12008-07-27 21:46:04 +0000794MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000795 return MVT::i8;
796}
797
798
Evan Cheng5a67b812008-01-23 23:17:41 +0000799/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
800/// the desired ByVal argument alignment.
801static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
802 if (MaxAlign == 16)
803 return;
804 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
805 if (VTy->getBitWidth() == 128)
806 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000807 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
808 unsigned EltAlign = 0;
809 getMaxByValAlign(ATy->getElementType(), EltAlign);
810 if (EltAlign > MaxAlign)
811 MaxAlign = EltAlign;
812 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
813 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
814 unsigned EltAlign = 0;
815 getMaxByValAlign(STy->getElementType(i), EltAlign);
816 if (EltAlign > MaxAlign)
817 MaxAlign = EltAlign;
818 if (MaxAlign == 16)
819 break;
820 }
821 }
822 return;
823}
824
825/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
826/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000827/// that contain SSE vectors are placed at 16-byte boundaries while the rest
828/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000829unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000830 if (Subtarget->is64Bit()) {
831 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000832 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000833 if (TyAlign > 8)
834 return TyAlign;
835 return 8;
836 }
837
Evan Cheng5a67b812008-01-23 23:17:41 +0000838 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000839 if (Subtarget->hasSSE1())
840 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000841 return Align;
842}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000843
Evan Cheng8c590372008-05-15 08:39:06 +0000844/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000845/// and store operations as a result of memset, memcpy, and memmove
846/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000847/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000848MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000849X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
850 bool isSrcConst, bool isSrcStr) const {
851 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
852 return MVT::v4i32;
853 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
854 return MVT::v4f32;
855 if (Subtarget->is64Bit() && Size >= 8)
856 return MVT::i64;
857 return MVT::i32;
858}
859
860
Evan Cheng6fb06762007-11-09 01:32:10 +0000861/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
862/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000863SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000864 SelectionDAG &DAG) const {
865 if (usesGlobalOffsetTable())
866 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
867 if (!Subtarget->isPICStyleRIPRel())
868 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
869 return Table;
870}
871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872//===----------------------------------------------------------------------===//
873// Return Value Calling Convention Implementation
874//===----------------------------------------------------------------------===//
875
876#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000877
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000879SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
881
882 SmallVector<CCValAssign, 16> RVLocs;
883 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
884 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
885 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000886 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000887
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 // If this is the first return lowered for this function, add the regs to the
889 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000890 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 for (unsigned i = 0; i != RVLocs.size(); ++i)
892 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000893 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000895 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000897 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000898 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000899 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000900 SDValue TailCall = Chain;
901 SDValue TargetAddress = TailCall.getOperand(1);
902 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000903 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000904 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000905 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000906 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000907 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
908 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000909 assert(StackAdjustment.getOpcode() == ISD::Constant &&
910 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000911
Dan Gohman8181bd12008-07-27 21:46:04 +0000912 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000913 Operands.push_back(Chain.getOperand(0));
914 Operands.push_back(TargetAddress);
915 Operands.push_back(StackAdjustment);
916 // Copy registers used by the call. Last operand is a flag so it is not
917 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000918 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000919 Operands.push_back(Chain.getOperand(i));
920 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000921 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
922 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000923 }
924
925 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000926 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000927
Dan Gohman8181bd12008-07-27 21:46:04 +0000928 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000929 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
930 // Operand #1 = Bytes To Pop
931 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
932
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000934 for (unsigned i = 0; i != RVLocs.size(); ++i) {
935 CCValAssign &VA = RVLocs[i];
936 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000937 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000938
Chris Lattnerb56cc342008-03-11 03:23:40 +0000939 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
940 // the RET instruction and handled by the FP Stackifier.
941 if (RVLocs[i].getLocReg() == X86::ST0 ||
942 RVLocs[i].getLocReg() == X86::ST1) {
943 // If this is a copy from an xmm register to ST(0), use an FPExtend to
944 // change the value to the FP stack register class.
945 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
946 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
947 RetOps.push_back(ValToCopy);
948 // Don't emit a copytoreg.
949 continue;
950 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000951
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000952 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000953 Flag = Chain.getValue(1);
954 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000955
956 // The x86-64 ABI for returning structs by value requires that we copy
957 // the sret argument into %rax for the return. We saved the argument into
958 // a virtual register in the entry block, so now we copy the value out
959 // and into %rax.
960 if (Subtarget->is64Bit() &&
961 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
962 MachineFunction &MF = DAG.getMachineFunction();
963 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
964 unsigned Reg = FuncInfo->getSRetReturnReg();
965 if (!Reg) {
966 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
967 FuncInfo->setSRetReturnReg(Reg);
968 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000969 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000970
971 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
972 Flag = Chain.getValue(1);
973 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974
Chris Lattnerb56cc342008-03-11 03:23:40 +0000975 RetOps[0] = Chain; // Update chain.
976
977 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000978 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000979 RetOps.push_back(Flag);
980
981 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982}
983
984
985/// LowerCallResult - Lower the result values of an ISD::CALL into the
986/// appropriate copies out of appropriate physical registers. This assumes that
987/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
988/// being lowered. The returns a SDNode with the same number of values as the
989/// ISD::CALL.
990SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000991LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 unsigned CallingConv, SelectionDAG &DAG) {
993
994 // Assign locations to each value returned by this call.
995 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +0000996 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
998 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
999
Dan Gohman8181bd12008-07-27 21:46:04 +00001000 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001
1002 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001003 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001004 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001005
1006 // If this is a call to a function that returns an fp value on the floating
1007 // point stack, but where we prefer to use the value in xmm registers, copy
1008 // 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 +00001009 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1010 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001011 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1012 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001014
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001015 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1016 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001017 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001018 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001019
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001020 if (CopyVT != RVLocs[i].getValVT()) {
1021 // Round the F80 the right size, which also moves to the appropriate xmm
1022 // register.
1023 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1024 // This truncation won't change the value.
1025 DAG.getIntPtrConstant(1));
1026 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001027
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001028 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001029 }
Duncan Sands698842f2008-07-02 17:40:58 +00001030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 // Merge everything together with a MERGE_VALUES node.
1032 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001033 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001034 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001035}
1036
1037
1038//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001039// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001040//===----------------------------------------------------------------------===//
1041// StdCall calling convention seems to be standard for many Windows' API
1042// routines and around. It differs from C calling convention just a little:
1043// callee should clean up the stack, not caller. Symbols should be also
1044// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001045// For info on fast calling convention see Fast Calling Convention (tail call)
1046// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001047
1048/// AddLiveIn - This helper function adds the specified physical register to the
1049/// MachineFunction as a live in value. It also creates a corresponding virtual
1050/// register for it.
1051static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1052 const TargetRegisterClass *RC) {
1053 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001054 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1055 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 return VReg;
1057}
1058
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001059/// CallIsStructReturn - Determines whether a CALL node uses struct return
1060/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001061static bool CallIsStructReturn(CallSDNode *TheCall) {
1062 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001063 if (!NumOps)
1064 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001065
Dan Gohman705e3f72008-09-13 01:54:27 +00001066 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001067}
1068
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001069/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1070/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001071static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001072 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001073 if (!NumArgs)
1074 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001075
1076 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001077}
1078
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001079/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1080/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001081/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001082bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001083 if (IsVarArg)
1084 return false;
1085
Dan Gohman705e3f72008-09-13 01:54:27 +00001086 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001087 default:
1088 return false;
1089 case CallingConv::X86_StdCall:
1090 return !Subtarget->is64Bit();
1091 case CallingConv::X86_FastCall:
1092 return !Subtarget->is64Bit();
1093 case CallingConv::Fast:
1094 return PerformTailCallOpt;
1095 }
1096}
1097
Dan Gohman705e3f72008-09-13 01:54:27 +00001098/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1099/// given CallingConvention value.
1100CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001101 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001102 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001103 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001104 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1105 return CC_X86_64_TailCall;
1106 else
1107 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001108 }
1109
Gordon Henriksen18ace102008-01-05 16:56:59 +00001110 if (CC == CallingConv::X86_FastCall)
1111 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001112 else if (CC == CallingConv::Fast)
1113 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001114 else
1115 return CC_X86_32_C;
1116}
1117
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001118/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1119/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001120NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001121X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001122 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001123 if (CC == CallingConv::X86_FastCall)
1124 return FastCall;
1125 else if (CC == CallingConv::X86_StdCall)
1126 return StdCall;
1127 return None;
1128}
1129
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001130
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001131/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1132/// in a register before calling.
1133bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1134 return !IsTailCall && !Is64Bit &&
1135 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1136 Subtarget->isPICStyleGOT();
1137}
1138
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001139/// CallRequiresFnAddressInReg - Check whether the call requires the function
1140/// address to be loaded in a register.
1141bool
1142X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1143 return !Is64Bit && IsTailCall &&
1144 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1145 Subtarget->isPICStyleGOT();
1146}
1147
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001148/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1149/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001150/// the specific parameter attribute. The copy will be passed as a byval
1151/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001152static SDValue
1153CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001154 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001155 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001156 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001157 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001158}
1159
Dan Gohman8181bd12008-07-27 21:46:04 +00001160SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001161 const CCValAssign &VA,
1162 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001163 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001164 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001165 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001166 ISD::ArgFlagsTy Flags =
1167 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001168 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001169 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001170
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001171 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1172 // changed with more analysis.
1173 // In case of tail call optimization mark all arguments mutable. Since they
1174 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001175 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001176 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001177 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001178 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001179 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001180 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001181 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001182}
1183
Dan Gohman8181bd12008-07-27 21:46:04 +00001184SDValue
1185X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001186 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001187 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1188
1189 const Function* Fn = MF.getFunction();
1190 if (Fn->hasExternalLinkage() &&
1191 Subtarget->isTargetCygMing() &&
1192 Fn->getName() == "main")
1193 FuncInfo->setForceFramePointer(true);
1194
1195 // Decorate the function name.
1196 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001199 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001200 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001201 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001202 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001203 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001204
1205 assert(!(isVarArg && CC == CallingConv::Fast) &&
1206 "Var args not supported with calling convention fastcc");
1207
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 // Assign locations to all of the incoming arguments.
1209 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001210 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001211 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001212
Dan Gohman8181bd12008-07-27 21:46:04 +00001213 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 unsigned LastVal = ~0U;
1215 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1216 CCValAssign &VA = ArgLocs[i];
1217 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1218 // places.
1219 assert(VA.getValNo() != LastVal &&
1220 "Don't support value assigned to multiple locs yet");
1221 LastVal = VA.getValNo();
1222
1223 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001224 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225 TargetRegisterClass *RC;
1226 if (RegVT == MVT::i32)
1227 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001228 else if (Is64Bit && RegVT == MVT::i64)
1229 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001230 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001231 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001232 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001233 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001234 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001235 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001236 else if (RegVT.isVector()) {
1237 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001238 if (!Is64Bit)
1239 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1240 else {
1241 // Darwin calling convention passes MMX values in either GPRs or
1242 // XMMs in x86-64. Other targets pass them in memory.
1243 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1244 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1245 RegVT = MVT::v2i64;
1246 } else {
1247 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1248 RegVT = MVT::i64;
1249 }
1250 }
1251 } else {
1252 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001254
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001256 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001257
1258 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1259 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1260 // right size.
1261 if (VA.getLocInfo() == CCValAssign::SExt)
1262 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1263 DAG.getValueType(VA.getValVT()));
1264 else if (VA.getLocInfo() == CCValAssign::ZExt)
1265 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1266 DAG.getValueType(VA.getValVT()));
1267
1268 if (VA.getLocInfo() != CCValAssign::Full)
1269 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1270
Gordon Henriksen18ace102008-01-05 16:56:59 +00001271 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001272 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001273 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001274 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1275 else if (RC == X86::VR128RegisterClass) {
1276 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1277 DAG.getConstant(0, MVT::i64));
1278 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1279 }
1280 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 ArgValues.push_back(ArgValue);
1283 } else {
1284 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001285 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 }
1287 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001288
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001289 // The x86-64 ABI for returning structs by value requires that we copy
1290 // the sret argument into %rax for the return. Save the argument into
1291 // a virtual register so that we can access it from the return points.
1292 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1293 MachineFunction &MF = DAG.getMachineFunction();
1294 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1295 unsigned Reg = FuncInfo->getSRetReturnReg();
1296 if (!Reg) {
1297 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1298 FuncInfo->setSRetReturnReg(Reg);
1299 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001300 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001301 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1302 }
1303
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001305 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001306 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001307 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001308
1309 // If the function takes variable number of arguments, make a frame index for
1310 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001311 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001312 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1313 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1314 }
1315 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001316 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1317
1318 // FIXME: We should really autogenerate these arrays
1319 static const unsigned GPR64ArgRegsWin64[] = {
1320 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001321 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001322 static const unsigned XMMArgRegsWin64[] = {
1323 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1324 };
1325 static const unsigned GPR64ArgRegs64Bit[] = {
1326 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1327 };
1328 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1330 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1331 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001332 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1333
1334 if (IsWin64) {
1335 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1336 GPR64ArgRegs = GPR64ArgRegsWin64;
1337 XMMArgRegs = XMMArgRegsWin64;
1338 } else {
1339 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1340 GPR64ArgRegs = GPR64ArgRegs64Bit;
1341 XMMArgRegs = XMMArgRegs64Bit;
1342 }
1343 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1344 TotalNumIntRegs);
1345 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1346 TotalNumXMMRegs);
1347
Gordon Henriksen18ace102008-01-05 16:56:59 +00001348 // For X86-64, if there are vararg parameters that are passed via
1349 // registers, then we must store them to their spots on the stack so they
1350 // may be loaded by deferencing the result of va_next.
1351 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001352 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1353 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1354 TotalNumXMMRegs * 16, 16);
1355
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001357 SmallVector<SDValue, 8> MemOps;
1358 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1359 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001360 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001361 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001362 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1363 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001364 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1365 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001366 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001367 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001368 MemOps.push_back(Store);
1369 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001370 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001371 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001372
Gordon Henriksen18ace102008-01-05 16:56:59 +00001373 // Now store the XMM (fp + vector) parameter registers.
1374 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001375 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001376 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1378 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001379 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1380 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001381 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001382 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001383 MemOps.push_back(Store);
1384 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001385 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001386 }
1387 if (!MemOps.empty())
1388 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1389 &MemOps[0], MemOps.size());
1390 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001391 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001392
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001393 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001394
Gordon Henriksen18ace102008-01-05 16:56:59 +00001395 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001396 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001397 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 BytesCallerReserves = 0;
1399 } else {
1400 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001402 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001403 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 BytesCallerReserves = StackSize;
1405 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001406
Gordon Henriksen18ace102008-01-05 16:56:59 +00001407 if (!Is64Bit) {
1408 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1409 if (CC == CallingConv::X86_FastCall)
1410 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412
Anton Korobeynikove844e472007-08-15 17:12:32 +00001413 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001414
1415 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001416 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001417 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001418}
1419
Dan Gohman8181bd12008-07-27 21:46:04 +00001420SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001421X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001422 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001423 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001424 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001425 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001426 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001427 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001428 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001429 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001430 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001431 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001432 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001433 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001434}
1435
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001436/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1437/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001438SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001439X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001440 SDValue &OutRetAddr,
1441 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001442 bool IsTailCall,
1443 bool Is64Bit,
1444 int FPDiff) {
1445 if (!IsTailCall || FPDiff==0) return Chain;
1446
1447 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001448 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001449 OutRetAddr = getReturnAddressFrameIndex(DAG);
1450 // Load the "old" Return address.
1451 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001452 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001453}
1454
1455/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1456/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001457static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001458EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001460 bool Is64Bit, int FPDiff) {
1461 // Store the return address to the appropriate stack slot.
1462 if (!FPDiff) return Chain;
1463 // Calculate the new stack slot for the return address.
1464 int SlotSize = Is64Bit ? 8 : 4;
1465 int NewReturnAddrFI =
1466 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001467 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001468 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001469 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001470 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001471 return Chain;
1472}
1473
Dan Gohman8181bd12008-07-27 21:46:04 +00001474SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001475 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001476 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1477 SDValue Chain = TheCall->getChain();
1478 unsigned CC = TheCall->getCallingConv();
1479 bool isVarArg = TheCall->isVarArg();
1480 bool IsTailCall = TheCall->isTailCall() &&
1481 CC == CallingConv::Fast && PerformTailCallOpt;
1482 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001483 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001484 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001485
1486 assert(!(isVarArg && CC == CallingConv::Fast) &&
1487 "Var args not supported with calling convention fastcc");
1488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 // Analyze operands of the call, assigning locations to each operand.
1490 SmallVector<CCValAssign, 16> ArgLocs;
1491 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001492 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001493
1494 // Get a count of how many bytes are to be pushed on the stack.
1495 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001496 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001497 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498
Gordon Henriksen18ace102008-01-05 16:56:59 +00001499 int FPDiff = 0;
1500 if (IsTailCall) {
1501 // Lower arguments at fp - stackoffset + fpdiff.
1502 unsigned NumBytesCallerPushed =
1503 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1504 FPDiff = NumBytesCallerPushed - NumBytes;
1505
1506 // Set the delta of movement of the returnaddr stackslot.
1507 // But only set if delta is greater than previous delta.
1508 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1509 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1510 }
1511
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001512 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513
Dan Gohman8181bd12008-07-27 21:46:04 +00001514 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001515 // Load return adress for tail calls.
1516 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1517 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001518
Dan Gohman8181bd12008-07-27 21:46:04 +00001519 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1520 SmallVector<SDValue, 8> MemOpChains;
1521 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001523 // Walk the register/memloc assignments, inserting copies/loads. In the case
1524 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001525 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1526 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001527 SDValue Arg = TheCall->getArg(i);
1528 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1529 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001530
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 // Promote the value if needed.
1532 switch (VA.getLocInfo()) {
1533 default: assert(0 && "Unknown loc info!");
1534 case CCValAssign::Full: break;
1535 case CCValAssign::SExt:
1536 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1537 break;
1538 case CCValAssign::ZExt:
1539 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1540 break;
1541 case CCValAssign::AExt:
1542 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1543 break;
1544 }
1545
1546 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001547 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001548 MVT RegVT = VA.getLocVT();
1549 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001550 switch (VA.getLocReg()) {
1551 default:
1552 break;
1553 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1554 case X86::R8: {
1555 // Special case: passing MMX values in GPR registers.
1556 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1557 break;
1558 }
1559 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1560 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1561 // Special case: passing MMX values in XMM registers.
1562 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1563 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1564 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1565 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1566 getMOVLMask(2, DAG));
1567 break;
1568 }
1569 }
1570 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1572 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001573 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001574 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001575 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001576 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1577
Dan Gohman705e3f72008-09-13 01:54:27 +00001578 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1579 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 }
1582 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001583
1584 if (!MemOpChains.empty())
1585 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1586 &MemOpChains[0], MemOpChains.size());
1587
1588 // Build a sequence of copy-to-reg nodes chained together with token chain
1589 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001590 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001591 // Tail call byval lowering might overwrite argument registers so in case of
1592 // tail call optimization the copies to registers are lowered later.
1593 if (!IsTailCall)
1594 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1595 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1596 InFlag);
1597 InFlag = Chain.getValue(1);
1598 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001599
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001600 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001601 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001602 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1603 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1604 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1605 InFlag);
1606 InFlag = Chain.getValue(1);
1607 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001608 // If we are tail calling and generating PIC/GOT style code load the address
1609 // of the callee into ecx. The value in ecx is used as target of the tail
1610 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1611 // calls on PIC/GOT architectures. Normally we would just put the address of
1612 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1613 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001614 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001615 // Note: The actual moving to ecx is done further down.
1616 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001617 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001618 !G->getGlobal()->hasProtectedVisibility())
1619 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001620 else if (isa<ExternalSymbolSDNode>(Callee))
1621 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001622 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001623
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624 if (Is64Bit && isVarArg) {
1625 // From AMD64 ABI document:
1626 // For calls that may call functions that use varargs or stdargs
1627 // (prototype-less calls or calls to functions containing ellipsis (...) in
1628 // the declaration) %al is used as hidden argument to specify the number
1629 // of SSE registers used. The contents of %al do not need to match exactly
1630 // the number of registers, but must be an ubound on the number of SSE
1631 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001632
1633 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001634 // Count the number of XMM registers allocated.
1635 static const unsigned XMMArgRegs[] = {
1636 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1637 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1638 };
1639 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1640
1641 Chain = DAG.getCopyToReg(Chain, X86::AL,
1642 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1643 InFlag = Chain.getValue(1);
1644 }
1645
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001646
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001647 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001648 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001649 SmallVector<SDValue, 8> MemOpChains2;
1650 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001651 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001652 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001653 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001654 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1655 CCValAssign &VA = ArgLocs[i];
1656 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001657 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001658 SDValue Arg = TheCall->getArg(i);
1659 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001660 // Create frame index.
1661 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001662 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001663 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001664 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001665
Duncan Sandsc93fae32008-03-21 09:14:45 +00001666 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001667 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001668 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001669 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001670 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1671 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1672
1673 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001674 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001675 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001676 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001677 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001678 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001679 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001680 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001681 }
1682 }
1683
1684 if (!MemOpChains2.empty())
1685 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001686 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001687
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001688 // Copy arguments to their registers.
1689 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1690 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1691 InFlag);
1692 InFlag = Chain.getValue(1);
1693 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001694 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001695
Gordon Henriksen18ace102008-01-05 16:56:59 +00001696 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001697 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1698 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001699 }
1700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001701 // If the callee is a GlobalAddress node (quite common, every direct call is)
1702 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1703 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1704 // We should use extra load for direct calls to dllimported functions in
1705 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001706 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1707 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001708 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1709 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001710 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1711 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001712 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001713 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001714
1715 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001716 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001717 Callee,InFlag);
1718 Callee = DAG.getRegister(Opc, getPointerTy());
1719 // Add register as live out.
1720 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001721 }
1722
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723 // Returns a chain & a flag for retval copy to use.
1724 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001725 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001726
1727 if (IsTailCall) {
1728 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001729 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1730 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001731 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001732 Ops.push_back(InFlag);
1733 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1734 InFlag = Chain.getValue(1);
1735
1736 // Returns a chain & a flag for retval copy to use.
1737 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1738 Ops.clear();
1739 }
1740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001741 Ops.push_back(Chain);
1742 Ops.push_back(Callee);
1743
Gordon Henriksen18ace102008-01-05 16:56:59 +00001744 if (IsTailCall)
1745 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001746
Gordon Henriksen18ace102008-01-05 16:56:59 +00001747 // Add argument registers to the end of the list so that they are known live
1748 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001749 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1750 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1751 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752
Evan Cheng8ba45e62008-03-18 23:36:35 +00001753 // Add an implicit use GOT pointer in EBX.
1754 if (!IsTailCall && !Is64Bit &&
1755 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1756 Subtarget->isPICStyleGOT())
1757 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1758
1759 // Add an implicit use of AL for x86 vararg functions.
1760 if (Is64Bit && isVarArg)
1761 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1762
Gabor Greif1c80d112008-08-28 21:40:38 +00001763 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001765
Gordon Henriksen18ace102008-01-05 16:56:59 +00001766 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001767 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 "Flag must be set. Depend on flag being set in LowerRET");
1769 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001770 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001771
Gabor Greif1c80d112008-08-28 21:40:38 +00001772 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001773 }
1774
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001775 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001776 InFlag = Chain.getValue(1);
1777
1778 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001780 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001782 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001783 // If this is is a call to a struct-return function, the callee
1784 // pops the hidden struct pointer, so we have to push it back.
1785 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001786 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001788 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001789
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001790 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001791 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001792 DAG.getIntPtrConstant(NumBytes, true),
1793 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1794 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001795 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001796 InFlag = Chain.getValue(1);
1797
1798 // Handle result values, copying them out of physregs into vregs that we
1799 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001800 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001801 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802}
1803
1804
1805//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001806// Fast Calling Convention (tail call) implementation
1807//===----------------------------------------------------------------------===//
1808
1809// Like std call, callee cleans arguments, convention except that ECX is
1810// reserved for storing the tail called function address. Only 2 registers are
1811// free for argument passing (inreg). Tail call optimization is performed
1812// provided:
1813// * tailcallopt is enabled
1814// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001815// On X86_64 architecture with GOT-style position independent code only local
1816// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001817// To keep the stack aligned according to platform abi the function
1818// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1819// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001820// If a tail called function callee has more arguments than the caller the
1821// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001822// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001823// original REtADDR, but before the saved framepointer or the spilled registers
1824// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1825// stack layout:
1826// arg1
1827// arg2
1828// RETADDR
1829// [ new RETADDR
1830// move area ]
1831// (possible EBP)
1832// ESI
1833// EDI
1834// local1 ..
1835
1836/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1837/// for a 16 byte align requirement.
1838unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1839 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001840 MachineFunction &MF = DAG.getMachineFunction();
1841 const TargetMachine &TM = MF.getTarget();
1842 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1843 unsigned StackAlignment = TFI.getStackAlignment();
1844 uint64_t AlignMask = StackAlignment - 1;
1845 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001846 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001847 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1848 // Number smaller than 12 so just add the difference.
1849 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1850 } else {
1851 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1852 Offset = ((~AlignMask) & Offset) + StackAlignment +
1853 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001854 }
Evan Chengded8f902008-09-07 09:07:23 +00001855 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001856}
1857
1858/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001859/// following the call is a return. A function is eligible if caller/callee
1860/// calling conventions match, currently only fastcc supports tail calls, and
1861/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001862bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001863 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001864 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001865 if (!PerformTailCallOpt)
1866 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001867
Dan Gohman705e3f72008-09-13 01:54:27 +00001868 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001869 MachineFunction &MF = DAG.getMachineFunction();
1870 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001871 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001873 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001874 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001875 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001876 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001877 return true;
1878
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001879 // Can only do local tail calls (in same module, hidden or protected) on
1880 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001881 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1882 return G->getGlobal()->hasHiddenVisibility()
1883 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001884 }
1885 }
Evan Chenge7a87392007-11-02 01:26:22 +00001886
1887 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001888}
1889
Dan Gohmanca4857a2008-09-03 23:12:08 +00001890FastISel *
1891X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001892 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001893 DenseMap<const Value *, unsigned> &vm,
1894 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001895 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001896 DenseMap<const AllocaInst *, int> &am
1897#ifndef NDEBUG
1898 , SmallSet<Instruction*, 8> &cil
1899#endif
1900 ) {
1901 return X86::createFastISel(mf, mmo, vm, bm, am
1902#ifndef NDEBUG
1903 , cil
1904#endif
1905 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001906}
1907
1908
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909//===----------------------------------------------------------------------===//
1910// Other Lowering Hooks
1911//===----------------------------------------------------------------------===//
1912
1913
Dan Gohman8181bd12008-07-27 21:46:04 +00001914SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001915 MachineFunction &MF = DAG.getMachineFunction();
1916 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1917 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001918 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001919
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001920 if (ReturnAddrIndex == 0) {
1921 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001922 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001923 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 }
1925
1926 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1927}
1928
1929
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001930/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1931/// specific condition code. It returns a false if it cannot do a direct
1932/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1933/// needed.
1934static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001935 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001936 SelectionDAG &DAG) {
1937 X86CC = X86::COND_INVALID;
1938 if (!isFP) {
1939 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1940 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1941 // X > -1 -> X == 0, jump !sign.
1942 RHS = DAG.getConstant(0, RHS.getValueType());
1943 X86CC = X86::COND_NS;
1944 return true;
1945 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1946 // X < 0 -> X == 0, jump on sign.
1947 X86CC = X86::COND_S;
1948 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001949 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001950 // X < 1 -> X <= 0
1951 RHS = DAG.getConstant(0, RHS.getValueType());
1952 X86CC = X86::COND_LE;
1953 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 }
1955 }
1956
1957 switch (SetCCOpcode) {
1958 default: break;
1959 case ISD::SETEQ: X86CC = X86::COND_E; break;
1960 case ISD::SETGT: X86CC = X86::COND_G; break;
1961 case ISD::SETGE: X86CC = X86::COND_GE; break;
1962 case ISD::SETLT: X86CC = X86::COND_L; break;
1963 case ISD::SETLE: X86CC = X86::COND_LE; break;
1964 case ISD::SETNE: X86CC = X86::COND_NE; break;
1965 case ISD::SETULT: X86CC = X86::COND_B; break;
1966 case ISD::SETUGT: X86CC = X86::COND_A; break;
1967 case ISD::SETULE: X86CC = X86::COND_BE; break;
1968 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1969 }
1970 } else {
Evan Chengb488ca32008-08-29 23:22:12 +00001971 // First determine if it requires or is profitable to flip the operands.
1972 bool Flip = false;
1973 switch (SetCCOpcode) {
1974 default: break;
1975 case ISD::SETOLT:
1976 case ISD::SETOLE:
1977 case ISD::SETUGT:
1978 case ISD::SETUGE:
1979 Flip = true;
1980 break;
1981 }
1982
1983 // If LHS is a foldable load, but RHS is not, flip the condition.
1984 if (!Flip &&
1985 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1986 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1987 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1988 Flip = true;
1989 }
1990 if (Flip)
1991 std::swap(LHS, RHS);
1992
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 // On a floating point condition, the flags are set as follows:
1994 // ZF PF CF op
1995 // 0 | 0 | 0 | X > Y
1996 // 0 | 0 | 1 | X < Y
1997 // 1 | 0 | 0 | X == Y
1998 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001999 switch (SetCCOpcode) {
2000 default: break;
2001 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002002 case ISD::SETEQ:
2003 X86CC = X86::COND_E;
2004 break;
2005 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002006 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002007 case ISD::SETGT:
2008 X86CC = X86::COND_A;
2009 break;
2010 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002012 case ISD::SETGE:
2013 X86CC = X86::COND_AE;
2014 break;
2015 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002017 case ISD::SETLT:
2018 X86CC = X86::COND_B;
2019 break;
2020 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002022 case ISD::SETLE:
2023 X86CC = X86::COND_BE;
2024 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002025 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002026 case ISD::SETNE:
2027 X86CC = X86::COND_NE;
2028 break;
2029 case ISD::SETUO:
2030 X86CC = X86::COND_P;
2031 break;
2032 case ISD::SETO:
2033 X86CC = X86::COND_NP;
2034 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002035 }
Evan Chengfc937c92008-08-28 23:48:31 +00002036 }
2037
Evan Chengc6162692008-08-29 22:13:21 +00002038 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002039}
2040
2041/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2042/// code. Current x86 isa includes the following FP cmov instructions:
2043/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2044static bool hasFPCMov(unsigned X86CC) {
2045 switch (X86CC) {
2046 default:
2047 return false;
2048 case X86::COND_B:
2049 case X86::COND_BE:
2050 case X86::COND_E:
2051 case X86::COND_P:
2052 case X86::COND_A:
2053 case X86::COND_AE:
2054 case X86::COND_NE:
2055 case X86::COND_NP:
2056 return true;
2057 }
2058}
2059
2060/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2061/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002062static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002063 if (Op.getOpcode() == ISD::UNDEF)
2064 return true;
2065
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002066 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067 return (Val >= Low && Val < Hi);
2068}
2069
2070/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2071/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002072static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002073 if (Op.getOpcode() == ISD::UNDEF)
2074 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002075 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076}
2077
2078/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2079/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2080bool X86::isPSHUFDMask(SDNode *N) {
2081 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2082
Dan Gohman7dc19012007-08-02 21:17:01 +00002083 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002084 return false;
2085
2086 // Check if the value doesn't reference the second vector.
2087 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002088 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002091 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 return false;
2093 }
2094
2095 return true;
2096}
2097
2098/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2099/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2100bool X86::isPSHUFHWMask(SDNode *N) {
2101 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2102
2103 if (N->getNumOperands() != 8)
2104 return false;
2105
2106 // Lower quadword copied in order.
2107 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002108 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002109 if (Arg.getOpcode() == ISD::UNDEF) continue;
2110 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002111 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002112 return false;
2113 }
2114
2115 // Upper quadword shuffled.
2116 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002117 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002118 if (Arg.getOpcode() == ISD::UNDEF) continue;
2119 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002120 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002121 if (Val < 4 || Val > 7)
2122 return false;
2123 }
2124
2125 return true;
2126}
2127
2128/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2129/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2130bool X86::isPSHUFLWMask(SDNode *N) {
2131 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2132
2133 if (N->getNumOperands() != 8)
2134 return false;
2135
2136 // Upper quadword copied in order.
2137 for (unsigned i = 4; i != 8; ++i)
2138 if (!isUndefOrEqual(N->getOperand(i), i))
2139 return false;
2140
2141 // Lower quadword shuffled.
2142 for (unsigned i = 0; i != 4; ++i)
2143 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2144 return false;
2145
2146 return true;
2147}
2148
2149/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2150/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002151static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002152 if (NumElems != 2 && NumElems != 4) return false;
2153
2154 unsigned Half = NumElems / 2;
2155 for (unsigned i = 0; i < Half; ++i)
2156 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2157 return false;
2158 for (unsigned i = Half; i < NumElems; ++i)
2159 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2160 return false;
2161
2162 return true;
2163}
2164
2165bool X86::isSHUFPMask(SDNode *N) {
2166 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2167 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2168}
2169
2170/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2171/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2172/// half elements to come from vector 1 (which would equal the dest.) and
2173/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002174static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175 if (NumOps != 2 && NumOps != 4) return false;
2176
2177 unsigned Half = NumOps / 2;
2178 for (unsigned i = 0; i < Half; ++i)
2179 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2180 return false;
2181 for (unsigned i = Half; i < NumOps; ++i)
2182 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2183 return false;
2184 return true;
2185}
2186
2187static bool isCommutedSHUFP(SDNode *N) {
2188 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2189 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2190}
2191
2192/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2193/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2194bool X86::isMOVHLPSMask(SDNode *N) {
2195 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2196
2197 if (N->getNumOperands() != 4)
2198 return false;
2199
2200 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2201 return isUndefOrEqual(N->getOperand(0), 6) &&
2202 isUndefOrEqual(N->getOperand(1), 7) &&
2203 isUndefOrEqual(N->getOperand(2), 2) &&
2204 isUndefOrEqual(N->getOperand(3), 3);
2205}
2206
2207/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2208/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2209/// <2, 3, 2, 3>
2210bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2211 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2212
2213 if (N->getNumOperands() != 4)
2214 return false;
2215
2216 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2217 return isUndefOrEqual(N->getOperand(0), 2) &&
2218 isUndefOrEqual(N->getOperand(1), 3) &&
2219 isUndefOrEqual(N->getOperand(2), 2) &&
2220 isUndefOrEqual(N->getOperand(3), 3);
2221}
2222
2223/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2224/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2225bool X86::isMOVLPMask(SDNode *N) {
2226 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2227
2228 unsigned NumElems = N->getNumOperands();
2229 if (NumElems != 2 && NumElems != 4)
2230 return false;
2231
2232 for (unsigned i = 0; i < NumElems/2; ++i)
2233 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2234 return false;
2235
2236 for (unsigned i = NumElems/2; i < NumElems; ++i)
2237 if (!isUndefOrEqual(N->getOperand(i), i))
2238 return false;
2239
2240 return true;
2241}
2242
2243/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2244/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2245/// and MOVLHPS.
2246bool X86::isMOVHPMask(SDNode *N) {
2247 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2248
2249 unsigned NumElems = N->getNumOperands();
2250 if (NumElems != 2 && NumElems != 4)
2251 return false;
2252
2253 for (unsigned i = 0; i < NumElems/2; ++i)
2254 if (!isUndefOrEqual(N->getOperand(i), i))
2255 return false;
2256
2257 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002258 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002259 if (!isUndefOrEqual(Arg, i + NumElems))
2260 return false;
2261 }
2262
2263 return true;
2264}
2265
2266/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2267/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002268bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269 bool V2IsSplat = false) {
2270 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2271 return false;
2272
2273 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002274 SDValue BitI = Elts[i];
2275 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002276 if (!isUndefOrEqual(BitI, j))
2277 return false;
2278 if (V2IsSplat) {
2279 if (isUndefOrEqual(BitI1, NumElts))
2280 return false;
2281 } else {
2282 if (!isUndefOrEqual(BitI1, j + NumElts))
2283 return false;
2284 }
2285 }
2286
2287 return true;
2288}
2289
2290bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2291 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2292 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2293}
2294
2295/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2296/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002297bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 bool V2IsSplat = false) {
2299 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2300 return false;
2301
2302 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002303 SDValue BitI = Elts[i];
2304 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 if (!isUndefOrEqual(BitI, j + NumElts/2))
2306 return false;
2307 if (V2IsSplat) {
2308 if (isUndefOrEqual(BitI1, NumElts))
2309 return false;
2310 } else {
2311 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2312 return false;
2313 }
2314 }
2315
2316 return true;
2317}
2318
2319bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2320 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2321 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2322}
2323
2324/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2325/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2326/// <0, 0, 1, 1>
2327bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2328 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2329
2330 unsigned NumElems = N->getNumOperands();
2331 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2332 return false;
2333
2334 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002335 SDValue BitI = N->getOperand(i);
2336 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337
2338 if (!isUndefOrEqual(BitI, j))
2339 return false;
2340 if (!isUndefOrEqual(BitI1, j))
2341 return false;
2342 }
2343
2344 return true;
2345}
2346
2347/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2348/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2349/// <2, 2, 3, 3>
2350bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2351 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2352
2353 unsigned NumElems = N->getNumOperands();
2354 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2355 return false;
2356
2357 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002358 SDValue BitI = N->getOperand(i);
2359 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002360
2361 if (!isUndefOrEqual(BitI, j))
2362 return false;
2363 if (!isUndefOrEqual(BitI1, j))
2364 return false;
2365 }
2366
2367 return true;
2368}
2369
2370/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2371/// specifies a shuffle of elements that is suitable for input to MOVSS,
2372/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002373static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002374 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002375 return false;
2376
2377 if (!isUndefOrEqual(Elts[0], NumElts))
2378 return false;
2379
2380 for (unsigned i = 1; i < NumElts; ++i) {
2381 if (!isUndefOrEqual(Elts[i], i))
2382 return false;
2383 }
2384
2385 return true;
2386}
2387
2388bool X86::isMOVLMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2391}
2392
2393/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2394/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2395/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002396static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002397 bool V2IsSplat = false,
2398 bool V2IsUndef = false) {
2399 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2400 return false;
2401
2402 if (!isUndefOrEqual(Ops[0], 0))
2403 return false;
2404
2405 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002406 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2408 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2409 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2410 return false;
2411 }
2412
2413 return true;
2414}
2415
2416static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2417 bool V2IsUndef = false) {
2418 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2419 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2420 V2IsSplat, V2IsUndef);
2421}
2422
2423/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2424/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2425bool X86::isMOVSHDUPMask(SDNode *N) {
2426 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2427
2428 if (N->getNumOperands() != 4)
2429 return false;
2430
2431 // Expect 1, 1, 3, 3
2432 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002433 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 if (Arg.getOpcode() == ISD::UNDEF) continue;
2435 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002436 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 if (Val != 1) return false;
2438 }
2439
2440 bool HasHi = false;
2441 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002442 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002443 if (Arg.getOpcode() == ISD::UNDEF) continue;
2444 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002445 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002446 if (Val != 3) return false;
2447 HasHi = true;
2448 }
2449
2450 // Don't use movshdup if it can be done with a shufps.
2451 return HasHi;
2452}
2453
2454/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2455/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2456bool X86::isMOVSLDUPMask(SDNode *N) {
2457 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2458
2459 if (N->getNumOperands() != 4)
2460 return false;
2461
2462 // Expect 0, 0, 2, 2
2463 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002464 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002465 if (Arg.getOpcode() == ISD::UNDEF) continue;
2466 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002467 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002468 if (Val != 0) return false;
2469 }
2470
2471 bool HasHi = false;
2472 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002473 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002474 if (Arg.getOpcode() == ISD::UNDEF) continue;
2475 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002476 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002477 if (Val != 2) return false;
2478 HasHi = true;
2479 }
2480
2481 // Don't use movshdup if it can be done with a shufps.
2482 return HasHi;
2483}
2484
2485/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2486/// specifies a identity operation on the LHS or RHS.
2487static bool isIdentityMask(SDNode *N, bool RHS = false) {
2488 unsigned NumElems = N->getNumOperands();
2489 for (unsigned i = 0; i < NumElems; ++i)
2490 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2491 return false;
2492 return true;
2493}
2494
2495/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2496/// a splat of a single element.
2497static bool isSplatMask(SDNode *N) {
2498 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2499
2500 // This is a splat operation if each element of the permute is the same, and
2501 // if the value doesn't reference the second vector.
2502 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002503 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 unsigned i = 0;
2505 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002506 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002507 if (isa<ConstantSDNode>(Elt)) {
2508 ElementBase = Elt;
2509 break;
2510 }
2511 }
2512
Gabor Greif1c80d112008-08-28 21:40:38 +00002513 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002514 return false;
2515
2516 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002517 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002518 if (Arg.getOpcode() == ISD::UNDEF) continue;
2519 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2520 if (Arg != ElementBase) return false;
2521 }
2522
2523 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002524 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525}
2526
2527/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2528/// a splat of a single element and it's a 2 or 4 element mask.
2529bool X86::isSplatMask(SDNode *N) {
2530 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2531
2532 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2533 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2534 return false;
2535 return ::isSplatMask(N);
2536}
2537
2538/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2539/// specifies a splat of zero element.
2540bool X86::isSplatLoMask(SDNode *N) {
2541 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2542
2543 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2544 if (!isUndefOrEqual(N->getOperand(i), 0))
2545 return false;
2546 return true;
2547}
2548
Evan Chenga2497eb2008-09-25 20:50:48 +00002549/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2550/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2551bool X86::isMOVDDUPMask(SDNode *N) {
2552 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2553
2554 unsigned e = N->getNumOperands() / 2;
2555 for (unsigned i = 0; i < e; ++i)
2556 if (!isUndefOrEqual(N->getOperand(i), i))
2557 return false;
2558 for (unsigned i = 0; i < e; ++i)
2559 if (!isUndefOrEqual(N->getOperand(e+i), i))
2560 return false;
2561 return true;
2562}
2563
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2565/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2566/// instructions.
2567unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2568 unsigned NumOperands = N->getNumOperands();
2569 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2570 unsigned Mask = 0;
2571 for (unsigned i = 0; i < NumOperands; ++i) {
2572 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002573 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002574 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002575 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576 if (Val >= NumOperands) Val -= NumOperands;
2577 Mask |= Val;
2578 if (i != NumOperands - 1)
2579 Mask <<= Shift;
2580 }
2581
2582 return Mask;
2583}
2584
2585/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2586/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2587/// instructions.
2588unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2589 unsigned Mask = 0;
2590 // 8 nodes, but we only care about the last 4.
2591 for (unsigned i = 7; i >= 4; --i) {
2592 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002593 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002595 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002596 Mask |= (Val - 4);
2597 if (i != 4)
2598 Mask <<= 2;
2599 }
2600
2601 return Mask;
2602}
2603
2604/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2605/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2606/// instructions.
2607unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2608 unsigned Mask = 0;
2609 // 8 nodes, but we only care about the first 4.
2610 for (int i = 3; i >= 0; --i) {
2611 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002612 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002613 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002614 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002615 Mask |= Val;
2616 if (i != 0)
2617 Mask <<= 2;
2618 }
2619
2620 return Mask;
2621}
2622
2623/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2624/// specifies a 8 element shuffle that can be broken into a pair of
2625/// PSHUFHW and PSHUFLW.
2626static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2627 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2628
2629 if (N->getNumOperands() != 8)
2630 return false;
2631
2632 // Lower quadword shuffled.
2633 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002634 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002635 if (Arg.getOpcode() == ISD::UNDEF) continue;
2636 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002637 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002638 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 return false;
2640 }
2641
2642 // Upper quadword shuffled.
2643 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002644 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002645 if (Arg.getOpcode() == ISD::UNDEF) continue;
2646 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002647 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002648 if (Val < 4 || Val > 7)
2649 return false;
2650 }
2651
2652 return true;
2653}
2654
Chris Lattnere6aa3862007-11-25 00:24:49 +00002655/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002657static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2658 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002660 MVT VT = Op.getValueType();
2661 MVT MaskVT = Mask.getValueType();
2662 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002664 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665
2666 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002667 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 if (Arg.getOpcode() == ISD::UNDEF) {
2669 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2670 continue;
2671 }
2672 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002673 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674 if (Val < NumElems)
2675 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2676 else
2677 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2678 }
2679
2680 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002681 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2683}
2684
Evan Chenga6769df2007-12-07 21:30:01 +00002685/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2686/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002687static
Dan Gohman8181bd12008-07-27 21:46:04 +00002688SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002689 MVT MaskVT = Mask.getValueType();
2690 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002691 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002692 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002693 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002694 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002695 if (Arg.getOpcode() == ISD::UNDEF) {
2696 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2697 continue;
2698 }
2699 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002700 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002701 if (Val < NumElems)
2702 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2703 else
2704 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2705 }
2706 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2707}
2708
2709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2711/// match movhlps. The lower half elements should come from upper half of
2712/// V1 (and in order), and the upper half elements should come from the upper
2713/// half of V2 (and in order).
2714static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2715 unsigned NumElems = Mask->getNumOperands();
2716 if (NumElems != 4)
2717 return false;
2718 for (unsigned i = 0, e = 2; i != e; ++i)
2719 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2720 return false;
2721 for (unsigned i = 2; i != 4; ++i)
2722 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2723 return false;
2724 return true;
2725}
2726
2727/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002728/// is promoted to a vector. It also returns the LoadSDNode by reference if
2729/// required.
2730static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002731 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2732 return false;
2733 N = N->getOperand(0).getNode();
2734 if (!ISD::isNON_EXTLoad(N))
2735 return false;
2736 if (LD)
2737 *LD = cast<LoadSDNode>(N);
2738 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002739}
2740
2741/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2742/// match movlp{s|d}. The lower half elements should come from lower half of
2743/// V1 (and in order), and the upper half elements should come from the upper
2744/// half of V2 (and in order). And since V1 will become the source of the
2745/// MOVLP, it must be either a vector load or a scalar load to vector.
2746static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2747 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2748 return false;
2749 // Is V2 is a vector load, don't do this transformation. We will try to use
2750 // load folding shufps op.
2751 if (ISD::isNON_EXTLoad(V2))
2752 return false;
2753
2754 unsigned NumElems = Mask->getNumOperands();
2755 if (NumElems != 2 && NumElems != 4)
2756 return false;
2757 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2758 if (!isUndefOrEqual(Mask->getOperand(i), i))
2759 return false;
2760 for (unsigned i = NumElems/2; i != NumElems; ++i)
2761 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2762 return false;
2763 return true;
2764}
2765
2766/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2767/// all the same.
2768static bool isSplatVector(SDNode *N) {
2769 if (N->getOpcode() != ISD::BUILD_VECTOR)
2770 return false;
2771
Dan Gohman8181bd12008-07-27 21:46:04 +00002772 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2774 if (N->getOperand(i) != SplatValue)
2775 return false;
2776 return true;
2777}
2778
2779/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2780/// to an undef.
2781static bool isUndefShuffle(SDNode *N) {
2782 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2783 return false;
2784
Dan Gohman8181bd12008-07-27 21:46:04 +00002785 SDValue V1 = N->getOperand(0);
2786 SDValue V2 = N->getOperand(1);
2787 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 unsigned NumElems = Mask.getNumOperands();
2789 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002790 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002791 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002792 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2794 return false;
2795 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2796 return false;
2797 }
2798 }
2799 return true;
2800}
2801
2802/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2803/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002804static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002805 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002806 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002807 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002808 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809}
2810
2811/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2812/// to an zero vector.
2813static bool isZeroShuffle(SDNode *N) {
2814 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2815 return false;
2816
Dan Gohman8181bd12008-07-27 21:46:04 +00002817 SDValue V1 = N->getOperand(0);
2818 SDValue V2 = N->getOperand(1);
2819 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002820 unsigned NumElems = Mask.getNumOperands();
2821 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002822 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002823 if (Arg.getOpcode() == ISD::UNDEF)
2824 continue;
2825
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002826 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002827 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002828 unsigned Opc = V1.getNode()->getOpcode();
2829 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002830 continue;
2831 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002832 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002833 return false;
2834 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002835 unsigned Opc = V2.getNode()->getOpcode();
2836 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002837 continue;
2838 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002839 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002840 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002841 }
2842 }
2843 return true;
2844}
2845
2846/// getZeroVector - Returns a vector of specified type with all zero elements.
2847///
Dan Gohman8181bd12008-07-27 21:46:04 +00002848static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002849 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002850
2851 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2852 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002853 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002854 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002855 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002856 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002857 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002858 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002859 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002860 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002861 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002862 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2863 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002864 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002865}
2866
Chris Lattnere6aa3862007-11-25 00:24:49 +00002867/// getOnesVector - Returns a vector of specified type with all bits set.
2868///
Dan Gohman8181bd12008-07-27 21:46:04 +00002869static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002870 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002871
2872 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2873 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002874 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2875 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002876 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002877 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2878 else // SSE
2879 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2880 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2881}
2882
2883
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002884/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2885/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002886static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002887 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2888
2889 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002890 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002891 unsigned NumElems = Mask.getNumOperands();
2892 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002893 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002895 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896 if (Val > NumElems) {
2897 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2898 Changed = true;
2899 }
2900 }
2901 MaskVec.push_back(Arg);
2902 }
2903
2904 if (Changed)
2905 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2906 &MaskVec[0], MaskVec.size());
2907 return Mask;
2908}
2909
2910/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2911/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002912static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002913 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2914 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915
Dan Gohman8181bd12008-07-27 21:46:04 +00002916 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002917 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2918 for (unsigned i = 1; i != NumElems; ++i)
2919 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2920 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2921}
2922
2923/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2924/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002925static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002926 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2927 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002928 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2930 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2931 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2932 }
2933 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2934}
2935
2936/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2937/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002938static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002939 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2940 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002941 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002942 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943 for (unsigned i = 0; i != Half; ++i) {
2944 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2945 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2946 }
2947 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2948}
2949
Chris Lattner2d91b962008-03-09 01:05:04 +00002950/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2951/// element #0 of a vector with the specified index, leaving the rest of the
2952/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002953static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002954 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002955 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2956 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002957 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002958 // Element #0 of the result gets the elt we are replacing.
2959 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2960 for (unsigned i = 1; i != NumElems; ++i)
2961 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2962 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2963}
2964
Evan Chengbf8b2c52008-04-05 00:30:36 +00002965/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002966static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002967 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2968 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002969 if (PVT == VT)
2970 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002971 SDValue V1 = Op.getOperand(0);
2972 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002973 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002974 // Special handling of v4f32 -> v4i32.
2975 if (VT != MVT::v4f32) {
2976 Mask = getUnpacklMask(NumElems, DAG);
2977 while (NumElems > 4) {
2978 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2979 NumElems >>= 1;
2980 }
Evan Cheng8c590372008-05-15 08:39:06 +00002981 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002982 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002983
Evan Chengbf8b2c52008-04-05 00:30:36 +00002984 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002985 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002986 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2988}
2989
Evan Chenga2497eb2008-09-25 20:50:48 +00002990/// isVectorLoad - Returns true if the node is a vector load, a scalar
2991/// load that's promoted to vector, or a load bitcasted.
2992static bool isVectorLoad(SDValue Op) {
2993 assert(Op.getValueType().isVector() && "Expected a vector type");
2994 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
2995 Op.getOpcode() == ISD::BIT_CONVERT) {
2996 return isa<LoadSDNode>(Op.getOperand(0));
2997 }
2998 return isa<LoadSDNode>(Op);
2999}
3000
3001
3002/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3003///
3004static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3005 SelectionDAG &DAG, bool HasSSE3) {
3006 // If we have sse3 and shuffle has more than one use or input is a load, then
3007 // use movddup. Otherwise, use movlhps.
3008 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3009 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3010 MVT VT = Op.getValueType();
3011 if (VT == PVT)
3012 return Op;
3013 unsigned NumElems = PVT.getVectorNumElements();
3014 if (NumElems == 2) {
3015 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3016 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3017 } else {
3018 assert(NumElems == 4);
3019 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3020 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3021 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3022 }
3023
3024 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3025 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3026 DAG.getNode(ISD::UNDEF, PVT), Mask);
3027 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3028}
3029
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003031/// vector of zero or undef vector. This produces a shuffle where the low
3032/// element of V2 is swizzled into the zero/undef vector, landing at element
3033/// 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 +00003034static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003035 bool isZero, bool HasSSE2,
3036 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003037 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003038 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003039 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003040 unsigned NumElems = V2.getValueType().getVectorNumElements();
3041 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3042 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003044 for (unsigned i = 0; i != NumElems; ++i)
3045 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3046 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3047 else
3048 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003049 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050 &MaskVec[0], MaskVec.size());
3051 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3052}
3053
Evan Chengdea99362008-05-29 08:22:04 +00003054/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3055/// a shuffle that is zero.
3056static
Dan Gohman8181bd12008-07-27 21:46:04 +00003057unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003058 unsigned NumElems, bool Low,
3059 SelectionDAG &DAG) {
3060 unsigned NumZeros = 0;
3061 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003062 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003063 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003064 if (Idx.getOpcode() == ISD::UNDEF) {
3065 ++NumZeros;
3066 continue;
3067 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003068 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3069 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003070 ++NumZeros;
3071 else
3072 break;
3073 }
3074 return NumZeros;
3075}
3076
3077/// isVectorShift - Returns true if the shuffle can be implemented as a
3078/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003079static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3080 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003081 unsigned NumElems = Mask.getNumOperands();
3082
3083 isLeft = true;
3084 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3085 if (!NumZeros) {
3086 isLeft = false;
3087 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3088 if (!NumZeros)
3089 return false;
3090 }
3091
3092 bool SeenV1 = false;
3093 bool SeenV2 = false;
3094 for (unsigned i = NumZeros; i < NumElems; ++i) {
3095 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003096 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003097 if (Idx.getOpcode() == ISD::UNDEF)
3098 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003099 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003100 if (Index < NumElems)
3101 SeenV1 = true;
3102 else {
3103 Index -= NumElems;
3104 SeenV2 = true;
3105 }
3106 if (Index != Val)
3107 return false;
3108 }
3109 if (SeenV1 && SeenV2)
3110 return false;
3111
3112 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3113 ShAmt = NumZeros;
3114 return true;
3115}
3116
3117
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3119///
Dan Gohman8181bd12008-07-27 21:46:04 +00003120static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003121 unsigned NumNonZero, unsigned NumZero,
3122 SelectionDAG &DAG, TargetLowering &TLI) {
3123 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003124 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125
Dan Gohman8181bd12008-07-27 21:46:04 +00003126 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003127 bool First = true;
3128 for (unsigned i = 0; i < 16; ++i) {
3129 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3130 if (ThisIsNonZero && First) {
3131 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003132 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 else
3134 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3135 First = false;
3136 }
3137
3138 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003139 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003140 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3141 if (LastIsNonZero) {
3142 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3143 }
3144 if (ThisIsNonZero) {
3145 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3146 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3147 ThisElt, DAG.getConstant(8, MVT::i8));
3148 if (LastIsNonZero)
3149 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3150 } else
3151 ThisElt = LastElt;
3152
Gabor Greif1c80d112008-08-28 21:40:38 +00003153 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003154 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003155 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 }
3157 }
3158
3159 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3160}
3161
3162/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3163///
Dan Gohman8181bd12008-07-27 21:46:04 +00003164static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003165 unsigned NumNonZero, unsigned NumZero,
3166 SelectionDAG &DAG, TargetLowering &TLI) {
3167 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003168 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169
Dan Gohman8181bd12008-07-27 21:46:04 +00003170 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 bool First = true;
3172 for (unsigned i = 0; i < 8; ++i) {
3173 bool isNonZero = (NonZeros & (1 << i)) != 0;
3174 if (isNonZero) {
3175 if (First) {
3176 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003177 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003178 else
3179 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3180 First = false;
3181 }
3182 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003183 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003184 }
3185 }
3186
3187 return V;
3188}
3189
Evan Chengdea99362008-05-29 08:22:04 +00003190/// getVShift - Return a vector logical shift node.
3191///
Dan Gohman8181bd12008-07-27 21:46:04 +00003192static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003193 unsigned NumBits, SelectionDAG &DAG,
3194 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003195 bool isMMX = VT.getSizeInBits() == 64;
3196 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003197 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3198 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3199 return DAG.getNode(ISD::BIT_CONVERT, VT,
3200 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003201 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003202}
3203
Dan Gohman8181bd12008-07-27 21:46:04 +00003204SDValue
3205X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003206 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003207 if (ISD::isBuildVectorAllZeros(Op.getNode())
3208 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003209 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3210 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3211 // eliminated on x86-32 hosts.
3212 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3213 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003214
Gabor Greif1c80d112008-08-28 21:40:38 +00003215 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003216 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003217 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003218 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219
Duncan Sands92c43912008-06-06 12:08:01 +00003220 MVT VT = Op.getValueType();
3221 MVT EVT = VT.getVectorElementType();
3222 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223
3224 unsigned NumElems = Op.getNumOperands();
3225 unsigned NumZero = 0;
3226 unsigned NumNonZero = 0;
3227 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003228 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003229 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003230 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003231 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003232 if (Elt.getOpcode() == ISD::UNDEF)
3233 continue;
3234 Values.insert(Elt);
3235 if (Elt.getOpcode() != ISD::Constant &&
3236 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003237 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003238 if (isZeroNode(Elt))
3239 NumZero++;
3240 else {
3241 NonZeros |= (1 << i);
3242 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003243 }
3244 }
3245
3246 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003247 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3248 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249 }
3250
Chris Lattner66a4dda2008-03-09 05:42:06 +00003251 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003252 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003253 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003254 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003255
Chris Lattner2d91b962008-03-09 01:05:04 +00003256 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3257 // the value are obviously zero, truncate the value to i32 and do the
3258 // insertion that way. Only do this if the value is non-constant or if the
3259 // value is a constant being inserted into element 0. It is cheaper to do
3260 // a constant pool load than it is to do a movd + shuffle.
3261 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3262 (!IsAllConstants || Idx == 0)) {
3263 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3264 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003265 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3266 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003267
3268 // Truncate the value (which may itself be a constant) to i32, and
3269 // convert it to a vector with movd (S2V+shuffle to zero extend).
3270 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3271 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003272 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3273 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003274
3275 // Now we have our 32-bit value zero extended in the low element of
3276 // a vector. If Idx != 0, swizzle it into place.
3277 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003278 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003279 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3280 getSwapEltZeroMask(VecElts, Idx, DAG)
3281 };
3282 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3283 }
3284 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3285 }
3286 }
3287
Chris Lattnerac914892008-03-08 22:59:52 +00003288 // If we have a constant or non-constant insertion into the low element of
3289 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3290 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3291 // depending on what the source datatype is. Because we can only get here
3292 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3293 if (Idx == 0 &&
3294 // Don't do this for i64 values on x86-32.
3295 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003296 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003298 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3299 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003300 }
Evan Chengdea99362008-05-29 08:22:04 +00003301
3302 // Is it a vector logical left shift?
3303 if (NumElems == 2 && Idx == 1 &&
3304 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003305 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003306 return getVShift(true, VT,
3307 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3308 NumBits/2, DAG, *this);
3309 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003310
3311 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003312 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003313
Chris Lattnerac914892008-03-08 22:59:52 +00003314 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3315 // is a non-constant being inserted into an element other than the low one,
3316 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3317 // movd/movss) to move this into the low element, then shuffle it into
3318 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003320 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3321
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003322 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003323 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3324 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003325 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3326 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003327 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003328 for (unsigned i = 0; i < NumElems; i++)
3329 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003330 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 &MaskVec[0], MaskVec.size());
3332 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3333 DAG.getNode(ISD::UNDEF, VT), Mask);
3334 }
3335 }
3336
Chris Lattner66a4dda2008-03-09 05:42:06 +00003337 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3338 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003340
Dan Gohman21463242007-07-24 22:55:08 +00003341 // A vector full of immediates; various special cases are already
3342 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003343 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003344 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003345
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003346 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003347 if (EVTBits == 64) {
3348 if (NumNonZero == 1) {
3349 // One half is zero or undef.
3350 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003351 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003352 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003353 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3354 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003355 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003356 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003357 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003358
3359 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3360 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003361 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003362 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003363 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003364 }
3365
3366 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003367 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003368 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003369 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003370 }
3371
3372 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003373 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003374 V.resize(NumElems);
3375 if (NumElems == 4 && NumZero > 0) {
3376 for (unsigned i = 0; i < 4; ++i) {
3377 bool isZero = !(NonZeros & (1 << i));
3378 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003379 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 else
3381 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3382 }
3383
3384 for (unsigned i = 0; i < 2; ++i) {
3385 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3386 default: break;
3387 case 0:
3388 V[i] = V[i*2]; // Must be a zero vector.
3389 break;
3390 case 1:
3391 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3392 getMOVLMask(NumElems, DAG));
3393 break;
3394 case 2:
3395 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3396 getMOVLMask(NumElems, DAG));
3397 break;
3398 case 3:
3399 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3400 getUnpacklMask(NumElems, DAG));
3401 break;
3402 }
3403 }
3404
Duncan Sands92c43912008-06-06 12:08:01 +00003405 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3406 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003407 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003408 bool Reverse = (NonZeros & 0x3) == 2;
3409 for (unsigned i = 0; i < 2; ++i)
3410 if (Reverse)
3411 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3412 else
3413 MaskVec.push_back(DAG.getConstant(i, EVT));
3414 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3415 for (unsigned i = 0; i < 2; ++i)
3416 if (Reverse)
3417 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3418 else
3419 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003420 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 &MaskVec[0], MaskVec.size());
3422 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3423 }
3424
3425 if (Values.size() > 2) {
3426 // Expand into a number of unpckl*.
3427 // e.g. for v4f32
3428 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3429 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3430 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003431 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003432 for (unsigned i = 0; i < NumElems; ++i)
3433 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3434 NumElems >>= 1;
3435 while (NumElems != 0) {
3436 for (unsigned i = 0; i < NumElems; ++i)
3437 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3438 UnpckMask);
3439 NumElems >>= 1;
3440 }
3441 return V[0];
3442 }
3443
Dan Gohman8181bd12008-07-27 21:46:04 +00003444 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003445}
3446
Evan Chengfca29242007-12-07 08:07:39 +00003447static
Dan Gohman8181bd12008-07-27 21:46:04 +00003448SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003449 SDValue PermMask, SelectionDAG &DAG,
3450 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003451 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003452 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3453 MVT MaskEVT = MaskVT.getVectorElementType();
3454 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003455 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3456 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003457
3458 // First record which half of which vector the low elements come from.
3459 SmallVector<unsigned, 4> LowQuad(4);
3460 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003461 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003462 if (Elt.getOpcode() == ISD::UNDEF)
3463 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003464 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003465 int QuadIdx = EltIdx / 4;
3466 ++LowQuad[QuadIdx];
3467 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003468
Evan Cheng75184a92007-12-11 01:46:18 +00003469 int BestLowQuad = -1;
3470 unsigned MaxQuad = 1;
3471 for (unsigned i = 0; i < 4; ++i) {
3472 if (LowQuad[i] > MaxQuad) {
3473 BestLowQuad = i;
3474 MaxQuad = LowQuad[i];
3475 }
Evan Chengfca29242007-12-07 08:07:39 +00003476 }
3477
Evan Cheng75184a92007-12-11 01:46:18 +00003478 // Record which half of which vector the high elements come from.
3479 SmallVector<unsigned, 4> HighQuad(4);
3480 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003482 if (Elt.getOpcode() == ISD::UNDEF)
3483 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003484 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003485 int QuadIdx = EltIdx / 4;
3486 ++HighQuad[QuadIdx];
3487 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003488
Evan Cheng75184a92007-12-11 01:46:18 +00003489 int BestHighQuad = -1;
3490 MaxQuad = 1;
3491 for (unsigned i = 0; i < 4; ++i) {
3492 if (HighQuad[i] > MaxQuad) {
3493 BestHighQuad = i;
3494 MaxQuad = HighQuad[i];
3495 }
3496 }
3497
3498 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3499 if (BestLowQuad != -1 || BestHighQuad != -1) {
3500 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003501 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003502
Evan Cheng75184a92007-12-11 01:46:18 +00003503 if (BestLowQuad != -1)
3504 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3505 else
3506 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003507
Evan Cheng75184a92007-12-11 01:46:18 +00003508 if (BestHighQuad != -1)
3509 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3510 else
3511 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003512
Dan Gohman8181bd12008-07-27 21:46:04 +00003513 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003514 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3515 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3516 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3517 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3518
3519 // Now sort high and low parts separately.
3520 BitVector InOrder(8);
3521 if (BestLowQuad != -1) {
3522 // Sort lower half in order using PSHUFLW.
3523 MaskVec.clear();
3524 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003525
Evan Cheng75184a92007-12-11 01:46:18 +00003526 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003528 if (Elt.getOpcode() == ISD::UNDEF) {
3529 MaskVec.push_back(Elt);
3530 InOrder.set(i);
3531 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003532 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003533 if (EltIdx != i)
3534 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003535
Evan Cheng75184a92007-12-11 01:46:18 +00003536 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003537
Evan Cheng75184a92007-12-11 01:46:18 +00003538 // If this element is in the right place after this shuffle, then
3539 // remember it.
3540 if ((int)(EltIdx / 4) == BestLowQuad)
3541 InOrder.set(i);
3542 }
3543 }
3544 if (AnyOutOrder) {
3545 for (unsigned i = 4; i != 8; ++i)
3546 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003547 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003548 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3549 }
3550 }
3551
3552 if (BestHighQuad != -1) {
3553 // Sort high half in order using PSHUFHW if possible.
3554 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003555
Evan Cheng75184a92007-12-11 01:46:18 +00003556 for (unsigned i = 0; i != 4; ++i)
3557 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003558
Evan Cheng75184a92007-12-11 01:46:18 +00003559 bool AnyOutOrder = false;
3560 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003561 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003562 if (Elt.getOpcode() == ISD::UNDEF) {
3563 MaskVec.push_back(Elt);
3564 InOrder.set(i);
3565 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003566 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003567 if (EltIdx != i)
3568 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003569
Evan Cheng75184a92007-12-11 01:46:18 +00003570 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003571
Evan Cheng75184a92007-12-11 01:46:18 +00003572 // If this element is in the right place after this shuffle, then
3573 // remember it.
3574 if ((int)(EltIdx / 4) == BestHighQuad)
3575 InOrder.set(i);
3576 }
3577 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003578
Evan Cheng75184a92007-12-11 01:46:18 +00003579 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003580 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003581 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3582 }
3583 }
3584
3585 // The other elements are put in the right place using pextrw and pinsrw.
3586 for (unsigned i = 0; i != 8; ++i) {
3587 if (InOrder[i])
3588 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003589 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003590 if (Elt.getOpcode() == ISD::UNDEF)
3591 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003592 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003593 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003594 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3595 DAG.getConstant(EltIdx, PtrVT))
3596 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3597 DAG.getConstant(EltIdx - 8, PtrVT));
3598 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3599 DAG.getConstant(i, PtrVT));
3600 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003601
Evan Cheng75184a92007-12-11 01:46:18 +00003602 return NewV;
3603 }
3604
Bill Wendling2c7cd592008-08-21 22:35:37 +00003605 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3606 // few as possible. First, let's find out how many elements are already in the
3607 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003608 unsigned V1InOrder = 0;
3609 unsigned V1FromV1 = 0;
3610 unsigned V2InOrder = 0;
3611 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003612 SmallVector<SDValue, 8> V1Elts;
3613 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003614 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003615 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003616 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003617 V1Elts.push_back(Elt);
3618 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003619 ++V1InOrder;
3620 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003621 continue;
3622 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003623 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003624 if (EltIdx == i) {
3625 V1Elts.push_back(Elt);
3626 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3627 ++V1InOrder;
3628 } else if (EltIdx == i+8) {
3629 V1Elts.push_back(Elt);
3630 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3631 ++V2InOrder;
3632 } else if (EltIdx < 8) {
3633 V1Elts.push_back(Elt);
3634 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003635 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003636 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3637 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003638 }
3639 }
3640
3641 if (V2InOrder > V1InOrder) {
3642 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3643 std::swap(V1, V2);
3644 std::swap(V1Elts, V2Elts);
3645 std::swap(V1FromV1, V2FromV2);
3646 }
3647
Evan Cheng75184a92007-12-11 01:46:18 +00003648 if ((V1FromV1 + V1InOrder) != 8) {
3649 // Some elements are from V2.
3650 if (V1FromV1) {
3651 // If there are elements that are from V1 but out of place,
3652 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003653 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003654 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003655 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003656 if (Elt.getOpcode() == ISD::UNDEF) {
3657 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3658 continue;
3659 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003660 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003661 if (EltIdx >= 8)
3662 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3663 else
3664 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3665 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003666 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003667 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003668 }
Evan Cheng75184a92007-12-11 01:46:18 +00003669
3670 NewV = V1;
3671 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003672 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003673 if (Elt.getOpcode() == ISD::UNDEF)
3674 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003675 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003676 if (EltIdx < 8)
3677 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003678 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003679 DAG.getConstant(EltIdx - 8, PtrVT));
3680 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3681 DAG.getConstant(i, PtrVT));
3682 }
3683 return NewV;
3684 } else {
3685 // All elements are from V1.
3686 NewV = V1;
3687 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003688 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003689 if (Elt.getOpcode() == ISD::UNDEF)
3690 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003691 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003692 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003693 DAG.getConstant(EltIdx, PtrVT));
3694 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3695 DAG.getConstant(i, PtrVT));
3696 }
3697 return NewV;
3698 }
3699}
3700
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003701/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3702/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3703/// done when every pair / quad of shuffle mask elements point to elements in
3704/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003705/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3706static
Dan Gohman8181bd12008-07-27 21:46:04 +00003707SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003708 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003709 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003710 TargetLowering &TLI) {
3711 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003712 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003713 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003714 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003715 MVT NewVT = MaskVT;
3716 switch (VT.getSimpleVT()) {
3717 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003718 case MVT::v4f32: NewVT = MVT::v2f64; break;
3719 case MVT::v4i32: NewVT = MVT::v2i64; break;
3720 case MVT::v8i16: NewVT = MVT::v4i32; break;
3721 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003722 }
3723
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003724 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003725 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003726 NewVT = MVT::v2i64;
3727 else
3728 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003729 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003730 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003731 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003732 for (unsigned i = 0; i < NumElems; i += Scale) {
3733 unsigned StartIdx = ~0U;
3734 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003735 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003736 if (Elt.getOpcode() == ISD::UNDEF)
3737 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003738 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003739 if (StartIdx == ~0U)
3740 StartIdx = EltIdx - (EltIdx % Scale);
3741 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003742 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003743 }
3744 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003745 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003746 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003747 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003748 }
3749
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003750 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3751 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3752 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3753 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3754 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003755}
3756
Evan Chenge9b9c672008-05-09 21:53:03 +00003757/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003758///
Dan Gohman8181bd12008-07-27 21:46:04 +00003759static SDValue getVZextMovL(MVT VT, MVT OpVT,
3760 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003761 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003762 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3763 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003764 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003765 LD = dyn_cast<LoadSDNode>(SrcOp);
3766 if (!LD) {
3767 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3768 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003769 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003770 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3771 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3772 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3773 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3774 // PR2108
3775 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3776 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003777 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003778 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003779 SrcOp.getOperand(0)
3780 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003781 }
3782 }
3783 }
3784
3785 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003786 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003787 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3788}
3789
Evan Chengf50554e2008-07-22 21:13:36 +00003790/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3791/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003792static SDValue
3793LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3794 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003795 MVT MaskVT = PermMask.getValueType();
3796 MVT MaskEVT = MaskVT.getVectorElementType();
3797 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003798 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003799 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003800 unsigned NumHi = 0;
3801 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003802 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003803 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003804 if (Elt.getOpcode() == ISD::UNDEF) {
3805 Locs[i] = std::make_pair(-1, -1);
3806 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003807 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003808 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003809 if (Val < 4) {
3810 Locs[i] = std::make_pair(0, NumLo);
3811 Mask1[NumLo] = Elt;
3812 NumLo++;
3813 } else {
3814 Locs[i] = std::make_pair(1, NumHi);
3815 if (2+NumHi < 4)
3816 Mask1[2+NumHi] = Elt;
3817 NumHi++;
3818 }
3819 }
3820 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003821
Evan Chengf50554e2008-07-22 21:13:36 +00003822 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003823 // If no more than two elements come from either vector. This can be
3824 // implemented with two shuffles. First shuffle gather the elements.
3825 // The second shuffle, which takes the first shuffle as both of its
3826 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003827 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3828 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3829 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003830
Dan Gohman8181bd12008-07-27 21:46:04 +00003831 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003832 for (unsigned i = 0; i != 4; ++i) {
3833 if (Locs[i].first == -1)
3834 continue;
3835 else {
3836 unsigned Idx = (i < 2) ? 0 : 4;
3837 Idx += Locs[i].first * 2 + Locs[i].second;
3838 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3839 }
3840 }
3841
3842 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3843 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3844 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003845 } else if (NumLo == 3 || NumHi == 3) {
3846 // Otherwise, we must have three elements from one vector, call it X, and
3847 // one element from the other, call it Y. First, use a shufps to build an
3848 // intermediate vector with the one element from Y and the element from X
3849 // that will be in the same half in the final destination (the indexes don't
3850 // matter). Then, use a shufps to build the final vector, taking the half
3851 // containing the element from Y from the intermediate, and the other half
3852 // from X.
3853 if (NumHi == 3) {
3854 // Normalize it so the 3 elements come from V1.
3855 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3856 std::swap(V1, V2);
3857 }
3858
3859 // Find the element from V2.
3860 unsigned HiIndex;
3861 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003862 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003863 if (Elt.getOpcode() == ISD::UNDEF)
3864 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003865 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003866 if (Val >= 4)
3867 break;
3868 }
3869
3870 Mask1[0] = PermMask.getOperand(HiIndex);
3871 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3872 Mask1[2] = PermMask.getOperand(HiIndex^1);
3873 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3874 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3875 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3876
3877 if (HiIndex >= 2) {
3878 Mask1[0] = PermMask.getOperand(0);
3879 Mask1[1] = PermMask.getOperand(1);
3880 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3881 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3882 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3883 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3884 } else {
3885 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3886 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3887 Mask1[2] = PermMask.getOperand(2);
3888 Mask1[3] = PermMask.getOperand(3);
3889 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003890 Mask1[2] =
3891 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3892 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003893 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003894 Mask1[3] =
3895 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3896 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003897 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3898 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3899 }
Evan Chengf50554e2008-07-22 21:13:36 +00003900 }
3901
3902 // Break it into (shuffle shuffle_hi, shuffle_lo).
3903 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003904 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3905 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3906 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003907 unsigned MaskIdx = 0;
3908 unsigned LoIdx = 0;
3909 unsigned HiIdx = 2;
3910 for (unsigned i = 0; i != 4; ++i) {
3911 if (i == 2) {
3912 MaskPtr = &HiMask;
3913 MaskIdx = 1;
3914 LoIdx = 0;
3915 HiIdx = 2;
3916 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003917 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003918 if (Elt.getOpcode() == ISD::UNDEF) {
3919 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003920 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003921 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3922 (*MaskPtr)[LoIdx] = Elt;
3923 LoIdx++;
3924 } else {
3925 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3926 (*MaskPtr)[HiIdx] = Elt;
3927 HiIdx++;
3928 }
3929 }
3930
Dan Gohman8181bd12008-07-27 21:46:04 +00003931 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003932 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3933 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003934 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003935 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3936 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003937 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003938 for (unsigned i = 0; i != 4; ++i) {
3939 if (Locs[i].first == -1) {
3940 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3941 } else {
3942 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3943 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3944 }
3945 }
3946 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3947 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3948 &MaskOps[0], MaskOps.size()));
3949}
3950
Dan Gohman8181bd12008-07-27 21:46:04 +00003951SDValue
3952X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3953 SDValue V1 = Op.getOperand(0);
3954 SDValue V2 = Op.getOperand(1);
3955 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003956 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003957 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003958 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003959 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3960 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3961 bool V1IsSplat = false;
3962 bool V2IsSplat = false;
3963
Gabor Greif1c80d112008-08-28 21:40:38 +00003964 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003965 return DAG.getNode(ISD::UNDEF, VT);
3966
Gabor Greif1c80d112008-08-28 21:40:38 +00003967 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003968 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003969
Gabor Greif1c80d112008-08-28 21:40:38 +00003970 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003971 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003973 return V2;
3974
Evan Chengae6c9212008-09-25 23:35:16 +00003975 // Canonicalize movddup shuffles.
3976 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003977 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003978 X86::isMOVDDUPMask(PermMask.getNode()))
3979 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3980
Gabor Greif1c80d112008-08-28 21:40:38 +00003981 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003982 if (isMMX || NumElems < 4) return Op;
3983 // Promote it to a v4{if}32 splat.
3984 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003985 }
3986
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003987 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3988 // do it!
3989 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003990 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003991 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003992 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3993 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3994 // FIXME: Figure out a cleaner way to do this.
3995 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003997 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003998 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003999 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004000 SDValue NewV1 = NewOp.getOperand(0);
4001 SDValue NewV2 = NewOp.getOperand(1);
4002 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004003 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004004 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004005 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004006 }
4007 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004008 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004009 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004010 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004011 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004012 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004013 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004014 }
4015 }
4016
Evan Chengdea99362008-05-29 08:22:04 +00004017 // Check if this can be converted into a logical shift.
4018 bool isLeft = false;
4019 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004020 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004021 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4022 if (isShift && ShVal.hasOneUse()) {
4023 // If the shifted value has multiple uses, it may be cheaper to use
4024 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004025 MVT EVT = VT.getVectorElementType();
4026 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004027 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4028 }
4029
Gabor Greif1c80d112008-08-28 21:40:38 +00004030 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004031 if (V1IsUndef)
4032 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004033 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004034 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004035 if (!isMMX)
4036 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004037 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004038
Gabor Greif1c80d112008-08-28 21:40:38 +00004039 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4040 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4041 X86::isMOVHLPSMask(PermMask.getNode()) ||
4042 X86::isMOVHPMask(PermMask.getNode()) ||
4043 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004044 return Op;
4045
Gabor Greif1c80d112008-08-28 21:40:38 +00004046 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4047 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4049
Evan Chengdea99362008-05-29 08:22:04 +00004050 if (isShift) {
4051 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004052 MVT EVT = VT.getVectorElementType();
4053 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004054 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4055 }
4056
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004057 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004058 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4059 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004060 V1IsSplat = isSplatVector(V1.getNode());
4061 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004062
4063 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004064 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4065 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4066 std::swap(V1IsSplat, V2IsSplat);
4067 std::swap(V1IsUndef, V2IsUndef);
4068 Commuted = true;
4069 }
4070
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004071 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004072 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004073 if (V2IsUndef) return V1;
4074 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4075 if (V2IsSplat) {
4076 // V2 is a splat, so the mask may be malformed. That is, it may point
4077 // to any V2 element. The instruction selectior won't like this. Get
4078 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004079 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004080 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004081 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4082 }
4083 return Op;
4084 }
4085
Gabor Greif1c80d112008-08-28 21:40:38 +00004086 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4087 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4088 X86::isUNPCKLMask(PermMask.getNode()) ||
4089 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004090 return Op;
4091
4092 if (V2IsSplat) {
4093 // Normalize mask so all entries that point to V2 points to its first
4094 // element then try to match unpck{h|l} again. If match, return a
4095 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004096 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004097 if (NewMask.getNode() != PermMask.getNode()) {
4098 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004099 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004101 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004102 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004103 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4104 }
4105 }
4106 }
4107
4108 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004109 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004110 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4111
4112 if (Commuted) {
4113 // Commute is back and try unpck* again.
4114 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004115 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4116 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4117 X86::isUNPCKLMask(PermMask.getNode()) ||
4118 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 return Op;
4120 }
4121
Evan Chengbf8b2c52008-04-05 00:30:36 +00004122 // Try PSHUF* first, then SHUFP*.
4123 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4124 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004125 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004126 if (V2.getOpcode() != ISD::UNDEF)
4127 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4128 DAG.getNode(ISD::UNDEF, VT), PermMask);
4129 return Op;
4130 }
4131
4132 if (!isMMX) {
4133 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004134 (X86::isPSHUFDMask(PermMask.getNode()) ||
4135 X86::isPSHUFHWMask(PermMask.getNode()) ||
4136 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004137 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004138 if (VT == MVT::v4f32) {
4139 RVT = MVT::v4i32;
4140 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4141 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4142 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4143 } else if (V2.getOpcode() != ISD::UNDEF)
4144 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4145 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4146 if (RVT != VT)
4147 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004148 return Op;
4149 }
4150
Evan Chengbf8b2c52008-04-05 00:30:36 +00004151 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004152 if (X86::isSHUFPMask(PermMask.getNode()) ||
4153 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004154 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 }
4156
Evan Cheng75184a92007-12-11 01:46:18 +00004157 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4158 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004159 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004160 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004161 return NewOp;
4162 }
4163
Evan Chengf50554e2008-07-22 21:13:36 +00004164 // Handle all 4 wide cases with a number of shuffles except for MMX.
4165 if (NumElems == 4 && !isMMX)
4166 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004167
Dan Gohman8181bd12008-07-27 21:46:04 +00004168 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169}
4170
Dan Gohman8181bd12008-07-27 21:46:04 +00004171SDValue
4172X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004173 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004174 MVT VT = Op.getValueType();
4175 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004176 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004177 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004178 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004179 DAG.getValueType(VT));
4180 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004181 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004182 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004183 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004184 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004185 DAG.getValueType(VT));
4186 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004187 } else if (VT == MVT::f32) {
4188 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4189 // the result back to FR32 register. It's only worth matching if the
Dan Gohman788db592008-04-16 02:32:24 +00004190 // result has a single use which is a store or a bitcast to i32.
Evan Cheng6c249332008-03-24 21:52:23 +00004191 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004192 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004193 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman788db592008-04-16 02:32:24 +00004194 if (User->getOpcode() != ISD::STORE &&
4195 (User->getOpcode() != ISD::BIT_CONVERT ||
4196 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004197 return SDValue();
4198 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004199 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4200 Op.getOperand(1));
4201 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004202 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004203 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004204}
4205
4206
Dan Gohman8181bd12008-07-27 21:46:04 +00004207SDValue
4208X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004209 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004210 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211
Evan Cheng6c249332008-03-24 21:52:23 +00004212 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004213 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004214 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004215 return Res;
4216 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004217
Duncan Sands92c43912008-06-06 12:08:01 +00004218 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004219 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004220 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004221 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004222 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004223 if (Idx == 0)
4224 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4225 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4226 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4227 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004229 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004230 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004231 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004232 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004233 DAG.getValueType(VT));
4234 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004235 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004236 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 if (Idx == 0)
4238 return Op;
4239 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004240 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004242 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004243 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004244 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004245 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004246 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004247 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004248 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004249 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004250 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004251 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004252 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004253 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4254 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4255 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004256 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004257 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004258 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4259 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4260 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004261 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 if (Idx == 0)
4263 return Op;
4264
4265 // UNPCKHPD the element to the lowest double word, then movsd.
4266 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4267 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004268 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004270 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004271 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004272 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004273 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004275 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004276 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4277 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4278 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004279 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004280 }
4281
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283}
4284
Dan Gohman8181bd12008-07-27 21:46:04 +00004285SDValue
4286X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004287 MVT VT = Op.getValueType();
4288 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004289
Dan Gohman8181bd12008-07-27 21:46:04 +00004290 SDValue N0 = Op.getOperand(0);
4291 SDValue N1 = Op.getOperand(1);
4292 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004293
Dan Gohman5a7af042008-08-14 22:53:18 +00004294 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4295 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004296 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004297 : X86ISD::PINSRW;
4298 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4299 // argument.
4300 if (N1.getValueType() != MVT::i32)
4301 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4302 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004303 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004304 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004305 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004306 // Bits [7:6] of the constant are the source select. This will always be
4307 // zero here. The DAG Combiner may combine an extract_elt index into these
4308 // bits. For example (insert (extract, 3), 2) could be matched by putting
4309 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4310 // Bits [5:4] of the constant are the destination select. This is the
4311 // value of the incoming immediate.
4312 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4313 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004314 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004315 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4316 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004317 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004318}
4319
Dan Gohman8181bd12008-07-27 21:46:04 +00004320SDValue
4321X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004322 MVT VT = Op.getValueType();
4323 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004324
4325 if (Subtarget->hasSSE41())
4326 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4327
Evan Chenge12a7eb2007-12-12 07:55:34 +00004328 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004329 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004330
Dan Gohman8181bd12008-07-27 21:46:04 +00004331 SDValue N0 = Op.getOperand(0);
4332 SDValue N1 = Op.getOperand(1);
4333 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004334
Duncan Sands92c43912008-06-06 12:08:01 +00004335 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004336 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4337 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004338 if (N1.getValueType() != MVT::i32)
4339 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4340 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004341 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004342 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004344 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004345}
4346
Dan Gohman8181bd12008-07-27 21:46:04 +00004347SDValue
4348X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004349 if (Op.getValueType() == MVT::v2f32)
4350 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4351 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4352 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4353 Op.getOperand(0))));
4354
Dan Gohman8181bd12008-07-27 21:46:04 +00004355 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004356 MVT VT = MVT::v2i32;
4357 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004358 default: break;
4359 case MVT::v16i8:
4360 case MVT::v8i16:
4361 VT = MVT::v4i32;
4362 break;
4363 }
4364 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4365 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004366}
4367
Bill Wendlingfef06052008-09-16 21:48:12 +00004368// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4369// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4370// one of the above mentioned nodes. It has to be wrapped because otherwise
4371// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4372// be used to form addressing mode. These wrapped nodes will be selected
4373// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004374SDValue
4375X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004376 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004377 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378 getPointerTy(),
4379 CP->getAlignment());
4380 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4381 // With PIC, the address is actually $g + Offset.
4382 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4383 !Subtarget->isPICStyleRIPRel()) {
4384 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4385 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4386 Result);
4387 }
4388
4389 return Result;
4390}
4391
Dan Gohman8181bd12008-07-27 21:46:04 +00004392SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004393X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004394 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004395 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004396 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4397 bool ExtraLoadRequired =
4398 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4399
4400 // Create the TargetGlobalAddress node, folding in the constant
4401 // offset if it is legal.
4402 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004403 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004404 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4405 Offset = 0;
4406 } else
4407 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004408 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004410 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004411 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004412 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4413 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4414 Result);
4415 }
4416
4417 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4418 // load the value at address GV, not the value of GV itself. This means that
4419 // the GlobalAddress must be in the base or index register of the address, not
4420 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4421 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004422 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004423 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004424 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004425
Dan Gohman36322c72008-10-18 02:06:02 +00004426 // If there was a non-zero offset that we didn't fold, create an explicit
4427 // addition for it.
4428 if (Offset != 0)
4429 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4430 DAG.getConstant(Offset, getPointerTy()));
4431
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004432 return Result;
4433}
4434
Evan Cheng7f250d62008-09-24 00:05:32 +00004435SDValue
4436X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4437 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004438 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4439 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004440}
4441
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004442// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004443static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004444LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004445 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004446 SDValue InFlag;
4447 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 DAG.getNode(X86ISD::GlobalBaseReg,
4449 PtrVT), InFlag);
4450 InFlag = Chain.getValue(1);
4451
4452 // emit leal symbol@TLSGD(,%ebx,1), %eax
4453 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004454 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 GA->getValueType(0),
4456 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004457 SDValue Ops[] = { Chain, TGA, InFlag };
4458 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004459 InFlag = Result.getValue(2);
4460 Chain = Result.getValue(1);
4461
4462 // call ___tls_get_addr. This function receives its argument in
4463 // the register EAX.
4464 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4465 InFlag = Chain.getValue(1);
4466
4467 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004468 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004469 DAG.getTargetExternalSymbol("___tls_get_addr",
4470 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004471 DAG.getRegister(X86::EAX, PtrVT),
4472 DAG.getRegister(X86::EBX, PtrVT),
4473 InFlag };
4474 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4475 InFlag = Chain.getValue(1);
4476
4477 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4478}
4479
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004480// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004481static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004482LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004483 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004484 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004485
4486 // emit leaq symbol@TLSGD(%rip), %rdi
4487 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004488 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004489 GA->getValueType(0),
4490 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004491 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4492 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004493 Chain = Result.getValue(1);
4494 InFlag = Result.getValue(2);
4495
aslb204cd52008-08-16 12:58:29 +00004496 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004497 // the register RDI.
4498 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4499 InFlag = Chain.getValue(1);
4500
4501 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004502 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004503 DAG.getTargetExternalSymbol("__tls_get_addr",
4504 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004505 DAG.getRegister(X86::RDI, PtrVT),
4506 InFlag };
4507 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4508 InFlag = Chain.getValue(1);
4509
4510 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4511}
4512
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4514// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004515static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004516 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004518 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4520 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004521 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004522 GA->getValueType(0),
4523 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004524 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004525
4526 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004527 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004528 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529
4530 // The address of the thread local variable is the add of the thread
4531 // pointer with the offset of the variable.
4532 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4533}
4534
Dan Gohman8181bd12008-07-27 21:46:04 +00004535SDValue
4536X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004537 // TODO: implement the "local dynamic" model
4538 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004539 assert(Subtarget->isTargetELF() &&
4540 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004541 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4542 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4543 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004544 if (Subtarget->is64Bit()) {
4545 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4546 } else {
4547 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4548 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4549 else
4550 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4551 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004552}
4553
Dan Gohman8181bd12008-07-27 21:46:04 +00004554SDValue
4555X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004556 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4557 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004558 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4559 // With PIC, the address is actually $g + Offset.
4560 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4561 !Subtarget->isPICStyleRIPRel()) {
4562 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4563 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4564 Result);
4565 }
4566
4567 return Result;
4568}
4569
Dan Gohman8181bd12008-07-27 21:46:04 +00004570SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004572 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4574 // With PIC, the address is actually $g + Offset.
4575 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4576 !Subtarget->isPICStyleRIPRel()) {
4577 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4578 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4579 Result);
4580 }
4581
4582 return Result;
4583}
4584
Chris Lattner62814a32007-10-17 06:02:13 +00004585/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4586/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004587SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004588 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004589 MVT VT = Op.getValueType();
4590 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004591 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004592 SDValue ShOpLo = Op.getOperand(0);
4593 SDValue ShOpHi = Op.getOperand(1);
4594 SDValue ShAmt = Op.getOperand(2);
4595 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004596 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4597 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004598
Dan Gohman8181bd12008-07-27 21:46:04 +00004599 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004600 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004601 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4602 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004603 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004604 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4605 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004606 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607
Dan Gohman8181bd12008-07-27 21:46:04 +00004608 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004609 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004610 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004611 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612
Dan Gohman8181bd12008-07-27 21:46:04 +00004613 SDValue Hi, Lo;
4614 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4615 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4616 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004617
Chris Lattner62814a32007-10-17 06:02:13 +00004618 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004619 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4620 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004621 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004622 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4623 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004624 }
4625
Dan Gohman8181bd12008-07-27 21:46:04 +00004626 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004627 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004628}
4629
Dan Gohman8181bd12008-07-27 21:46:04 +00004630SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004631 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004632 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004633 "Unknown SINT_TO_FP to lower!");
4634
4635 // These are really Legal; caller falls through into that case.
4636 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004637 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004638 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4639 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004640 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004641
Duncan Sands92c43912008-06-06 12:08:01 +00004642 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 MachineFunction &MF = DAG.getMachineFunction();
4644 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004645 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4646 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004647 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004648 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004649
4650 // Build the FILD
4651 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004652 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004653 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4655 else
4656 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658 Ops.push_back(Chain);
4659 Ops.push_back(StackSlot);
4660 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004661 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004662 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663
Dale Johannesen2fc20782007-09-14 22:26:36 +00004664 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004666 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667
4668 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4669 // shouldn't be necessary except that RFP cannot be live across
4670 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4671 MachineFunction &MF = DAG.getMachineFunction();
4672 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004673 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004675 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676 Ops.push_back(Chain);
4677 Ops.push_back(Result);
4678 Ops.push_back(StackSlot);
4679 Ops.push_back(DAG.getValueType(Op.getValueType()));
4680 Ops.push_back(InFlag);
4681 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004682 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004683 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004684 }
4685
4686 return Result;
4687}
4688
Dan Gohman8181bd12008-07-27 21:46:04 +00004689std::pair<SDValue,SDValue> X86TargetLowering::
4690FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004691 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4692 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004694
Dale Johannesen2fc20782007-09-14 22:26:36 +00004695 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004696 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004697 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004698 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004699 if (Subtarget->is64Bit() &&
4700 Op.getValueType() == MVT::i64 &&
4701 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004702 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004703
Evan Cheng05441e62007-10-15 20:11:21 +00004704 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4705 // stack slot.
4706 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004707 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004708 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004709 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004711 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004712 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4713 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4714 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4715 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004716 }
4717
Dan Gohman8181bd12008-07-27 21:46:04 +00004718 SDValue Chain = DAG.getEntryNode();
4719 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004720 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004722 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004723 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004725 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4727 };
4728 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4729 Chain = Value.getValue(1);
4730 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4731 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4732 }
4733
4734 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004735 SDValue Ops[] = { Chain, Value, StackSlot };
4736 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004737
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004738 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004739}
4740
Dan Gohman8181bd12008-07-27 21:46:04 +00004741SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4742 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4743 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004744 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004745
4746 // Load the result.
4747 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4748}
4749
4750SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004751 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4752 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004753 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004754
4755 MVT VT = N->getValueType(0);
4756
4757 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004758 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004759
Duncan Sands698842f2008-07-02 17:40:58 +00004760 // Use MERGE_VALUES to drop the chain result value and get a node with one
4761 // result. This requires turning off getMergeValues simplification, since
4762 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004763 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004764}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004765
Dan Gohman8181bd12008-07-27 21:46:04 +00004766SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004767 MVT VT = Op.getValueType();
4768 MVT EltVT = VT;
4769 if (VT.isVector())
4770 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004771 std::vector<Constant*> CV;
4772 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004773 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 CV.push_back(C);
4775 CV.push_back(C);
4776 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004777 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004778 CV.push_back(C);
4779 CV.push_back(C);
4780 CV.push_back(C);
4781 CV.push_back(C);
4782 }
Dan Gohman11821702007-07-27 17:16:43 +00004783 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004784 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4785 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004786 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004787 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4789}
4790
Dan Gohman8181bd12008-07-27 21:46:04 +00004791SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004792 MVT VT = Op.getValueType();
4793 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004794 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004795 if (VT.isVector()) {
4796 EltVT = VT.getVectorElementType();
4797 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004798 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 std::vector<Constant*> CV;
4800 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004801 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 CV.push_back(C);
4803 CV.push_back(C);
4804 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004805 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 CV.push_back(C);
4807 CV.push_back(C);
4808 CV.push_back(C);
4809 CV.push_back(C);
4810 }
Dan Gohman11821702007-07-27 17:16:43 +00004811 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004812 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4813 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004814 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004815 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004816 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004817 return DAG.getNode(ISD::BIT_CONVERT, VT,
4818 DAG.getNode(ISD::XOR, MVT::v2i64,
4819 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4820 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4821 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004822 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4823 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004824}
4825
Dan Gohman8181bd12008-07-27 21:46:04 +00004826SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4827 SDValue Op0 = Op.getOperand(0);
4828 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004829 MVT VT = Op.getValueType();
4830 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004831
4832 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004833 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004834 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4835 SrcVT = VT;
4836 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004837 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004838 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004839 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004840 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004841 }
4842
4843 // At this point the operands and the result should have the same
4844 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845
4846 // First get the sign bit of second operand.
4847 std::vector<Constant*> CV;
4848 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004849 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4850 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004851 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004852 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4853 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4854 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4855 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004856 }
Dan Gohman11821702007-07-27 17:16:43 +00004857 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004858 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4859 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004860 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004861 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004862 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004863
4864 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004865 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004866 // Op0 is MVT::f32, Op1 is MVT::f64.
4867 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4868 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4869 DAG.getConstant(32, MVT::i32));
4870 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4871 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004872 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004873 }
4874
4875 // Clear first operand sign bit.
4876 CV.clear();
4877 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004878 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4879 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004881 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4882 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4883 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4884 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004885 }
Dan Gohman11821702007-07-27 17:16:43 +00004886 C = ConstantVector::get(CV);
4887 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004888 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004889 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004890 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004891 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004892
4893 // Or the value with the sign bit.
4894 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4895}
4896
Dan Gohman8181bd12008-07-27 21:46:04 +00004897SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00004898 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00004899 SDValue Cond;
4900 SDValue Op0 = Op.getOperand(0);
4901 SDValue Op1 = Op.getOperand(1);
4902 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00004903 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00004904 unsigned X86CC;
4905
Evan Cheng950aac02007-09-25 01:57:46 +00004906 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00004907 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00004908 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4909 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00004910 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00004911 }
Evan Cheng950aac02007-09-25 01:57:46 +00004912
Evan Cheng71343822008-10-15 02:05:31 +00004913 assert(0 && "Illegal SetCC!");
4914 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00004915}
4916
Dan Gohman8181bd12008-07-27 21:46:04 +00004917SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4918 SDValue Cond;
4919 SDValue Op0 = Op.getOperand(0);
4920 SDValue Op1 = Op.getOperand(1);
4921 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00004922 MVT VT = Op.getValueType();
4923 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4924 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4925
4926 if (isFP) {
4927 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00004928 MVT VT0 = Op0.getValueType();
4929 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4930 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00004931 bool Swap = false;
4932
4933 switch (SetCCOpcode) {
4934 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004935 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00004936 case ISD::SETEQ: SSECC = 0; break;
4937 case ISD::SETOGT:
4938 case ISD::SETGT: Swap = true; // Fallthrough
4939 case ISD::SETLT:
4940 case ISD::SETOLT: SSECC = 1; break;
4941 case ISD::SETOGE:
4942 case ISD::SETGE: Swap = true; // Fallthrough
4943 case ISD::SETLE:
4944 case ISD::SETOLE: SSECC = 2; break;
4945 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004946 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00004947 case ISD::SETNE: SSECC = 4; break;
4948 case ISD::SETULE: Swap = true;
4949 case ISD::SETUGE: SSECC = 5; break;
4950 case ISD::SETULT: Swap = true;
4951 case ISD::SETUGT: SSECC = 6; break;
4952 case ISD::SETO: SSECC = 7; break;
4953 }
4954 if (Swap)
4955 std::swap(Op0, Op1);
4956
Nate Begeman6357f9d2008-07-25 19:05:58 +00004957 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00004958 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00004959 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004960 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004961 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4962 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4963 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4964 }
4965 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004966 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00004967 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4968 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4969 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4970 }
4971 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00004972 }
4973 // Handle all other FP comparisons here.
4974 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4975 }
4976
4977 // We are handling one of the integer comparisons here. Since SSE only has
4978 // GT and EQ comparisons for integer, swapping operands and multiple
4979 // operations may be required for some comparisons.
4980 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4981 bool Swap = false, Invert = false, FlipSigns = false;
4982
4983 switch (VT.getSimpleVT()) {
4984 default: break;
4985 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4986 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4987 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4988 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4989 }
4990
4991 switch (SetCCOpcode) {
4992 default: break;
4993 case ISD::SETNE: Invert = true;
4994 case ISD::SETEQ: Opc = EQOpc; break;
4995 case ISD::SETLT: Swap = true;
4996 case ISD::SETGT: Opc = GTOpc; break;
4997 case ISD::SETGE: Swap = true;
4998 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4999 case ISD::SETULT: Swap = true;
5000 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5001 case ISD::SETUGE: Swap = true;
5002 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5003 }
5004 if (Swap)
5005 std::swap(Op0, Op1);
5006
5007 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5008 // bits of the inputs before performing those operations.
5009 if (FlipSigns) {
5010 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005011 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5012 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5013 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005014 SignBits.size());
5015 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5016 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5017 }
5018
Dan Gohman8181bd12008-07-27 21:46:04 +00005019 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005020
5021 // If the logical-not of the result is required, perform that now.
5022 if (Invert) {
5023 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005024 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5025 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5026 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005027 NegOnes.size());
5028 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5029 }
5030 return Result;
5031}
Evan Cheng950aac02007-09-25 01:57:46 +00005032
Dan Gohman8181bd12008-07-27 21:46:04 +00005033SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005035 SDValue Cond = Op.getOperand(0);
5036 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005037
5038 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005039 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040
Evan Cheng50d37ab2007-10-08 22:16:29 +00005041 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5042 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005043 if (Cond.getOpcode() == X86ISD::SETCC) {
5044 CC = Cond.getOperand(0);
5045
Dan Gohman8181bd12008-07-27 21:46:04 +00005046 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005047 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005048 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005049
Evan Cheng50d37ab2007-10-08 22:16:29 +00005050 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005051 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005052 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005053 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005054
Evan Cheng621216e2007-09-29 00:00:36 +00005055 if ((Opc == X86ISD::CMP ||
5056 Opc == X86ISD::COMI ||
5057 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005058 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005059 addTest = false;
5060 }
5061 }
5062
5063 if (addTest) {
5064 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005065 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005066 }
5067
Duncan Sands92c43912008-06-06 12:08:01 +00005068 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005069 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005070 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005071 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5072 // condition is true.
5073 Ops.push_back(Op.getOperand(2));
5074 Ops.push_back(Op.getOperand(1));
5075 Ops.push_back(CC);
5076 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005077 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005078}
5079
Dan Gohman8181bd12008-07-27 21:46:04 +00005080SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005081 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005082 SDValue Chain = Op.getOperand(0);
5083 SDValue Cond = Op.getOperand(1);
5084 SDValue Dest = Op.getOperand(2);
5085 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005086
5087 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005088 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005089
Evan Cheng50d37ab2007-10-08 22:16:29 +00005090 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5091 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005092 if (Cond.getOpcode() == X86ISD::SETCC) {
5093 CC = Cond.getOperand(0);
5094
Dan Gohman8181bd12008-07-27 21:46:04 +00005095 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005096 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005097 if (Opc == X86ISD::CMP ||
5098 Opc == X86ISD::COMI ||
5099 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005100 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005101 addTest = false;
5102 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005103 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5104 // two branches instead of an explicit OR instruction with a
5105 // separate test.
5106 } else if (Cond.getOpcode() == ISD::OR &&
5107 Cond.hasOneUse() &&
5108 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5109 Cond.getOperand(0).hasOneUse() &&
5110 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5111 Cond.getOperand(1).hasOneUse()) {
5112 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5113 unsigned Opc = Cmp.getOpcode();
5114 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5115 (Opc == X86ISD::CMP ||
5116 Opc == X86ISD::COMI ||
5117 Opc == X86ISD::UCOMI)) {
5118 CC = Cond.getOperand(0).getOperand(0);
5119 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5120 Chain, Dest, CC, Cmp);
5121 CC = Cond.getOperand(1).getOperand(0);
5122 Cond = Cmp;
5123 addTest = false;
5124 }
5125 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5126 // two branches instead of an explicit AND instruction with a
5127 // separate test. However, we only do this if this block doesn't
5128 // have a fall-through edge, because this requires an explicit
5129 // jmp when the condition is false.
5130 } else if (Cond.getOpcode() == ISD::AND &&
5131 Cond.hasOneUse() &&
5132 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5133 Cond.getOperand(0).hasOneUse() &&
5134 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5135 Cond.getOperand(1).hasOneUse()) {
5136 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5137 unsigned Opc = Cmp.getOpcode();
5138 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5139 (Opc == X86ISD::CMP ||
5140 Opc == X86ISD::COMI ||
5141 Opc == X86ISD::UCOMI) &&
5142 Op.getNode()->hasOneUse()) {
5143 X86::CondCode CCode =
5144 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5145 CCode = X86::GetOppositeBranchCondition(CCode);
5146 CC = DAG.getConstant(CCode, MVT::i8);
5147 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5148 // Look for an unconditional branch following this conditional branch.
5149 // We need this because we need to reverse the successors in order
5150 // to implement FCMP_OEQ.
5151 if (User.getOpcode() == ISD::BR) {
5152 SDValue FalseBB = User.getOperand(1);
5153 SDValue NewBR =
5154 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5155 assert(NewBR == User);
5156 Dest = FalseBB;
5157
5158 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5159 Chain, Dest, CC, Cmp);
5160 X86::CondCode CCode =
5161 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5162 CCode = X86::GetOppositeBranchCondition(CCode);
5163 CC = DAG.getConstant(CCode, MVT::i8);
5164 Cond = Cmp;
5165 addTest = false;
5166 }
5167 }
Evan Cheng950aac02007-09-25 01:57:46 +00005168 }
5169
5170 if (addTest) {
5171 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005172 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005173 }
Evan Cheng621216e2007-09-29 00:00:36 +00005174 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005175 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005176}
5177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005178
5179// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5180// Calls to _alloca is needed to probe the stack when allocating more than 4k
5181// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5182// that the guard pages used by the OS virtual memory manager are allocated in
5183// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005184SDValue
5185X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186 SelectionDAG &DAG) {
5187 assert(Subtarget->isTargetCygMing() &&
5188 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005190 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005191 SDValue Chain = Op.getOperand(0);
5192 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005193 // FIXME: Ensure alignment here
5194
Dan Gohman8181bd12008-07-27 21:46:04 +00005195 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005196
Duncan Sands92c43912008-06-06 12:08:01 +00005197 MVT IntPtr = getPointerTy();
5198 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005200 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5203 Flag = Chain.getValue(1);
5204
5205 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005206 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005207 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005208 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005209 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005210 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005211 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005212 Flag = Chain.getValue(1);
5213
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005214 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005215 DAG.getIntPtrConstant(0, true),
5216 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005217 Flag);
5218
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005219 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005220
Dan Gohman8181bd12008-07-27 21:46:04 +00005221 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005222 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005223}
5224
Dan Gohman8181bd12008-07-27 21:46:04 +00005225SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005226X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005227 SDValue Chain,
5228 SDValue Dst, SDValue Src,
5229 SDValue Size, unsigned Align,
5230 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005231 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005232 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005233
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005234 // If not DWORD aligned or size is more than the threshold, call the library.
5235 // The libc version is likely to be faster for these cases. It can use the
5236 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005237 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005238 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005239 ConstantSize->getZExtValue() >
5240 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005241 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005242
5243 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005244 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005245
Bill Wendling4b2e3782008-10-01 00:59:58 +00005246 if (const char *bzeroEntry = V &&
5247 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5248 MVT IntPtr = getPointerTy();
5249 const Type *IntPtrTy = TD->getIntPtrType();
5250 TargetLowering::ArgListTy Args;
5251 TargetLowering::ArgListEntry Entry;
5252 Entry.Node = Dst;
5253 Entry.Ty = IntPtrTy;
5254 Args.push_back(Entry);
5255 Entry.Node = Size;
5256 Args.push_back(Entry);
5257 std::pair<SDValue,SDValue> CallResult =
5258 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5259 CallingConv::C, false,
5260 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5261 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005262 }
5263
Dan Gohmane8b391e2008-04-12 04:36:06 +00005264 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005265 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005266 }
5267
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005268 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005269 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005270 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005271 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005272 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005273 unsigned BytesLeft = 0;
5274 bool TwoRepStos = false;
5275 if (ValC) {
5276 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005277 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278
5279 // If the value is a constant, then we can potentially use larger sets.
5280 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005281 case 2: // WORD aligned
5282 AVT = MVT::i16;
5283 ValReg = X86::AX;
5284 Val = (Val << 8) | Val;
5285 break;
5286 case 0: // DWORD aligned
5287 AVT = MVT::i32;
5288 ValReg = X86::EAX;
5289 Val = (Val << 8) | Val;
5290 Val = (Val << 16) | Val;
5291 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5292 AVT = MVT::i64;
5293 ValReg = X86::RAX;
5294 Val = (Val << 32) | Val;
5295 }
5296 break;
5297 default: // Byte aligned
5298 AVT = MVT::i8;
5299 ValReg = X86::AL;
5300 Count = DAG.getIntPtrConstant(SizeVal);
5301 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005302 }
5303
Duncan Sandsec142ee2008-06-08 20:54:56 +00005304 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005305 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005306 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5307 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308 }
5309
5310 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5311 InFlag);
5312 InFlag = Chain.getValue(1);
5313 } else {
5314 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005315 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005316 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005317 InFlag = Chain.getValue(1);
5318 }
5319
5320 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5321 Count, InFlag);
5322 InFlag = Chain.getValue(1);
5323 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005324 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 InFlag = Chain.getValue(1);
5326
5327 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005328 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329 Ops.push_back(Chain);
5330 Ops.push_back(DAG.getValueType(AVT));
5331 Ops.push_back(InFlag);
5332 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5333
5334 if (TwoRepStos) {
5335 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005336 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005337 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005338 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005339 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5340 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5341 Left, InFlag);
5342 InFlag = Chain.getValue(1);
5343 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5344 Ops.clear();
5345 Ops.push_back(Chain);
5346 Ops.push_back(DAG.getValueType(MVT::i8));
5347 Ops.push_back(InFlag);
5348 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5349 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005350 // Handle the last 1 - 7 bytes.
5351 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005352 MVT AddrVT = Dst.getValueType();
5353 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005354
5355 Chain = DAG.getMemset(Chain,
5356 DAG.getNode(ISD::ADD, AddrVT, Dst,
5357 DAG.getConstant(Offset, AddrVT)),
5358 Src,
5359 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005360 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361 }
5362
Dan Gohmane8b391e2008-04-12 04:36:06 +00005363 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005364 return Chain;
5365}
5366
Dan Gohman8181bd12008-07-27 21:46:04 +00005367SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005368X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005369 SDValue Chain, SDValue Dst, SDValue Src,
5370 SDValue Size, unsigned Align,
5371 bool AlwaysInline,
5372 const Value *DstSV, uint64_t DstSVOff,
5373 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005374 // This requires the copy size to be a constant, preferrably
5375 // within a subtarget-specific limit.
5376 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5377 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005378 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005379 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005380 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005381 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005382
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005383 /// If not DWORD aligned, call the library.
5384 if ((Align & 3) != 0)
5385 return SDValue();
5386
5387 // DWORD aligned
5388 MVT AVT = MVT::i32;
5389 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005390 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005391
Duncan Sands92c43912008-06-06 12:08:01 +00005392 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005393 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005394 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005395 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005396
Dan Gohman8181bd12008-07-27 21:46:04 +00005397 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005398 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5399 Count, InFlag);
5400 InFlag = Chain.getValue(1);
5401 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005402 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005403 InFlag = Chain.getValue(1);
5404 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005405 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005406 InFlag = Chain.getValue(1);
5407
5408 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005409 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005410 Ops.push_back(Chain);
5411 Ops.push_back(DAG.getValueType(AVT));
5412 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414
Dan Gohman8181bd12008-07-27 21:46:04 +00005415 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005416 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005417 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005418 // Handle the last 1 - 7 bytes.
5419 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005420 MVT DstVT = Dst.getValueType();
5421 MVT SrcVT = Src.getValueType();
5422 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005423 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005424 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005425 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005426 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005427 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005428 DAG.getConstant(BytesLeft, SizeVT),
5429 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005430 DstSV, DstSVOff + Offset,
5431 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005432 }
5433
Dan Gohmane8b391e2008-04-12 04:36:06 +00005434 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435}
5436
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005437/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5438SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005440 SDValue TheChain = N->getOperand(0);
5441 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005442 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005443 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5444 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005445 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005446 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005447 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005448 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005449 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450 };
5451
Gabor Greif1c80d112008-08-28 21:40:38 +00005452 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005453 }
5454
Dan Gohman8181bd12008-07-27 21:46:04 +00005455 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5456 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005457 MVT::i32, eax.getValue(2));
5458 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005459 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005460 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5461
5462 // Use a MERGE_VALUES to return the value and chain.
5463 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005464 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005465}
5466
Dan Gohman8181bd12008-07-27 21:46:04 +00005467SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005468 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005469
5470 if (!Subtarget->is64Bit()) {
5471 // vastart just stores the address of the VarArgsFrameIndex slot into the
5472 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005473 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005474 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005475 }
5476
5477 // __va_list_tag:
5478 // gp_offset (0 - 6 * 8)
5479 // fp_offset (48 - 48 + 8 * 16)
5480 // overflow_arg_area (point to parameters coming in memory).
5481 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005482 SmallVector<SDValue, 8> MemOps;
5483 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005484 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005485 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005486 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005487 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005488 MemOps.push_back(Store);
5489
5490 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005491 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005492 Store = DAG.getStore(Op.getOperand(0),
5493 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005494 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005495 MemOps.push_back(Store);
5496
5497 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005498 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005499 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005500 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005501 MemOps.push_back(Store);
5502
5503 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005504 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005505 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005506 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 MemOps.push_back(Store);
5508 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5509}
5510
Dan Gohman8181bd12008-07-27 21:46:04 +00005511SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005512 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5513 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005514 SDValue Chain = Op.getOperand(0);
5515 SDValue SrcPtr = Op.getOperand(1);
5516 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005517
5518 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5519 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005520 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005521}
5522
Dan Gohman8181bd12008-07-27 21:46:04 +00005523SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005525 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005526 SDValue Chain = Op.getOperand(0);
5527 SDValue DstPtr = Op.getOperand(1);
5528 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005529 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5530 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005531
Dan Gohman840ff5c2008-04-18 20:55:41 +00005532 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5533 DAG.getIntPtrConstant(24), 8, false,
5534 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005535}
5536
Dan Gohman8181bd12008-07-27 21:46:04 +00005537SDValue
5538X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005539 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005540 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005541 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005542 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005543 case Intrinsic::x86_sse_comieq_ss:
5544 case Intrinsic::x86_sse_comilt_ss:
5545 case Intrinsic::x86_sse_comile_ss:
5546 case Intrinsic::x86_sse_comigt_ss:
5547 case Intrinsic::x86_sse_comige_ss:
5548 case Intrinsic::x86_sse_comineq_ss:
5549 case Intrinsic::x86_sse_ucomieq_ss:
5550 case Intrinsic::x86_sse_ucomilt_ss:
5551 case Intrinsic::x86_sse_ucomile_ss:
5552 case Intrinsic::x86_sse_ucomigt_ss:
5553 case Intrinsic::x86_sse_ucomige_ss:
5554 case Intrinsic::x86_sse_ucomineq_ss:
5555 case Intrinsic::x86_sse2_comieq_sd:
5556 case Intrinsic::x86_sse2_comilt_sd:
5557 case Intrinsic::x86_sse2_comile_sd:
5558 case Intrinsic::x86_sse2_comigt_sd:
5559 case Intrinsic::x86_sse2_comige_sd:
5560 case Intrinsic::x86_sse2_comineq_sd:
5561 case Intrinsic::x86_sse2_ucomieq_sd:
5562 case Intrinsic::x86_sse2_ucomilt_sd:
5563 case Intrinsic::x86_sse2_ucomile_sd:
5564 case Intrinsic::x86_sse2_ucomigt_sd:
5565 case Intrinsic::x86_sse2_ucomige_sd:
5566 case Intrinsic::x86_sse2_ucomineq_sd: {
5567 unsigned Opc = 0;
5568 ISD::CondCode CC = ISD::SETCC_INVALID;
5569 switch (IntNo) {
5570 default: break;
5571 case Intrinsic::x86_sse_comieq_ss:
5572 case Intrinsic::x86_sse2_comieq_sd:
5573 Opc = X86ISD::COMI;
5574 CC = ISD::SETEQ;
5575 break;
5576 case Intrinsic::x86_sse_comilt_ss:
5577 case Intrinsic::x86_sse2_comilt_sd:
5578 Opc = X86ISD::COMI;
5579 CC = ISD::SETLT;
5580 break;
5581 case Intrinsic::x86_sse_comile_ss:
5582 case Intrinsic::x86_sse2_comile_sd:
5583 Opc = X86ISD::COMI;
5584 CC = ISD::SETLE;
5585 break;
5586 case Intrinsic::x86_sse_comigt_ss:
5587 case Intrinsic::x86_sse2_comigt_sd:
5588 Opc = X86ISD::COMI;
5589 CC = ISD::SETGT;
5590 break;
5591 case Intrinsic::x86_sse_comige_ss:
5592 case Intrinsic::x86_sse2_comige_sd:
5593 Opc = X86ISD::COMI;
5594 CC = ISD::SETGE;
5595 break;
5596 case Intrinsic::x86_sse_comineq_ss:
5597 case Intrinsic::x86_sse2_comineq_sd:
5598 Opc = X86ISD::COMI;
5599 CC = ISD::SETNE;
5600 break;
5601 case Intrinsic::x86_sse_ucomieq_ss:
5602 case Intrinsic::x86_sse2_ucomieq_sd:
5603 Opc = X86ISD::UCOMI;
5604 CC = ISD::SETEQ;
5605 break;
5606 case Intrinsic::x86_sse_ucomilt_ss:
5607 case Intrinsic::x86_sse2_ucomilt_sd:
5608 Opc = X86ISD::UCOMI;
5609 CC = ISD::SETLT;
5610 break;
5611 case Intrinsic::x86_sse_ucomile_ss:
5612 case Intrinsic::x86_sse2_ucomile_sd:
5613 Opc = X86ISD::UCOMI;
5614 CC = ISD::SETLE;
5615 break;
5616 case Intrinsic::x86_sse_ucomigt_ss:
5617 case Intrinsic::x86_sse2_ucomigt_sd:
5618 Opc = X86ISD::UCOMI;
5619 CC = ISD::SETGT;
5620 break;
5621 case Intrinsic::x86_sse_ucomige_ss:
5622 case Intrinsic::x86_sse2_ucomige_sd:
5623 Opc = X86ISD::UCOMI;
5624 CC = ISD::SETGE;
5625 break;
5626 case Intrinsic::x86_sse_ucomineq_ss:
5627 case Intrinsic::x86_sse2_ucomineq_sd:
5628 Opc = X86ISD::UCOMI;
5629 CC = ISD::SETNE;
5630 break;
5631 }
5632
5633 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005634 SDValue LHS = Op.getOperand(1);
5635 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5637
Dan Gohman8181bd12008-07-27 21:46:04 +00005638 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5639 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005640 DAG.getConstant(X86CC, MVT::i8), Cond);
5641 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005642 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005643
5644 // Fix vector shift instructions where the last operand is a non-immediate
5645 // i32 value.
5646 case Intrinsic::x86_sse2_pslli_w:
5647 case Intrinsic::x86_sse2_pslli_d:
5648 case Intrinsic::x86_sse2_pslli_q:
5649 case Intrinsic::x86_sse2_psrli_w:
5650 case Intrinsic::x86_sse2_psrli_d:
5651 case Intrinsic::x86_sse2_psrli_q:
5652 case Intrinsic::x86_sse2_psrai_w:
5653 case Intrinsic::x86_sse2_psrai_d:
5654 case Intrinsic::x86_mmx_pslli_w:
5655 case Intrinsic::x86_mmx_pslli_d:
5656 case Intrinsic::x86_mmx_pslli_q:
5657 case Intrinsic::x86_mmx_psrli_w:
5658 case Intrinsic::x86_mmx_psrli_d:
5659 case Intrinsic::x86_mmx_psrli_q:
5660 case Intrinsic::x86_mmx_psrai_w:
5661 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005662 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005663 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005664 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005665
5666 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005667 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005668 switch (IntNo) {
5669 case Intrinsic::x86_sse2_pslli_w:
5670 NewIntNo = Intrinsic::x86_sse2_psll_w;
5671 break;
5672 case Intrinsic::x86_sse2_pslli_d:
5673 NewIntNo = Intrinsic::x86_sse2_psll_d;
5674 break;
5675 case Intrinsic::x86_sse2_pslli_q:
5676 NewIntNo = Intrinsic::x86_sse2_psll_q;
5677 break;
5678 case Intrinsic::x86_sse2_psrli_w:
5679 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5680 break;
5681 case Intrinsic::x86_sse2_psrli_d:
5682 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5683 break;
5684 case Intrinsic::x86_sse2_psrli_q:
5685 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5686 break;
5687 case Intrinsic::x86_sse2_psrai_w:
5688 NewIntNo = Intrinsic::x86_sse2_psra_w;
5689 break;
5690 case Intrinsic::x86_sse2_psrai_d:
5691 NewIntNo = Intrinsic::x86_sse2_psra_d;
5692 break;
5693 default: {
5694 ShAmtVT = MVT::v2i32;
5695 switch (IntNo) {
5696 case Intrinsic::x86_mmx_pslli_w:
5697 NewIntNo = Intrinsic::x86_mmx_psll_w;
5698 break;
5699 case Intrinsic::x86_mmx_pslli_d:
5700 NewIntNo = Intrinsic::x86_mmx_psll_d;
5701 break;
5702 case Intrinsic::x86_mmx_pslli_q:
5703 NewIntNo = Intrinsic::x86_mmx_psll_q;
5704 break;
5705 case Intrinsic::x86_mmx_psrli_w:
5706 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5707 break;
5708 case Intrinsic::x86_mmx_psrli_d:
5709 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5710 break;
5711 case Intrinsic::x86_mmx_psrli_q:
5712 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5713 break;
5714 case Intrinsic::x86_mmx_psrai_w:
5715 NewIntNo = Intrinsic::x86_mmx_psra_w;
5716 break;
5717 case Intrinsic::x86_mmx_psrai_d:
5718 NewIntNo = Intrinsic::x86_mmx_psra_d;
5719 break;
5720 default: abort(); // Can't reach here.
5721 }
5722 break;
5723 }
5724 }
Duncan Sands92c43912008-06-06 12:08:01 +00005725 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005726 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5727 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5728 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5729 DAG.getConstant(NewIntNo, MVT::i32),
5730 Op.getOperand(1), ShAmt);
5731 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 }
5733}
5734
Dan Gohman8181bd12008-07-27 21:46:04 +00005735SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005736 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005737 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739
5740 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005741 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005742 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5743}
5744
Dan Gohman8181bd12008-07-27 21:46:04 +00005745SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005746 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5747 MFI->setFrameAddressIsTaken(true);
5748 MVT VT = Op.getValueType();
5749 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5750 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5751 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5752 while (Depth--)
5753 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5754 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005755}
5756
Dan Gohman8181bd12008-07-27 21:46:04 +00005757SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005758 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005759 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760}
5761
Dan Gohman8181bd12008-07-27 21:46:04 +00005762SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005763{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005765 SDValue Chain = Op.getOperand(0);
5766 SDValue Offset = Op.getOperand(1);
5767 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005768
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005769 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5770 getPointerTy());
5771 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005772
Dan Gohman8181bd12008-07-27 21:46:04 +00005773 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005774 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005775 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5776 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005777 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5778 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005779
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005780 return DAG.getNode(X86ISD::EH_RETURN,
5781 MVT::Other,
5782 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005783}
5784
Dan Gohman8181bd12008-07-27 21:46:04 +00005785SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005786 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005787 SDValue Root = Op.getOperand(0);
5788 SDValue Trmp = Op.getOperand(1); // trampoline
5789 SDValue FPtr = Op.getOperand(2); // nested function
5790 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005791
Dan Gohman12a9c082008-02-06 22:27:42 +00005792 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005793
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005794 const X86InstrInfo *TII =
5795 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5796
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005797 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005798 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005799
5800 // Large code-model.
5801
5802 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5803 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5804
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005805 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5806 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005807
5808 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5809
5810 // Load the pointer to the nested function into R11.
5811 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005812 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005813 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005814 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005815
5816 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005817 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005818
5819 // Load the 'nest' parameter value into R10.
5820 // R10 is specified in X86CallingConv.td
5821 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5822 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5823 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005824 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005825
5826 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005827 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005828
5829 // Jump to the nested function.
5830 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5831 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5832 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005833 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005834
5835 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5836 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5837 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005838 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005839
Dan Gohman8181bd12008-07-27 21:46:04 +00005840 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005841 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005842 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005843 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005844 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005845 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5846 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005847 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005848
5849 switch (CC) {
5850 default:
5851 assert(0 && "Unsupported calling convention");
5852 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005853 case CallingConv::X86_StdCall: {
5854 // Pass 'nest' parameter in ECX.
5855 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005856 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005857
5858 // Check that ECX wasn't needed by an 'inreg' parameter.
5859 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005860 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005861
Chris Lattner1c8733e2008-03-12 17:45:29 +00005862 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005863 unsigned InRegCount = 0;
5864 unsigned Idx = 1;
5865
5866 for (FunctionType::param_iterator I = FTy->param_begin(),
5867 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005868 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005869 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005870 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005871
5872 if (InRegCount > 2) {
5873 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5874 abort();
5875 }
5876 }
5877 break;
5878 }
5879 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005880 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005881 // Pass 'nest' parameter in EAX.
5882 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005883 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005884 break;
5885 }
5886
Dan Gohman8181bd12008-07-27 21:46:04 +00005887 SDValue OutChains[4];
5888 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005889
5890 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5891 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5892
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005893 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005894 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00005895 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00005896 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005897
5898 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005899 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005900
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005901 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005902 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5903 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005904 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005905
5906 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00005907 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005908
Dan Gohman8181bd12008-07-27 21:46:04 +00005909 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00005910 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00005911 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005912 }
5913}
5914
Dan Gohman8181bd12008-07-27 21:46:04 +00005915SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005916 /*
5917 The rounding mode is in bits 11:10 of FPSR, and has the following
5918 settings:
5919 00 Round to nearest
5920 01 Round to -inf
5921 10 Round to +inf
5922 11 Round to 0
5923
5924 FLT_ROUNDS, on the other hand, expects the following:
5925 -1 Undefined
5926 0 Round to 0
5927 1 Round to nearest
5928 2 Round to +inf
5929 3 Round to -inf
5930
5931 To perform the conversion, we do:
5932 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5933 */
5934
5935 MachineFunction &MF = DAG.getMachineFunction();
5936 const TargetMachine &TM = MF.getTarget();
5937 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5938 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00005939 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005940
5941 // Save FP Control Word to stack slot
5942 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00005943 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005944
Dan Gohman8181bd12008-07-27 21:46:04 +00005945 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00005946 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005947
5948 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00005949 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005950
5951 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00005952 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005953 DAG.getNode(ISD::SRL, MVT::i16,
5954 DAG.getNode(ISD::AND, MVT::i16,
5955 CWD, DAG.getConstant(0x800, MVT::i16)),
5956 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005957 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005958 DAG.getNode(ISD::SRL, MVT::i16,
5959 DAG.getNode(ISD::AND, MVT::i16,
5960 CWD, DAG.getConstant(0x400, MVT::i16)),
5961 DAG.getConstant(9, MVT::i8));
5962
Dan Gohman8181bd12008-07-27 21:46:04 +00005963 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005964 DAG.getNode(ISD::AND, MVT::i16,
5965 DAG.getNode(ISD::ADD, MVT::i16,
5966 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5967 DAG.getConstant(1, MVT::i16)),
5968 DAG.getConstant(3, MVT::i16));
5969
5970
Duncan Sands92c43912008-06-06 12:08:01 +00005971 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00005972 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5973}
5974
Dan Gohman8181bd12008-07-27 21:46:04 +00005975SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00005976 MVT VT = Op.getValueType();
5977 MVT OpVT = VT;
5978 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00005979
5980 Op = Op.getOperand(0);
5981 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005982 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00005983 OpVT = MVT::i32;
5984 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5985 }
Evan Cheng48679f42007-12-14 02:13:44 +00005986
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005987 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5988 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5989 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5990
5991 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00005992 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00005993 Ops.push_back(Op);
5994 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5995 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5996 Ops.push_back(Op.getValue(1));
5997 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5998
5999 // Finally xor with NumBits-1.
6000 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6001
Evan Cheng48679f42007-12-14 02:13:44 +00006002 if (VT == MVT::i8)
6003 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6004 return Op;
6005}
6006
Dan Gohman8181bd12008-07-27 21:46:04 +00006007SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006008 MVT VT = Op.getValueType();
6009 MVT OpVT = VT;
6010 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006011
6012 Op = Op.getOperand(0);
6013 if (VT == MVT::i8) {
6014 OpVT = MVT::i32;
6015 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6016 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006017
6018 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6019 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6020 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6021
6022 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006023 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006024 Ops.push_back(Op);
6025 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6026 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6027 Ops.push_back(Op.getValue(1));
6028 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6029
Evan Cheng48679f42007-12-14 02:13:44 +00006030 if (VT == MVT::i8)
6031 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6032 return Op;
6033}
6034
Dan Gohman8181bd12008-07-27 21:46:04 +00006035SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006036 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006037 unsigned Reg = 0;
6038 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006039 switch(T.getSimpleVT()) {
6040 default:
6041 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006042 case MVT::i8: Reg = X86::AL; size = 1; break;
6043 case MVT::i16: Reg = X86::AX; size = 2; break;
6044 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006045 case MVT::i64:
6046 if (Subtarget->is64Bit()) {
6047 Reg = X86::RAX; size = 8;
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006048 } else //Should go away when LegalizeType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00006049 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00006050 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006051 };
Dan Gohman8181bd12008-07-27 21:46:04 +00006052 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006053 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006054 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006055 Op.getOperand(1),
6056 Op.getOperand(3),
6057 DAG.getTargetConstant(size, MVT::i8),
6058 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006059 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006060 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6061 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006062 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6063 return cpOut;
6064}
6065
Gabor Greif825aa892008-08-28 23:19:51 +00006066SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
6067 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006068 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006069 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00006070 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006071 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006072 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006073 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006074 DAG.getConstant(1, MVT::i32));
6075 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00006076 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00006077 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
6078 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006079 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006080 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006081 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006082 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006083 DAG.getConstant(1, MVT::i32));
6084 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6085 swapInL, cpInH.getValue(1));
6086 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6087 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006088 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006089 Op->getOperand(1),
6090 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00006091 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006092 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6093 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006094 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006095 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006096 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00006097 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6098 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6099 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006100 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006101}
6102
Dale Johannesenf160d802008-10-02 18:53:47 +00006103SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6104 SelectionDAG &DAG,
6105 unsigned NewOp) {
6106 SDNode *Node = Op.getNode();
6107 MVT T = Node->getValueType(0);
6108 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6109
6110 SDValue Chain = Node->getOperand(0);
6111 SDValue In1 = Node->getOperand(1);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006112 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6113 Node->getOperand(2), DAG.getIntPtrConstant(0));
6114 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6115 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dale Johannesen44eb5372008-10-03 19:41:08 +00006116 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6117 // have a MemOperand. Pass the info through as a normal operand.
6118 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6119 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesenf160d802008-10-02 18:53:47 +00006120 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006121 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesenf160d802008-10-02 18:53:47 +00006122 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6123 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6124 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6125 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6126}
6127
Dale Johannesen9011d872008-09-29 22:25:26 +00006128SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6129 SDNode *Node = Op.getNode();
6130 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006131 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006132 DAG.getConstant(0, T), Node->getOperand(2));
6133 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6134 ISD::ATOMIC_LOAD_ADD_8 :
6135 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6136 ISD::ATOMIC_LOAD_ADD_16 :
6137 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6138 ISD::ATOMIC_LOAD_ADD_32 :
6139 ISD::ATOMIC_LOAD_ADD_64),
6140 Node->getOperand(0),
6141 Node->getOperand(1), negOp,
6142 cast<AtomicSDNode>(Node)->getSrcValue(),
6143 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006144}
6145
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006146/// LowerOperation - Provide custom lowering hooks for some operations.
6147///
Dan Gohman8181bd12008-07-27 21:46:04 +00006148SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006149 switch (Op.getOpcode()) {
6150 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006151 case ISD::ATOMIC_CMP_SWAP_8:
6152 case ISD::ATOMIC_CMP_SWAP_16:
6153 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006154 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006155 case ISD::ATOMIC_LOAD_SUB_8:
6156 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006157 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006158 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006159 LowerLOAD_SUB(Op,DAG) :
6160 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006161 X86ISD::ATOMSUB64_DAG);
6162 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6163 X86ISD::ATOMAND64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006164 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006165 X86ISD::ATOMOR64_DAG);
6166 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6167 X86ISD::ATOMXOR64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006168 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006169 X86ISD::ATOMNAND64_DAG);
6170 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6171 X86ISD::ATOMADD64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006172 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6173 X86ISD::ATOMSWAP64_DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006174 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6175 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6176 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6177 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6178 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6179 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6180 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6181 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006182 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006183 case ISD::SHL_PARTS:
6184 case ISD::SRA_PARTS:
6185 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6186 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
6187 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6188 case ISD::FABS: return LowerFABS(Op, DAG);
6189 case ISD::FNEG: return LowerFNEG(Op, DAG);
6190 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006191 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006192 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006193 case ISD::SELECT: return LowerSELECT(Op, DAG);
6194 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006195 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6196 case ISD::CALL: return LowerCALL(Op, DAG);
6197 case ISD::RET: return LowerRET(Op, DAG);
6198 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006199 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006200 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006201 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6202 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6203 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6204 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6205 case ISD::FRAME_TO_ARGS_OFFSET:
6206 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6207 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6208 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006209 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006210 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006211 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6212 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006213
6214 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6215 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006216 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006217 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006218}
6219
Duncan Sandsac496a12008-07-04 11:47:58 +00006220/// ReplaceNodeResults - Replace a node with an illegal result type
6221/// with a new node built out of custom code.
6222SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006223 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006224 default:
6225 return X86TargetLowering::LowerOperation(SDValue (N, 0), DAG).getNode();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006226 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6227 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006228 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006230}
6231
6232const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6233 switch (Opcode) {
6234 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006235 case X86ISD::BSF: return "X86ISD::BSF";
6236 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006237 case X86ISD::SHLD: return "X86ISD::SHLD";
6238 case X86ISD::SHRD: return "X86ISD::SHRD";
6239 case X86ISD::FAND: return "X86ISD::FAND";
6240 case X86ISD::FOR: return "X86ISD::FOR";
6241 case X86ISD::FXOR: return "X86ISD::FXOR";
6242 case X86ISD::FSRL: return "X86ISD::FSRL";
6243 case X86ISD::FILD: return "X86ISD::FILD";
6244 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6245 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6246 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6247 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6248 case X86ISD::FLD: return "X86ISD::FLD";
6249 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006250 case X86ISD::CALL: return "X86ISD::CALL";
6251 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6252 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6253 case X86ISD::CMP: return "X86ISD::CMP";
6254 case X86ISD::COMI: return "X86ISD::COMI";
6255 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6256 case X86ISD::SETCC: return "X86ISD::SETCC";
6257 case X86ISD::CMOV: return "X86ISD::CMOV";
6258 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6259 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6260 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6261 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6263 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006264 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006265 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006266 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6267 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006268 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6269 case X86ISD::FMAX: return "X86ISD::FMAX";
6270 case X86ISD::FMIN: return "X86ISD::FMIN";
6271 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6272 case X86ISD::FRCP: return "X86ISD::FRCP";
6273 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6274 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6275 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006276 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006277 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006278 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6279 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006280 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6281 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6282 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6283 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6284 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6285 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006286 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6287 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006288 case X86ISD::VSHL: return "X86ISD::VSHL";
6289 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006290 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6291 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6292 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6293 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6294 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6295 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6296 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6297 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6298 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6299 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006300 }
6301}
6302
6303// isLegalAddressingMode - Return true if the addressing mode represented
6304// by AM is legal for this target, for a load/store of the specified type.
6305bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6306 const Type *Ty) const {
6307 // X86 supports extremely general addressing modes.
6308
6309 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6310 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6311 return false;
6312
6313 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006314 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006315 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6316 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006317
6318 // X86-64 only supports addr of globals in small code model.
6319 if (Subtarget->is64Bit()) {
6320 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6321 return false;
6322 // If lower 4G is not available, then we must use rip-relative addressing.
6323 if (AM.BaseOffs || AM.Scale > 1)
6324 return false;
6325 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006326 }
6327
6328 switch (AM.Scale) {
6329 case 0:
6330 case 1:
6331 case 2:
6332 case 4:
6333 case 8:
6334 // These scales always work.
6335 break;
6336 case 3:
6337 case 5:
6338 case 9:
6339 // These scales are formed with basereg+scalereg. Only accept if there is
6340 // no basereg yet.
6341 if (AM.HasBaseReg)
6342 return false;
6343 break;
6344 default: // Other stuff never works.
6345 return false;
6346 }
6347
6348 return true;
6349}
6350
6351
Evan Cheng27a820a2007-10-26 01:56:11 +00006352bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6353 if (!Ty1->isInteger() || !Ty2->isInteger())
6354 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006355 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6356 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006357 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006358 return false;
6359 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006360}
6361
Duncan Sands92c43912008-06-06 12:08:01 +00006362bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6363 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006364 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006365 unsigned NumBits1 = VT1.getSizeInBits();
6366 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006367 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006368 return false;
6369 return Subtarget->is64Bit() || NumBits1 < 64;
6370}
Evan Cheng27a820a2007-10-26 01:56:11 +00006371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372/// isShuffleMaskLegal - Targets can use this to indicate that they only
6373/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6374/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6375/// are assumed to be legal.
6376bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006377X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006378 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006379 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006380 return (Mask.getNode()->getNumOperands() <= 4 ||
6381 isIdentityMask(Mask.getNode()) ||
6382 isIdentityMask(Mask.getNode(), true) ||
6383 isSplatMask(Mask.getNode()) ||
6384 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6385 X86::isUNPCKLMask(Mask.getNode()) ||
6386 X86::isUNPCKHMask(Mask.getNode()) ||
6387 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6388 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006389}
6390
Dan Gohman48d5f062008-04-09 20:09:42 +00006391bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006392X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006393 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006394 unsigned NumElts = BVOps.size();
6395 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006396 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006397 if (NumElts == 2) return true;
6398 if (NumElts == 4) {
6399 return (isMOVLMask(&BVOps[0], 4) ||
6400 isCommutedMOVL(&BVOps[0], 4, true) ||
6401 isSHUFPMask(&BVOps[0], 4) ||
6402 isCommutedSHUFP(&BVOps[0], 4));
6403 }
6404 return false;
6405}
6406
6407//===----------------------------------------------------------------------===//
6408// X86 Scheduler Hooks
6409//===----------------------------------------------------------------------===//
6410
Mon P Wang078a62d2008-05-05 19:05:59 +00006411// private utility function
6412MachineBasicBlock *
6413X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6414 MachineBasicBlock *MBB,
6415 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006416 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006417 unsigned LoadOpc,
6418 unsigned CXchgOpc,
6419 unsigned copyOpc,
6420 unsigned notOpc,
6421 unsigned EAXreg,
6422 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006423 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006424 // For the atomic bitwise operator, we generate
6425 // thisMBB:
6426 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006427 // ld t1 = [bitinstr.addr]
6428 // op t2 = t1, [bitinstr.val]
6429 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006430 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6431 // bz newMBB
6432 // fallthrough -->nextMBB
6433 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6434 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006435 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006436 ++MBBIter;
6437
6438 /// First build the CFG
6439 MachineFunction *F = MBB->getParent();
6440 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006441 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6442 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6443 F->insert(MBBIter, newMBB);
6444 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006445
6446 // Move all successors to thisMBB to nextMBB
6447 nextMBB->transferSuccessors(thisMBB);
6448
6449 // Update thisMBB to fall through to newMBB
6450 thisMBB->addSuccessor(newMBB);
6451
6452 // newMBB jumps to itself and fall through to nextMBB
6453 newMBB->addSuccessor(nextMBB);
6454 newMBB->addSuccessor(newMBB);
6455
6456 // Insert instructions into newMBB based on incoming instruction
6457 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6458 MachineOperand& destOper = bInstr->getOperand(0);
6459 MachineOperand* argOpers[6];
6460 int numArgs = bInstr->getNumOperands() - 1;
6461 for (int i=0; i < numArgs; ++i)
6462 argOpers[i] = &bInstr->getOperand(i+1);
6463
6464 // x86 address has 4 operands: base, index, scale, and displacement
6465 int lastAddrIndx = 3; // [0,3]
6466 int valArgIndx = 4;
6467
Dale Johannesend20e4452008-08-19 18:47:28 +00006468 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6469 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006470 for (int i=0; i <= lastAddrIndx; ++i)
6471 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006472
Dale Johannesend20e4452008-08-19 18:47:28 +00006473 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006474 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006475 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006476 }
6477 else
6478 tt = t1;
6479
Dale Johannesend20e4452008-08-19 18:47:28 +00006480 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006481 assert((argOpers[valArgIndx]->isReg() ||
6482 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006483 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006484 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006485 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6486 else
6487 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006488 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006489 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006490
Dale Johannesend20e4452008-08-19 18:47:28 +00006491 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006492 MIB.addReg(t1);
6493
Dale Johannesend20e4452008-08-19 18:47:28 +00006494 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006495 for (int i=0; i <= lastAddrIndx; ++i)
6496 (*MIB).addOperand(*argOpers[i]);
6497 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006498 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6499 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6500
Dale Johannesend20e4452008-08-19 18:47:28 +00006501 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6502 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006503
6504 // insert branch
6505 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6506
Dan Gohman221a4372008-07-07 23:14:23 +00006507 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006508 return nextMBB;
6509}
6510
Dale Johannesen44eb5372008-10-03 19:41:08 +00006511// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006512MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006513X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6514 MachineBasicBlock *MBB,
6515 unsigned regOpcL,
6516 unsigned regOpcH,
6517 unsigned immOpcL,
6518 unsigned immOpcH,
6519 bool invSrc) {
6520 // For the atomic bitwise operator, we generate
6521 // thisMBB (instructions are in pairs, except cmpxchg8b)
6522 // ld t1,t2 = [bitinstr.addr]
6523 // newMBB:
6524 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6525 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006526 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006527 // mov ECX, EBX <- t5, t6
6528 // mov EAX, EDX <- t1, t2
6529 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6530 // mov t3, t4 <- EAX, EDX
6531 // bz newMBB
6532 // result in out1, out2
6533 // fallthrough -->nextMBB
6534
6535 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6536 const unsigned LoadOpc = X86::MOV32rm;
6537 const unsigned copyOpc = X86::MOV32rr;
6538 const unsigned NotOpc = X86::NOT32r;
6539 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6540 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6541 MachineFunction::iterator MBBIter = MBB;
6542 ++MBBIter;
6543
6544 /// First build the CFG
6545 MachineFunction *F = MBB->getParent();
6546 MachineBasicBlock *thisMBB = MBB;
6547 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6548 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6549 F->insert(MBBIter, newMBB);
6550 F->insert(MBBIter, nextMBB);
6551
6552 // Move all successors to thisMBB to nextMBB
6553 nextMBB->transferSuccessors(thisMBB);
6554
6555 // Update thisMBB to fall through to newMBB
6556 thisMBB->addSuccessor(newMBB);
6557
6558 // newMBB jumps to itself and fall through to nextMBB
6559 newMBB->addSuccessor(nextMBB);
6560 newMBB->addSuccessor(newMBB);
6561
6562 // Insert instructions into newMBB based on incoming instruction
6563 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6564 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6565 MachineOperand& dest1Oper = bInstr->getOperand(0);
6566 MachineOperand& dest2Oper = bInstr->getOperand(1);
6567 MachineOperand* argOpers[6];
6568 for (int i=0; i < 6; ++i)
6569 argOpers[i] = &bInstr->getOperand(i+2);
6570
6571 // x86 address has 4 operands: base, index, scale, and displacement
6572 int lastAddrIndx = 3; // [0,3]
6573
6574 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6575 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6576 for (int i=0; i <= lastAddrIndx; ++i)
6577 (*MIB).addOperand(*argOpers[i]);
6578 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6579 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006580 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006581 for (int i=0; i <= lastAddrIndx-1; ++i)
6582 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006583 MachineOperand newOp3 = *(argOpers[3]);
6584 if (newOp3.isImm())
6585 newOp3.setImm(newOp3.getImm()+4);
6586 else
6587 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006588 (*MIB).addOperand(newOp3);
6589
6590 // t3/4 are defined later, at the bottom of the loop
6591 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6592 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6593 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6594 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6595 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6596 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6597
6598 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6599 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6600 if (invSrc) {
6601 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6602 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6603 } else {
6604 tt1 = t1;
6605 tt2 = t2;
6606 }
6607
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006608 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006609 "invalid operand");
6610 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6611 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006612 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006613 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6614 else
6615 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006616 if (regOpcL != X86::MOV32rr)
6617 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006618 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006619 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6620 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6621 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006622 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6623 else
6624 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006625 if (regOpcH != X86::MOV32rr)
6626 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006627 (*MIB).addOperand(*argOpers[5]);
6628
6629 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6630 MIB.addReg(t1);
6631 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6632 MIB.addReg(t2);
6633
6634 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6635 MIB.addReg(t5);
6636 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6637 MIB.addReg(t6);
6638
6639 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6640 for (int i=0; i <= lastAddrIndx; ++i)
6641 (*MIB).addOperand(*argOpers[i]);
6642
6643 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6644 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6645
6646 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6647 MIB.addReg(X86::EAX);
6648 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6649 MIB.addReg(X86::EDX);
6650
6651 // insert branch
6652 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6653
6654 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6655 return nextMBB;
6656}
6657
6658// private utility function
6659MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006660X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6661 MachineBasicBlock *MBB,
6662 unsigned cmovOpc) {
6663 // For the atomic min/max operator, we generate
6664 // thisMBB:
6665 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006666 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006667 // mov t2 = [min/max.val]
6668 // cmp t1, t2
6669 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006670 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006671 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6672 // bz newMBB
6673 // fallthrough -->nextMBB
6674 //
6675 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6676 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006677 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006678 ++MBBIter;
6679
6680 /// First build the CFG
6681 MachineFunction *F = MBB->getParent();
6682 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006683 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6684 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6685 F->insert(MBBIter, newMBB);
6686 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006687
6688 // Move all successors to thisMBB to nextMBB
6689 nextMBB->transferSuccessors(thisMBB);
6690
6691 // Update thisMBB to fall through to newMBB
6692 thisMBB->addSuccessor(newMBB);
6693
6694 // newMBB jumps to newMBB and fall through to nextMBB
6695 newMBB->addSuccessor(nextMBB);
6696 newMBB->addSuccessor(newMBB);
6697
6698 // Insert instructions into newMBB based on incoming instruction
6699 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6700 MachineOperand& destOper = mInstr->getOperand(0);
6701 MachineOperand* argOpers[6];
6702 int numArgs = mInstr->getNumOperands() - 1;
6703 for (int i=0; i < numArgs; ++i)
6704 argOpers[i] = &mInstr->getOperand(i+1);
6705
6706 // x86 address has 4 operands: base, index, scale, and displacement
6707 int lastAddrIndx = 3; // [0,3]
6708 int valArgIndx = 4;
6709
Mon P Wang318b0372008-05-05 22:56:23 +00006710 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6711 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006712 for (int i=0; i <= lastAddrIndx; ++i)
6713 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006714
Mon P Wang078a62d2008-05-05 19:05:59 +00006715 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006716 assert((argOpers[valArgIndx]->isReg() ||
6717 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006718 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006719
6720 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006721 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006722 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6723 else
6724 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6725 (*MIB).addOperand(*argOpers[valArgIndx]);
6726
Mon P Wang318b0372008-05-05 22:56:23 +00006727 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6728 MIB.addReg(t1);
6729
Mon P Wang078a62d2008-05-05 19:05:59 +00006730 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6731 MIB.addReg(t1);
6732 MIB.addReg(t2);
6733
6734 // Generate movc
6735 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6736 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6737 MIB.addReg(t2);
6738 MIB.addReg(t1);
6739
6740 // Cmp and exchange if none has modified the memory location
6741 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6742 for (int i=0; i <= lastAddrIndx; ++i)
6743 (*MIB).addOperand(*argOpers[i]);
6744 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006745 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6746 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006747
6748 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6749 MIB.addReg(X86::EAX);
6750
6751 // insert branch
6752 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6753
Dan Gohman221a4372008-07-07 23:14:23 +00006754 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006755 return nextMBB;
6756}
6757
6758
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006759MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006760X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6761 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006762 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6763 switch (MI->getOpcode()) {
6764 default: assert(false && "Unexpected instr type to insert");
6765 case X86::CMOV_FR32:
6766 case X86::CMOV_FR64:
6767 case X86::CMOV_V4F32:
6768 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006769 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006770 // To "insert" a SELECT_CC instruction, we actually have to insert the
6771 // diamond control-flow pattern. The incoming instruction knows the
6772 // destination vreg to set, the condition code register to branch on, the
6773 // true/false values to select between, and a branch opcode to use.
6774 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006775 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006776 ++It;
6777
6778 // thisMBB:
6779 // ...
6780 // TrueVal = ...
6781 // cmpTY ccX, r1, r2
6782 // bCC copy1MBB
6783 // fallthrough --> copy0MBB
6784 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006785 MachineFunction *F = BB->getParent();
6786 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6787 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006788 unsigned Opc =
6789 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6790 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006791 F->insert(It, copy0MBB);
6792 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006793 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006794 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006795 sinkMBB->transferSuccessors(BB);
6796
6797 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006798 BB->addSuccessor(copy0MBB);
6799 BB->addSuccessor(sinkMBB);
6800
6801 // copy0MBB:
6802 // %FalseValue = ...
6803 // # fallthrough to sinkMBB
6804 BB = copy0MBB;
6805
6806 // Update machine-CFG edges
6807 BB->addSuccessor(sinkMBB);
6808
6809 // sinkMBB:
6810 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6811 // ...
6812 BB = sinkMBB;
6813 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6814 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6815 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6816
Dan Gohman221a4372008-07-07 23:14:23 +00006817 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006818 return BB;
6819 }
6820
6821 case X86::FP32_TO_INT16_IN_MEM:
6822 case X86::FP32_TO_INT32_IN_MEM:
6823 case X86::FP32_TO_INT64_IN_MEM:
6824 case X86::FP64_TO_INT16_IN_MEM:
6825 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006826 case X86::FP64_TO_INT64_IN_MEM:
6827 case X86::FP80_TO_INT16_IN_MEM:
6828 case X86::FP80_TO_INT32_IN_MEM:
6829 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830 // Change the floating point control register to use "round towards zero"
6831 // mode when truncating to an integer value.
6832 MachineFunction *F = BB->getParent();
6833 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6834 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6835
6836 // Load the old value of the high byte of the control word...
6837 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006838 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006839 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6840
6841 // Set the high part to be round to zero...
6842 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6843 .addImm(0xC7F);
6844
6845 // Reload the modified control word now...
6846 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6847
6848 // Restore the memory image of control word to original value
6849 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6850 .addReg(OldCW);
6851
6852 // Get the X86 opcode to use.
6853 unsigned Opc;
6854 switch (MI->getOpcode()) {
6855 default: assert(0 && "illegal opcode!");
6856 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6857 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6858 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6859 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6860 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6861 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006862 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6863 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6864 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006865 }
6866
6867 X86AddressMode AM;
6868 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006869 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006870 AM.BaseType = X86AddressMode::RegBase;
6871 AM.Base.Reg = Op.getReg();
6872 } else {
6873 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006874 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006875 }
6876 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006877 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006878 AM.Scale = Op.getImm();
6879 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006880 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006881 AM.IndexReg = Op.getImm();
6882 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006883 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006884 AM.GV = Op.getGlobal();
6885 } else {
6886 AM.Disp = Op.getImm();
6887 }
6888 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6889 .addReg(MI->getOperand(4).getReg());
6890
6891 // Reload the original control word now.
6892 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6893
Dan Gohman221a4372008-07-07 23:14:23 +00006894 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006895 return BB;
6896 }
Mon P Wang078a62d2008-05-05 19:05:59 +00006897 case X86::ATOMAND32:
6898 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006899 X86::AND32ri, X86::MOV32rm,
6900 X86::LCMPXCHG32, X86::MOV32rr,
6901 X86::NOT32r, X86::EAX,
6902 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006903 case X86::ATOMOR32:
6904 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006905 X86::OR32ri, X86::MOV32rm,
6906 X86::LCMPXCHG32, X86::MOV32rr,
6907 X86::NOT32r, X86::EAX,
6908 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00006909 case X86::ATOMXOR32:
6910 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006911 X86::XOR32ri, X86::MOV32rm,
6912 X86::LCMPXCHG32, X86::MOV32rr,
6913 X86::NOT32r, X86::EAX,
6914 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006915 case X86::ATOMNAND32:
6916 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00006917 X86::AND32ri, X86::MOV32rm,
6918 X86::LCMPXCHG32, X86::MOV32rr,
6919 X86::NOT32r, X86::EAX,
6920 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00006921 case X86::ATOMMIN32:
6922 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6923 case X86::ATOMMAX32:
6924 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6925 case X86::ATOMUMIN32:
6926 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6927 case X86::ATOMUMAX32:
6928 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00006929
6930 case X86::ATOMAND16:
6931 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6932 X86::AND16ri, X86::MOV16rm,
6933 X86::LCMPXCHG16, X86::MOV16rr,
6934 X86::NOT16r, X86::AX,
6935 X86::GR16RegisterClass);
6936 case X86::ATOMOR16:
6937 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6938 X86::OR16ri, X86::MOV16rm,
6939 X86::LCMPXCHG16, X86::MOV16rr,
6940 X86::NOT16r, X86::AX,
6941 X86::GR16RegisterClass);
6942 case X86::ATOMXOR16:
6943 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6944 X86::XOR16ri, X86::MOV16rm,
6945 X86::LCMPXCHG16, X86::MOV16rr,
6946 X86::NOT16r, X86::AX,
6947 X86::GR16RegisterClass);
6948 case X86::ATOMNAND16:
6949 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6950 X86::AND16ri, X86::MOV16rm,
6951 X86::LCMPXCHG16, X86::MOV16rr,
6952 X86::NOT16r, X86::AX,
6953 X86::GR16RegisterClass, true);
6954 case X86::ATOMMIN16:
6955 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6956 case X86::ATOMMAX16:
6957 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6958 case X86::ATOMUMIN16:
6959 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6960 case X86::ATOMUMAX16:
6961 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6962
6963 case X86::ATOMAND8:
6964 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6965 X86::AND8ri, X86::MOV8rm,
6966 X86::LCMPXCHG8, X86::MOV8rr,
6967 X86::NOT8r, X86::AL,
6968 X86::GR8RegisterClass);
6969 case X86::ATOMOR8:
6970 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6971 X86::OR8ri, X86::MOV8rm,
6972 X86::LCMPXCHG8, X86::MOV8rr,
6973 X86::NOT8r, X86::AL,
6974 X86::GR8RegisterClass);
6975 case X86::ATOMXOR8:
6976 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6977 X86::XOR8ri, X86::MOV8rm,
6978 X86::LCMPXCHG8, X86::MOV8rr,
6979 X86::NOT8r, X86::AL,
6980 X86::GR8RegisterClass);
6981 case X86::ATOMNAND8:
6982 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6983 X86::AND8ri, X86::MOV8rm,
6984 X86::LCMPXCHG8, X86::MOV8rr,
6985 X86::NOT8r, X86::AL,
6986 X86::GR8RegisterClass, true);
6987 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00006988 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00006989 case X86::ATOMAND64:
6990 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6991 X86::AND64ri32, X86::MOV64rm,
6992 X86::LCMPXCHG64, X86::MOV64rr,
6993 X86::NOT64r, X86::RAX,
6994 X86::GR64RegisterClass);
6995 case X86::ATOMOR64:
6996 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6997 X86::OR64ri32, X86::MOV64rm,
6998 X86::LCMPXCHG64, X86::MOV64rr,
6999 X86::NOT64r, X86::RAX,
7000 X86::GR64RegisterClass);
7001 case X86::ATOMXOR64:
7002 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7003 X86::XOR64ri32, X86::MOV64rm,
7004 X86::LCMPXCHG64, X86::MOV64rr,
7005 X86::NOT64r, X86::RAX,
7006 X86::GR64RegisterClass);
7007 case X86::ATOMNAND64:
7008 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7009 X86::AND64ri32, X86::MOV64rm,
7010 X86::LCMPXCHG64, X86::MOV64rr,
7011 X86::NOT64r, X86::RAX,
7012 X86::GR64RegisterClass, true);
7013 case X86::ATOMMIN64:
7014 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7015 case X86::ATOMMAX64:
7016 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7017 case X86::ATOMUMIN64:
7018 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7019 case X86::ATOMUMAX64:
7020 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007021
7022 // This group does 64-bit operations on a 32-bit host.
7023 case X86::ATOMAND6432:
7024 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7025 X86::AND32rr, X86::AND32rr,
7026 X86::AND32ri, X86::AND32ri,
7027 false);
7028 case X86::ATOMOR6432:
7029 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7030 X86::OR32rr, X86::OR32rr,
7031 X86::OR32ri, X86::OR32ri,
7032 false);
7033 case X86::ATOMXOR6432:
7034 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7035 X86::XOR32rr, X86::XOR32rr,
7036 X86::XOR32ri, X86::XOR32ri,
7037 false);
7038 case X86::ATOMNAND6432:
7039 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7040 X86::AND32rr, X86::AND32rr,
7041 X86::AND32ri, X86::AND32ri,
7042 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007043 case X86::ATOMADD6432:
7044 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7045 X86::ADD32rr, X86::ADC32rr,
7046 X86::ADD32ri, X86::ADC32ri,
7047 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007048 case X86::ATOMSUB6432:
7049 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7050 X86::SUB32rr, X86::SBB32rr,
7051 X86::SUB32ri, X86::SBB32ri,
7052 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007053 case X86::ATOMSWAP6432:
7054 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7055 X86::MOV32rr, X86::MOV32rr,
7056 X86::MOV32ri, X86::MOV32ri,
7057 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007058 }
7059}
7060
7061//===----------------------------------------------------------------------===//
7062// X86 Optimization Hooks
7063//===----------------------------------------------------------------------===//
7064
Dan Gohman8181bd12008-07-27 21:46:04 +00007065void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007066 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007067 APInt &KnownZero,
7068 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007069 const SelectionDAG &DAG,
7070 unsigned Depth) const {
7071 unsigned Opc = Op.getOpcode();
7072 assert((Opc >= ISD::BUILTIN_OP_END ||
7073 Opc == ISD::INTRINSIC_WO_CHAIN ||
7074 Opc == ISD::INTRINSIC_W_CHAIN ||
7075 Opc == ISD::INTRINSIC_VOID) &&
7076 "Should use MaskedValueIsZero if you don't know whether Op"
7077 " is a target node!");
7078
Dan Gohman1d79e432008-02-13 23:07:24 +00007079 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007080 switch (Opc) {
7081 default: break;
7082 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007083 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7084 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007085 break;
7086 }
7087}
7088
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007089/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007090/// node is a GlobalAddress + offset.
7091bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7092 GlobalValue* &GA, int64_t &Offset) const{
7093 if (N->getOpcode() == X86ISD::Wrapper) {
7094 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007095 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007096 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007097 return true;
7098 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007099 }
Evan Chengef7be082008-05-12 19:56:52 +00007100 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007101}
7102
Evan Chengef7be082008-05-12 19:56:52 +00007103static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7104 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007105 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007106 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007107 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007108 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007109 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007110 return false;
7111}
7112
Dan Gohman8181bd12008-07-27 21:46:04 +00007113static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007114 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007115 SDNode *&Base,
7116 SelectionDAG &DAG, MachineFrameInfo *MFI,
7117 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007118 Base = NULL;
7119 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007120 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007121 if (Idx.getOpcode() == ISD::UNDEF) {
7122 if (!Base)
7123 return false;
7124 continue;
7125 }
7126
Dan Gohman8181bd12008-07-27 21:46:04 +00007127 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007128 if (!Elt.getNode() ||
7129 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007130 return false;
7131 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007132 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007133 if (Base->getOpcode() == ISD::UNDEF)
7134 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007135 continue;
7136 }
7137 if (Elt.getOpcode() == ISD::UNDEF)
7138 continue;
7139
Gabor Greif1c80d112008-08-28 21:40:38 +00007140 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007141 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007142 return false;
7143 }
7144 return true;
7145}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007146
7147/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7148/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7149/// if the load addresses are consecutive, non-overlapping, and in the right
7150/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007151static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007152 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007153 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007154 MVT VT = N->getValueType(0);
7155 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007156 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007157 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007158 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007159 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7160 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007161 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007162
Dan Gohman11821702007-07-27 17:16:43 +00007163 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007164 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007165 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007166 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007167 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7168 LD->getSrcValueOffset(), LD->isVolatile(),
7169 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007170}
7171
Evan Chengb6290462008-05-12 23:04:07 +00007172/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007173static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007174 const X86Subtarget *Subtarget,
7175 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007176 unsigned NumOps = N->getNumOperands();
7177
Evan Chenge9b9c672008-05-09 21:53:03 +00007178 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007179 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007180 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007181
Duncan Sands92c43912008-06-06 12:08:01 +00007182 MVT VT = N->getValueType(0);
7183 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007184 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7185 // We are looking for load i64 and zero extend. We want to transform
7186 // it before legalizer has a chance to expand it. Also look for i64
7187 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007188 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007189 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007190 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007191 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007192 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007193
7194 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007195 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007196 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007197 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007198 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007199 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007200 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007201 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007202 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007203
7204 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007205 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007206
7207 // Load must not be an extload.
7208 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007209 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007210
Evan Cheng6617eed2008-09-24 23:26:36 +00007211 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7212 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7213 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7214 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7215 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007216}
7217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007218/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007219static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007220 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007221 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007222
7223 // If we have SSE[12] support, try to form min/max nodes.
7224 if (Subtarget->hasSSE2() &&
7225 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7226 if (Cond.getOpcode() == ISD::SETCC) {
7227 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007228 SDValue LHS = N->getOperand(1);
7229 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007230 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7231
7232 unsigned Opcode = 0;
7233 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7234 switch (CC) {
7235 default: break;
7236 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7237 case ISD::SETULE:
7238 case ISD::SETLE:
7239 if (!UnsafeFPMath) break;
7240 // FALL THROUGH.
7241 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7242 case ISD::SETLT:
7243 Opcode = X86ISD::FMIN;
7244 break;
7245
7246 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7247 case ISD::SETUGT:
7248 case ISD::SETGT:
7249 if (!UnsafeFPMath) break;
7250 // FALL THROUGH.
7251 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7252 case ISD::SETGE:
7253 Opcode = X86ISD::FMAX;
7254 break;
7255 }
7256 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7257 switch (CC) {
7258 default: break;
7259 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7260 case ISD::SETUGT:
7261 case ISD::SETGT:
7262 if (!UnsafeFPMath) break;
7263 // FALL THROUGH.
7264 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7265 case ISD::SETGE:
7266 Opcode = X86ISD::FMIN;
7267 break;
7268
7269 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7270 case ISD::SETULE:
7271 case ISD::SETLE:
7272 if (!UnsafeFPMath) break;
7273 // FALL THROUGH.
7274 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7275 case ISD::SETLT:
7276 Opcode = X86ISD::FMAX;
7277 break;
7278 }
7279 }
7280
7281 if (Opcode)
7282 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7283 }
7284
7285 }
7286
Dan Gohman8181bd12008-07-27 21:46:04 +00007287 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007288}
7289
Chris Lattnerce84ae42008-02-22 02:09:43 +00007290/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007291static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007292 const X86Subtarget *Subtarget) {
7293 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7294 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007295 // A preferable solution to the general problem is to figure out the right
7296 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007297 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007298 if (St->getValue().getValueType().isVector() &&
7299 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007300 isa<LoadSDNode>(St->getValue()) &&
7301 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7302 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007303 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007304 LoadSDNode *Ld = 0;
7305 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007306 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007307 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007308 // Must be a store of a load. We currently handle two cases: the load
7309 // is a direct child, and it's under an intervening TokenFactor. It is
7310 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007311 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007312 Ld = cast<LoadSDNode>(St->getChain());
7313 else if (St->getValue().hasOneUse() &&
7314 ChainVal->getOpcode() == ISD::TokenFactor) {
7315 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007316 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007317 TokenFactorIndex = i;
7318 Ld = cast<LoadSDNode>(St->getValue());
7319 } else
7320 Ops.push_back(ChainVal->getOperand(i));
7321 }
7322 }
7323 if (Ld) {
7324 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7325 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007326 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007327 Ld->getBasePtr(), Ld->getSrcValue(),
7328 Ld->getSrcValueOffset(), Ld->isVolatile(),
7329 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007330 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007331 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007332 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007333 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7334 Ops.size());
7335 }
7336 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7337 St->getSrcValue(), St->getSrcValueOffset(),
7338 St->isVolatile(), St->getAlignment());
7339 }
7340
7341 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007342 SDValue LoAddr = Ld->getBasePtr();
7343 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007344 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007345
Dan Gohman8181bd12008-07-27 21:46:04 +00007346 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007347 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7348 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007349 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007350 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7351 Ld->isVolatile(),
7352 MinAlign(Ld->getAlignment(), 4));
7353
Dan Gohman8181bd12008-07-27 21:46:04 +00007354 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007355 if (TokenFactorIndex != -1) {
7356 Ops.push_back(LoLd);
7357 Ops.push_back(HiLd);
7358 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7359 Ops.size());
7360 }
7361
7362 LoAddr = St->getBasePtr();
7363 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007364 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007365
Dan Gohman8181bd12008-07-27 21:46:04 +00007366 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007367 St->getSrcValue(), St->getSrcValueOffset(),
7368 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007369 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007370 St->getSrcValue(),
7371 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007372 St->isVolatile(),
7373 MinAlign(St->getAlignment(), 4));
7374 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007375 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007376 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007377 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007378}
7379
Chris Lattner470d5dc2008-01-25 06:14:17 +00007380/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7381/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007382static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007383 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7384 // F[X]OR(0.0, x) -> x
7385 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007386 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7387 if (C->getValueAPF().isPosZero())
7388 return N->getOperand(1);
7389 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7390 if (C->getValueAPF().isPosZero())
7391 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007392 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007393}
7394
7395/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007396static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007397 // FAND(0.0, x) -> 0.0
7398 // FAND(x, 0.0) -> 0.0
7399 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7400 if (C->getValueAPF().isPosZero())
7401 return N->getOperand(0);
7402 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7403 if (C->getValueAPF().isPosZero())
7404 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007405 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007406}
7407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007408
Dan Gohman8181bd12008-07-27 21:46:04 +00007409SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007410 DAGCombinerInfo &DCI) const {
7411 SelectionDAG &DAG = DCI.DAG;
7412 switch (N->getOpcode()) {
7413 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007414 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7415 case ISD::BUILD_VECTOR:
7416 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007417 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007418 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007419 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007420 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7421 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007422 }
7423
Dan Gohman8181bd12008-07-27 21:46:04 +00007424 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007425}
7426
7427//===----------------------------------------------------------------------===//
7428// X86 Inline Assembly Support
7429//===----------------------------------------------------------------------===//
7430
7431/// getConstraintType - Given a constraint letter, return the type of
7432/// constraint it is for this target.
7433X86TargetLowering::ConstraintType
7434X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7435 if (Constraint.size() == 1) {
7436 switch (Constraint[0]) {
7437 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007438 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007439 case 'r':
7440 case 'R':
7441 case 'l':
7442 case 'q':
7443 case 'Q':
7444 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007445 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007446 case 'Y':
7447 return C_RegisterClass;
7448 default:
7449 break;
7450 }
7451 }
7452 return TargetLowering::getConstraintType(Constraint);
7453}
7454
Dale Johannesene99fc902008-01-29 02:21:21 +00007455/// LowerXConstraint - try to replace an X constraint, which matches anything,
7456/// with another that has more specific requirements based on the type of the
7457/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007458const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007459LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007460 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7461 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007462 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007463 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007464 return "Y";
7465 if (Subtarget->hasSSE1())
7466 return "x";
7467 }
7468
7469 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007470}
7471
Chris Lattnera531abc2007-08-25 00:47:38 +00007472/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7473/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007474void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007475 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007476 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007477 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007478 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007479 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007480
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007481 switch (Constraint) {
7482 default: break;
7483 case 'I':
7484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007485 if (C->getZExtValue() <= 31) {
7486 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007487 break;
7488 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007489 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007490 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007491 case 'J':
7492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7493 if (C->getZExtValue() <= 63) {
7494 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7495 break;
7496 }
7497 }
7498 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007499 case 'N':
7500 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007501 if (C->getZExtValue() <= 255) {
7502 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007503 break;
7504 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007506 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007507 case 'i': {
7508 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007509 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007510 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007511 break;
7512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007513
7514 // If we are in non-pic codegen mode, we allow the address of a global (with
7515 // an optional displacement) to be used with 'i'.
7516 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7517 int64_t Offset = 0;
7518
7519 // Match either (GA) or (GA+C)
7520 if (GA) {
7521 Offset = GA->getOffset();
7522 } else if (Op.getOpcode() == ISD::ADD) {
7523 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7524 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7525 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007526 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007527 } else {
7528 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7529 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7530 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007531 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007532 else
7533 C = 0, GA = 0;
7534 }
7535 }
7536
7537 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007538 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007539 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007540 else
7541 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7542 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007543 Result = Op;
7544 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007545 }
7546
7547 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007548 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007549 }
7550 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007551
Gabor Greif1c80d112008-08-28 21:40:38 +00007552 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007553 Ops.push_back(Result);
7554 return;
7555 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007556 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7557 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007558}
7559
7560std::vector<unsigned> X86TargetLowering::
7561getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007562 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007563 if (Constraint.size() == 1) {
7564 // FIXME: not handling fp-stack yet!
7565 switch (Constraint[0]) { // GCC X86 Constraint Letters
7566 default: break; // Unknown constraint letter
7567 case 'A': // EAX/EDX
7568 if (VT == MVT::i32 || VT == MVT::i64)
7569 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7570 break;
7571 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7572 case 'Q': // Q_REGS
7573 if (VT == MVT::i32)
7574 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7575 else if (VT == MVT::i16)
7576 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7577 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007578 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007579 else if (VT == MVT::i64)
7580 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7581 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007582 }
7583 }
7584
7585 return std::vector<unsigned>();
7586}
7587
7588std::pair<unsigned, const TargetRegisterClass*>
7589X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007590 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007591 // First, see if this is a constraint that directly corresponds to an LLVM
7592 // register class.
7593 if (Constraint.size() == 1) {
7594 // GCC Constraint Letters
7595 switch (Constraint[0]) {
7596 default: break;
7597 case 'r': // GENERAL_REGS
7598 case 'R': // LEGACY_REGS
7599 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007600 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007601 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007602 if (VT == MVT::i16)
7603 return std::make_pair(0U, X86::GR16RegisterClass);
7604 if (VT == MVT::i32 || !Subtarget->is64Bit())
7605 return std::make_pair(0U, X86::GR32RegisterClass);
7606 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007607 case 'f': // FP Stack registers.
7608 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7609 // value to the correct fpstack register class.
7610 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7611 return std::make_pair(0U, X86::RFP32RegisterClass);
7612 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7613 return std::make_pair(0U, X86::RFP64RegisterClass);
7614 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007615 case 'y': // MMX_REGS if MMX allowed.
7616 if (!Subtarget->hasMMX()) break;
7617 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007618 case 'Y': // SSE_REGS if SSE2 allowed
7619 if (!Subtarget->hasSSE2()) break;
7620 // FALL THROUGH.
7621 case 'x': // SSE_REGS if SSE1 allowed
7622 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007623
7624 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007625 default: break;
7626 // Scalar SSE types.
7627 case MVT::f32:
7628 case MVT::i32:
7629 return std::make_pair(0U, X86::FR32RegisterClass);
7630 case MVT::f64:
7631 case MVT::i64:
7632 return std::make_pair(0U, X86::FR64RegisterClass);
7633 // Vector types.
7634 case MVT::v16i8:
7635 case MVT::v8i16:
7636 case MVT::v4i32:
7637 case MVT::v2i64:
7638 case MVT::v4f32:
7639 case MVT::v2f64:
7640 return std::make_pair(0U, X86::VR128RegisterClass);
7641 }
7642 break;
7643 }
7644 }
7645
7646 // Use the default implementation in TargetLowering to convert the register
7647 // constraint into a member of a register class.
7648 std::pair<unsigned, const TargetRegisterClass*> Res;
7649 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7650
7651 // Not found as a standard register?
7652 if (Res.second == 0) {
7653 // GCC calls "st(0)" just plain "st".
7654 if (StringsEqualNoCase("{st}", Constraint)) {
7655 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007656 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007657 }
7658
7659 return Res;
7660 }
7661
7662 // Otherwise, check to see if this is a register class of the wrong value
7663 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7664 // turn into {ax},{dx}.
7665 if (Res.second->hasType(VT))
7666 return Res; // Correct type already, nothing to do.
7667
7668 // All of the single-register GCC register classes map their values onto
7669 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7670 // really want an 8-bit or 32-bit register, map to the appropriate register
7671 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007672 if (Res.second == X86::GR16RegisterClass) {
7673 if (VT == MVT::i8) {
7674 unsigned DestReg = 0;
7675 switch (Res.first) {
7676 default: break;
7677 case X86::AX: DestReg = X86::AL; break;
7678 case X86::DX: DestReg = X86::DL; break;
7679 case X86::CX: DestReg = X86::CL; break;
7680 case X86::BX: DestReg = X86::BL; break;
7681 }
7682 if (DestReg) {
7683 Res.first = DestReg;
7684 Res.second = Res.second = X86::GR8RegisterClass;
7685 }
7686 } else if (VT == MVT::i32) {
7687 unsigned DestReg = 0;
7688 switch (Res.first) {
7689 default: break;
7690 case X86::AX: DestReg = X86::EAX; break;
7691 case X86::DX: DestReg = X86::EDX; break;
7692 case X86::CX: DestReg = X86::ECX; break;
7693 case X86::BX: DestReg = X86::EBX; break;
7694 case X86::SI: DestReg = X86::ESI; break;
7695 case X86::DI: DestReg = X86::EDI; break;
7696 case X86::BP: DestReg = X86::EBP; break;
7697 case X86::SP: DestReg = X86::ESP; break;
7698 }
7699 if (DestReg) {
7700 Res.first = DestReg;
7701 Res.second = Res.second = X86::GR32RegisterClass;
7702 }
7703 } else if (VT == MVT::i64) {
7704 unsigned DestReg = 0;
7705 switch (Res.first) {
7706 default: break;
7707 case X86::AX: DestReg = X86::RAX; break;
7708 case X86::DX: DestReg = X86::RDX; break;
7709 case X86::CX: DestReg = X86::RCX; break;
7710 case X86::BX: DestReg = X86::RBX; break;
7711 case X86::SI: DestReg = X86::RSI; break;
7712 case X86::DI: DestReg = X86::RDI; break;
7713 case X86::BP: DestReg = X86::RBP; break;
7714 case X86::SP: DestReg = X86::RSP; break;
7715 }
7716 if (DestReg) {
7717 Res.first = DestReg;
7718 Res.second = Res.second = X86::GR64RegisterClass;
7719 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007720 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007721 } else if (Res.second == X86::FR32RegisterClass ||
7722 Res.second == X86::FR64RegisterClass ||
7723 Res.second == X86::VR128RegisterClass) {
7724 // Handle references to XMM physical registers that got mapped into the
7725 // wrong class. This can happen with constraints like {xmm0} where the
7726 // target independent register mapper will just pick the first match it can
7727 // find, ignoring the required type.
7728 if (VT == MVT::f32)
7729 Res.second = X86::FR32RegisterClass;
7730 else if (VT == MVT::f64)
7731 Res.second = X86::FR64RegisterClass;
7732 else if (X86::VR128RegisterClass->hasType(VT))
7733 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007734 }
7735
7736 return Res;
7737}