blob: 221f29fa728f2ab4f69314267caf5f2b429e926b [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 Johannesena359b8b2008-10-21 20:50:01 +0000115 if (X86ScalarSSEf64) {
116 // We have an impenetrably clever algorithm for ui64->double only.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
119 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
Dale Johannesena359b8b2008-10-21 20:50:01 +0000120 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
122 }
123
124 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
128 // SSE has no i16 to fp conversion, only i32
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000129 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000131 // f32 and f64 cases are Legal, f80 case is not
132 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
133 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
135 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
136 }
137
Dale Johannesen958b08b2007-09-19 23:55:34 +0000138 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
139 // are Legal, f80 is custom lowered.
140 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
141 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
143 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
144 // this operation.
145 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
146 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
147
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000148 if (X86ScalarSSEf32) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000150 // f32 and f64 cases are Legal, f80 case is not
151 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 } else {
153 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
154 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
155 }
156
157 // Handle FP_TO_UINT by promoting the destination to a larger signed
158 // conversion.
159 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
160 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
161 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
162
163 if (Subtarget->is64Bit()) {
164 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
165 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
166 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000167 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 // Expand FP_TO_UINT into a select.
169 // FIXME: We would like to use a Custom expander here eventually to do
170 // the optimal thing for SSE vs. the default expansion in the legalizer.
171 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
172 else
173 // With SSE3 we can use fisttpll to convert to a signed i64.
174 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
175 }
176
177 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000178 if (!X86ScalarSSEf64) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
180 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
181 }
182
Dan Gohman8450d862008-02-18 19:34:53 +0000183 // Scalar integer divide and remainder are lowered to use operations that
184 // produce two results, to match the available instructions. This exposes
185 // the two-result form to trivial CSE, which is able to combine x/y and x%y
186 // into a single instruction.
187 //
188 // Scalar integer multiply-high is also lowered to use two-result
189 // operations, to match the available instructions. However, plain multiply
190 // (low) operations are left as Legal, as there are single-result
191 // instructions for this in x86. Using the two-result multiply instructions
192 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman5a199552007-10-08 18:33:35 +0000193 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
194 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
195 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
196 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
197 setOperationAction(ISD::SREM , MVT::i8 , Expand);
198 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000199 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
200 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
201 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
202 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
203 setOperationAction(ISD::SREM , MVT::i16 , Expand);
204 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000205 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
206 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
207 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
208 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
209 setOperationAction(ISD::SREM , MVT::i32 , Expand);
210 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman5a199552007-10-08 18:33:35 +0000211 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
212 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
213 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
214 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
215 setOperationAction(ISD::SREM , MVT::i64 , Expand);
216 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
219 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
220 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
221 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 if (Subtarget->is64Bit())
Christopher Lamb0a7c8662007-08-10 21:48:46 +0000223 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
224 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
225 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
227 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000228 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerb7a5cca2008-03-07 06:36:32 +0000230 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman819574c2008-01-31 00:41:03 +0000231 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000237 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
238 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000240 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
241 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 if (Subtarget->is64Bit()) {
243 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng48679f42007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 }
247
248 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
249 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
250
251 // These should be promoted to a larger select which is supported.
252 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
253 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
254 // X86 wants to expand cmov itself.
255 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
256 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
257 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
258 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000259 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
261 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
262 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
263 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
264 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000265 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (Subtarget->is64Bit()) {
267 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
268 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
269 }
270 // X86 ret instruction may pop stack.
271 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000272 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273
274 // Darwin ABI issue.
275 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
276 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
277 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
278 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000279 if (Subtarget->is64Bit())
280 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000281 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
284 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
285 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendlingfef06052008-09-16 21:48:12 +0000286 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 }
288 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
289 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
290 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
291 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000292 if (Subtarget->is64Bit()) {
293 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
294 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
295 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
296 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297
Evan Cheng8d51ab32008-03-10 19:38:10 +0000298 if (Subtarget->hasSSE1())
299 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000300
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000301 if (!Subtarget->hasSSE2())
302 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
303
Mon P Wang078a62d2008-05-05 19:05:59 +0000304 // Expand certain atomics
Dale Johannesenbc187662008-08-28 02:44:49 +0000305 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
306 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
307 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
308 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000309
Dale Johannesen9011d872008-09-29 22:25:26 +0000310 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
311 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
312 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
313 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000314
Dale Johannesenf160d802008-10-02 18:53:47 +0000315 if (!Subtarget->is64Bit()) {
316 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
323 }
324
Dan Gohman472d12c2008-06-30 20:59:49 +0000325 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
326 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 // FIXME - use subtarget debug flags
328 if (!Subtarget->isTargetDarwin() &&
329 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000330 !Subtarget->isTargetCygMing()) {
331 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
332 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
333 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
336 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
337 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
338 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
339 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 setExceptionPointerRegister(X86::RAX);
341 setExceptionSelectorRegister(X86::RDX);
342 } else {
343 setExceptionPointerRegister(X86::EAX);
344 setExceptionSelectorRegister(X86::EDX);
345 }
Anton Korobeynikov23ca9c52007-09-03 00:36:06 +0000346 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000347 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
348
Duncan Sands7407a9f2007-09-11 14:10:23 +0000349 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000350
Chris Lattner56b941f2008-01-15 21:58:22 +0000351 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000352
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
354 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000356 if (Subtarget->is64Bit()) {
357 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000359 } else {
360 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000362 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363
364 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
365 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
366 if (Subtarget->is64Bit())
367 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
368 if (Subtarget->isTargetCygMing())
369 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
370 else
371 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
372
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000373 if (X86ScalarSSEf64) {
374 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 // Set up the FP register classes.
376 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
377 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
378
379 // Use ANDPD to simulate FABS.
380 setOperationAction(ISD::FABS , MVT::f64, Custom);
381 setOperationAction(ISD::FABS , MVT::f32, Custom);
382
383 // Use XORP to simulate FNEG.
384 setOperationAction(ISD::FNEG , MVT::f64, Custom);
385 setOperationAction(ISD::FNEG , MVT::f32, Custom);
386
387 // Use ANDPD and ORPD to simulate FCOPYSIGN.
388 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
389 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
390
391 // We don't support sin/cos/fmod
392 setOperationAction(ISD::FSIN , MVT::f64, Expand);
393 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 setOperationAction(ISD::FSIN , MVT::f32, Expand);
395 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396
397 // Expand FP immediates into loads from the stack, except for the special
398 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000399 addLegalFPImmediate(APFloat(+0.0)); // xorpd
400 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000401
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000402 // Floating truncations from f80 and extensions to f80 go through memory.
403 // If optimizing, we lie about this though and handle it in
404 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
405 if (Fast) {
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f64, MVT::f80, Expand);
408 setConvertAction(MVT::f80, MVT::f32, Expand);
409 setConvertAction(MVT::f80, MVT::f64, Expand);
410 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000411 } else if (X86ScalarSSEf32) {
412 // Use SSE for f32, x87 for f64.
413 // Set up the FP register classes.
414 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
415 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
416
417 // Use ANDPS to simulate FABS.
418 setOperationAction(ISD::FABS , MVT::f32, Custom);
419
420 // Use XORP to simulate FNEG.
421 setOperationAction(ISD::FNEG , MVT::f32, Custom);
422
423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
424
425 // Use ANDPS and ORPS to simulate FCOPYSIGN.
426 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
427 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
428
429 // We don't support sin/cos/fmod
430 setOperationAction(ISD::FSIN , MVT::f32, Expand);
431 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000432
Nate Begemane2ba64f2008-02-14 08:57:00 +0000433 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000434 addLegalFPImmediate(APFloat(+0.0f)); // xorps
435 addLegalFPImmediate(APFloat(+0.0)); // FLD0
436 addLegalFPImmediate(APFloat(+1.0)); // FLD1
437 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
438 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
439
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000440 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
441 // this though and handle it in InstructionSelectPreprocess so that
442 // dagcombine2 can hack on these.
443 if (Fast) {
444 setConvertAction(MVT::f32, MVT::f64, Expand);
445 setConvertAction(MVT::f32, MVT::f80, Expand);
446 setConvertAction(MVT::f80, MVT::f32, Expand);
447 setConvertAction(MVT::f64, MVT::f32, Expand);
448 // And x87->x87 truncations also.
449 setConvertAction(MVT::f80, MVT::f64, Expand);
450 }
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000451
452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 } else {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000457 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 // Set up the FP register classes.
459 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
460 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
461
462 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
463 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
464 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
465 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000466
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000467 // Floating truncations go through memory. If optimizing, we lie about
468 // this though and handle it in InstructionSelectPreprocess so that
469 // dagcombine2 can hack on these.
470 if (Fast) {
471 setConvertAction(MVT::f80, MVT::f32, Expand);
472 setConvertAction(MVT::f64, MVT::f32, Expand);
473 setConvertAction(MVT::f80, MVT::f64, Expand);
474 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475
476 if (!UnsafeFPMath) {
477 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
478 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
479 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000484 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 }
489
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000490 // Long double always uses X87.
491 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000492 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattnerdd867392008-01-27 06:19:31 +0000494 {
Dale Johannesen6e547b42008-10-09 23:00:39 +0000495 bool ignored;
Chris Lattnerdd867392008-01-27 06:19:31 +0000496 APFloat TmpFlt(+0.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000497 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
498 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000499 addLegalFPImmediate(TmpFlt); // FLD0
500 TmpFlt.changeSign();
501 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
502 APFloat TmpFlt2(+1.0);
Dale Johannesen6e547b42008-10-09 23:00:39 +0000503 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
504 &ignored);
Chris Lattnerdd867392008-01-27 06:19:31 +0000505 addLegalFPImmediate(TmpFlt2); // FLD1
506 TmpFlt2.changeSign();
507 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
508 }
509
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000510 if (!UnsafeFPMath) {
511 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
512 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
513 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000514
Dan Gohman2f7b1982007-10-11 23:21:31 +0000515 // Always use a library call for pow.
516 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
517 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
518 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
519
Dale Johannesen92b33082008-09-04 00:47:13 +0000520 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000521 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000522 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000523 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000524 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
525
Mon P Wanga5a239f2008-11-06 05:31:54 +0000526 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000527 // (for widening) or expand (for scalarization). Then we will selectively
528 // turn on ones that can be effectively codegen'd.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
530 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands92c43912008-06-06 12:08:01 +0000531 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif825aa892008-08-28 23:19:51 +0000544 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
546 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands92c43912008-06-06 12:08:01 +0000547 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesen177edff2008-09-10 17:31:40 +0000569 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 }
575
576 if (Subtarget->hasMMX()) {
577 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
578 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
579 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena585daf2008-06-24 22:01:44 +0000580 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
582
583 // FIXME: add MMX packed arithmetics
584
585 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
586 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
587 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
588 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
589
590 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
591 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
592 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen6b65c332007-10-30 01:18:38 +0000593 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594
595 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
596 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
597
598 setOperationAction(ISD::AND, MVT::v8i8, Promote);
599 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
600 setOperationAction(ISD::AND, MVT::v4i16, Promote);
601 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
602 setOperationAction(ISD::AND, MVT::v2i32, Promote);
603 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
604 setOperationAction(ISD::AND, MVT::v1i64, Legal);
605
606 setOperationAction(ISD::OR, MVT::v8i8, Promote);
607 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
608 setOperationAction(ISD::OR, MVT::v4i16, Promote);
609 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
610 setOperationAction(ISD::OR, MVT::v2i32, Promote);
611 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
612 setOperationAction(ISD::OR, MVT::v1i64, Legal);
613
614 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
615 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
616 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
617 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
618 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
619 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
620 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
621
622 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
623 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
624 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
625 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
626 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
627 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena585daf2008-06-24 22:01:44 +0000628 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
629 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000630 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
631
632 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
633 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
634 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena585daf2008-06-24 22:01:44 +0000635 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000636 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
637
638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
639 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
642
Evan Cheng759fe022008-07-22 18:39:19 +0000643 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000647
648 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649 }
650
651 if (Subtarget->hasSSE1()) {
652 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
653
654 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
655 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
656 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
657 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
658 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
659 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
661 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
662 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
663 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
664 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000665 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 }
667
668 if (Subtarget->hasSSE2()) {
669 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
670 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
671 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
672 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
674
675 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
676 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
677 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
678 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
679 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
680 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
681 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
682 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
683 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
684 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
685 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
686 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
687 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
688 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
689 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690
Nate Begeman03605a02008-07-17 16:51:19 +0000691 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
692 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
693 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
694 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000695
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000696 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
697 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
699 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
701
702 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands92c43912008-06-06 12:08:01 +0000703 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
704 MVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000705 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000706 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000707 continue;
Duncan Sands92c43912008-06-06 12:08:01 +0000708 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
709 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
710 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
713 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
714 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
715 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000718 if (Subtarget->is64Bit()) {
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen2ff963d2007-10-31 00:32:36 +0000720 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000721 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722
723 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
724 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000725 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
726 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
727 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
728 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
729 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
731 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
732 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
733 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
734 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 }
736
Chris Lattner3bc08502008-01-17 19:59:44 +0000737 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000738
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 // Custom lower v2i64 and v2f64 selects.
740 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
741 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
742 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
743 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000744
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000745 }
Nate Begemand77e59e2008-02-11 04:19:36 +0000746
747 if (Subtarget->hasSSE41()) {
748 // FIXME: Do we need to handle scalar-to-vector here?
749 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohmane3731f52008-05-23 17:49:40 +0000750 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000751
752 // i8 and i16 vectors are custom , because the source register and source
753 // source memory operand types are not the same width. f32 vectors are
754 // custom since the immediate controlling the insert encodes additional
755 // information.
756 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
760
761 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
762 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
763 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng6c249332008-03-24 21:52:23 +0000764 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000765
766 if (Subtarget->is64Bit()) {
Nate Begeman4294c1f2008-02-12 22:51:28 +0000767 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
768 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000769 }
770 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000771
Nate Begeman03605a02008-07-17 16:51:19 +0000772 if (Subtarget->hasSSE42()) {
773 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
774 }
775
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000776 // We want to custom lower some of our intrinsics.
777 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
778
779 // We have target-specific dag combine patterns for the following nodes:
780 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000781 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000782 setTargetDAGCombine(ISD::SELECT);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000783 setTargetDAGCombine(ISD::STORE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784
785 computeRegisterProperties();
786
787 // FIXME: These should be based on subtarget info. Plus, the values should
788 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +0000789 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
790 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
791 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Cheng45c1edb2008-02-28 00:43:03 +0000793 setPrefLoopAlignment(16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794}
795
Scott Michel502151f2008-03-10 15:42:14 +0000796
Dan Gohman8181bd12008-07-27 21:46:04 +0000797MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel502151f2008-03-10 15:42:14 +0000798 return MVT::i8;
799}
800
801
Evan Cheng5a67b812008-01-23 23:17:41 +0000802/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
803/// the desired ByVal argument alignment.
804static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
805 if (MaxAlign == 16)
806 return;
807 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
808 if (VTy->getBitWidth() == 128)
809 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +0000810 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
811 unsigned EltAlign = 0;
812 getMaxByValAlign(ATy->getElementType(), EltAlign);
813 if (EltAlign > MaxAlign)
814 MaxAlign = EltAlign;
815 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
816 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
817 unsigned EltAlign = 0;
818 getMaxByValAlign(STy->getElementType(i), EltAlign);
819 if (EltAlign > MaxAlign)
820 MaxAlign = EltAlign;
821 if (MaxAlign == 16)
822 break;
823 }
824 }
825 return;
826}
827
828/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
829/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +0000830/// that contain SSE vectors are placed at 16-byte boundaries while the rest
831/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +0000832unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000833 if (Subtarget->is64Bit()) {
834 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +0000835 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +0000836 if (TyAlign > 8)
837 return TyAlign;
838 return 8;
839 }
840
Evan Cheng5a67b812008-01-23 23:17:41 +0000841 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +0000842 if (Subtarget->hasSSE1())
843 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +0000844 return Align;
845}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846
Evan Cheng8c590372008-05-15 08:39:06 +0000847/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +0000848/// and store operations as a result of memset, memcpy, and memmove
849/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +0000850/// determining it.
Duncan Sands92c43912008-06-06 12:08:01 +0000851MVT
Evan Cheng8c590372008-05-15 08:39:06 +0000852X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
853 bool isSrcConst, bool isSrcStr) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +0000854 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
855 // linux. This is because the stack realignment code can't handle certain
856 // cases like PR2962. This should be removed when PR2962 is fixed.
857 if (Subtarget->getStackAlignment() >= 16) {
858 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
859 return MVT::v4i32;
860 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
861 return MVT::v4f32;
862 }
Evan Cheng8c590372008-05-15 08:39:06 +0000863 if (Subtarget->is64Bit() && Size >= 8)
864 return MVT::i64;
865 return MVT::i32;
866}
867
868
Evan Cheng6fb06762007-11-09 01:32:10 +0000869/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
870/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +0000871SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +0000872 SelectionDAG &DAG) const {
873 if (usesGlobalOffsetTable())
874 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
875 if (!Subtarget->isPICStyleRIPRel())
876 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
877 return Table;
878}
879
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880//===----------------------------------------------------------------------===//
881// Return Value Calling Convention Implementation
882//===----------------------------------------------------------------------===//
883
884#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000885
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000886/// LowerRET - Lower an ISD::RET node.
Dan Gohman8181bd12008-07-27 21:46:04 +0000887SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
889
890 SmallVector<CCValAssign, 16> RVLocs;
891 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
892 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
893 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greif1c80d112008-08-28 21:40:38 +0000894 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000895
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 // If this is the first return lowered for this function, add the regs to the
897 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +0000898 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 for (unsigned i = 0; i != RVLocs.size(); ++i)
900 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +0000901 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000903 SDValue Chain = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000905 // Handle tail call return.
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000906 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000907 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000908 SDValue TailCall = Chain;
909 SDValue TargetAddress = TailCall.getOperand(1);
910 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerf8decf52008-01-16 05:52:18 +0000911 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +0000912 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000913 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendlingfef06052008-09-16 21:48:12 +0000914 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000915 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
916 "Expecting an global address, external symbol, or register");
Chris Lattnerf8decf52008-01-16 05:52:18 +0000917 assert(StackAdjustment.getOpcode() == ISD::Constant &&
918 "Expecting a const value");
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000919
Dan Gohman8181bd12008-07-27 21:46:04 +0000920 SmallVector<SDValue,8> Operands;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000921 Operands.push_back(Chain.getOperand(0));
922 Operands.push_back(TargetAddress);
923 Operands.push_back(StackAdjustment);
924 // Copy registers used by the call. Last operand is a flag so it is not
925 // copied.
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000926 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000927 Operands.push_back(Chain.getOperand(i));
928 }
Arnold Schwaighofer10202b32007-10-16 09:05:00 +0000929 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
930 Operands.size());
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000931 }
932
933 // Regular return.
Dan Gohman8181bd12008-07-27 21:46:04 +0000934 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +0000935
Dan Gohman8181bd12008-07-27 21:46:04 +0000936 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +0000937 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
938 // Operand #1 = Bytes To Pop
939 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
940
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000941 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000942 for (unsigned i = 0; i != RVLocs.size(); ++i) {
943 CCValAssign &VA = RVLocs[i];
944 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman8181bd12008-07-27 21:46:04 +0000945 SDValue ValToCopy = Op.getOperand(i*2+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000946
Chris Lattnerb56cc342008-03-11 03:23:40 +0000947 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
948 // the RET instruction and handled by the FP Stackifier.
949 if (RVLocs[i].getLocReg() == X86::ST0 ||
950 RVLocs[i].getLocReg() == X86::ST1) {
951 // If this is a copy from an xmm register to ST(0), use an FPExtend to
952 // change the value to the FP stack register class.
953 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
954 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
955 RetOps.push_back(ValToCopy);
956 // Don't emit a copytoreg.
957 continue;
958 }
Dale Johannesena585daf2008-06-24 22:01:44 +0000959
Chris Lattnere22e1fb2008-03-10 21:08:41 +0000960 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961 Flag = Chain.getValue(1);
962 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000963
964 // The x86-64 ABI for returning structs by value requires that we copy
965 // the sret argument into %rax for the return. We saved the argument into
966 // a virtual register in the entry block, so now we copy the value out
967 // and into %rax.
968 if (Subtarget->is64Bit() &&
969 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
970 MachineFunction &MF = DAG.getMachineFunction();
971 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
972 unsigned Reg = FuncInfo->getSRetReturnReg();
973 if (!Reg) {
974 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
975 FuncInfo->setSRetReturnReg(Reg);
976 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000977 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +0000978
979 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
980 Flag = Chain.getValue(1);
981 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982
Chris Lattnerb56cc342008-03-11 03:23:40 +0000983 RetOps[0] = Chain; // Update chain.
984
985 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +0000986 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +0000987 RetOps.push_back(Flag);
988
989 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990}
991
992
993/// LowerCallResult - Lower the result values of an ISD::CALL into the
994/// appropriate copies out of appropriate physical registers. This assumes that
995/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
996/// being lowered. The returns a SDNode with the same number of values as the
997/// ISD::CALL.
998SDNode *X86TargetLowering::
Dan Gohman705e3f72008-09-13 01:54:27 +0000999LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001000 unsigned CallingConv, SelectionDAG &DAG) {
1001
1002 // Assign locations to each value returned by this call.
1003 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman705e3f72008-09-13 01:54:27 +00001004 bool isVarArg = TheCall->isVarArg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
1006 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1007
Dan Gohman8181bd12008-07-27 21:46:04 +00001008 SmallVector<SDValue, 8> ResultVals;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009
1010 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001011 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +00001012 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001013
1014 // If this is a call to a function that returns an fp value on the floating
1015 // point stack, but where we prefer to use the value in xmm registers, copy
1016 // 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 +00001017 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1018 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001019 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1020 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001022
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001023 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1024 CopyVT, InFlag).getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00001025 SDValue Val = Chain.getValue(0);
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001026 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001027
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001028 if (CopyVT != RVLocs[i].getValVT()) {
1029 // Round the F80 the right size, which also moves to the appropriate xmm
1030 // register.
1031 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1032 // This truncation won't change the value.
1033 DAG.getIntPtrConstant(1));
1034 }
Chris Lattnerdec9cb52008-01-24 08:07:48 +00001035
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001036 ResultVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001037 }
Duncan Sands698842f2008-07-02 17:40:58 +00001038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001039 // Merge everything together with a MERGE_VALUES node.
1040 ResultVals.push_back(Chain);
Duncan Sandsf19591c2008-06-30 10:19:09 +00001041 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greif1c80d112008-08-28 21:40:38 +00001042 ResultVals.size()).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001043}
1044
1045
1046//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001047// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001048//===----------------------------------------------------------------------===//
1049// StdCall calling convention seems to be standard for many Windows' API
1050// routines and around. It differs from C calling convention just a little:
1051// callee should clean up the stack, not caller. Symbols should be also
1052// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001053// For info on fast calling convention see Fast Calling Convention (tail call)
1054// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001055
1056/// AddLiveIn - This helper function adds the specified physical register to the
1057/// MachineFunction as a live in value. It also creates a corresponding virtual
1058/// register for it.
1059static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
1060 const TargetRegisterClass *RC) {
1061 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner1b989192007-12-31 04:13:23 +00001062 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1063 MF.getRegInfo().addLiveIn(PReg, VReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 return VReg;
1065}
1066
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001067/// CallIsStructReturn - Determines whether a CALL node uses struct return
1068/// semantics.
Dan Gohman705e3f72008-09-13 01:54:27 +00001069static bool CallIsStructReturn(CallSDNode *TheCall) {
1070 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001071 if (!NumOps)
1072 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001073
Dan Gohman705e3f72008-09-13 01:54:27 +00001074 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001075}
1076
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001077/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1078/// return semantics.
Dan Gohman8181bd12008-07-27 21:46:04 +00001079static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001080 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001081 if (!NumArgs)
1082 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001083
1084 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001085}
1086
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001087/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1088/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001089/// calls.
Dan Gohman705e3f72008-09-13 01:54:27 +00001090bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001091 if (IsVarArg)
1092 return false;
1093
Dan Gohman705e3f72008-09-13 01:54:27 +00001094 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001095 default:
1096 return false;
1097 case CallingConv::X86_StdCall:
1098 return !Subtarget->is64Bit();
1099 case CallingConv::X86_FastCall:
1100 return !Subtarget->is64Bit();
1101 case CallingConv::Fast:
1102 return PerformTailCallOpt;
1103 }
1104}
1105
Dan Gohman705e3f72008-09-13 01:54:27 +00001106/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1107/// given CallingConvention value.
1108CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001109 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001110 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001111 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001112 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1113 return CC_X86_64_TailCall;
1114 else
1115 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001116 }
1117
Gordon Henriksen18ace102008-01-05 16:56:59 +00001118 if (CC == CallingConv::X86_FastCall)
1119 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001120 else if (CC == CallingConv::Fast)
1121 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001122 else
1123 return CC_X86_32_C;
1124}
1125
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001126/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1127/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001128NameDecorationStyle
Dan Gohman8181bd12008-07-27 21:46:04 +00001129X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001130 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001131 if (CC == CallingConv::X86_FastCall)
1132 return FastCall;
1133 else if (CC == CallingConv::X86_StdCall)
1134 return StdCall;
1135 return None;
1136}
1137
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001138
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001139/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1140/// in a register before calling.
1141bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1142 return !IsTailCall && !Is64Bit &&
1143 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1144 Subtarget->isPICStyleGOT();
1145}
1146
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001147/// CallRequiresFnAddressInReg - Check whether the call requires the function
1148/// address to be loaded in a register.
1149bool
1150X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1151 return !Is64Bit && IsTailCall &&
1152 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1153 Subtarget->isPICStyleGOT();
1154}
1155
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001156/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1157/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001158/// the specific parameter attribute. The copy will be passed as a byval
1159/// function parameter.
Dan Gohman8181bd12008-07-27 21:46:04 +00001160static SDValue
1161CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sandsc93fae32008-03-21 09:14:45 +00001162 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001163 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohmane8b391e2008-04-12 04:36:06 +00001164 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001165 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001166}
1167
Dan Gohman8181bd12008-07-27 21:46:04 +00001168SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001169 const CCValAssign &VA,
1170 MachineFrameInfo *MFI,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001171 unsigned CC,
Dan Gohman8181bd12008-07-27 21:46:04 +00001172 SDValue Root, unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001173 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sandsc93fae32008-03-21 09:14:45 +00001174 ISD::ArgFlagsTy Flags =
1175 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001176 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001177 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Cheng3e42a522008-01-10 02:24:25 +00001178
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001179 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1180 // changed with more analysis.
1181 // In case of tail call optimization mark all arguments mutable. Since they
1182 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands92c43912008-06-06 12:08:01 +00001183 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001184 VA.getLocMemOffset(), isImmutable);
Dan Gohman8181bd12008-07-27 21:46:04 +00001185 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001186 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001187 return FIN;
Dan Gohman12a9c082008-02-06 22:27:42 +00001188 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001189 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001190}
1191
Dan Gohman8181bd12008-07-27 21:46:04 +00001192SDValue
1193X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001194 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001195 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1196
1197 const Function* Fn = MF.getFunction();
1198 if (Fn->hasExternalLinkage() &&
1199 Subtarget->isTargetCygMing() &&
1200 Fn->getName() == "main")
1201 FuncInfo->setForceFramePointer(true);
1202
1203 // Decorate the function name.
1204 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman8181bd12008-07-27 21:46:04 +00001207 SDValue Root = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001208 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001209 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001210 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001211 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001212
1213 assert(!(isVarArg && CC == CallingConv::Fast) &&
1214 "Var args not supported with calling convention fastcc");
1215
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001216 // Assign locations to all of the incoming arguments.
1217 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001218 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001219 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001220
Dan Gohman8181bd12008-07-27 21:46:04 +00001221 SmallVector<SDValue, 8> ArgValues;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001222 unsigned LastVal = ~0U;
1223 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1224 CCValAssign &VA = ArgLocs[i];
1225 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1226 // places.
1227 assert(VA.getValNo() != LastVal &&
1228 "Don't support value assigned to multiple locs yet");
1229 LastVal = VA.getValNo();
1230
1231 if (VA.isRegLoc()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001232 MVT RegVT = VA.getLocVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 TargetRegisterClass *RC;
1234 if (RegVT == MVT::i32)
1235 RC = X86::GR32RegisterClass;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001236 else if (Is64Bit && RegVT == MVT::i64)
1237 RC = X86::GR64RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001238 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001239 RC = X86::FR32RegisterClass;
Dale Johannesen51552f62008-02-05 20:46:33 +00001240 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001241 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001242 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001243 RC = X86::VR128RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001244 else if (RegVT.isVector()) {
1245 assert(RegVT.getSizeInBits() == 64);
Evan Chengf5af6fe2008-04-25 07:56:45 +00001246 if (!Is64Bit)
1247 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1248 else {
1249 // Darwin calling convention passes MMX values in either GPRs or
1250 // XMMs in x86-64. Other targets pass them in memory.
1251 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1252 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1253 RegVT = MVT::v2i64;
1254 } else {
1255 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1256 RegVT = MVT::i64;
1257 }
1258 }
1259 } else {
1260 assert(0 && "Unknown argument type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001261 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001262
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman8181bd12008-07-27 21:46:04 +00001264 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265
1266 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1267 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1268 // right size.
1269 if (VA.getLocInfo() == CCValAssign::SExt)
1270 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1271 DAG.getValueType(VA.getValVT()));
1272 else if (VA.getLocInfo() == CCValAssign::ZExt)
1273 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1274 DAG.getValueType(VA.getValVT()));
1275
1276 if (VA.getLocInfo() != CCValAssign::Full)
1277 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1278
Gordon Henriksen18ace102008-01-05 16:56:59 +00001279 // Handle MMX values passed in GPRs.
Evan Chengad6980b2008-04-25 20:13:28 +00001280 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands92c43912008-06-06 12:08:01 +00001281 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Chengad6980b2008-04-25 20:13:28 +00001282 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1283 else if (RC == X86::VR128RegisterClass) {
1284 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1285 DAG.getConstant(0, MVT::i64));
1286 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1287 }
1288 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001289
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 ArgValues.push_back(ArgValue);
1291 } else {
1292 assert(VA.isMemLoc());
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001293 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001294 }
1295 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001296
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001297 // The x86-64 ABI for returning structs by value requires that we copy
1298 // the sret argument into %rax for the return. Save the argument into
1299 // a virtual register so that we can access it from the return points.
1300 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1301 MachineFunction &MF = DAG.getMachineFunction();
1302 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1303 unsigned Reg = FuncInfo->getSRetReturnReg();
1304 if (!Reg) {
1305 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1306 FuncInfo->setSRetReturnReg(Reg);
1307 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001308 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001309 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1310 }
1311
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001313 // align stack specially for tail calls
Evan Chengded8f902008-09-07 09:07:23 +00001314 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001315 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316
1317 // If the function takes variable number of arguments, make a frame index for
1318 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001319 if (isVarArg) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001320 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1321 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1322 }
1323 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001324 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1325
1326 // FIXME: We should really autogenerate these arrays
1327 static const unsigned GPR64ArgRegsWin64[] = {
1328 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001329 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001330 static const unsigned XMMArgRegsWin64[] = {
1331 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1332 };
1333 static const unsigned GPR64ArgRegs64Bit[] = {
1334 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1335 };
1336 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001337 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1338 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1339 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001340 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1341
1342 if (IsWin64) {
1343 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1344 GPR64ArgRegs = GPR64ArgRegsWin64;
1345 XMMArgRegs = XMMArgRegsWin64;
1346 } else {
1347 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1348 GPR64ArgRegs = GPR64ArgRegs64Bit;
1349 XMMArgRegs = XMMArgRegs64Bit;
1350 }
1351 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1352 TotalNumIntRegs);
1353 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1354 TotalNumXMMRegs);
1355
Gordon Henriksen18ace102008-01-05 16:56:59 +00001356 // For X86-64, if there are vararg parameters that are passed via
1357 // registers, then we must store them to their spots on the stack so they
1358 // may be loaded by deferencing the result of va_next.
1359 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001360 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1361 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1362 TotalNumXMMRegs * 16, 16);
1363
Gordon Henriksen18ace102008-01-05 16:56:59 +00001364 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001365 SmallVector<SDValue, 8> MemOps;
1366 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1367 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001368 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001369 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1371 X86::GR64RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001372 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1373 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001374 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001375 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001376 MemOps.push_back(Store);
1377 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001378 DAG.getIntPtrConstant(8));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001379 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001380
Gordon Henriksen18ace102008-01-05 16:56:59 +00001381 // Now store the XMM (fp + vector) parameter registers.
1382 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001383 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001384 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001385 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1386 X86::VR128RegisterClass);
Dan Gohman8181bd12008-07-27 21:46:04 +00001387 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1388 SDValue Store =
Dan Gohman12a9c082008-02-06 22:27:42 +00001389 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001390 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001391 MemOps.push_back(Store);
1392 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner5872a362008-01-17 07:00:52 +00001393 DAG.getIntPtrConstant(16));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001394 }
1395 if (!MemOps.empty())
1396 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1397 &MemOps[0], MemOps.size());
1398 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001399 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001400
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001401 ArgValues.push_back(Root);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001402
Gordon Henriksen18ace102008-01-05 16:56:59 +00001403 // Some CCs need callee pop.
Dan Gohman705e3f72008-09-13 01:54:27 +00001404 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001405 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001406 BytesCallerReserves = 0;
1407 } else {
1408 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001409 // If this is an sret function, the return should pop the hidden pointer.
Evan Chenga9d15b92008-09-10 18:25:29 +00001410 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001411 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 BytesCallerReserves = StackSize;
1413 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001414
Gordon Henriksen18ace102008-01-05 16:56:59 +00001415 if (!Is64Bit) {
1416 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1417 if (CC == CallingConv::X86_FastCall)
1418 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1419 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420
Anton Korobeynikove844e472007-08-15 17:12:32 +00001421 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001422
1423 // Return the new list of results.
Gabor Greif1c80d112008-08-28 21:40:38 +00001424 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif46bf5472008-08-26 22:36:50 +00001425 ArgValues.size()).getValue(Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426}
1427
Dan Gohman8181bd12008-07-27 21:46:04 +00001428SDValue
Dan Gohman705e3f72008-09-13 01:54:27 +00001429X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001430 const SDValue &StackPtr,
Evan Chengbc077bf2008-01-10 00:09:10 +00001431 const CCValAssign &VA,
Dan Gohman8181bd12008-07-27 21:46:04 +00001432 SDValue Chain,
Dan Gohman705e3f72008-09-13 01:54:27 +00001433 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman1190f3a2008-02-07 16:28:05 +00001434 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001435 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001436 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001437 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001438 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengbc077bf2008-01-10 00:09:10 +00001439 }
Dan Gohman1190f3a2008-02-07 16:28:05 +00001440 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001441 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001442}
1443
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001444/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1445/// optimization is performed and it is required.
Dan Gohman8181bd12008-07-27 21:46:04 +00001446SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001447X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001448 SDValue &OutRetAddr,
1449 SDValue Chain,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001450 bool IsTailCall,
1451 bool Is64Bit,
1452 int FPDiff) {
1453 if (!IsTailCall || FPDiff==0) return Chain;
1454
1455 // Adjust the Return address stack slot.
Duncan Sands92c43912008-06-06 12:08:01 +00001456 MVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001457 OutRetAddr = getReturnAddressFrameIndex(DAG);
1458 // Load the "old" Return address.
1459 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001460 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001461}
1462
1463/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1464/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman8181bd12008-07-27 21:46:04 +00001465static SDValue
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001466EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001467 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001468 bool Is64Bit, int FPDiff) {
1469 // Store the return address to the appropriate stack slot.
1470 if (!FPDiff) return Chain;
1471 // Calculate the new stack slot for the return address.
1472 int SlotSize = Is64Bit ? 8 : 4;
1473 int NewReturnAddrFI =
1474 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands92c43912008-06-06 12:08:01 +00001475 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001476 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001477 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001478 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001479 return Chain;
1480}
1481
Dan Gohman8181bd12008-07-27 21:46:04 +00001482SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001483 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman705e3f72008-09-13 01:54:27 +00001484 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1485 SDValue Chain = TheCall->getChain();
1486 unsigned CC = TheCall->getCallingConv();
1487 bool isVarArg = TheCall->isVarArg();
1488 bool IsTailCall = TheCall->isTailCall() &&
1489 CC == CallingConv::Fast && PerformTailCallOpt;
1490 SDValue Callee = TheCall->getCallee();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001491 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman705e3f72008-09-13 01:54:27 +00001492 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001493
1494 assert(!(isVarArg && CC == CallingConv::Fast) &&
1495 "Var args not supported with calling convention fastcc");
1496
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001497 // Analyze operands of the call, assigning locations to each operand.
1498 SmallVector<CCValAssign, 16> ArgLocs;
1499 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman705e3f72008-09-13 01:54:27 +00001500 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501
1502 // Get a count of how many bytes are to be pushed on the stack.
1503 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofere91fdbf2008-09-11 20:28:43 +00001504 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001505 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506
Gordon Henriksen18ace102008-01-05 16:56:59 +00001507 int FPDiff = 0;
1508 if (IsTailCall) {
1509 // Lower arguments at fp - stackoffset + fpdiff.
1510 unsigned NumBytesCallerPushed =
1511 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1512 FPDiff = NumBytesCallerPushed - NumBytes;
1513
1514 // Set the delta of movement of the returnaddr stackslot.
1515 // But only set if delta is greater than previous delta.
1516 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1517 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1518 }
1519
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001520 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521
Dan Gohman8181bd12008-07-27 21:46:04 +00001522 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001523 // Load return adress for tail calls.
1524 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1525 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001526
Dan Gohman8181bd12008-07-27 21:46:04 +00001527 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1528 SmallVector<SDValue, 8> MemOpChains;
1529 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001530
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001531 // Walk the register/memloc assignments, inserting copies/loads. In the case
1532 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1534 CCValAssign &VA = ArgLocs[i];
Dan Gohman705e3f72008-09-13 01:54:27 +00001535 SDValue Arg = TheCall->getArg(i);
1536 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1537 bool isByVal = Flags.isByVal();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001538
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001539 // Promote the value if needed.
1540 switch (VA.getLocInfo()) {
1541 default: assert(0 && "Unknown loc info!");
1542 case CCValAssign::Full: break;
1543 case CCValAssign::SExt:
1544 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1545 break;
1546 case CCValAssign::ZExt:
1547 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1548 break;
1549 case CCValAssign::AExt:
1550 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1551 break;
1552 }
1553
1554 if (VA.isRegLoc()) {
Evan Cheng2aea0b42008-04-25 19:11:04 +00001555 if (Is64Bit) {
Duncan Sands92c43912008-06-06 12:08:01 +00001556 MVT RegVT = VA.getLocVT();
1557 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng2aea0b42008-04-25 19:11:04 +00001558 switch (VA.getLocReg()) {
1559 default:
1560 break;
1561 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1562 case X86::R8: {
1563 // Special case: passing MMX values in GPR registers.
1564 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1565 break;
1566 }
1567 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1568 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1569 // Special case: passing MMX values in XMM registers.
1570 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1571 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1572 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1573 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1574 getMOVLMask(2, DAG));
1575 break;
1576 }
1577 }
1578 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1580 } else {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001581 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001582 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001583 if (StackPtr.getNode() == 0)
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001584 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1585
Dan Gohman705e3f72008-09-13 01:54:27 +00001586 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1587 Chain, Arg, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 }
1590 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591
1592 if (!MemOpChains.empty())
1593 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1594 &MemOpChains[0], MemOpChains.size());
1595
1596 // Build a sequence of copy-to-reg nodes chained together with token chain
1597 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001598 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001599 // Tail call byval lowering might overwrite argument registers so in case of
1600 // tail call optimization the copies to registers are lowered later.
1601 if (!IsTailCall)
1602 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1603 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1604 InFlag);
1605 InFlag = Chain.getValue(1);
1606 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001608 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001609 // GOT pointer.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001610 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1611 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1612 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1613 InFlag);
1614 InFlag = Chain.getValue(1);
1615 }
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001616 // If we are tail calling and generating PIC/GOT style code load the address
1617 // of the callee into ecx. The value in ecx is used as target of the tail
1618 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1619 // calls on PIC/GOT architectures. Normally we would just put the address of
1620 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1621 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer87f75262008-02-26 22:21:54 +00001622 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001623 // Note: The actual moving to ecx is done further down.
1624 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Cheng7f250d62008-09-24 00:05:32 +00001625 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001626 !G->getGlobal()->hasProtectedVisibility())
1627 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00001628 else if (isa<ExternalSymbolSDNode>(Callee))
1629 Callee = LowerExternalSymbol(Callee,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001630 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001631
Gordon Henriksen18ace102008-01-05 16:56:59 +00001632 if (Is64Bit && isVarArg) {
1633 // From AMD64 ABI document:
1634 // For calls that may call functions that use varargs or stdargs
1635 // (prototype-less calls or calls to functions containing ellipsis (...) in
1636 // the declaration) %al is used as hidden argument to specify the number
1637 // of SSE registers used. The contents of %al do not need to match exactly
1638 // the number of registers, but must be an ubound on the number of SSE
1639 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001640
1641 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001642 // Count the number of XMM registers allocated.
1643 static const unsigned XMMArgRegs[] = {
1644 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1645 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1646 };
1647 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1648
1649 Chain = DAG.getCopyToReg(Chain, X86::AL,
1650 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1651 InFlag = Chain.getValue(1);
1652 }
1653
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001654
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001655 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001656 if (IsTailCall) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001657 SmallVector<SDValue, 8> MemOpChains2;
1658 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001659 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001660 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001661 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001662 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1663 CCValAssign &VA = ArgLocs[i];
1664 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001665 assert(VA.isMemLoc());
Dan Gohman705e3f72008-09-13 01:54:27 +00001666 SDValue Arg = TheCall->getArg(i);
1667 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001668 // Create frame index.
1669 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001670 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001671 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001672 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001673
Duncan Sandsc93fae32008-03-21 09:14:45 +00001674 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001675 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001676 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001677 if (StackPtr.getNode() == 0)
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001678 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1679 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1680
1681 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng5817a0e2008-01-12 01:08:07 +00001682 Flags, DAG));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001683 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001684 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001685 MemOpChains2.push_back(
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001686 DAG.getStore(Chain, Arg, FIN,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00001687 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001688 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001689 }
1690 }
1691
1692 if (!MemOpChains2.empty())
1693 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001694 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001695
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001696 // Copy arguments to their registers.
1697 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1698 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1699 InFlag);
1700 InFlag = Chain.getValue(1);
1701 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001702 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001703
Gordon Henriksen18ace102008-01-05 16:56:59 +00001704 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001705 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1706 FPDiff);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 }
1708
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001709 // If the callee is a GlobalAddress node (quite common, every direct call is)
1710 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1711 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1712 // We should use extra load for direct calls to dllimported functions in
1713 // non-JIT mode.
Evan Cheng1f282202008-07-16 01:34:02 +00001714 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1715 getTargetMachine(), true))
Dan Gohman36322c72008-10-18 02:06:02 +00001716 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1717 G->getOffset());
Bill Wendlingfef06052008-09-16 21:48:12 +00001718 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1719 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001720 } else if (IsTailCall) {
Arnold Schwaighofer4da27f62008-09-22 14:50:07 +00001721 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001722
1723 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001724 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00001725 Callee,InFlag);
1726 Callee = DAG.getRegister(Opc, getPointerTy());
1727 // Add register as live out.
1728 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001729 }
1730
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 // Returns a chain & a flag for retval copy to use.
1732 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00001733 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001734
1735 if (IsTailCall) {
1736 Ops.push_back(Chain);
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001737 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1738 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greif1c80d112008-08-28 21:40:38 +00001739 if (InFlag.getNode())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001740 Ops.push_back(InFlag);
1741 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1742 InFlag = Chain.getValue(1);
1743
1744 // Returns a chain & a flag for retval copy to use.
1745 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1746 Ops.clear();
1747 }
1748
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 Ops.push_back(Chain);
1750 Ops.push_back(Callee);
1751
Gordon Henriksen18ace102008-01-05 16:56:59 +00001752 if (IsTailCall)
1753 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754
Gordon Henriksen18ace102008-01-05 16:56:59 +00001755 // Add argument registers to the end of the list so that they are known live
1756 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00001757 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1758 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1759 RegsToPass[i].second.getValueType()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001760
Evan Cheng8ba45e62008-03-18 23:36:35 +00001761 // Add an implicit use GOT pointer in EBX.
1762 if (!IsTailCall && !Is64Bit &&
1763 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1764 Subtarget->isPICStyleGOT())
1765 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1766
1767 // Add an implicit use of AL for x86 vararg functions.
1768 if (Is64Bit && isVarArg)
1769 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1770
Gabor Greif1c80d112008-08-28 21:40:38 +00001771 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001772 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001773
Gordon Henriksen18ace102008-01-05 16:56:59 +00001774 if (IsTailCall) {
Gabor Greif1c80d112008-08-28 21:40:38 +00001775 assert(InFlag.getNode() &&
Gordon Henriksen18ace102008-01-05 16:56:59 +00001776 "Flag must be set. Depend on flag being set in LowerRET");
1777 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman705e3f72008-09-13 01:54:27 +00001778 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001779
Gabor Greif1c80d112008-08-28 21:40:38 +00001780 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001781 }
1782
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001783 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 InFlag = Chain.getValue(1);
1785
1786 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001787 unsigned NumBytesForCalleeToPush;
Dan Gohman705e3f72008-09-13 01:54:27 +00001788 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen18ace102008-01-05 16:56:59 +00001789 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chenga9d15b92008-09-10 18:25:29 +00001790 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001791 // If this is is a call to a struct-return function, the callee
1792 // pops the hidden struct pointer, so we have to push it back.
1793 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001794 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001795 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001796 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001797
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00001799 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001800 DAG.getIntPtrConstant(NumBytes, true),
1801 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1802 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00001803 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 InFlag = Chain.getValue(1);
1805
1806 // Handle result values, copying them out of physregs into vregs that we
1807 // return.
Dan Gohman705e3f72008-09-13 01:54:27 +00001808 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif825aa892008-08-28 23:19:51 +00001809 Op.getResNo());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001810}
1811
1812
1813//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001814// Fast Calling Convention (tail call) implementation
1815//===----------------------------------------------------------------------===//
1816
1817// Like std call, callee cleans arguments, convention except that ECX is
1818// reserved for storing the tail called function address. Only 2 registers are
1819// free for argument passing (inreg). Tail call optimization is performed
1820// provided:
1821// * tailcallopt is enabled
1822// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001823// On X86_64 architecture with GOT-style position independent code only local
1824// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001825// To keep the stack aligned according to platform abi the function
1826// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1827// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001828// If a tail called function callee has more arguments than the caller the
1829// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00001830// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001831// original REtADDR, but before the saved framepointer or the spilled registers
1832// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1833// stack layout:
1834// arg1
1835// arg2
1836// RETADDR
1837// [ new RETADDR
1838// move area ]
1839// (possible EBP)
1840// ESI
1841// EDI
1842// local1 ..
1843
1844/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1845/// for a 16 byte align requirement.
1846unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1847 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00001848 MachineFunction &MF = DAG.getMachineFunction();
1849 const TargetMachine &TM = MF.getTarget();
1850 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1851 unsigned StackAlignment = TFI.getStackAlignment();
1852 uint64_t AlignMask = StackAlignment - 1;
1853 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001854 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00001855 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1856 // Number smaller than 12 so just add the difference.
1857 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1858 } else {
1859 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1860 Offset = ((~AlignMask) & Offset) + StackAlignment +
1861 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001862 }
Evan Chengded8f902008-09-07 09:07:23 +00001863 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001864}
1865
1866/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Chenge7a87392007-11-02 01:26:22 +00001867/// following the call is a return. A function is eligible if caller/callee
1868/// calling conventions match, currently only fastcc supports tail calls, and
1869/// the function CALL is immediatly followed by a RET.
Dan Gohman705e3f72008-09-13 01:54:27 +00001870bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman8181bd12008-07-27 21:46:04 +00001871 SDValue Ret,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001872 SelectionDAG& DAG) const {
Evan Chenge7a87392007-11-02 01:26:22 +00001873 if (!PerformTailCallOpt)
1874 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001875
Dan Gohman705e3f72008-09-13 01:54:27 +00001876 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001877 MachineFunction &MF = DAG.getMachineFunction();
1878 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman705e3f72008-09-13 01:54:27 +00001879 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001880 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman705e3f72008-09-13 01:54:27 +00001881 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001882 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Chenge7a87392007-11-02 01:26:22 +00001883 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001884 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Chenge7a87392007-11-02 01:26:22 +00001885 return true;
1886
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00001887 // Can only do local tail calls (in same module, hidden or protected) on
1888 // x86_64 PIC/GOT at the moment.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001889 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1890 return G->getGlobal()->hasHiddenVisibility()
1891 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001892 }
1893 }
Evan Chenge7a87392007-11-02 01:26:22 +00001894
1895 return false;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001896}
1897
Dan Gohmanca4857a2008-09-03 23:12:08 +00001898FastISel *
1899X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00001900 MachineModuleInfo *mmo,
Dan Gohmanca4857a2008-09-03 23:12:08 +00001901 DenseMap<const Value *, unsigned> &vm,
1902 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00001903 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00001904 DenseMap<const AllocaInst *, int> &am
1905#ifndef NDEBUG
1906 , SmallSet<Instruction*, 8> &cil
1907#endif
1908 ) {
1909 return X86::createFastISel(mf, mmo, vm, bm, am
1910#ifndef NDEBUG
1911 , cil
1912#endif
1913 );
Dan Gohman97805ee2008-08-19 21:32:53 +00001914}
1915
1916
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001917//===----------------------------------------------------------------------===//
1918// Other Lowering Hooks
1919//===----------------------------------------------------------------------===//
1920
1921
Dan Gohman8181bd12008-07-27 21:46:04 +00001922SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00001923 MachineFunction &MF = DAG.getMachineFunction();
1924 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1925 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001926 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikove844e472007-08-15 17:12:32 +00001927
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001928 if (ReturnAddrIndex == 0) {
1929 // Set up a frame object for the return address.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001930 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikove844e472007-08-15 17:12:32 +00001931 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932 }
1933
1934 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1935}
1936
1937
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1939/// specific condition code. It returns a false if it cannot do a direct
1940/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1941/// needed.
1942static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman8181bd12008-07-27 21:46:04 +00001943 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001944 SelectionDAG &DAG) {
1945 X86CC = X86::COND_INVALID;
1946 if (!isFP) {
1947 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1948 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1949 // X > -1 -> X == 0, jump !sign.
1950 RHS = DAG.getConstant(0, RHS.getValueType());
1951 X86CC = X86::COND_NS;
1952 return true;
1953 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1954 // X < 0 -> X == 0, jump on sign.
1955 X86CC = X86::COND_S;
1956 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00001957 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00001958 // X < 1 -> X <= 0
1959 RHS = DAG.getConstant(0, RHS.getValueType());
1960 X86CC = X86::COND_LE;
1961 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001962 }
1963 }
1964
1965 switch (SetCCOpcode) {
1966 default: break;
1967 case ISD::SETEQ: X86CC = X86::COND_E; break;
1968 case ISD::SETGT: X86CC = X86::COND_G; break;
1969 case ISD::SETGE: X86CC = X86::COND_GE; break;
1970 case ISD::SETLT: X86CC = X86::COND_L; break;
1971 case ISD::SETLE: X86CC = X86::COND_LE; break;
1972 case ISD::SETNE: X86CC = X86::COND_NE; break;
1973 case ISD::SETULT: X86CC = X86::COND_B; break;
1974 case ISD::SETUGT: X86CC = X86::COND_A; break;
1975 case ISD::SETULE: X86CC = X86::COND_BE; break;
1976 case ISD::SETUGE: X86CC = X86::COND_AE; break;
1977 }
1978 } else {
Duncan Sandsc2a04622008-10-24 13:03:10 +00001979 // First determine if it is required or is profitable to flip the operands.
1980
1981 // If LHS is a foldable load, but RHS is not, flip the condition.
1982 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1983 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1984 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1985 std::swap(LHS, RHS);
1986 }
1987
Evan Chengb488ca32008-08-29 23:22:12 +00001988 switch (SetCCOpcode) {
1989 default: break;
1990 case ISD::SETOLT:
1991 case ISD::SETOLE:
1992 case ISD::SETUGT:
1993 case ISD::SETUGE:
Duncan Sandsc2a04622008-10-24 13:03:10 +00001994 std::swap(LHS, RHS);
Evan Chengb488ca32008-08-29 23:22:12 +00001995 break;
1996 }
1997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 // On a floating point condition, the flags are set as follows:
1999 // ZF PF CF op
2000 // 0 | 0 | 0 | X > Y
2001 // 0 | 0 | 1 | X < Y
2002 // 1 | 0 | 0 | X == Y
2003 // 1 | 1 | 1 | unordered
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002004 switch (SetCCOpcode) {
2005 default: break;
2006 case ISD::SETUEQ:
Evan Chengb488ca32008-08-29 23:22:12 +00002007 case ISD::SETEQ:
2008 X86CC = X86::COND_E;
2009 break;
2010 case ISD::SETOLT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 case ISD::SETOGT:
Evan Chengb488ca32008-08-29 23:22:12 +00002012 case ISD::SETGT:
2013 X86CC = X86::COND_A;
2014 break;
2015 case ISD::SETOLE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002016 case ISD::SETOGE:
Evan Chengb488ca32008-08-29 23:22:12 +00002017 case ISD::SETGE:
2018 X86CC = X86::COND_AE;
2019 break;
2020 case ISD::SETUGT: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 case ISD::SETULT:
Evan Chengb488ca32008-08-29 23:22:12 +00002022 case ISD::SETLT:
2023 X86CC = X86::COND_B;
2024 break;
2025 case ISD::SETUGE: // flipped
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 case ISD::SETULE:
Evan Chengb488ca32008-08-29 23:22:12 +00002027 case ISD::SETLE:
2028 X86CC = X86::COND_BE;
2029 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 case ISD::SETONE:
Evan Chengb488ca32008-08-29 23:22:12 +00002031 case ISD::SETNE:
2032 X86CC = X86::COND_NE;
2033 break;
2034 case ISD::SETUO:
2035 X86CC = X86::COND_P;
2036 break;
2037 case ISD::SETO:
2038 X86CC = X86::COND_NP;
2039 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002040 }
Evan Chengfc937c92008-08-28 23:48:31 +00002041 }
2042
Evan Chengc6162692008-08-29 22:13:21 +00002043 return X86CC != X86::COND_INVALID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002044}
2045
2046/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2047/// code. Current x86 isa includes the following FP cmov instructions:
2048/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2049static bool hasFPCMov(unsigned X86CC) {
2050 switch (X86CC) {
2051 default:
2052 return false;
2053 case X86::COND_B:
2054 case X86::COND_BE:
2055 case X86::COND_E:
2056 case X86::COND_P:
2057 case X86::COND_A:
2058 case X86::COND_AE:
2059 case X86::COND_NE:
2060 case X86::COND_NP:
2061 return true;
2062 }
2063}
2064
2065/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
2066/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman8181bd12008-07-27 21:46:04 +00002067static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068 if (Op.getOpcode() == ISD::UNDEF)
2069 return true;
2070
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002071 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002072 return (Val >= Low && Val < Hi);
2073}
2074
2075/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2076/// true if Op is undef or if its value equal to the specified value.
Dan Gohman8181bd12008-07-27 21:46:04 +00002077static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002078 if (Op.getOpcode() == ISD::UNDEF)
2079 return true;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002080 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081}
2082
2083/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2084/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2085bool X86::isPSHUFDMask(SDNode *N) {
2086 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2087
Dan Gohman7dc19012007-08-02 21:17:01 +00002088 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002089 return false;
2090
2091 // Check if the value doesn't reference the second vector.
2092 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002093 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002094 if (Arg.getOpcode() == ISD::UNDEF) continue;
2095 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002096 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002097 return false;
2098 }
2099
2100 return true;
2101}
2102
2103/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2104/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2105bool X86::isPSHUFHWMask(SDNode *N) {
2106 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2107
2108 if (N->getNumOperands() != 8)
2109 return false;
2110
2111 // Lower quadword copied in order.
2112 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002113 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002114 if (Arg.getOpcode() == ISD::UNDEF) continue;
2115 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002116 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 return false;
2118 }
2119
2120 // Upper quadword shuffled.
2121 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002122 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002123 if (Arg.getOpcode() == ISD::UNDEF) continue;
2124 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002125 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002126 if (Val < 4 || Val > 7)
2127 return false;
2128 }
2129
2130 return true;
2131}
2132
2133/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2134/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2135bool X86::isPSHUFLWMask(SDNode *N) {
2136 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2137
2138 if (N->getNumOperands() != 8)
2139 return false;
2140
2141 // Upper quadword copied in order.
2142 for (unsigned i = 4; i != 8; ++i)
2143 if (!isUndefOrEqual(N->getOperand(i), i))
2144 return false;
2145
2146 // Lower quadword shuffled.
2147 for (unsigned i = 0; i != 4; ++i)
2148 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2149 return false;
2150
2151 return true;
2152}
2153
2154/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2155/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002156static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002157 if (NumElems != 2 && NumElems != 4) return false;
2158
2159 unsigned Half = NumElems / 2;
2160 for (unsigned i = 0; i < Half; ++i)
2161 if (!isUndefOrInRange(Elems[i], 0, NumElems))
2162 return false;
2163 for (unsigned i = Half; i < NumElems; ++i)
2164 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2165 return false;
2166
2167 return true;
2168}
2169
2170bool X86::isSHUFPMask(SDNode *N) {
2171 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2172 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2173}
2174
2175/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2176/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2177/// half elements to come from vector 1 (which would equal the dest.) and
2178/// the upper half to come from vector 2.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002179static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002180 if (NumOps != 2 && NumOps != 4) return false;
2181
2182 unsigned Half = NumOps / 2;
2183 for (unsigned i = 0; i < Half; ++i)
2184 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2185 return false;
2186 for (unsigned i = Half; i < NumOps; ++i)
2187 if (!isUndefOrInRange(Ops[i], 0, NumOps))
2188 return false;
2189 return true;
2190}
2191
2192static bool isCommutedSHUFP(SDNode *N) {
2193 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2194 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2195}
2196
2197/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2198/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2199bool X86::isMOVHLPSMask(SDNode *N) {
2200 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2201
2202 if (N->getNumOperands() != 4)
2203 return false;
2204
2205 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2206 return isUndefOrEqual(N->getOperand(0), 6) &&
2207 isUndefOrEqual(N->getOperand(1), 7) &&
2208 isUndefOrEqual(N->getOperand(2), 2) &&
2209 isUndefOrEqual(N->getOperand(3), 3);
2210}
2211
2212/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2213/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2214/// <2, 3, 2, 3>
2215bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2216 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2217
2218 if (N->getNumOperands() != 4)
2219 return false;
2220
2221 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2222 return isUndefOrEqual(N->getOperand(0), 2) &&
2223 isUndefOrEqual(N->getOperand(1), 3) &&
2224 isUndefOrEqual(N->getOperand(2), 2) &&
2225 isUndefOrEqual(N->getOperand(3), 3);
2226}
2227
2228/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2229/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2230bool X86::isMOVLPMask(SDNode *N) {
2231 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2232
2233 unsigned NumElems = N->getNumOperands();
2234 if (NumElems != 2 && NumElems != 4)
2235 return false;
2236
2237 for (unsigned i = 0; i < NumElems/2; ++i)
2238 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2239 return false;
2240
2241 for (unsigned i = NumElems/2; i < NumElems; ++i)
2242 if (!isUndefOrEqual(N->getOperand(i), i))
2243 return false;
2244
2245 return true;
2246}
2247
2248/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2249/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2250/// and MOVLHPS.
2251bool X86::isMOVHPMask(SDNode *N) {
2252 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2253
2254 unsigned NumElems = N->getNumOperands();
2255 if (NumElems != 2 && NumElems != 4)
2256 return false;
2257
2258 for (unsigned i = 0; i < NumElems/2; ++i)
2259 if (!isUndefOrEqual(N->getOperand(i), i))
2260 return false;
2261
2262 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002263 SDValue Arg = N->getOperand(i + NumElems/2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 if (!isUndefOrEqual(Arg, i + NumElems))
2265 return false;
2266 }
2267
2268 return true;
2269}
2270
2271/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2272/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002273bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 bool V2IsSplat = false) {
2275 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2276 return false;
2277
2278 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002279 SDValue BitI = Elts[i];
2280 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002281 if (!isUndefOrEqual(BitI, j))
2282 return false;
2283 if (V2IsSplat) {
2284 if (isUndefOrEqual(BitI1, NumElts))
2285 return false;
2286 } else {
2287 if (!isUndefOrEqual(BitI1, j + NumElts))
2288 return false;
2289 }
2290 }
2291
2292 return true;
2293}
2294
2295bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2296 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2297 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2298}
2299
2300/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2301/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002302bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002303 bool V2IsSplat = false) {
2304 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2305 return false;
2306
2307 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002308 SDValue BitI = Elts[i];
2309 SDValue BitI1 = Elts[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002310 if (!isUndefOrEqual(BitI, j + NumElts/2))
2311 return false;
2312 if (V2IsSplat) {
2313 if (isUndefOrEqual(BitI1, NumElts))
2314 return false;
2315 } else {
2316 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2317 return false;
2318 }
2319 }
2320
2321 return true;
2322}
2323
2324bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2325 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2326 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2327}
2328
2329/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2330/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2331/// <0, 0, 1, 1>
2332bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2333 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2334
2335 unsigned NumElems = N->getNumOperands();
2336 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2337 return false;
2338
2339 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002340 SDValue BitI = N->getOperand(i);
2341 SDValue BitI1 = N->getOperand(i+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002342
2343 if (!isUndefOrEqual(BitI, j))
2344 return false;
2345 if (!isUndefOrEqual(BitI1, j))
2346 return false;
2347 }
2348
2349 return true;
2350}
2351
2352/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2353/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2354/// <2, 2, 3, 3>
2355bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2356 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2357
2358 unsigned NumElems = N->getNumOperands();
2359 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2360 return false;
2361
2362 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002363 SDValue BitI = N->getOperand(i);
2364 SDValue BitI1 = N->getOperand(i + 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002365
2366 if (!isUndefOrEqual(BitI, j))
2367 return false;
2368 if (!isUndefOrEqual(BitI1, j))
2369 return false;
2370 }
2371
2372 return true;
2373}
2374
2375/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2376/// specifies a shuffle of elements that is suitable for input to MOVSS,
2377/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002378static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng62cdc642007-12-06 22:14:22 +00002379 if (NumElts != 2 && NumElts != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002380 return false;
2381
2382 if (!isUndefOrEqual(Elts[0], NumElts))
2383 return false;
2384
2385 for (unsigned i = 1; i < NumElts; ++i) {
2386 if (!isUndefOrEqual(Elts[i], i))
2387 return false;
2388 }
2389
2390 return true;
2391}
2392
2393bool X86::isMOVLMask(SDNode *N) {
2394 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2395 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2396}
2397
2398/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2399/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2400/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein98b8fcb2008-04-16 16:15:27 +00002401static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002402 bool V2IsSplat = false,
2403 bool V2IsUndef = false) {
2404 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2405 return false;
2406
2407 if (!isUndefOrEqual(Ops[0], 0))
2408 return false;
2409
2410 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002411 SDValue Arg = Ops[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002412 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2413 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2414 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2415 return false;
2416 }
2417
2418 return true;
2419}
2420
2421static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2422 bool V2IsUndef = false) {
2423 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2424 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2425 V2IsSplat, V2IsUndef);
2426}
2427
2428/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2429/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2430bool X86::isMOVSHDUPMask(SDNode *N) {
2431 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2432
2433 if (N->getNumOperands() != 4)
2434 return false;
2435
2436 // Expect 1, 1, 3, 3
2437 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002438 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 if (Arg.getOpcode() == ISD::UNDEF) continue;
2440 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002441 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 if (Val != 1) return false;
2443 }
2444
2445 bool HasHi = false;
2446 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002447 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002448 if (Arg.getOpcode() == ISD::UNDEF) continue;
2449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002450 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 if (Val != 3) return false;
2452 HasHi = true;
2453 }
2454
2455 // Don't use movshdup if it can be done with a shufps.
2456 return HasHi;
2457}
2458
2459/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2460/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2461bool X86::isMOVSLDUPMask(SDNode *N) {
2462 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2463
2464 if (N->getNumOperands() != 4)
2465 return false;
2466
2467 // Expect 0, 0, 2, 2
2468 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002469 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002470 if (Arg.getOpcode() == ISD::UNDEF) continue;
2471 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002472 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002473 if (Val != 0) return false;
2474 }
2475
2476 bool HasHi = false;
2477 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002478 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002479 if (Arg.getOpcode() == ISD::UNDEF) continue;
2480 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002481 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 if (Val != 2) return false;
2483 HasHi = true;
2484 }
2485
2486 // Don't use movshdup if it can be done with a shufps.
2487 return HasHi;
2488}
2489
2490/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2491/// specifies a identity operation on the LHS or RHS.
2492static bool isIdentityMask(SDNode *N, bool RHS = false) {
2493 unsigned NumElems = N->getNumOperands();
2494 for (unsigned i = 0; i < NumElems; ++i)
2495 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2496 return false;
2497 return true;
2498}
2499
2500/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2501/// a splat of a single element.
2502static bool isSplatMask(SDNode *N) {
2503 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2504
2505 // This is a splat operation if each element of the permute is the same, and
2506 // if the value doesn't reference the second vector.
2507 unsigned NumElems = N->getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002508 SDValue ElementBase;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002509 unsigned i = 0;
2510 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002511 SDValue Elt = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002512 if (isa<ConstantSDNode>(Elt)) {
2513 ElementBase = Elt;
2514 break;
2515 }
2516 }
2517
Gabor Greif1c80d112008-08-28 21:40:38 +00002518 if (!ElementBase.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002519 return false;
2520
2521 for (; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002522 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002523 if (Arg.getOpcode() == ISD::UNDEF) continue;
2524 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2525 if (Arg != ElementBase) return false;
2526 }
2527
2528 // Make sure it is a splat of the first vector operand.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002529 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002530}
2531
2532/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2533/// a splat of a single element and it's a 2 or 4 element mask.
2534bool X86::isSplatMask(SDNode *N) {
2535 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2536
2537 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2538 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2539 return false;
2540 return ::isSplatMask(N);
2541}
2542
2543/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2544/// specifies a splat of zero element.
2545bool X86::isSplatLoMask(SDNode *N) {
2546 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2547
2548 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2549 if (!isUndefOrEqual(N->getOperand(i), 0))
2550 return false;
2551 return true;
2552}
2553
Evan Chenga2497eb2008-09-25 20:50:48 +00002554/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2555/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2556bool X86::isMOVDDUPMask(SDNode *N) {
2557 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2558
2559 unsigned e = N->getNumOperands() / 2;
2560 for (unsigned i = 0; i < e; ++i)
2561 if (!isUndefOrEqual(N->getOperand(i), i))
2562 return false;
2563 for (unsigned i = 0; i < e; ++i)
2564 if (!isUndefOrEqual(N->getOperand(e+i), i))
2565 return false;
2566 return true;
2567}
2568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002569/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2570/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2571/// instructions.
2572unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2573 unsigned NumOperands = N->getNumOperands();
2574 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2575 unsigned Mask = 0;
2576 for (unsigned i = 0; i < NumOperands; ++i) {
2577 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002578 SDValue Arg = N->getOperand(NumOperands-i-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002580 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 if (Val >= NumOperands) Val -= NumOperands;
2582 Mask |= Val;
2583 if (i != NumOperands - 1)
2584 Mask <<= Shift;
2585 }
2586
2587 return Mask;
2588}
2589
2590/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2591/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2592/// instructions.
2593unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2594 unsigned Mask = 0;
2595 // 8 nodes, but we only care about the last 4.
2596 for (unsigned i = 7; i >= 4; --i) {
2597 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002598 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002599 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002600 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002601 Mask |= (Val - 4);
2602 if (i != 4)
2603 Mask <<= 2;
2604 }
2605
2606 return Mask;
2607}
2608
2609/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2610/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2611/// instructions.
2612unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2613 unsigned Mask = 0;
2614 // 8 nodes, but we only care about the first 4.
2615 for (int i = 3; i >= 0; --i) {
2616 unsigned Val = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00002617 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002618 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002619 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 Mask |= Val;
2621 if (i != 0)
2622 Mask <<= 2;
2623 }
2624
2625 return Mask;
2626}
2627
2628/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2629/// specifies a 8 element shuffle that can be broken into a pair of
2630/// PSHUFHW and PSHUFLW.
2631static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2632 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2633
2634 if (N->getNumOperands() != 8)
2635 return false;
2636
2637 // Lower quadword shuffled.
2638 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002639 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002640 if (Arg.getOpcode() == ISD::UNDEF) continue;
2641 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002642 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00002643 if (Val >= 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 return false;
2645 }
2646
2647 // Upper quadword shuffled.
2648 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002649 SDValue Arg = N->getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002650 if (Arg.getOpcode() == ISD::UNDEF) continue;
2651 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002652 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 if (Val < 4 || Val > 7)
2654 return false;
2655 }
2656
2657 return true;
2658}
2659
Chris Lattnere6aa3862007-11-25 00:24:49 +00002660/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002661/// values in ther permute mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00002662static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2663 SDValue &V2, SDValue &Mask,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002664 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002665 MVT VT = Op.getValueType();
2666 MVT MaskVT = Mask.getValueType();
2667 MVT EltVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002668 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002669 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002670
2671 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002672 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002673 if (Arg.getOpcode() == ISD::UNDEF) {
2674 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2675 continue;
2676 }
2677 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002678 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002679 if (Val < NumElems)
2680 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2681 else
2682 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2683 }
2684
2685 std::swap(V1, V2);
Evan Chengfca29242007-12-07 08:07:39 +00002686 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002687 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2688}
2689
Evan Chenga6769df2007-12-07 21:30:01 +00002690/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2691/// the two vector operands have swapped position.
Evan Chengfca29242007-12-07 08:07:39 +00002692static
Dan Gohman8181bd12008-07-27 21:46:04 +00002693SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002694 MVT MaskVT = Mask.getValueType();
2695 MVT EltVT = MaskVT.getVectorElementType();
Evan Chengfca29242007-12-07 08:07:39 +00002696 unsigned NumElems = Mask.getNumOperands();
Dan Gohman8181bd12008-07-27 21:46:04 +00002697 SmallVector<SDValue, 8> MaskVec;
Evan Chengfca29242007-12-07 08:07:39 +00002698 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002699 SDValue Arg = Mask.getOperand(i);
Evan Chengfca29242007-12-07 08:07:39 +00002700 if (Arg.getOpcode() == ISD::UNDEF) {
2701 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2702 continue;
2703 }
2704 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002705 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengfca29242007-12-07 08:07:39 +00002706 if (Val < NumElems)
2707 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2708 else
2709 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2710 }
2711 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2712}
2713
2714
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002715/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2716/// match movhlps. The lower half elements should come from upper half of
2717/// V1 (and in order), and the upper half elements should come from the upper
2718/// half of V2 (and in order).
2719static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2720 unsigned NumElems = Mask->getNumOperands();
2721 if (NumElems != 4)
2722 return false;
2723 for (unsigned i = 0, e = 2; i != e; ++i)
2724 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2725 return false;
2726 for (unsigned i = 2; i != 4; ++i)
2727 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2728 return false;
2729 return true;
2730}
2731
2732/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002733/// is promoted to a vector. It also returns the LoadSDNode by reference if
2734/// required.
2735static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00002736 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2737 return false;
2738 N = N->getOperand(0).getNode();
2739 if (!ISD::isNON_EXTLoad(N))
2740 return false;
2741 if (LD)
2742 *LD = cast<LoadSDNode>(N);
2743 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744}
2745
2746/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2747/// match movlp{s|d}. The lower half elements should come from lower half of
2748/// V1 (and in order), and the upper half elements should come from the upper
2749/// half of V2 (and in order). And since V1 will become the source of the
2750/// MOVLP, it must be either a vector load or a scalar load to vector.
2751static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2752 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2753 return false;
2754 // Is V2 is a vector load, don't do this transformation. We will try to use
2755 // load folding shufps op.
2756 if (ISD::isNON_EXTLoad(V2))
2757 return false;
2758
2759 unsigned NumElems = Mask->getNumOperands();
2760 if (NumElems != 2 && NumElems != 4)
2761 return false;
2762 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2763 if (!isUndefOrEqual(Mask->getOperand(i), i))
2764 return false;
2765 for (unsigned i = NumElems/2; i != NumElems; ++i)
2766 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2767 return false;
2768 return true;
2769}
2770
2771/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2772/// all the same.
2773static bool isSplatVector(SDNode *N) {
2774 if (N->getOpcode() != ISD::BUILD_VECTOR)
2775 return false;
2776
Dan Gohman8181bd12008-07-27 21:46:04 +00002777 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002778 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2779 if (N->getOperand(i) != SplatValue)
2780 return false;
2781 return true;
2782}
2783
2784/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2785/// to an undef.
2786static bool isUndefShuffle(SDNode *N) {
2787 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2788 return false;
2789
Dan Gohman8181bd12008-07-27 21:46:04 +00002790 SDValue V1 = N->getOperand(0);
2791 SDValue V2 = N->getOperand(1);
2792 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002793 unsigned NumElems = Mask.getNumOperands();
2794 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002795 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002797 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2799 return false;
2800 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2801 return false;
2802 }
2803 }
2804 return true;
2805}
2806
2807/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2808/// constant +0.0.
Dan Gohman8181bd12008-07-27 21:46:04 +00002809static inline bool isZeroNode(SDValue Elt) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002810 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002811 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002812 (isa<ConstantFPSDNode>(Elt) &&
Dale Johannesendf8a8312007-08-31 04:03:46 +00002813 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002814}
2815
2816/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2817/// to an zero vector.
2818static bool isZeroShuffle(SDNode *N) {
2819 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2820 return false;
2821
Dan Gohman8181bd12008-07-27 21:46:04 +00002822 SDValue V1 = N->getOperand(0);
2823 SDValue V2 = N->getOperand(1);
2824 SDValue Mask = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825 unsigned NumElems = Mask.getNumOperands();
2826 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002827 SDValue Arg = Mask.getOperand(i);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002828 if (Arg.getOpcode() == ISD::UNDEF)
2829 continue;
2830
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002831 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattnere6aa3862007-11-25 00:24:49 +00002832 if (Idx < NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002833 unsigned Opc = V1.getNode()->getOpcode();
2834 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002835 continue;
2836 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002837 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002838 return false;
2839 } else if (Idx >= NumElems) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002840 unsigned Opc = V2.getNode()->getOpcode();
2841 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002842 continue;
2843 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greif1c80d112008-08-28 21:40:38 +00002844 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00002845 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002846 }
2847 }
2848 return true;
2849}
2850
2851/// getZeroVector - Returns a vector of specified type with all zero elements.
2852///
Dan Gohman8181bd12008-07-27 21:46:04 +00002853static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002854 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002855
2856 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2857 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002858 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002859 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman8181bd12008-07-27 21:46:04 +00002860 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002861 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002862 } else if (HasSSE2) { // SSE2
Dan Gohman8181bd12008-07-27 21:46:04 +00002863 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattnere6aa3862007-11-25 00:24:49 +00002864 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00002865 } else { // SSE1
Dan Gohman8181bd12008-07-27 21:46:04 +00002866 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Cheng8c590372008-05-15 08:39:06 +00002867 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2868 }
Chris Lattnere6aa3862007-11-25 00:24:49 +00002869 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002870}
2871
Chris Lattnere6aa3862007-11-25 00:24:49 +00002872/// getOnesVector - Returns a vector of specified type with all bits set.
2873///
Dan Gohman8181bd12008-07-27 21:46:04 +00002874static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002875 assert(VT.isVector() && "Expected a vector type");
Chris Lattnere6aa3862007-11-25 00:24:49 +00002876
2877 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2878 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00002879 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2880 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00002881 if (VT.getSizeInBits() == 64) // MMX
Chris Lattnere6aa3862007-11-25 00:24:49 +00002882 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2883 else // SSE
2884 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2885 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2886}
2887
2888
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2890/// that point to V2 points to its first element.
Dan Gohman8181bd12008-07-27 21:46:04 +00002891static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2893
2894 bool Changed = false;
Dan Gohman8181bd12008-07-27 21:46:04 +00002895 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896 unsigned NumElems = Mask.getNumOperands();
2897 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002898 SDValue Arg = Mask.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002900 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 if (Val > NumElems) {
2902 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2903 Changed = true;
2904 }
2905 }
2906 MaskVec.push_back(Arg);
2907 }
2908
2909 if (Changed)
2910 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2911 &MaskVec[0], MaskVec.size());
2912 return Mask;
2913}
2914
2915/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2916/// operation of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002917static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002918 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2919 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002920
Dan Gohman8181bd12008-07-27 21:46:04 +00002921 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002922 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2923 for (unsigned i = 1; i != NumElems; ++i)
2924 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2925 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2926}
2927
2928/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2929/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002930static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002931 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2932 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002933 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2935 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2936 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2937 }
2938 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2939}
2940
2941/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2942/// of specified width.
Dan Gohman8181bd12008-07-27 21:46:04 +00002943static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002944 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2945 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002946 unsigned Half = NumElems/2;
Dan Gohman8181bd12008-07-27 21:46:04 +00002947 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002948 for (unsigned i = 0; i != Half; ++i) {
2949 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2950 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2951 }
2952 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2953}
2954
Chris Lattner2d91b962008-03-09 01:05:04 +00002955/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2956/// element #0 of a vector with the specified index, leaving the rest of the
2957/// elements in place.
Dan Gohman8181bd12008-07-27 21:46:04 +00002958static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner2d91b962008-03-09 01:05:04 +00002959 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00002960 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2961 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00002962 SmallVector<SDValue, 8> MaskVec;
Chris Lattner2d91b962008-03-09 01:05:04 +00002963 // Element #0 of the result gets the elt we are replacing.
2964 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2965 for (unsigned i = 1; i != NumElems; ++i)
2966 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2967 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2968}
2969
Evan Chengbf8b2c52008-04-05 00:30:36 +00002970/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman8181bd12008-07-27 21:46:04 +00002971static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands92c43912008-06-06 12:08:01 +00002972 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2973 MVT VT = Op.getValueType();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002974 if (PVT == VT)
2975 return Op;
Dan Gohman8181bd12008-07-27 21:46:04 +00002976 SDValue V1 = Op.getOperand(0);
2977 SDValue Mask = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002978 unsigned NumElems = Mask.getNumOperands();
Evan Chengbf8b2c52008-04-05 00:30:36 +00002979 // Special handling of v4f32 -> v4i32.
2980 if (VT != MVT::v4f32) {
2981 Mask = getUnpacklMask(NumElems, DAG);
2982 while (NumElems > 4) {
2983 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2984 NumElems >>= 1;
2985 }
Evan Cheng8c590372008-05-15 08:39:06 +00002986 Mask = getZeroVector(MVT::v4i32, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002988
Evan Chengbf8b2c52008-04-05 00:30:36 +00002989 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman8181bd12008-07-27 21:46:04 +00002990 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Chengbf8b2c52008-04-05 00:30:36 +00002991 DAG.getNode(ISD::UNDEF, PVT), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2993}
2994
Evan Chenga2497eb2008-09-25 20:50:48 +00002995/// isVectorLoad - Returns true if the node is a vector load, a scalar
2996/// load that's promoted to vector, or a load bitcasted.
2997static bool isVectorLoad(SDValue Op) {
2998 assert(Op.getValueType().isVector() && "Expected a vector type");
2999 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3000 Op.getOpcode() == ISD::BIT_CONVERT) {
3001 return isa<LoadSDNode>(Op.getOperand(0));
3002 }
3003 return isa<LoadSDNode>(Op);
3004}
3005
3006
3007/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3008///
3009static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3010 SelectionDAG &DAG, bool HasSSE3) {
3011 // If we have sse3 and shuffle has more than one use or input is a load, then
3012 // use movddup. Otherwise, use movlhps.
3013 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3014 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3015 MVT VT = Op.getValueType();
3016 if (VT == PVT)
3017 return Op;
3018 unsigned NumElems = PVT.getVectorNumElements();
3019 if (NumElems == 2) {
3020 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3021 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3022 } else {
3023 assert(NumElems == 4);
3024 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3025 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3026 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3027 }
3028
3029 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3030 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3031 DAG.getNode(ISD::UNDEF, PVT), Mask);
3032 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3033}
3034
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003036/// vector of zero or undef vector. This produces a shuffle where the low
3037/// element of V2 is swizzled into the zero/undef vector, landing at element
3038/// 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 +00003039static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003040 bool isZero, bool HasSSE2,
3041 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00003042 MVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue V1 = isZero
Evan Cheng8c590372008-05-15 08:39:06 +00003044 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands92c43912008-06-06 12:08:01 +00003045 unsigned NumElems = V2.getValueType().getVectorNumElements();
3046 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3047 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003048 SmallVector<SDValue, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003049 for (unsigned i = 0; i != NumElems; ++i)
3050 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3051 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3052 else
3053 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003055 &MaskVec[0], MaskVec.size());
3056 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3057}
3058
Evan Chengdea99362008-05-29 08:22:04 +00003059/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3060/// a shuffle that is zero.
3061static
Dan Gohman8181bd12008-07-27 21:46:04 +00003062unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengdea99362008-05-29 08:22:04 +00003063 unsigned NumElems, bool Low,
3064 SelectionDAG &DAG) {
3065 unsigned NumZeros = 0;
3066 for (unsigned i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003067 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman8181bd12008-07-27 21:46:04 +00003068 SDValue Idx = Mask.getOperand(Index);
Evan Chengdea99362008-05-29 08:22:04 +00003069 if (Idx.getOpcode() == ISD::UNDEF) {
3070 ++NumZeros;
3071 continue;
3072 }
Gabor Greif1c80d112008-08-28 21:40:38 +00003073 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3074 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003075 ++NumZeros;
3076 else
3077 break;
3078 }
3079 return NumZeros;
3080}
3081
3082/// isVectorShift - Returns true if the shuffle can be implemented as a
3083/// logical left or right shift of a vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00003084static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3085 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengdea99362008-05-29 08:22:04 +00003086 unsigned NumElems = Mask.getNumOperands();
3087
3088 isLeft = true;
3089 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3090 if (!NumZeros) {
3091 isLeft = false;
3092 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3093 if (!NumZeros)
3094 return false;
3095 }
3096
3097 bool SeenV1 = false;
3098 bool SeenV2 = false;
3099 for (unsigned i = NumZeros; i < NumElems; ++i) {
3100 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman8181bd12008-07-27 21:46:04 +00003101 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengdea99362008-05-29 08:22:04 +00003102 if (Idx.getOpcode() == ISD::UNDEF)
3103 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003104 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengdea99362008-05-29 08:22:04 +00003105 if (Index < NumElems)
3106 SeenV1 = true;
3107 else {
3108 Index -= NumElems;
3109 SeenV2 = true;
3110 }
3111 if (Index != Val)
3112 return false;
3113 }
3114 if (SeenV1 && SeenV2)
3115 return false;
3116
3117 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3118 ShAmt = NumZeros;
3119 return true;
3120}
3121
3122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3124///
Dan Gohman8181bd12008-07-27 21:46:04 +00003125static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003126 unsigned NumNonZero, unsigned NumZero,
3127 SelectionDAG &DAG, TargetLowering &TLI) {
3128 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003129 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003130
Dan Gohman8181bd12008-07-27 21:46:04 +00003131 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 bool First = true;
3133 for (unsigned i = 0; i < 16; ++i) {
3134 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3135 if (ThisIsNonZero && First) {
3136 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003137 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003138 else
3139 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3140 First = false;
3141 }
3142
3143 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003144 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003145 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3146 if (LastIsNonZero) {
3147 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3148 }
3149 if (ThisIsNonZero) {
3150 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3151 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3152 ThisElt, DAG.getConstant(8, MVT::i8));
3153 if (LastIsNonZero)
3154 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3155 } else
3156 ThisElt = LastElt;
3157
Gabor Greif1c80d112008-08-28 21:40:38 +00003158 if (ThisElt.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003160 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161 }
3162 }
3163
3164 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3165}
3166
3167/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3168///
Dan Gohman8181bd12008-07-27 21:46:04 +00003169static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003170 unsigned NumNonZero, unsigned NumZero,
3171 SelectionDAG &DAG, TargetLowering &TLI) {
3172 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003173 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003174
Dan Gohman8181bd12008-07-27 21:46:04 +00003175 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003176 bool First = true;
3177 for (unsigned i = 0; i < 8; ++i) {
3178 bool isNonZero = (NonZeros & (1 << i)) != 0;
3179 if (isNonZero) {
3180 if (First) {
3181 if (NumZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003182 V = getZeroVector(MVT::v8i16, true, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003183 else
3184 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3185 First = false;
3186 }
3187 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003188 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003189 }
3190 }
3191
3192 return V;
3193}
3194
Evan Chengdea99362008-05-29 08:22:04 +00003195/// getVShift - Return a vector logical shift node.
3196///
Dan Gohman8181bd12008-07-27 21:46:04 +00003197static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengdea99362008-05-29 08:22:04 +00003198 unsigned NumBits, SelectionDAG &DAG,
3199 const TargetLowering &TLI) {
Duncan Sands92c43912008-06-06 12:08:01 +00003200 bool isMMX = VT.getSizeInBits() == 64;
3201 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003202 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3203 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3204 return DAG.getNode(ISD::BIT_CONVERT, VT,
3205 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003206 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003207}
3208
Dan Gohman8181bd12008-07-27 21:46:04 +00003209SDValue
3210X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003211 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003212 if (ISD::isBuildVectorAllZeros(Op.getNode())
3213 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003214 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3215 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3216 // eliminated on x86-32 hosts.
3217 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3218 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003219
Gabor Greif1c80d112008-08-28 21:40:38 +00003220 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003221 return getOnesVector(Op.getValueType(), DAG);
Evan Cheng8c590372008-05-15 08:39:06 +00003222 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003223 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003224
Duncan Sands92c43912008-06-06 12:08:01 +00003225 MVT VT = Op.getValueType();
3226 MVT EVT = VT.getVectorElementType();
3227 unsigned EVTBits = EVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003228
3229 unsigned NumElems = Op.getNumOperands();
3230 unsigned NumZero = 0;
3231 unsigned NumNonZero = 0;
3232 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003233 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003234 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003235 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003236 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003237 if (Elt.getOpcode() == ISD::UNDEF)
3238 continue;
3239 Values.insert(Elt);
3240 if (Elt.getOpcode() != ISD::Constant &&
3241 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003242 IsAllConstants = false;
Evan Chengc1073492007-12-12 06:45:40 +00003243 if (isZeroNode(Elt))
3244 NumZero++;
3245 else {
3246 NonZeros |= (1 << i);
3247 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 }
3249 }
3250
3251 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003252 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3253 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254 }
3255
Chris Lattner66a4dda2008-03-09 05:42:06 +00003256 // Special case for single non-zero, non-undef, element.
Evan Chengc1073492007-12-12 06:45:40 +00003257 if (NumNonZero == 1 && NumElems <= 4) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003259 SDValue Item = Op.getOperand(Idx);
Chris Lattnerac914892008-03-08 22:59:52 +00003260
Chris Lattner2d91b962008-03-09 01:05:04 +00003261 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3262 // the value are obviously zero, truncate the value to i32 and do the
3263 // insertion that way. Only do this if the value is non-constant or if the
3264 // value is a constant being inserted into element 0. It is cheaper to do
3265 // a constant pool load than it is to do a movd + shuffle.
3266 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3267 (!IsAllConstants || Idx == 0)) {
3268 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3269 // Handle MMX and SSE both.
Duncan Sands92c43912008-06-06 12:08:01 +00003270 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3271 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner2d91b962008-03-09 01:05:04 +00003272
3273 // Truncate the value (which may itself be a constant) to i32, and
3274 // convert it to a vector with movd (S2V+shuffle to zero extend).
3275 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3276 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003277 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3278 Subtarget->hasSSE2(), DAG);
Chris Lattner2d91b962008-03-09 01:05:04 +00003279
3280 // Now we have our 32-bit value zero extended in the low element of
3281 // a vector. If Idx != 0, swizzle it into place.
3282 if (Idx != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003283 SDValue Ops[] = {
Chris Lattner2d91b962008-03-09 01:05:04 +00003284 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3285 getSwapEltZeroMask(VecElts, Idx, DAG)
3286 };
3287 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3288 }
3289 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3290 }
3291 }
3292
Chris Lattnerac914892008-03-08 22:59:52 +00003293 // If we have a constant or non-constant insertion into the low element of
3294 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3295 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3296 // depending on what the source datatype is. Because we can only get here
3297 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3298 if (Idx == 0 &&
3299 // Don't do this for i64 values on x86-32.
3300 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003301 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003302 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003303 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3304 Subtarget->hasSSE2(), DAG);
Chris Lattner92bdcb52008-03-08 22:48:29 +00003305 }
Evan Chengdea99362008-05-29 08:22:04 +00003306
3307 // Is it a vector logical left shift?
3308 if (NumElems == 2 && Idx == 1 &&
3309 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003310 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003311 return getVShift(true, VT,
3312 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3313 NumBits/2, DAG, *this);
3314 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003315
3316 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003317 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003318
Chris Lattnerac914892008-03-08 22:59:52 +00003319 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3320 // is a non-constant being inserted into an element other than the low one,
3321 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3322 // movd/movss) to move this into the low element, then shuffle it into
3323 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003324 if (EVTBits == 32) {
Chris Lattner92bdcb52008-03-08 22:48:29 +00003325 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3326
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003328 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3329 Subtarget->hasSSE2(), DAG);
Duncan Sands92c43912008-06-06 12:08:01 +00003330 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3331 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003332 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 for (unsigned i = 0; i < NumElems; i++)
3334 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003335 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 &MaskVec[0], MaskVec.size());
3337 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3338 DAG.getNode(ISD::UNDEF, VT), Mask);
3339 }
3340 }
3341
Chris Lattner66a4dda2008-03-09 05:42:06 +00003342 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3343 if (Values.size() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00003344 return SDValue();
Chris Lattner66a4dda2008-03-09 05:42:06 +00003345
Dan Gohman21463242007-07-24 22:55:08 +00003346 // A vector full of immediates; various special cases are already
3347 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003348 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003349 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003351 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003352 if (EVTBits == 64) {
3353 if (NumNonZero == 1) {
3354 // One half is zero or undef.
3355 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003356 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003357 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003358 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3359 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003360 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003361 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003362 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003363
3364 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3365 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003366 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003367 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003368 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003369 }
3370
3371 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003372 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003373 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003374 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003375 }
3376
3377 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003378 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003379 V.resize(NumElems);
3380 if (NumElems == 4 && NumZero > 0) {
3381 for (unsigned i = 0; i < 4; ++i) {
3382 bool isZero = !(NonZeros & (1 << i));
3383 if (isZero)
Evan Cheng8c590372008-05-15 08:39:06 +00003384 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003385 else
3386 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3387 }
3388
3389 for (unsigned i = 0; i < 2; ++i) {
3390 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3391 default: break;
3392 case 0:
3393 V[i] = V[i*2]; // Must be a zero vector.
3394 break;
3395 case 1:
3396 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3397 getMOVLMask(NumElems, DAG));
3398 break;
3399 case 2:
3400 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3401 getMOVLMask(NumElems, DAG));
3402 break;
3403 case 3:
3404 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3405 getUnpacklMask(NumElems, DAG));
3406 break;
3407 }
3408 }
3409
Duncan Sands92c43912008-06-06 12:08:01 +00003410 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3411 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003412 SmallVector<SDValue, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003413 bool Reverse = (NonZeros & 0x3) == 2;
3414 for (unsigned i = 0; i < 2; ++i)
3415 if (Reverse)
3416 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3417 else
3418 MaskVec.push_back(DAG.getConstant(i, EVT));
3419 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3420 for (unsigned i = 0; i < 2; ++i)
3421 if (Reverse)
3422 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3423 else
3424 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003425 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003426 &MaskVec[0], MaskVec.size());
3427 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3428 }
3429
3430 if (Values.size() > 2) {
3431 // Expand into a number of unpckl*.
3432 // e.g. for v4f32
3433 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3434 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3435 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 for (unsigned i = 0; i < NumElems; ++i)
3438 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3439 NumElems >>= 1;
3440 while (NumElems != 0) {
3441 for (unsigned i = 0; i < NumElems; ++i)
3442 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3443 UnpckMask);
3444 NumElems >>= 1;
3445 }
3446 return V[0];
3447 }
3448
Dan Gohman8181bd12008-07-27 21:46:04 +00003449 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003450}
3451
Evan Chengfca29242007-12-07 08:07:39 +00003452static
Dan Gohman8181bd12008-07-27 21:46:04 +00003453SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendling2c7cd592008-08-21 22:35:37 +00003454 SDValue PermMask, SelectionDAG &DAG,
3455 TargetLowering &TLI) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003456 SDValue NewV;
Duncan Sands92c43912008-06-06 12:08:01 +00003457 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3458 MVT MaskEVT = MaskVT.getVectorElementType();
3459 MVT PtrVT = TLI.getPointerTy();
Gabor Greif1c80d112008-08-28 21:40:38 +00003460 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3461 PermMask.getNode()->op_end());
Evan Cheng75184a92007-12-11 01:46:18 +00003462
3463 // First record which half of which vector the low elements come from.
3464 SmallVector<unsigned, 4> LowQuad(4);
3465 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003466 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003467 if (Elt.getOpcode() == ISD::UNDEF)
3468 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003469 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003470 int QuadIdx = EltIdx / 4;
3471 ++LowQuad[QuadIdx];
3472 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003473
Evan Cheng75184a92007-12-11 01:46:18 +00003474 int BestLowQuad = -1;
3475 unsigned MaxQuad = 1;
3476 for (unsigned i = 0; i < 4; ++i) {
3477 if (LowQuad[i] > MaxQuad) {
3478 BestLowQuad = i;
3479 MaxQuad = LowQuad[i];
3480 }
Evan Chengfca29242007-12-07 08:07:39 +00003481 }
3482
Evan Cheng75184a92007-12-11 01:46:18 +00003483 // Record which half of which vector the high elements come from.
3484 SmallVector<unsigned, 4> HighQuad(4);
3485 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003486 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003487 if (Elt.getOpcode() == ISD::UNDEF)
3488 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003489 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003490 int QuadIdx = EltIdx / 4;
3491 ++HighQuad[QuadIdx];
3492 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003493
Evan Cheng75184a92007-12-11 01:46:18 +00003494 int BestHighQuad = -1;
3495 MaxQuad = 1;
3496 for (unsigned i = 0; i < 4; ++i) {
3497 if (HighQuad[i] > MaxQuad) {
3498 BestHighQuad = i;
3499 MaxQuad = HighQuad[i];
3500 }
3501 }
3502
3503 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3504 if (BestLowQuad != -1 || BestHighQuad != -1) {
3505 // First sort the 4 chunks in order using shufpd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003506 SmallVector<SDValue, 8> MaskVec;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003507
Evan Cheng75184a92007-12-11 01:46:18 +00003508 if (BestLowQuad != -1)
3509 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3510 else
3511 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003512
Evan Cheng75184a92007-12-11 01:46:18 +00003513 if (BestHighQuad != -1)
3514 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3515 else
3516 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003517
Dan Gohman8181bd12008-07-27 21:46:04 +00003518 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng75184a92007-12-11 01:46:18 +00003519 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3520 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3521 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3522 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3523
3524 // Now sort high and low parts separately.
3525 BitVector InOrder(8);
3526 if (BestLowQuad != -1) {
3527 // Sort lower half in order using PSHUFLW.
3528 MaskVec.clear();
3529 bool AnyOutOrder = false;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003530
Evan Cheng75184a92007-12-11 01:46:18 +00003531 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003532 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003533 if (Elt.getOpcode() == ISD::UNDEF) {
3534 MaskVec.push_back(Elt);
3535 InOrder.set(i);
3536 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003537 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003538 if (EltIdx != i)
3539 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003540
Evan Cheng75184a92007-12-11 01:46:18 +00003541 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003542
Evan Cheng75184a92007-12-11 01:46:18 +00003543 // If this element is in the right place after this shuffle, then
3544 // remember it.
3545 if ((int)(EltIdx / 4) == BestLowQuad)
3546 InOrder.set(i);
3547 }
3548 }
3549 if (AnyOutOrder) {
3550 for (unsigned i = 4; i != 8; ++i)
3551 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00003552 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003553 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3554 }
3555 }
3556
3557 if (BestHighQuad != -1) {
3558 // Sort high half in order using PSHUFHW if possible.
3559 MaskVec.clear();
Bill Wendling2c7cd592008-08-21 22:35:37 +00003560
Evan Cheng75184a92007-12-11 01:46:18 +00003561 for (unsigned i = 0; i != 4; ++i)
3562 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003563
Evan Cheng75184a92007-12-11 01:46:18 +00003564 bool AnyOutOrder = false;
3565 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003566 SDValue Elt = MaskElts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003567 if (Elt.getOpcode() == ISD::UNDEF) {
3568 MaskVec.push_back(Elt);
3569 InOrder.set(i);
3570 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003571 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003572 if (EltIdx != i)
3573 AnyOutOrder = true;
Bill Wendling2c7cd592008-08-21 22:35:37 +00003574
Evan Cheng75184a92007-12-11 01:46:18 +00003575 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendling2c7cd592008-08-21 22:35:37 +00003576
Evan Cheng75184a92007-12-11 01:46:18 +00003577 // If this element is in the right place after this shuffle, then
3578 // remember it.
3579 if ((int)(EltIdx / 4) == BestHighQuad)
3580 InOrder.set(i);
3581 }
3582 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003583
Evan Cheng75184a92007-12-11 01:46:18 +00003584 if (AnyOutOrder) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003585 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003586 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3587 }
3588 }
3589
3590 // The other elements are put in the right place using pextrw and pinsrw.
3591 for (unsigned i = 0; i != 8; ++i) {
3592 if (InOrder[i])
3593 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003594 SDValue Elt = MaskElts[i];
Bill Wendling49bd4db2008-08-21 22:36:36 +00003595 if (Elt.getOpcode() == ISD::UNDEF)
3596 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003597 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003598 SDValue ExtOp = (EltIdx < 8)
Evan Cheng75184a92007-12-11 01:46:18 +00003599 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3600 DAG.getConstant(EltIdx, PtrVT))
3601 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3602 DAG.getConstant(EltIdx - 8, PtrVT));
3603 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3604 DAG.getConstant(i, PtrVT));
3605 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003606
Evan Cheng75184a92007-12-11 01:46:18 +00003607 return NewV;
3608 }
3609
Bill Wendling2c7cd592008-08-21 22:35:37 +00003610 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3611 // few as possible. First, let's find out how many elements are already in the
3612 // right order.
Evan Chengfca29242007-12-07 08:07:39 +00003613 unsigned V1InOrder = 0;
3614 unsigned V1FromV1 = 0;
3615 unsigned V2InOrder = 0;
3616 unsigned V2FromV2 = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00003617 SmallVector<SDValue, 8> V1Elts;
3618 SmallVector<SDValue, 8> V2Elts;
Evan Chengfca29242007-12-07 08:07:39 +00003619 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003620 SDValue Elt = MaskElts[i];
Evan Chengfca29242007-12-07 08:07:39 +00003621 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng75184a92007-12-11 01:46:18 +00003622 V1Elts.push_back(Elt);
3623 V2Elts.push_back(Elt);
Evan Chengfca29242007-12-07 08:07:39 +00003624 ++V1InOrder;
3625 ++V2InOrder;
Evan Cheng75184a92007-12-11 01:46:18 +00003626 continue;
3627 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003628 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003629 if (EltIdx == i) {
3630 V1Elts.push_back(Elt);
3631 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3632 ++V1InOrder;
3633 } else if (EltIdx == i+8) {
3634 V1Elts.push_back(Elt);
3635 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3636 ++V2InOrder;
3637 } else if (EltIdx < 8) {
3638 V1Elts.push_back(Elt);
3639 ++V1FromV1;
Evan Chengfca29242007-12-07 08:07:39 +00003640 } else {
Evan Cheng75184a92007-12-11 01:46:18 +00003641 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3642 ++V2FromV2;
Evan Chengfca29242007-12-07 08:07:39 +00003643 }
3644 }
3645
3646 if (V2InOrder > V1InOrder) {
3647 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3648 std::swap(V1, V2);
3649 std::swap(V1Elts, V2Elts);
3650 std::swap(V1FromV1, V2FromV2);
3651 }
3652
Evan Cheng75184a92007-12-11 01:46:18 +00003653 if ((V1FromV1 + V1InOrder) != 8) {
3654 // Some elements are from V2.
3655 if (V1FromV1) {
3656 // If there are elements that are from V1 but out of place,
3657 // then first sort them in place
Dan Gohman8181bd12008-07-27 21:46:04 +00003658 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003659 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003660 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003661 if (Elt.getOpcode() == ISD::UNDEF) {
3662 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3663 continue;
3664 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003665 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003666 if (EltIdx >= 8)
3667 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3668 else
3669 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3670 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003671 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng75184a92007-12-11 01:46:18 +00003672 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Chengfca29242007-12-07 08:07:39 +00003673 }
Evan Cheng75184a92007-12-11 01:46:18 +00003674
3675 NewV = V1;
3676 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003677 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003678 if (Elt.getOpcode() == ISD::UNDEF)
3679 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003680 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003681 if (EltIdx < 8)
3682 continue;
Dan Gohman8181bd12008-07-27 21:46:04 +00003683 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng75184a92007-12-11 01:46:18 +00003684 DAG.getConstant(EltIdx - 8, PtrVT));
3685 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3686 DAG.getConstant(i, PtrVT));
3687 }
3688 return NewV;
3689 } else {
3690 // All elements are from V1.
3691 NewV = V1;
3692 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003693 SDValue Elt = V1Elts[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003694 if (Elt.getOpcode() == ISD::UNDEF)
3695 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003696 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00003697 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng75184a92007-12-11 01:46:18 +00003698 DAG.getConstant(EltIdx, PtrVT));
3699 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3700 DAG.getConstant(i, PtrVT));
3701 }
3702 return NewV;
3703 }
3704}
3705
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003706/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3707/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3708/// done when every pair / quad of shuffle mask elements point to elements in
3709/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00003710/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3711static
Dan Gohman8181bd12008-07-27 21:46:04 +00003712SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands92c43912008-06-06 12:08:01 +00003713 MVT VT,
Dan Gohman8181bd12008-07-27 21:46:04 +00003714 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng75184a92007-12-11 01:46:18 +00003715 TargetLowering &TLI) {
3716 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003717 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands92c43912008-06-06 12:08:01 +00003718 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd3ace282008-07-21 10:20:31 +00003719 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003720 MVT NewVT = MaskVT;
3721 switch (VT.getSimpleVT()) {
3722 default: assert(false && "Unexpected!");
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003723 case MVT::v4f32: NewVT = MVT::v2f64; break;
3724 case MVT::v4i32: NewVT = MVT::v2i64; break;
3725 case MVT::v8i16: NewVT = MVT::v4i32; break;
3726 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003727 }
3728
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003729 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00003730 if (VT.isInteger())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003731 NewVT = MVT::v2i64;
3732 else
3733 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00003734 }
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003735 unsigned Scale = NumElems / NewWidth;
Dan Gohman8181bd12008-07-27 21:46:04 +00003736 SmallVector<SDValue, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00003737 for (unsigned i = 0; i < NumElems; i += Scale) {
3738 unsigned StartIdx = ~0U;
3739 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003740 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng75184a92007-12-11 01:46:18 +00003741 if (Elt.getOpcode() == ISD::UNDEF)
3742 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003743 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003744 if (StartIdx == ~0U)
3745 StartIdx = EltIdx - (EltIdx % Scale);
3746 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00003747 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00003748 }
3749 if (StartIdx == ~0U)
Duncan Sandsd3ace282008-07-21 10:20:31 +00003750 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng75184a92007-12-11 01:46:18 +00003751 else
Duncan Sandsd3ace282008-07-21 10:20:31 +00003752 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Chengfca29242007-12-07 08:07:39 +00003753 }
3754
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003755 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3756 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3757 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3758 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3759 &MaskVec[0], MaskVec.size()));
Evan Chengfca29242007-12-07 08:07:39 +00003760}
3761
Evan Chenge9b9c672008-05-09 21:53:03 +00003762/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003763///
Dan Gohman8181bd12008-07-27 21:46:04 +00003764static SDValue getVZextMovL(MVT VT, MVT OpVT,
3765 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00003766 const X86Subtarget *Subtarget) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00003767 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3768 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00003769 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00003770 LD = dyn_cast<LoadSDNode>(SrcOp);
3771 if (!LD) {
3772 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3773 // instead.
Duncan Sands92c43912008-06-06 12:08:01 +00003774 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng40ee6e52008-05-08 00:57:18 +00003775 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3776 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3777 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3778 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3779 // PR2108
3780 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3781 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003782 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003783 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00003784 SrcOp.getOperand(0)
3785 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00003786 }
3787 }
3788 }
3789
3790 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chenge9b9c672008-05-09 21:53:03 +00003791 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003792 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3793}
3794
Evan Chengf50554e2008-07-22 21:13:36 +00003795/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3796/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003797static SDValue
3798LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3799 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengf50554e2008-07-22 21:13:36 +00003800 MVT MaskVT = PermMask.getValueType();
3801 MVT MaskEVT = MaskVT.getVectorElementType();
3802 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00003803 Locs.resize(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00003804 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003805 unsigned NumHi = 0;
3806 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00003807 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003808 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003809 if (Elt.getOpcode() == ISD::UNDEF) {
3810 Locs[i] = std::make_pair(-1, -1);
3811 } else {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003812 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmance57fd92008-08-04 23:09:15 +00003813 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengf50554e2008-07-22 21:13:36 +00003814 if (Val < 4) {
3815 Locs[i] = std::make_pair(0, NumLo);
3816 Mask1[NumLo] = Elt;
3817 NumLo++;
3818 } else {
3819 Locs[i] = std::make_pair(1, NumHi);
3820 if (2+NumHi < 4)
3821 Mask1[2+NumHi] = Elt;
3822 NumHi++;
3823 }
3824 }
3825 }
Evan Cheng3cae0332008-07-23 00:22:17 +00003826
Evan Chengf50554e2008-07-22 21:13:36 +00003827 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00003828 // If no more than two elements come from either vector. This can be
3829 // implemented with two shuffles. First shuffle gather the elements.
3830 // The second shuffle, which takes the first shuffle as both of its
3831 // vector operands, put the elements into the right order.
Evan Chengf50554e2008-07-22 21:13:36 +00003832 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3833 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3834 &Mask1[0], Mask1.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003835
Dan Gohman8181bd12008-07-27 21:46:04 +00003836 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengf50554e2008-07-22 21:13:36 +00003837 for (unsigned i = 0; i != 4; ++i) {
3838 if (Locs[i].first == -1)
3839 continue;
3840 else {
3841 unsigned Idx = (i < 2) ? 0 : 4;
3842 Idx += Locs[i].first * 2 + Locs[i].second;
3843 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3844 }
3845 }
3846
3847 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3848 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3849 &Mask2[0], Mask2.size()));
Evan Cheng3cae0332008-07-23 00:22:17 +00003850 } else if (NumLo == 3 || NumHi == 3) {
3851 // Otherwise, we must have three elements from one vector, call it X, and
3852 // one element from the other, call it Y. First, use a shufps to build an
3853 // intermediate vector with the one element from Y and the element from X
3854 // that will be in the same half in the final destination (the indexes don't
3855 // matter). Then, use a shufps to build the final vector, taking the half
3856 // containing the element from Y from the intermediate, and the other half
3857 // from X.
3858 if (NumHi == 3) {
3859 // Normalize it so the 3 elements come from V1.
3860 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3861 std::swap(V1, V2);
3862 }
3863
3864 // Find the element from V2.
3865 unsigned HiIndex;
3866 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003867 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng3cae0332008-07-23 00:22:17 +00003868 if (Elt.getOpcode() == ISD::UNDEF)
3869 continue;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003870 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng3cae0332008-07-23 00:22:17 +00003871 if (Val >= 4)
3872 break;
3873 }
3874
3875 Mask1[0] = PermMask.getOperand(HiIndex);
3876 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3877 Mask1[2] = PermMask.getOperand(HiIndex^1);
3878 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3879 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3880 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3881
3882 if (HiIndex >= 2) {
3883 Mask1[0] = PermMask.getOperand(0);
3884 Mask1[1] = PermMask.getOperand(1);
3885 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3886 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3887 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3888 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3889 } else {
3890 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3891 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3892 Mask1[2] = PermMask.getOperand(2);
3893 Mask1[3] = PermMask.getOperand(3);
3894 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003895 Mask1[2] =
3896 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3897 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003898 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003899 Mask1[3] =
3900 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3901 MaskEVT);
Evan Cheng3cae0332008-07-23 00:22:17 +00003902 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3903 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3904 }
Evan Chengf50554e2008-07-22 21:13:36 +00003905 }
3906
3907 // Break it into (shuffle shuffle_hi, shuffle_lo).
3908 Locs.clear();
Dan Gohman8181bd12008-07-27 21:46:04 +00003909 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3910 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3911 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00003912 unsigned MaskIdx = 0;
3913 unsigned LoIdx = 0;
3914 unsigned HiIdx = 2;
3915 for (unsigned i = 0; i != 4; ++i) {
3916 if (i == 2) {
3917 MaskPtr = &HiMask;
3918 MaskIdx = 1;
3919 LoIdx = 0;
3920 HiIdx = 2;
3921 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003922 SDValue Elt = PermMask.getOperand(i);
Evan Chengf50554e2008-07-22 21:13:36 +00003923 if (Elt.getOpcode() == ISD::UNDEF) {
3924 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003925 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00003926 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3927 (*MaskPtr)[LoIdx] = Elt;
3928 LoIdx++;
3929 } else {
3930 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3931 (*MaskPtr)[HiIdx] = Elt;
3932 HiIdx++;
3933 }
3934 }
3935
Dan Gohman8181bd12008-07-27 21:46:04 +00003936 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003937 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3938 &LoMask[0], LoMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003939 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengf50554e2008-07-22 21:13:36 +00003940 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3941 &HiMask[0], HiMask.size()));
Dan Gohman8181bd12008-07-27 21:46:04 +00003942 SmallVector<SDValue, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00003943 for (unsigned i = 0; i != 4; ++i) {
3944 if (Locs[i].first == -1) {
3945 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3946 } else {
3947 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3948 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3949 }
3950 }
3951 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3952 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3953 &MaskOps[0], MaskOps.size()));
3954}
3955
Dan Gohman8181bd12008-07-27 21:46:04 +00003956SDValue
3957X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3958 SDValue V1 = Op.getOperand(0);
3959 SDValue V2 = Op.getOperand(1);
3960 SDValue PermMask = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00003961 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003962 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands92c43912008-06-06 12:08:01 +00003963 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3965 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3966 bool V1IsSplat = false;
3967 bool V2IsSplat = false;
3968
Gabor Greif1c80d112008-08-28 21:40:38 +00003969 if (isUndefShuffle(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970 return DAG.getNode(ISD::UNDEF, VT);
3971
Gabor Greif1c80d112008-08-28 21:40:38 +00003972 if (isZeroShuffle(Op.getNode()))
Evan Cheng8c590372008-05-15 08:39:06 +00003973 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003974
Gabor Greif1c80d112008-08-28 21:40:38 +00003975 if (isIdentityMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003976 return V1;
Gabor Greif1c80d112008-08-28 21:40:38 +00003977 else if (isIdentityMask(PermMask.getNode(), true))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 return V2;
3979
Evan Chengae6c9212008-09-25 23:35:16 +00003980 // Canonicalize movddup shuffles.
3981 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Chengbdd9d9f2008-10-06 21:13:08 +00003982 VT.getSizeInBits() == 128 &&
Evan Chengae6c9212008-09-25 23:35:16 +00003983 X86::isMOVDDUPMask(PermMask.getNode()))
3984 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3985
Gabor Greif1c80d112008-08-28 21:40:38 +00003986 if (isSplatMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00003987 if (isMMX || NumElems < 4) return Op;
3988 // Promote it to a v4{if}32 splat.
3989 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003990 }
3991
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003992 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3993 // do it!
3994 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003995 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003996 if (NewOp.getNode())
Evan Cheng15e8f5a2007-12-15 03:00:47 +00003997 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3998 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3999 // FIXME: Figure out a cleaner way to do this.
4000 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004001 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004002 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004003 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004004 if (NewOp.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004005 SDValue NewV1 = NewOp.getOperand(0);
4006 SDValue NewV2 = NewOp.getOperand(1);
4007 SDValue NewMask = NewOp.getOperand(2);
Gabor Greif1c80d112008-08-28 21:40:38 +00004008 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004009 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chenge9b9c672008-05-09 21:53:03 +00004010 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004011 }
4012 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004013 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004014 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng40ee6e52008-05-08 00:57:18 +00004015 DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004016 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004017 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng40ee6e52008-05-08 00:57:18 +00004018 DAG, Subtarget);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004019 }
4020 }
4021
Evan Chengdea99362008-05-29 08:22:04 +00004022 // Check if this can be converted into a logical shift.
4023 bool isLeft = false;
4024 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004025 SDValue ShVal;
Evan Chengdea99362008-05-29 08:22:04 +00004026 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4027 if (isShift && ShVal.hasOneUse()) {
4028 // If the shifted value has multiple uses, it may be cheaper to use
4029 // v_set0 + movlhps or movhlps, etc.
Duncan Sands92c43912008-06-06 12:08:01 +00004030 MVT EVT = VT.getVectorElementType();
4031 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004032 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4033 }
4034
Gabor Greif1c80d112008-08-28 21:40:38 +00004035 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004036 if (V1IsUndef)
4037 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004038 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chenge9b9c672008-05-09 21:53:03 +00004039 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004040 if (!isMMX)
4041 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004042 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043
Gabor Greif1c80d112008-08-28 21:40:38 +00004044 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4045 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4046 X86::isMOVHLPSMask(PermMask.getNode()) ||
4047 X86::isMOVHPMask(PermMask.getNode()) ||
4048 X86::isMOVLPMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004049 return Op;
4050
Gabor Greif1c80d112008-08-28 21:40:38 +00004051 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4052 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004053 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4054
Evan Chengdea99362008-05-29 08:22:04 +00004055 if (isShift) {
4056 // No better options. Use a vshl / vsrl.
Duncan Sands92c43912008-06-06 12:08:01 +00004057 MVT EVT = VT.getVectorElementType();
4058 ShAmt *= EVT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00004059 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4060 }
4061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004063 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4064 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004065 V1IsSplat = isSplatVector(V1.getNode());
4066 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattnere6aa3862007-11-25 00:24:49 +00004067
4068 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004069 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
4070 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4071 std::swap(V1IsSplat, V2IsSplat);
4072 std::swap(V1IsUndef, V2IsUndef);
4073 Commuted = true;
4074 }
4075
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004076 // FIXME: Figure out a cleaner way to do this.
Gabor Greif1c80d112008-08-28 21:40:38 +00004077 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004078 if (V2IsUndef) return V1;
4079 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4080 if (V2IsSplat) {
4081 // V2 is a splat, so the mask may be malformed. That is, it may point
4082 // to any V2 element. The instruction selectior won't like this. Get
4083 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman8181bd12008-07-27 21:46:04 +00004084 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004085 if (NewMask.getNode() != PermMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004086 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4087 }
4088 return Op;
4089 }
4090
Gabor Greif1c80d112008-08-28 21:40:38 +00004091 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4092 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4093 X86::isUNPCKLMask(PermMask.getNode()) ||
4094 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004095 return Op;
4096
4097 if (V2IsSplat) {
4098 // Normalize mask so all entries that point to V2 points to its first
4099 // element then try to match unpck{h|l} again. If match, return a
4100 // new vector_shuffle with the corrected mask.
Dan Gohman8181bd12008-07-27 21:46:04 +00004101 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004102 if (NewMask.getNode() != PermMask.getNode()) {
4103 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004104 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greif1c80d112008-08-28 21:40:38 +00004106 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004107 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004108 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
4109 }
4110 }
4111 }
4112
4113 // Normalize the node to match x86 shuffle ops if needed
Gabor Greif1c80d112008-08-28 21:40:38 +00004114 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004115 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4116
4117 if (Commuted) {
4118 // Commute is back and try unpck* again.
4119 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004120 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4121 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4122 X86::isUNPCKLMask(PermMask.getNode()) ||
4123 X86::isUNPCKHMask(PermMask.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004124 return Op;
4125 }
4126
Evan Chengbf8b2c52008-04-05 00:30:36 +00004127 // Try PSHUF* first, then SHUFP*.
4128 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4129 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greif1c80d112008-08-28 21:40:38 +00004130 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Chengbf8b2c52008-04-05 00:30:36 +00004131 if (V2.getOpcode() != ISD::UNDEF)
4132 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4133 DAG.getNode(ISD::UNDEF, VT), PermMask);
4134 return Op;
4135 }
4136
4137 if (!isMMX) {
4138 if (Subtarget->hasSSE2() &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004139 (X86::isPSHUFDMask(PermMask.getNode()) ||
4140 X86::isPSHUFHWMask(PermMask.getNode()) ||
4141 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands92c43912008-06-06 12:08:01 +00004142 MVT RVT = VT;
Evan Chengbf8b2c52008-04-05 00:30:36 +00004143 if (VT == MVT::v4f32) {
4144 RVT = MVT::v4i32;
4145 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4146 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4147 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4148 } else if (V2.getOpcode() != ISD::UNDEF)
4149 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4150 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4151 if (RVT != VT)
4152 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004153 return Op;
4154 }
4155
Evan Chengbf8b2c52008-04-05 00:30:36 +00004156 // Binary or unary shufps.
Gabor Greif1c80d112008-08-28 21:40:38 +00004157 if (X86::isSHUFPMask(PermMask.getNode()) ||
4158 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004159 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004160 }
4161
Evan Cheng75184a92007-12-11 01:46:18 +00004162 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4163 if (VT == MVT::v8i16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004164 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004165 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004166 return NewOp;
4167 }
4168
Evan Chengf50554e2008-07-22 21:13:36 +00004169 // Handle all 4 wide cases with a number of shuffles except for MMX.
4170 if (NumElems == 4 && !isMMX)
4171 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172
Dan Gohman8181bd12008-07-27 21:46:04 +00004173 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174}
4175
Dan Gohman8181bd12008-07-27 21:46:04 +00004176SDValue
4177X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004178 SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004179 MVT VT = Op.getValueType();
4180 if (VT.getSizeInBits() == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004181 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004182 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004183 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004184 DAG.getValueType(VT));
4185 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004186 } else if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004187 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004188 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004189 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004190 DAG.getValueType(VT));
4191 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng6c249332008-03-24 21:52:23 +00004192 } else if (VT == MVT::f32) {
4193 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4194 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004195 // result has a single use which is a store or a bitcast to i32. And in
4196 // the case of a store, it's not worth it if the index is a constant 0,
4197 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004198 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004199 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004200 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004201 if ((User->getOpcode() != ISD::STORE ||
4202 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4203 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004204 (User->getOpcode() != ISD::BIT_CONVERT ||
4205 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004206 return SDValue();
4207 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng6c249332008-03-24 21:52:23 +00004208 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4209 Op.getOperand(1));
4210 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begemand77e59e2008-02-11 04:19:36 +00004211 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004212 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004213}
4214
4215
Dan Gohman8181bd12008-07-27 21:46:04 +00004216SDValue
4217X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004219 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004220
Evan Cheng6c249332008-03-24 21:52:23 +00004221 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004222 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004223 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004224 return Res;
4225 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004226
Duncan Sands92c43912008-06-06 12:08:01 +00004227 MVT VT = Op.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004228 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004229 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004230 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004231 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004232 if (Idx == 0)
4233 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4234 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4235 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4236 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004237 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands92c43912008-06-06 12:08:01 +00004238 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004239 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004241 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004242 DAG.getValueType(VT));
4243 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004244 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004245 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004246 if (Idx == 0)
4247 return Op;
4248 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands92c43912008-06-06 12:08:01 +00004249 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004250 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004251 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004252 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004253 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004254 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004255 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004256 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004257 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004258 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004259 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004260 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004261 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4263 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4264 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004265 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004266 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004267 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4268 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4269 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004270 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 if (Idx == 0)
4272 return Op;
4273
4274 // UNPCKHPD the element to the lowest double word, then movsd.
4275 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4276 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd3ace282008-07-21 10:20:31 +00004277 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman8181bd12008-07-27 21:46:04 +00004278 SmallVector<SDValue, 8> IdxVec;
Duncan Sands92c43912008-06-06 12:08:01 +00004279 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00004280 IdxVec.
Duncan Sands92c43912008-06-06 12:08:01 +00004281 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman8181bd12008-07-27 21:46:04 +00004282 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004283 &IdxVec[0], IdxVec.size());
Dan Gohman8181bd12008-07-27 21:46:04 +00004284 SDValue Vec = Op.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004285 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4286 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4287 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004288 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004289 }
4290
Dan Gohman8181bd12008-07-27 21:46:04 +00004291 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004292}
4293
Dan Gohman8181bd12008-07-27 21:46:04 +00004294SDValue
4295X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands92c43912008-06-06 12:08:01 +00004296 MVT VT = Op.getValueType();
4297 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004298
Dan Gohman8181bd12008-07-27 21:46:04 +00004299 SDValue N0 = Op.getOperand(0);
4300 SDValue N1 = Op.getOperand(1);
4301 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004302
Dan Gohman5a7af042008-08-14 22:53:18 +00004303 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4304 isa<ConstantSDNode>(N2)) {
Duncan Sands92c43912008-06-06 12:08:01 +00004305 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemand77e59e2008-02-11 04:19:36 +00004306 : X86ISD::PINSRW;
4307 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4308 // argument.
4309 if (N1.getValueType() != MVT::i32)
4310 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4311 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004312 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begemand77e59e2008-02-11 04:19:36 +00004313 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanfd7369a2008-08-14 22:43:26 +00004314 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004315 // Bits [7:6] of the constant are the source select. This will always be
4316 // zero here. The DAG Combiner may combine an extract_elt index into these
4317 // bits. For example (insert (extract, 3), 2) could be matched by putting
4318 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4319 // Bits [5:4] of the constant are the destination select. This is the
4320 // value of the incoming immediate.
4321 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4322 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004323 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begemand77e59e2008-02-11 04:19:36 +00004324 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4325 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004326 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004327}
4328
Dan Gohman8181bd12008-07-27 21:46:04 +00004329SDValue
4330X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004331 MVT VT = Op.getValueType();
4332 MVT EVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004333
4334 if (Subtarget->hasSSE41())
4335 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4336
Evan Chenge12a7eb2007-12-12 07:55:34 +00004337 if (EVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004338 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004339
Dan Gohman8181bd12008-07-27 21:46:04 +00004340 SDValue N0 = Op.getOperand(0);
4341 SDValue N1 = Op.getOperand(1);
4342 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004343
Duncan Sands92c43912008-06-06 12:08:01 +00004344 if (EVT.getSizeInBits() == 16) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004345 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4346 // as its second argument.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004347 if (N1.getValueType() != MVT::i32)
4348 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4349 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004350 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004351 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004352 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004353 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004354}
4355
Dan Gohman8181bd12008-07-27 21:46:04 +00004356SDValue
4357X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng759fe022008-07-22 18:39:19 +00004358 if (Op.getValueType() == MVT::v2f32)
4359 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4360 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4361 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4362 Op.getOperand(0))));
4363
Dan Gohman8181bd12008-07-27 21:46:04 +00004364 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004365 MVT VT = MVT::v2i32;
4366 switch (Op.getValueType().getSimpleVT()) {
Evan Chengd1045a62008-02-18 23:04:32 +00004367 default: break;
4368 case MVT::v16i8:
4369 case MVT::v8i16:
4370 VT = MVT::v4i32;
4371 break;
4372 }
4373 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4374 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375}
4376
Bill Wendlingfef06052008-09-16 21:48:12 +00004377// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4378// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4379// one of the above mentioned nodes. It has to be wrapped because otherwise
4380// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4381// be used to form addressing mode. These wrapped nodes will be selected
4382// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004383SDValue
4384X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004386 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004387 getPointerTy(),
4388 CP->getAlignment());
4389 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4390 // With PIC, the address is actually $g + Offset.
4391 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4392 !Subtarget->isPICStyleRIPRel()) {
4393 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4394 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4395 Result);
4396 }
4397
4398 return Result;
4399}
4400
Dan Gohman8181bd12008-07-27 21:46:04 +00004401SDValue
Evan Cheng7f250d62008-09-24 00:05:32 +00004402X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman36322c72008-10-18 02:06:02 +00004403 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004404 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004405 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4406 bool ExtraLoadRequired =
4407 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4408
4409 // Create the TargetGlobalAddress node, folding in the constant
4410 // offset if it is legal.
4411 SDValue Result;
Dan Gohman3d5257c2008-10-21 03:38:42 +00004412 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman36322c72008-10-18 02:06:02 +00004413 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4414 Offset = 0;
4415 } else
4416 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004418
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004419 // With PIC, the address is actually $g + Offset.
Dan Gohman36322c72008-10-18 02:06:02 +00004420 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004421 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4422 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4423 Result);
4424 }
4425
4426 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4427 // load the value at address GV, not the value of GV itself. This means that
4428 // the GlobalAddress must be in the base or index register of the address, not
4429 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
4430 // The same applies for external symbols during PIC codegen
Dan Gohman36322c72008-10-18 02:06:02 +00004431 if (ExtraLoadRequired)
Dan Gohman12a9c082008-02-06 22:27:42 +00004432 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004433 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004434
Dan Gohman36322c72008-10-18 02:06:02 +00004435 // If there was a non-zero offset that we didn't fold, create an explicit
4436 // addition for it.
4437 if (Offset != 0)
4438 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4439 DAG.getConstant(Offset, getPointerTy()));
4440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004441 return Result;
4442}
4443
Evan Cheng7f250d62008-09-24 00:05:32 +00004444SDValue
4445X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4446 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004447 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4448 return LowerGlobalAddress(GV, Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004449}
4450
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004451// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004452static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004453LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004454 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004455 SDValue InFlag;
4456 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004457 DAG.getNode(X86ISD::GlobalBaseReg,
4458 PtrVT), InFlag);
4459 InFlag = Chain.getValue(1);
4460
4461 // emit leal symbol@TLSGD(,%ebx,1), %eax
4462 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004463 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 GA->getValueType(0),
4465 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004466 SDValue Ops[] = { Chain, TGA, InFlag };
4467 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 InFlag = Result.getValue(2);
4469 Chain = Result.getValue(1);
4470
4471 // call ___tls_get_addr. This function receives its argument in
4472 // the register EAX.
4473 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4474 InFlag = Chain.getValue(1);
4475
4476 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004477 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004478 DAG.getTargetExternalSymbol("___tls_get_addr",
4479 PtrVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004480 DAG.getRegister(X86::EAX, PtrVT),
4481 DAG.getRegister(X86::EBX, PtrVT),
4482 InFlag };
4483 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4484 InFlag = Chain.getValue(1);
4485
4486 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4487}
4488
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004489// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004490static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004491LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004492 const MVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue InFlag, Chain;
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004494
4495 // emit leaq symbol@TLSGD(%rip), %rdi
4496 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004497 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004498 GA->getValueType(0),
4499 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004500 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4501 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004502 Chain = Result.getValue(1);
4503 InFlag = Result.getValue(2);
4504
aslb204cd52008-08-16 12:58:29 +00004505 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004506 // the register RDI.
4507 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4508 InFlag = Chain.getValue(1);
4509
4510 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00004511 SDValue Ops1[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00004512 DAG.getTargetExternalSymbol("__tls_get_addr",
4513 PtrVT),
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004514 DAG.getRegister(X86::RDI, PtrVT),
4515 InFlag };
4516 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4517 InFlag = Chain.getValue(1);
4518
4519 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4520}
4521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004522// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4523// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00004524static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands92c43912008-06-06 12:08:01 +00004525 const MVT PtrVT) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004526 // Get the Thread Pointer
Dan Gohman8181bd12008-07-27 21:46:04 +00004527 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004528 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4529 // exec)
Dan Gohman8181bd12008-07-27 21:46:04 +00004530 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004531 GA->getValueType(0),
4532 GA->getOffset());
Dan Gohman8181bd12008-07-27 21:46:04 +00004533 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534
4535 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman12a9c082008-02-06 22:27:42 +00004536 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004537 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004538
4539 // The address of the thread local variable is the add of the thread
4540 // pointer with the offset of the variable.
4541 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4542}
4543
Dan Gohman8181bd12008-07-27 21:46:04 +00004544SDValue
4545X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546 // TODO: implement the "local dynamic" model
4547 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004548 assert(Subtarget->isTargetELF() &&
4549 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4551 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4552 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004553 if (Subtarget->is64Bit()) {
4554 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4555 } else {
4556 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4557 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4558 else
4559 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4560 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561}
4562
Dan Gohman8181bd12008-07-27 21:46:04 +00004563SDValue
4564X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendlingfef06052008-09-16 21:48:12 +00004565 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4566 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004567 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4568 // With PIC, the address is actually $g + Offset.
4569 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4570 !Subtarget->isPICStyleRIPRel()) {
4571 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4572 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4573 Result);
4574 }
4575
4576 return Result;
4577}
4578
Dan Gohman8181bd12008-07-27 21:46:04 +00004579SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004580 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004582 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4583 // With PIC, the address is actually $g + Offset.
4584 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4585 !Subtarget->isPICStyleRIPRel()) {
4586 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4587 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4588 Result);
4589 }
4590
4591 return Result;
4592}
4593
Chris Lattner62814a32007-10-17 06:02:13 +00004594/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4595/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00004596SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00004597 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands92c43912008-06-06 12:08:01 +00004598 MVT VT = Op.getValueType();
4599 unsigned VTBits = VT.getSizeInBits();
Chris Lattner62814a32007-10-17 06:02:13 +00004600 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00004601 SDValue ShOpLo = Op.getOperand(0);
4602 SDValue ShOpHi = Op.getOperand(1);
4603 SDValue ShAmt = Op.getOperand(2);
4604 SDValue Tmp1 = isSRA ?
Dan Gohman092014e2008-03-03 22:22:09 +00004605 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4606 DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607
Dan Gohman8181bd12008-07-27 21:46:04 +00004608 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00004609 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman092014e2008-03-03 22:22:09 +00004610 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4611 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004612 } else {
Dan Gohman092014e2008-03-03 22:22:09 +00004613 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4614 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00004615 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004616
Dan Gohman8181bd12008-07-27 21:46:04 +00004617 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman092014e2008-03-03 22:22:09 +00004618 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00004619 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner62814a32007-10-17 06:02:13 +00004620 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004621
Dan Gohman8181bd12008-07-27 21:46:04 +00004622 SDValue Hi, Lo;
4623 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4624 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4625 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00004626
Chris Lattner62814a32007-10-17 06:02:13 +00004627 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004628 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4629 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004630 } else {
Duncan Sandsf19591c2008-06-30 10:19:09 +00004631 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4632 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00004633 }
4634
Dan Gohman8181bd12008-07-27 21:46:04 +00004635 SDValue Ops[2] = { Lo, Hi };
Duncan Sands698842f2008-07-02 17:40:58 +00004636 return DAG.getMergeValues(Ops, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004637}
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004640 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00004641 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004642 "Unknown SINT_TO_FP to lower!");
4643
4644 // These are really Legal; caller falls through into that case.
4645 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004646 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004647 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4648 Subtarget->is64Bit())
Dan Gohman8181bd12008-07-27 21:46:04 +00004649 return SDValue();
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004650
Duncan Sands92c43912008-06-06 12:08:01 +00004651 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004652 MachineFunction &MF = DAG.getMachineFunction();
4653 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman8181bd12008-07-27 21:46:04 +00004654 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4655 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman12a9c082008-02-06 22:27:42 +00004656 StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004657 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004658
4659 // Build the FILD
4660 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00004661 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004662 if (useSSE)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004663 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4664 else
4665 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004666 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004667 Ops.push_back(Chain);
4668 Ops.push_back(StackSlot);
4669 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman8181bd12008-07-27 21:46:04 +00004670 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerdd3e1422008-02-27 05:57:41 +00004671 Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672
Dale Johannesen2fc20782007-09-14 22:26:36 +00004673 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004674 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00004675 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004676
4677 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4678 // shouldn't be necessary except that RFP cannot be live across
4679 // multiple blocks. When stackifier is fixed, they can be uncoupled.
4680 MachineFunction &MF = DAG.getMachineFunction();
4681 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman8181bd12008-07-27 21:46:04 +00004682 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683 Tys = DAG.getVTList(MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004684 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004685 Ops.push_back(Chain);
4686 Ops.push_back(Result);
4687 Ops.push_back(StackSlot);
4688 Ops.push_back(DAG.getValueType(Op.getValueType()));
4689 Ops.push_back(InFlag);
4690 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman12a9c082008-02-06 22:27:42 +00004691 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004692 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004693 }
4694
4695 return Result;
4696}
4697
Dale Johannesena359b8b2008-10-21 20:50:01 +00004698SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4699 MVT SrcVT = Op.getOperand(0).getValueType();
4700 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4701
4702 // We only handle SSE2 f64 target here; caller can handle the rest.
4703 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4704 return SDValue();
4705
Dale Johannesenfb019af2008-10-21 23:07:49 +00004706 // This algorithm is not obvious. Here it is in C code, more or less:
4707/*
4708 double uint64_to_double( uint32_t hi, uint32_t lo )
4709 {
4710 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4711 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4712
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004713 // copy ints to xmm registers
Dale Johannesenfb019af2008-10-21 23:07:49 +00004714 __m128i xh = _mm_cvtsi32_si128( hi );
4715 __m128i xl = _mm_cvtsi32_si128( lo );
4716
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004717 // combine into low half of a single xmm register
Dale Johannesenfb019af2008-10-21 23:07:49 +00004718 __m128i x = _mm_unpacklo_epi32( xh, xl );
4719 __m128d d;
4720 double sd;
4721
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004722 // merge in appropriate exponents to give the integer bits the
Dale Johannesenfb019af2008-10-21 23:07:49 +00004723 // right magnitude
4724 x = _mm_unpacklo_epi32( x, exp );
4725
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004726 // subtract away the biases to deal with the IEEE-754 double precision
4727 // implicit 1
Dale Johannesenfb019af2008-10-21 23:07:49 +00004728 d = _mm_sub_pd( (__m128d) x, bias );
4729
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004730 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesenfb019af2008-10-21 23:07:49 +00004731 // calculated using the
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004732 // current rounding mode using the following horizontal add.
Dale Johannesenfb019af2008-10-21 23:07:49 +00004733 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4734 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen3299a9b2008-10-22 00:02:32 +00004735 // store doesn't really need to be here (except maybe to zero the other
4736 // double)
Dale Johannesenfb019af2008-10-21 23:07:49 +00004737 return sd;
4738 }
4739*/
4740
Dale Johannesena359b8b2008-10-21 20:50:01 +00004741 // Build some magic constants.
4742 std::vector<Constant*>CV0;
4743 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4744 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4745 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4746 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4747 Constant *C0 = ConstantVector::get(CV0);
4748 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4749
4750 std::vector<Constant*>CV1;
4751 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4752 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4753 Constant *C1 = ConstantVector::get(CV1);
4754 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4755
4756 SmallVector<SDValue, 4> MaskVec;
4757 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4758 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4759 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4760 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4761 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4762 MaskVec.size());
4763 SmallVector<SDValue, 4> MaskVec2;
Duncan Sandsca872ca2008-10-22 11:24:12 +00004764 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4765 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4766 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesena359b8b2008-10-21 20:50:01 +00004767 MaskVec2.size());
4768
4769 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004770 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4771 Op.getOperand(0),
4772 DAG.getIntPtrConstant(1)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004773 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00004774 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4775 Op.getOperand(0),
4776 DAG.getIntPtrConstant(0)));
Dale Johannesena359b8b2008-10-21 20:50:01 +00004777 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4778 XR1, XR2, UnpcklMask);
4779 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4780 PseudoSourceValue::getConstantPool(), 0, false, 16);
4781 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4782 Unpck1, CLod0, UnpcklMask);
4783 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4784 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4785 PseudoSourceValue::getConstantPool(), 0, false, 16);
4786 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4787 // Add the halves; easiest way is to swap them into another reg first.
4788 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4789 Sub, Sub, ShufMask);
4790 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4791 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4792 DAG.getIntPtrConstant(0));
4793}
4794
Dan Gohman8181bd12008-07-27 21:46:04 +00004795std::pair<SDValue,SDValue> X86TargetLowering::
4796FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00004797 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4798 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004799 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004800
Dale Johannesen2fc20782007-09-14 22:26:36 +00004801 // These are really Legal.
Dale Johannesene0e0fd02007-09-23 14:52:20 +00004802 if (Op.getValueType() == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00004803 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00004804 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00004805 if (Subtarget->is64Bit() &&
4806 Op.getValueType() == MVT::i64 &&
4807 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman8181bd12008-07-27 21:46:04 +00004808 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00004809
Evan Cheng05441e62007-10-15 20:11:21 +00004810 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4811 // stack slot.
4812 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands92c43912008-06-06 12:08:01 +00004813 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng05441e62007-10-15 20:11:21 +00004814 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman8181bd12008-07-27 21:46:04 +00004815 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004816 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +00004817 switch (Op.getValueType().getSimpleVT()) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004818 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4819 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4820 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4821 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004822 }
4823
Dan Gohman8181bd12008-07-27 21:46:04 +00004824 SDValue Chain = DAG.getEntryNode();
4825 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00004826 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman12a9c082008-02-06 22:27:42 +00004828 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman1fc34bc2008-07-11 22:44:52 +00004829 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00004831 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004832 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4833 };
4834 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4835 Chain = Value.getValue(1);
4836 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4837 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4838 }
4839
4840 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00004841 SDValue Ops[] = { Chain, Value, StackSlot };
4842 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004844 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004845}
4846
Dan Gohman8181bd12008-07-27 21:46:04 +00004847SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4848 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4849 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004850 if (FIST.getNode() == 0) return SDValue();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004851
4852 // Load the result.
4853 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4854}
4855
4856SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004857 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4858 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greif1c80d112008-08-28 21:40:38 +00004859 if (FIST.getNode() == 0) return 0;
Duncan Sandsf19591c2008-06-30 10:19:09 +00004860
4861 MVT VT = N->getValueType(0);
4862
4863 // Return a load from the stack slot.
Dan Gohman8181bd12008-07-27 21:46:04 +00004864 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004865
Duncan Sands698842f2008-07-02 17:40:58 +00004866 // Use MERGE_VALUES to drop the chain result value and get a node with one
4867 // result. This requires turning off getMergeValues simplification, since
4868 // otherwise it will give us Res back.
Gabor Greif1c80d112008-08-28 21:40:38 +00004869 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf19591c2008-06-30 10:19:09 +00004870}
Chris Lattnerdfb947d2007-11-24 07:07:01 +00004871
Dan Gohman8181bd12008-07-27 21:46:04 +00004872SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004873 MVT VT = Op.getValueType();
4874 MVT EltVT = VT;
4875 if (VT.isVector())
4876 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004877 std::vector<Constant*> CV;
4878 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004879 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 CV.push_back(C);
4881 CV.push_back(C);
4882 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004883 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004884 CV.push_back(C);
4885 CV.push_back(C);
4886 CV.push_back(C);
4887 CV.push_back(C);
4888 }
Dan Gohman11821702007-07-27 17:16:43 +00004889 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004890 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4891 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004892 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004893 false, 16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004894 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4895}
4896
Dan Gohman8181bd12008-07-27 21:46:04 +00004897SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00004898 MVT VT = Op.getValueType();
4899 MVT EltVT = VT;
Evan Cheng92b8f782007-07-19 23:36:01 +00004900 unsigned EltNum = 1;
Duncan Sands92c43912008-06-06 12:08:01 +00004901 if (VT.isVector()) {
4902 EltVT = VT.getVectorElementType();
4903 EltNum = VT.getVectorNumElements();
Evan Cheng92b8f782007-07-19 23:36:01 +00004904 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004905 std::vector<Constant*> CV;
4906 if (EltVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004907 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004908 CV.push_back(C);
4909 CV.push_back(C);
4910 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004911 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004912 CV.push_back(C);
4913 CV.push_back(C);
4914 CV.push_back(C);
4915 CV.push_back(C);
4916 }
Dan Gohman11821702007-07-27 17:16:43 +00004917 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004918 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4919 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004920 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004921 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00004922 if (VT.isVector()) {
Evan Cheng92b8f782007-07-19 23:36:01 +00004923 return DAG.getNode(ISD::BIT_CONVERT, VT,
4924 DAG.getNode(ISD::XOR, MVT::v2i64,
4925 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4926 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4927 } else {
Evan Cheng92b8f782007-07-19 23:36:01 +00004928 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4929 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004930}
4931
Dan Gohman8181bd12008-07-27 21:46:04 +00004932SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4933 SDValue Op0 = Op.getOperand(0);
4934 SDValue Op1 = Op.getOperand(1);
Duncan Sands92c43912008-06-06 12:08:01 +00004935 MVT VT = Op.getValueType();
4936 MVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004937
4938 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004939 if (SrcVT.bitsLT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4941 SrcVT = VT;
4942 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004943 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004944 if (SrcVT.bitsGT(VT)) {
Chris Lattner5872a362008-01-17 07:00:52 +00004945 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004946 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00004947 }
4948
4949 // At this point the operands and the result should have the same
4950 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004951
4952 // First get the sign bit of second operand.
4953 std::vector<Constant*> CV;
4954 if (SrcVT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004955 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4956 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004957 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004958 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4959 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4960 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4961 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004962 }
Dan Gohman11821702007-07-27 17:16:43 +00004963 Constant *C = ConstantVector::get(CV);
Dan Gohman8181bd12008-07-27 21:46:04 +00004964 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4965 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004966 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004967 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004968 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004969
4970 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00004971 if (SrcVT.bitsGT(VT)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004972 // Op0 is MVT::f32, Op1 is MVT::f64.
4973 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4974 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4975 DAG.getConstant(32, MVT::i32));
4976 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4977 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00004978 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004979 }
4980
4981 // Clear first operand sign bit.
4982 CV.clear();
4983 if (VT == MVT::f64) {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004984 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4985 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004986 } else {
Chris Lattner5e0610f2008-04-20 00:41:09 +00004987 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4989 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 }
Dan Gohman11821702007-07-27 17:16:43 +00004992 C = ConstantVector::get(CV);
4993 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman8181bd12008-07-27 21:46:04 +00004994 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004995 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00004996 false, 16);
Dan Gohman8181bd12008-07-27 21:46:04 +00004997 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004998
4999 // Or the value with the sign bit.
5000 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
5001}
5002
Dan Gohman8181bd12008-07-27 21:46:04 +00005003SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng950aac02007-09-25 01:57:46 +00005004 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 SDValue Cond;
5006 SDValue Op0 = Op.getOperand(0);
5007 SDValue Op1 = Op.getOperand(1);
5008 SDValue CC = Op.getOperand(2);
Duncan Sands92c43912008-06-06 12:08:01 +00005009 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng950aac02007-09-25 01:57:46 +00005010 unsigned X86CC;
5011
Evan Cheng950aac02007-09-25 01:57:46 +00005012 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng6afec3d2007-09-26 00:45:55 +00005013 Op0, Op1, DAG)) {
Evan Cheng621216e2007-09-29 00:00:36 +00005014 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5015 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng950aac02007-09-25 01:57:46 +00005016 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng6afec3d2007-09-26 00:45:55 +00005017 }
Evan Cheng950aac02007-09-25 01:57:46 +00005018
Evan Cheng71343822008-10-15 02:05:31 +00005019 assert(0 && "Illegal SetCC!");
5020 return SDValue();
Evan Cheng950aac02007-09-25 01:57:46 +00005021}
5022
Dan Gohman8181bd12008-07-27 21:46:04 +00005023SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5024 SDValue Cond;
5025 SDValue Op0 = Op.getOperand(0);
5026 SDValue Op1 = Op.getOperand(1);
5027 SDValue CC = Op.getOperand(2);
Nate Begeman03605a02008-07-17 16:51:19 +00005028 MVT VT = Op.getValueType();
5029 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5030 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5031
5032 if (isFP) {
5033 unsigned SSECC = 8;
Evan Cheng33754092008-08-05 22:19:15 +00005034 MVT VT0 = Op0.getValueType();
5035 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5036 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005037 bool Swap = false;
5038
5039 switch (SetCCOpcode) {
5040 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005041 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005042 case ISD::SETEQ: SSECC = 0; break;
5043 case ISD::SETOGT:
5044 case ISD::SETGT: Swap = true; // Fallthrough
5045 case ISD::SETLT:
5046 case ISD::SETOLT: SSECC = 1; break;
5047 case ISD::SETOGE:
5048 case ISD::SETGE: Swap = true; // Fallthrough
5049 case ISD::SETLE:
5050 case ISD::SETOLE: SSECC = 2; break;
5051 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005052 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005053 case ISD::SETNE: SSECC = 4; break;
5054 case ISD::SETULE: Swap = true;
5055 case ISD::SETUGE: SSECC = 5; break;
5056 case ISD::SETULT: Swap = true;
5057 case ISD::SETUGT: SSECC = 6; break;
5058 case ISD::SETO: SSECC = 7; break;
5059 }
5060 if (Swap)
5061 std::swap(Op0, Op1);
5062
Nate Begeman6357f9d2008-07-25 19:05:58 +00005063 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005064 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005065 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005066 SDValue UNORD, EQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005067 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5068 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5069 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5070 }
5071 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005072 SDValue ORD, NEQ;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005073 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5074 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5075 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5076 }
5077 assert(0 && "Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005078 }
5079 // Handle all other FP comparisons here.
5080 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5081 }
5082
5083 // We are handling one of the integer comparisons here. Since SSE only has
5084 // GT and EQ comparisons for integer, swapping operands and multiple
5085 // operations may be required for some comparisons.
5086 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5087 bool Swap = false, Invert = false, FlipSigns = false;
5088
5089 switch (VT.getSimpleVT()) {
5090 default: break;
5091 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5092 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5093 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5094 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5095 }
5096
5097 switch (SetCCOpcode) {
5098 default: break;
5099 case ISD::SETNE: Invert = true;
5100 case ISD::SETEQ: Opc = EQOpc; break;
5101 case ISD::SETLT: Swap = true;
5102 case ISD::SETGT: Opc = GTOpc; break;
5103 case ISD::SETGE: Swap = true;
5104 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5105 case ISD::SETULT: Swap = true;
5106 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5107 case ISD::SETUGE: Swap = true;
5108 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5109 }
5110 if (Swap)
5111 std::swap(Op0, Op1);
5112
5113 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5114 // bits of the inputs before performing those operations.
5115 if (FlipSigns) {
5116 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005117 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5118 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5119 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005120 SignBits.size());
5121 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5122 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5123 }
5124
Dan Gohman8181bd12008-07-27 21:46:04 +00005125 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005126
5127 // If the logical-not of the result is required, perform that now.
5128 if (Invert) {
5129 MVT EltVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005130 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5131 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5132 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman03605a02008-07-17 16:51:19 +00005133 NegOnes.size());
5134 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5135 }
5136 return Result;
5137}
Evan Cheng950aac02007-09-25 01:57:46 +00005138
Dan Gohman8181bd12008-07-27 21:46:04 +00005139SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005140 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005141 SDValue Cond = Op.getOperand(0);
5142 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005143
5144 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005145 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005146
Evan Cheng50d37ab2007-10-08 22:16:29 +00005147 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5148 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149 if (Cond.getOpcode() == X86ISD::SETCC) {
5150 CC = Cond.getOperand(0);
5151
Dan Gohman8181bd12008-07-27 21:46:04 +00005152 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005153 unsigned Opc = Cmp.getOpcode();
Duncan Sands92c43912008-06-06 12:08:01 +00005154 MVT VT = Op.getValueType();
Chris Lattnerfca7f222008-01-16 06:19:45 +00005155
Evan Cheng50d37ab2007-10-08 22:16:29 +00005156 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00005157 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005158 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00005159 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattnerfca7f222008-01-16 06:19:45 +00005160
Evan Cheng621216e2007-09-29 00:00:36 +00005161 if ((Opc == X86ISD::CMP ||
5162 Opc == X86ISD::COMI ||
5163 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005164 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005165 addTest = false;
5166 }
5167 }
5168
5169 if (addTest) {
5170 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng50d37ab2007-10-08 22:16:29 +00005171 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005172 }
5173
Duncan Sands92c43912008-06-06 12:08:01 +00005174 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng950aac02007-09-25 01:57:46 +00005175 MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005176 SmallVector<SDValue, 4> Ops;
Evan Cheng950aac02007-09-25 01:57:46 +00005177 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5178 // condition is true.
5179 Ops.push_back(Op.getOperand(2));
5180 Ops.push_back(Op.getOperand(1));
5181 Ops.push_back(CC);
5182 Ops.push_back(Cond);
Evan Cheng621216e2007-09-29 00:00:36 +00005183 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng950aac02007-09-25 01:57:46 +00005184}
5185
Dan Gohman8181bd12008-07-27 21:46:04 +00005186SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005187 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005188 SDValue Chain = Op.getOperand(0);
5189 SDValue Cond = Op.getOperand(1);
5190 SDValue Dest = Op.getOperand(2);
5191 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192
5193 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng621216e2007-09-29 00:00:36 +00005194 Cond = LowerSETCC(Cond, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005195
Evan Cheng50d37ab2007-10-08 22:16:29 +00005196 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5197 // setting operand in place of the X86ISD::SETCC.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005198 if (Cond.getOpcode() == X86ISD::SETCC) {
5199 CC = Cond.getOperand(0);
5200
Dan Gohman8181bd12008-07-27 21:46:04 +00005201 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005202 unsigned Opc = Cmp.getOpcode();
Evan Cheng621216e2007-09-29 00:00:36 +00005203 if (Opc == X86ISD::CMP ||
5204 Opc == X86ISD::COMI ||
5205 Opc == X86ISD::UCOMI) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00005206 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00005207 addTest = false;
5208 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005209 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5210 // two branches instead of an explicit OR instruction with a
5211 // separate test.
5212 } else if (Cond.getOpcode() == ISD::OR &&
5213 Cond.hasOneUse() &&
5214 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5215 Cond.getOperand(0).hasOneUse() &&
5216 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5217 Cond.getOperand(1).hasOneUse()) {
5218 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5219 unsigned Opc = Cmp.getOpcode();
5220 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5221 (Opc == X86ISD::CMP ||
5222 Opc == X86ISD::COMI ||
5223 Opc == X86ISD::UCOMI)) {
5224 CC = Cond.getOperand(0).getOperand(0);
5225 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5226 Chain, Dest, CC, Cmp);
5227 CC = Cond.getOperand(1).getOperand(0);
5228 Cond = Cmp;
5229 addTest = false;
5230 }
5231 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5232 // two branches instead of an explicit AND instruction with a
5233 // separate test. However, we only do this if this block doesn't
5234 // have a fall-through edge, because this requires an explicit
5235 // jmp when the condition is false.
5236 } else if (Cond.getOpcode() == ISD::AND &&
5237 Cond.hasOneUse() &&
5238 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5239 Cond.getOperand(0).hasOneUse() &&
5240 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5241 Cond.getOperand(1).hasOneUse()) {
5242 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5243 unsigned Opc = Cmp.getOpcode();
5244 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5245 (Opc == X86ISD::CMP ||
5246 Opc == X86ISD::COMI ||
5247 Opc == X86ISD::UCOMI) &&
5248 Op.getNode()->hasOneUse()) {
5249 X86::CondCode CCode =
5250 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5251 CCode = X86::GetOppositeBranchCondition(CCode);
5252 CC = DAG.getConstant(CCode, MVT::i8);
5253 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5254 // Look for an unconditional branch following this conditional branch.
5255 // We need this because we need to reverse the successors in order
5256 // to implement FCMP_OEQ.
5257 if (User.getOpcode() == ISD::BR) {
5258 SDValue FalseBB = User.getOperand(1);
5259 SDValue NewBR =
5260 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5261 assert(NewBR == User);
5262 Dest = FalseBB;
5263
5264 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5265 Chain, Dest, CC, Cmp);
5266 X86::CondCode CCode =
5267 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5268 CCode = X86::GetOppositeBranchCondition(CCode);
5269 CC = DAG.getConstant(CCode, MVT::i8);
5270 Cond = Cmp;
5271 addTest = false;
5272 }
5273 }
Evan Cheng950aac02007-09-25 01:57:46 +00005274 }
5275
5276 if (addTest) {
5277 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng621216e2007-09-29 00:00:36 +00005278 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng950aac02007-09-25 01:57:46 +00005279 }
Evan Cheng621216e2007-09-29 00:00:36 +00005280 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00005281 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005282}
5283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005284
5285// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5286// Calls to _alloca is needed to probe the stack when allocating more than 4k
5287// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5288// that the guard pages used by the OS virtual memory manager are allocated in
5289// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00005290SDValue
5291X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 SelectionDAG &DAG) {
5293 assert(Subtarget->isTargetCygMing() &&
5294 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005297 SDValue Chain = Op.getOperand(0);
5298 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005299 // FIXME: Ensure alignment here
5300
Dan Gohman8181bd12008-07-27 21:46:04 +00005301 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005302
Duncan Sands92c43912008-06-06 12:08:01 +00005303 MVT IntPtr = getPointerTy();
5304 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005306 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005307
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005308 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5309 Flag = Chain.getValue(1);
5310
5311 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005312 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00005313 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005314 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005315 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005316 Flag };
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005317 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 Flag = Chain.getValue(1);
5319
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005320 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00005321 DAG.getIntPtrConstant(0, true),
5322 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005323 Flag);
5324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005325 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00005326
Dan Gohman8181bd12008-07-27 21:46:04 +00005327 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands698842f2008-07-02 17:40:58 +00005328 return DAG.getMergeValues(Ops1, 2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329}
5330
Dan Gohman8181bd12008-07-27 21:46:04 +00005331SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005332X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005333 SDValue Chain,
5334 SDValue Dst, SDValue Src,
5335 SDValue Size, unsigned Align,
5336 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00005337 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005338 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005339
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005340 // If not DWORD aligned or size is more than the threshold, call the library.
5341 // The libc version is likely to be faster for these cases. It can use the
5342 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005343 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00005344 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005345 ConstantSize->getZExtValue() >
5346 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005347 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005348
5349 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00005350 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00005351
Bill Wendling4b2e3782008-10-01 00:59:58 +00005352 if (const char *bzeroEntry = V &&
5353 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5354 MVT IntPtr = getPointerTy();
5355 const Type *IntPtrTy = TD->getIntPtrType();
5356 TargetLowering::ArgListTy Args;
5357 TargetLowering::ArgListEntry Entry;
5358 Entry.Node = Dst;
5359 Entry.Ty = IntPtrTy;
5360 Args.push_back(Entry);
5361 Entry.Node = Size;
5362 Args.push_back(Entry);
5363 std::pair<SDValue,SDValue> CallResult =
5364 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5365 CallingConv::C, false,
5366 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5367 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00005368 }
5369
Dan Gohmane8b391e2008-04-12 04:36:06 +00005370 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00005371 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005372 }
5373
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005374 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00005375 SDValue InFlag(0, 0);
Duncan Sands92c43912008-06-06 12:08:01 +00005376 MVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00005377 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005378 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005379 unsigned BytesLeft = 0;
5380 bool TwoRepStos = false;
5381 if (ValC) {
5382 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005383 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005384
5385 // If the value is a constant, then we can potentially use larger sets.
5386 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005387 case 2: // WORD aligned
5388 AVT = MVT::i16;
5389 ValReg = X86::AX;
5390 Val = (Val << 8) | Val;
5391 break;
5392 case 0: // DWORD aligned
5393 AVT = MVT::i32;
5394 ValReg = X86::EAX;
5395 Val = (Val << 8) | Val;
5396 Val = (Val << 16) | Val;
5397 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5398 AVT = MVT::i64;
5399 ValReg = X86::RAX;
5400 Val = (Val << 32) | Val;
5401 }
5402 break;
5403 default: // Byte aligned
5404 AVT = MVT::i8;
5405 ValReg = X86::AL;
5406 Count = DAG.getIntPtrConstant(SizeVal);
5407 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005408 }
5409
Duncan Sandsec142ee2008-06-08 20:54:56 +00005410 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00005411 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005412 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5413 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414 }
5415
5416 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5417 InFlag);
5418 InFlag = Chain.getValue(1);
5419 } else {
5420 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00005421 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005422 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005423 InFlag = Chain.getValue(1);
5424 }
5425
5426 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5427 Count, InFlag);
5428 InFlag = Chain.getValue(1);
5429 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005430 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005431 InFlag = Chain.getValue(1);
5432
5433 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005434 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005435 Ops.push_back(Chain);
5436 Ops.push_back(DAG.getValueType(AVT));
5437 Ops.push_back(InFlag);
5438 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5439
5440 if (TwoRepStos) {
5441 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00005442 Count = Size;
Duncan Sands92c43912008-06-06 12:08:01 +00005443 MVT CVT = Count.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005444 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005445 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5446 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5447 Left, InFlag);
5448 InFlag = Chain.getValue(1);
5449 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5450 Ops.clear();
5451 Ops.push_back(Chain);
5452 Ops.push_back(DAG.getValueType(MVT::i8));
5453 Ops.push_back(InFlag);
5454 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
5455 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005456 // Handle the last 1 - 7 bytes.
5457 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005458 MVT AddrVT = Dst.getValueType();
5459 MVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005460
5461 Chain = DAG.getMemset(Chain,
5462 DAG.getNode(ISD::ADD, AddrVT, Dst,
5463 DAG.getConstant(Offset, AddrVT)),
5464 Src,
5465 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00005466 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467 }
5468
Dan Gohmane8b391e2008-04-12 04:36:06 +00005469 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005470 return Chain;
5471}
5472
Dan Gohman8181bd12008-07-27 21:46:04 +00005473SDValue
Dan Gohmane8b391e2008-04-12 04:36:06 +00005474X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005475 SDValue Chain, SDValue Dst, SDValue Src,
5476 SDValue Size, unsigned Align,
5477 bool AlwaysInline,
5478 const Value *DstSV, uint64_t DstSVOff,
5479 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005480 // This requires the copy size to be a constant, preferrably
5481 // within a subtarget-specific limit.
5482 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5483 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00005484 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005485 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005486 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00005487 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00005488
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005489 /// If not DWORD aligned, call the library.
5490 if ((Align & 3) != 0)
5491 return SDValue();
5492
5493 // DWORD aligned
5494 MVT AVT = MVT::i32;
5495 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohmane8b391e2008-04-12 04:36:06 +00005496 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005497
Duncan Sands92c43912008-06-06 12:08:01 +00005498 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00005499 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00005500 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00005501 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005502
Dan Gohman8181bd12008-07-27 21:46:04 +00005503 SDValue InFlag(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005504 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5505 Count, InFlag);
5506 InFlag = Chain.getValue(1);
5507 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005508 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005509 InFlag = Chain.getValue(1);
5510 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005511 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005512 InFlag = Chain.getValue(1);
5513
5514 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005515 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005516 Ops.push_back(Chain);
5517 Ops.push_back(DAG.getValueType(AVT));
5518 Ops.push_back(InFlag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005519 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520
Dan Gohman8181bd12008-07-27 21:46:04 +00005521 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00005522 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00005523 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00005524 // Handle the last 1 - 7 bytes.
5525 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands92c43912008-06-06 12:08:01 +00005526 MVT DstVT = Dst.getValueType();
5527 MVT SrcVT = Src.getValueType();
5528 MVT SizeVT = Size.getValueType();
Evan Cheng38d3c522008-04-25 00:26:43 +00005529 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohmane8b391e2008-04-12 04:36:06 +00005530 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00005531 DAG.getConstant(Offset, DstVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005532 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00005533 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00005534 DAG.getConstant(BytesLeft, SizeVT),
5535 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00005536 DstSV, DstSVOff + Offset,
5537 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005538 }
5539
Dan Gohmane8b391e2008-04-12 04:36:06 +00005540 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541}
5542
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005543/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5544SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005545 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00005546 SDValue TheChain = N->getOperand(0);
5547 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005548 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005549 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5550 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005551 MVT::i64, rax.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00005552 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005553 DAG.getConstant(32, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005554 SDValue Ops[] = {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005555 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005556 };
5557
Gabor Greif1c80d112008-08-28 21:40:38 +00005558 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005559 }
5560
Dan Gohman8181bd12008-07-27 21:46:04 +00005561 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5562 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005563 MVT::i32, eax.getValue(2));
5564 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman8181bd12008-07-27 21:46:04 +00005565 SDValue Ops[] = { eax, edx };
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005566 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5567
5568 // Use a MERGE_VALUES to return the value and chain.
5569 Ops[1] = edx.getValue(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00005570 return DAG.getMergeValues(Ops, 2).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571}
5572
Dan Gohman8181bd12008-07-27 21:46:04 +00005573SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00005574 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005575
5576 if (!Subtarget->is64Bit()) {
5577 // vastart just stores the address of the VarArgsFrameIndex slot into the
5578 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00005579 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005580 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005581 }
5582
5583 // __va_list_tag:
5584 // gp_offset (0 - 6 * 8)
5585 // fp_offset (48 - 48 + 8 * 16)
5586 // overflow_arg_area (point to parameters coming in memory).
5587 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00005588 SmallVector<SDValue, 8> MemOps;
5589 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 // Store gp_offset
Dan Gohman8181bd12008-07-27 21:46:04 +00005591 SDValue Store = DAG.getStore(Op.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005593 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 MemOps.push_back(Store);
5595
5596 // Store fp_offset
Chris Lattner5872a362008-01-17 07:00:52 +00005597 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 Store = DAG.getStore(Op.getOperand(0),
5599 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00005600 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005601 MemOps.push_back(Store);
5602
5603 // Store ptr to overflow_arg_area
Chris Lattner5872a362008-01-17 07:00:52 +00005604 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00005605 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005606 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607 MemOps.push_back(Store);
5608
5609 // Store ptr to reg_save_area.
Chris Lattner5872a362008-01-17 07:00:52 +00005610 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00005611 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman12a9c082008-02-06 22:27:42 +00005612 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 MemOps.push_back(Store);
5614 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
5615}
5616
Dan Gohman8181bd12008-07-27 21:46:04 +00005617SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00005618 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5619 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005620 SDValue Chain = Op.getOperand(0);
5621 SDValue SrcPtr = Op.getOperand(1);
5622 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00005623
5624 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5625 abort();
Dan Gohman8181bd12008-07-27 21:46:04 +00005626 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00005627}
5628
Dan Gohman8181bd12008-07-27 21:46:04 +00005629SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005630 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00005631 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00005632 SDValue Chain = Op.getOperand(0);
5633 SDValue DstPtr = Op.getOperand(1);
5634 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00005635 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5636 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005637
Dan Gohman840ff5c2008-04-18 20:55:41 +00005638 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5639 DAG.getIntPtrConstant(24), 8, false,
5640 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005641}
5642
Dan Gohman8181bd12008-07-27 21:46:04 +00005643SDValue
5644X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005645 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005646 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005647 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005648 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649 case Intrinsic::x86_sse_comieq_ss:
5650 case Intrinsic::x86_sse_comilt_ss:
5651 case Intrinsic::x86_sse_comile_ss:
5652 case Intrinsic::x86_sse_comigt_ss:
5653 case Intrinsic::x86_sse_comige_ss:
5654 case Intrinsic::x86_sse_comineq_ss:
5655 case Intrinsic::x86_sse_ucomieq_ss:
5656 case Intrinsic::x86_sse_ucomilt_ss:
5657 case Intrinsic::x86_sse_ucomile_ss:
5658 case Intrinsic::x86_sse_ucomigt_ss:
5659 case Intrinsic::x86_sse_ucomige_ss:
5660 case Intrinsic::x86_sse_ucomineq_ss:
5661 case Intrinsic::x86_sse2_comieq_sd:
5662 case Intrinsic::x86_sse2_comilt_sd:
5663 case Intrinsic::x86_sse2_comile_sd:
5664 case Intrinsic::x86_sse2_comigt_sd:
5665 case Intrinsic::x86_sse2_comige_sd:
5666 case Intrinsic::x86_sse2_comineq_sd:
5667 case Intrinsic::x86_sse2_ucomieq_sd:
5668 case Intrinsic::x86_sse2_ucomilt_sd:
5669 case Intrinsic::x86_sse2_ucomile_sd:
5670 case Intrinsic::x86_sse2_ucomigt_sd:
5671 case Intrinsic::x86_sse2_ucomige_sd:
5672 case Intrinsic::x86_sse2_ucomineq_sd: {
5673 unsigned Opc = 0;
5674 ISD::CondCode CC = ISD::SETCC_INVALID;
5675 switch (IntNo) {
5676 default: break;
5677 case Intrinsic::x86_sse_comieq_ss:
5678 case Intrinsic::x86_sse2_comieq_sd:
5679 Opc = X86ISD::COMI;
5680 CC = ISD::SETEQ;
5681 break;
5682 case Intrinsic::x86_sse_comilt_ss:
5683 case Intrinsic::x86_sse2_comilt_sd:
5684 Opc = X86ISD::COMI;
5685 CC = ISD::SETLT;
5686 break;
5687 case Intrinsic::x86_sse_comile_ss:
5688 case Intrinsic::x86_sse2_comile_sd:
5689 Opc = X86ISD::COMI;
5690 CC = ISD::SETLE;
5691 break;
5692 case Intrinsic::x86_sse_comigt_ss:
5693 case Intrinsic::x86_sse2_comigt_sd:
5694 Opc = X86ISD::COMI;
5695 CC = ISD::SETGT;
5696 break;
5697 case Intrinsic::x86_sse_comige_ss:
5698 case Intrinsic::x86_sse2_comige_sd:
5699 Opc = X86ISD::COMI;
5700 CC = ISD::SETGE;
5701 break;
5702 case Intrinsic::x86_sse_comineq_ss:
5703 case Intrinsic::x86_sse2_comineq_sd:
5704 Opc = X86ISD::COMI;
5705 CC = ISD::SETNE;
5706 break;
5707 case Intrinsic::x86_sse_ucomieq_ss:
5708 case Intrinsic::x86_sse2_ucomieq_sd:
5709 Opc = X86ISD::UCOMI;
5710 CC = ISD::SETEQ;
5711 break;
5712 case Intrinsic::x86_sse_ucomilt_ss:
5713 case Intrinsic::x86_sse2_ucomilt_sd:
5714 Opc = X86ISD::UCOMI;
5715 CC = ISD::SETLT;
5716 break;
5717 case Intrinsic::x86_sse_ucomile_ss:
5718 case Intrinsic::x86_sse2_ucomile_sd:
5719 Opc = X86ISD::UCOMI;
5720 CC = ISD::SETLE;
5721 break;
5722 case Intrinsic::x86_sse_ucomigt_ss:
5723 case Intrinsic::x86_sse2_ucomigt_sd:
5724 Opc = X86ISD::UCOMI;
5725 CC = ISD::SETGT;
5726 break;
5727 case Intrinsic::x86_sse_ucomige_ss:
5728 case Intrinsic::x86_sse2_ucomige_sd:
5729 Opc = X86ISD::UCOMI;
5730 CC = ISD::SETGE;
5731 break;
5732 case Intrinsic::x86_sse_ucomineq_ss:
5733 case Intrinsic::x86_sse2_ucomineq_sd:
5734 Opc = X86ISD::UCOMI;
5735 CC = ISD::SETNE;
5736 break;
5737 }
5738
5739 unsigned X86CC;
Dan Gohman8181bd12008-07-27 21:46:04 +00005740 SDValue LHS = Op.getOperand(1);
5741 SDValue RHS = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005742 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5743
Dan Gohman8181bd12008-07-27 21:46:04 +00005744 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5745 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng89c17632008-08-17 19:22:34 +00005746 DAG.getConstant(X86CC, MVT::i8), Cond);
5747 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005748 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005749
5750 // Fix vector shift instructions where the last operand is a non-immediate
5751 // i32 value.
5752 case Intrinsic::x86_sse2_pslli_w:
5753 case Intrinsic::x86_sse2_pslli_d:
5754 case Intrinsic::x86_sse2_pslli_q:
5755 case Intrinsic::x86_sse2_psrli_w:
5756 case Intrinsic::x86_sse2_psrli_d:
5757 case Intrinsic::x86_sse2_psrli_q:
5758 case Intrinsic::x86_sse2_psrai_w:
5759 case Intrinsic::x86_sse2_psrai_d:
5760 case Intrinsic::x86_mmx_pslli_w:
5761 case Intrinsic::x86_mmx_pslli_d:
5762 case Intrinsic::x86_mmx_pslli_q:
5763 case Intrinsic::x86_mmx_psrli_w:
5764 case Intrinsic::x86_mmx_psrli_d:
5765 case Intrinsic::x86_mmx_psrli_q:
5766 case Intrinsic::x86_mmx_psrai_w:
5767 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00005768 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005769 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005770 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005771
5772 unsigned NewIntNo = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00005773 MVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005774 switch (IntNo) {
5775 case Intrinsic::x86_sse2_pslli_w:
5776 NewIntNo = Intrinsic::x86_sse2_psll_w;
5777 break;
5778 case Intrinsic::x86_sse2_pslli_d:
5779 NewIntNo = Intrinsic::x86_sse2_psll_d;
5780 break;
5781 case Intrinsic::x86_sse2_pslli_q:
5782 NewIntNo = Intrinsic::x86_sse2_psll_q;
5783 break;
5784 case Intrinsic::x86_sse2_psrli_w:
5785 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5786 break;
5787 case Intrinsic::x86_sse2_psrli_d:
5788 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5789 break;
5790 case Intrinsic::x86_sse2_psrli_q:
5791 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5792 break;
5793 case Intrinsic::x86_sse2_psrai_w:
5794 NewIntNo = Intrinsic::x86_sse2_psra_w;
5795 break;
5796 case Intrinsic::x86_sse2_psrai_d:
5797 NewIntNo = Intrinsic::x86_sse2_psra_d;
5798 break;
5799 default: {
5800 ShAmtVT = MVT::v2i32;
5801 switch (IntNo) {
5802 case Intrinsic::x86_mmx_pslli_w:
5803 NewIntNo = Intrinsic::x86_mmx_psll_w;
5804 break;
5805 case Intrinsic::x86_mmx_pslli_d:
5806 NewIntNo = Intrinsic::x86_mmx_psll_d;
5807 break;
5808 case Intrinsic::x86_mmx_pslli_q:
5809 NewIntNo = Intrinsic::x86_mmx_psll_q;
5810 break;
5811 case Intrinsic::x86_mmx_psrli_w:
5812 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5813 break;
5814 case Intrinsic::x86_mmx_psrli_d:
5815 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5816 break;
5817 case Intrinsic::x86_mmx_psrli_q:
5818 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5819 break;
5820 case Intrinsic::x86_mmx_psrai_w:
5821 NewIntNo = Intrinsic::x86_mmx_psra_w;
5822 break;
5823 case Intrinsic::x86_mmx_psrai_d:
5824 NewIntNo = Intrinsic::x86_mmx_psra_d;
5825 break;
5826 default: abort(); // Can't reach here.
5827 }
5828 break;
5829 }
5830 }
Duncan Sands92c43912008-06-06 12:08:01 +00005831 MVT VT = Op.getValueType();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00005832 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5833 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5834 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5835 DAG.getConstant(NewIntNo, MVT::i32),
5836 Op.getOperand(1), ShAmt);
5837 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 }
5839}
5840
Dan Gohman8181bd12008-07-27 21:46:04 +00005841SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005842 // Depths > 0 not supported yet!
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005843 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00005844 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005845
5846 // Just load the return address
Dan Gohman8181bd12008-07-27 21:46:04 +00005847 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5849}
5850
Dan Gohman8181bd12008-07-27 21:46:04 +00005851SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00005852 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5853 MFI->setFrameAddressIsTaken(true);
5854 MVT VT = Op.getValueType();
5855 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5856 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5857 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5858 while (Depth--)
5859 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5860 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005861}
5862
Dan Gohman8181bd12008-07-27 21:46:04 +00005863SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00005864 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005865 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005866}
5867
Dan Gohman8181bd12008-07-27 21:46:04 +00005868SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005869{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005870 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00005871 SDValue Chain = Op.getOperand(0);
5872 SDValue Offset = Op.getOperand(1);
5873 SDValue Handler = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005874
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005875 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5876 getPointerTy());
5877 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005878
Dan Gohman8181bd12008-07-27 21:46:04 +00005879 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005880 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005881 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5882 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005883 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5884 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005885
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00005886 return DAG.getNode(X86ISD::EH_RETURN,
5887 MVT::Other,
5888 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005889}
5890
Dan Gohman8181bd12008-07-27 21:46:04 +00005891SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005892 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005893 SDValue Root = Op.getOperand(0);
5894 SDValue Trmp = Op.getOperand(1); // trampoline
5895 SDValue FPtr = Op.getOperand(2); // nested function
5896 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005897
Dan Gohman12a9c082008-02-06 22:27:42 +00005898 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005899
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005900 const X86InstrInfo *TII =
5901 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5902
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005903 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005904 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005905
5906 // Large code-model.
5907
5908 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5909 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5910
Dan Gohmanb41dfba2008-05-14 01:58:56 +00005911 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5912 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005913
5914 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5915
5916 // Load the pointer to the nested function into R11.
5917 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00005918 SDValue Addr = Trmp;
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005919 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005920 TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005921
5922 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005923 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005924
5925 // Load the 'nest' parameter value into R10.
5926 // R10 is specified in X86CallingConv.td
5927 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5928 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5929 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005930 TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005931
5932 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman12a9c082008-02-06 22:27:42 +00005933 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005934
5935 // Jump to the nested function.
5936 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5937 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5938 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005939 TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005940
5941 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5942 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5943 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00005944 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005945
Dan Gohman8181bd12008-07-27 21:46:04 +00005946 SDValue Ops[] =
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005947 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands698842f2008-07-02 17:40:58 +00005948 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005949 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00005950 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005951 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5952 unsigned CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00005953 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005954
5955 switch (CC) {
5956 default:
5957 assert(0 && "Unsupported calling convention");
5958 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005959 case CallingConv::X86_StdCall: {
5960 // Pass 'nest' parameter in ECX.
5961 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005962 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005963
5964 // Check that ECX wasn't needed by an 'inreg' parameter.
5965 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00005966 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005967
Chris Lattner1c8733e2008-03-12 17:45:29 +00005968 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005969 unsigned InRegCount = 0;
5970 unsigned Idx = 1;
5971
5972 for (FunctionType::param_iterator I = FTy->param_begin(),
5973 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00005974 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005975 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00005976 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005977
5978 if (InRegCount > 2) {
5979 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5980 abort();
5981 }
5982 }
5983 break;
5984 }
5985 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00005986 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005987 // Pass 'nest' parameter in EAX.
5988 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00005989 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005990 break;
5991 }
5992
Dan Gohman8181bd12008-07-27 21:46:04 +00005993 SDValue OutChains[4];
5994 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00005995
5996 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5997 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5998
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00005999 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006000 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sands466eadd2007-08-29 19:01:20 +00006001 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006002 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006003
6004 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006005 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006006
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006007 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006008 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6009 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006010 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006011
6012 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman12a9c082008-02-06 22:27:42 +00006013 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006014
Dan Gohman8181bd12008-07-27 21:46:04 +00006015 SDValue Ops[] =
Duncan Sands7407a9f2007-09-11 14:10:23 +00006016 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands698842f2008-07-02 17:40:58 +00006017 return DAG.getMergeValues(Ops, 2);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006018 }
6019}
6020
Dan Gohman8181bd12008-07-27 21:46:04 +00006021SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006022 /*
6023 The rounding mode is in bits 11:10 of FPSR, and has the following
6024 settings:
6025 00 Round to nearest
6026 01 Round to -inf
6027 10 Round to +inf
6028 11 Round to 0
6029
6030 FLT_ROUNDS, on the other hand, expects the following:
6031 -1 Undefined
6032 0 Round to 0
6033 1 Round to nearest
6034 2 Round to +inf
6035 3 Round to -inf
6036
6037 To perform the conversion, we do:
6038 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6039 */
6040
6041 MachineFunction &MF = DAG.getMachineFunction();
6042 const TargetMachine &TM = MF.getTarget();
6043 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6044 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands92c43912008-06-06 12:08:01 +00006045 MVT VT = Op.getValueType();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006046
6047 // Save FP Control Word to stack slot
6048 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman8181bd12008-07-27 21:46:04 +00006049 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006050
Dan Gohman8181bd12008-07-27 21:46:04 +00006051 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00006052 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006053
6054 // Load FP Control Word from stack slot
Dan Gohman8181bd12008-07-27 21:46:04 +00006055 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006056
6057 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00006058 SDValue CWD1 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006059 DAG.getNode(ISD::SRL, MVT::i16,
6060 DAG.getNode(ISD::AND, MVT::i16,
6061 CWD, DAG.getConstant(0x800, MVT::i16)),
6062 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006063 SDValue CWD2 =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006064 DAG.getNode(ISD::SRL, MVT::i16,
6065 DAG.getNode(ISD::AND, MVT::i16,
6066 CWD, DAG.getConstant(0x400, MVT::i16)),
6067 DAG.getConstant(9, MVT::i8));
6068
Dan Gohman8181bd12008-07-27 21:46:04 +00006069 SDValue RetVal =
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006070 DAG.getNode(ISD::AND, MVT::i16,
6071 DAG.getNode(ISD::ADD, MVT::i16,
6072 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6073 DAG.getConstant(1, MVT::i16)),
6074 DAG.getConstant(3, MVT::i16));
6075
6076
Duncan Sands92c43912008-06-06 12:08:01 +00006077 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006078 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6079}
6080
Dan Gohman8181bd12008-07-27 21:46:04 +00006081SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006082 MVT VT = Op.getValueType();
6083 MVT OpVT = VT;
6084 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006085
6086 Op = Op.getOperand(0);
6087 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006088 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng48679f42007-12-14 02:13:44 +00006089 OpVT = MVT::i32;
6090 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6091 }
Evan Cheng48679f42007-12-14 02:13:44 +00006092
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006093 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6094 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6095 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6096
6097 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006098 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006099 Ops.push_back(Op);
6100 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6101 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6102 Ops.push_back(Op.getValue(1));
6103 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6104
6105 // Finally xor with NumBits-1.
6106 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6107
Evan Cheng48679f42007-12-14 02:13:44 +00006108 if (VT == MVT::i8)
6109 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6110 return Op;
6111}
6112
Dan Gohman8181bd12008-07-27 21:46:04 +00006113SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands92c43912008-06-06 12:08:01 +00006114 MVT VT = Op.getValueType();
6115 MVT OpVT = VT;
6116 unsigned NumBits = VT.getSizeInBits();
Evan Cheng48679f42007-12-14 02:13:44 +00006117
6118 Op = Op.getOperand(0);
6119 if (VT == MVT::i8) {
6120 OpVT = MVT::i32;
6121 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6122 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006123
6124 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6125 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6126 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6127
6128 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman8181bd12008-07-27 21:46:04 +00006129 SmallVector<SDValue, 4> Ops;
Evan Cheng7cfbfe32007-12-14 08:30:15 +00006130 Ops.push_back(Op);
6131 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6132 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6133 Ops.push_back(Op.getValue(1));
6134 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6135
Evan Cheng48679f42007-12-14 02:13:44 +00006136 if (VT == MVT::i8)
6137 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6138 return Op;
6139}
6140
Dan Gohman8181bd12008-07-27 21:46:04 +00006141SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006142 MVT T = Op.getValueType();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00006143 unsigned Reg = 0;
6144 unsigned size = 0;
Duncan Sands92c43912008-06-06 12:08:01 +00006145 switch(T.getSimpleVT()) {
6146 default:
6147 assert(false && "Invalid value type!");
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006148 case MVT::i8: Reg = X86::AL; size = 1; break;
6149 case MVT::i16: Reg = X86::AX; size = 2; break;
6150 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharth81580822008-03-05 01:15:49 +00006151 case MVT::i64:
6152 if (Subtarget->is64Bit()) {
6153 Reg = X86::RAX; size = 8;
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006154 } else //Should go away when LegalizeType stuff lands
Gabor Greif1c80d112008-08-28 21:40:38 +00006155 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharth81580822008-03-05 01:15:49 +00006156 break;
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006157 };
Dan Gohman8181bd12008-07-27 21:46:04 +00006158 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00006159 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00006160 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006161 Op.getOperand(1),
6162 Op.getOperand(3),
6163 DAG.getTargetConstant(size, MVT::i8),
6164 cpIn.getValue(1) };
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006165 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006166 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6167 SDValue cpOut =
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00006168 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6169 return cpOut;
6170}
6171
Gabor Greif825aa892008-08-28 23:19:51 +00006172SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
6173 SelectionDAG &DAG) {
Dan Gohmanc70fa752008-06-25 16:07:49 +00006174 MVT T = Op->getValueType(0);
Mon P Wang6bde9ec2008-06-25 08:15:39 +00006175 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman8181bd12008-07-27 21:46:04 +00006176 SDValue cpInL, cpInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006177 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006178 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006179 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharth81580822008-03-05 01:15:49 +00006180 DAG.getConstant(1, MVT::i32));
6181 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman8181bd12008-07-27 21:46:04 +00006182 cpInL, SDValue());
Andrew Lenharth81580822008-03-05 01:15:49 +00006183 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
6184 cpInH, cpInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006185 SDValue swapInL, swapInH;
Dale Johannesenddb761b2008-09-11 03:12:59 +00006186 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006187 DAG.getConstant(0, MVT::i32));
Dale Johannesenddb761b2008-09-11 03:12:59 +00006188 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharth81580822008-03-05 01:15:49 +00006189 DAG.getConstant(1, MVT::i32));
6190 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6191 swapInL, cpInH.getValue(1));
6192 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6193 swapInH, swapInL.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006194 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00006195 Op->getOperand(1),
6196 swapInH.getValue(1) };
Andrew Lenharth81580822008-03-05 01:15:49 +00006197 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006198 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6199 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006200 Result.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00006201 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharth81580822008-03-05 01:15:49 +00006202 cpOutL.getValue(2));
Dan Gohman8181bd12008-07-27 21:46:04 +00006203 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6204 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6205 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greif1c80d112008-08-28 21:40:38 +00006206 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharth81580822008-03-05 01:15:49 +00006207}
6208
Dale Johannesenf160d802008-10-02 18:53:47 +00006209SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6210 SelectionDAG &DAG,
6211 unsigned NewOp) {
6212 SDNode *Node = Op.getNode();
6213 MVT T = Node->getValueType(0);
6214 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6215
6216 SDValue Chain = Node->getOperand(0);
6217 SDValue In1 = Node->getOperand(1);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006218 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6219 Node->getOperand(2), DAG.getIntPtrConstant(0));
6220 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6221 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dale Johannesen44eb5372008-10-03 19:41:08 +00006222 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6223 // have a MemOperand. Pass the info through as a normal operand.
6224 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6225 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesenf160d802008-10-02 18:53:47 +00006226 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen44eb5372008-10-03 19:41:08 +00006227 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesenf160d802008-10-02 18:53:47 +00006228 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6229 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6230 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6231 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6232}
6233
Dale Johannesen9011d872008-09-29 22:25:26 +00006234SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6235 SDNode *Node = Op.getNode();
6236 MVT T = Node->getValueType(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00006237 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen9011d872008-09-29 22:25:26 +00006238 DAG.getConstant(0, T), Node->getOperand(2));
6239 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6240 ISD::ATOMIC_LOAD_ADD_8 :
6241 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6242 ISD::ATOMIC_LOAD_ADD_16 :
6243 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6244 ISD::ATOMIC_LOAD_ADD_32 :
6245 ISD::ATOMIC_LOAD_ADD_64),
6246 Node->getOperand(0),
6247 Node->getOperand(1), negOp,
6248 cast<AtomicSDNode>(Node)->getSrcValue(),
6249 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00006250}
6251
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006252/// LowerOperation - Provide custom lowering hooks for some operations.
6253///
Dan Gohman8181bd12008-07-27 21:46:04 +00006254SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006255 switch (Op.getOpcode()) {
6256 default: assert(0 && "Should not custom lower this!");
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006257 case ISD::ATOMIC_CMP_SWAP_8:
6258 case ISD::ATOMIC_CMP_SWAP_16:
6259 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesenbc187662008-08-28 02:44:49 +00006260 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006261 case ISD::ATOMIC_LOAD_SUB_8:
6262 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen9011d872008-09-29 22:25:26 +00006263 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesenf160d802008-10-02 18:53:47 +00006264 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006265 LowerLOAD_SUB(Op,DAG) :
6266 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006267 X86ISD::ATOMSUB64_DAG);
6268 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6269 X86ISD::ATOMAND64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006270 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006271 X86ISD::ATOMOR64_DAG);
6272 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6273 X86ISD::ATOMXOR64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006274 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesenf160d802008-10-02 18:53:47 +00006275 X86ISD::ATOMNAND64_DAG);
6276 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6277 X86ISD::ATOMADD64_DAG);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006278 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6279 X86ISD::ATOMSWAP64_DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006280 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6281 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6282 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6283 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6284 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6285 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6286 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
6287 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00006288 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 case ISD::SHL_PARTS:
6290 case ISD::SRA_PARTS:
6291 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6292 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00006293 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006294 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6295 case ISD::FABS: return LowerFABS(Op, DAG);
6296 case ISD::FNEG: return LowerFNEG(Op, DAG);
6297 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006298 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00006299 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00006300 case ISD::SELECT: return LowerSELECT(Op, DAG);
6301 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
6303 case ISD::CALL: return LowerCALL(Op, DAG);
6304 case ISD::RET: return LowerRET(Op, DAG);
6305 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006306 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006307 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006308 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
6309 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
6310 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6311 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
6312 case ISD::FRAME_TO_ARGS_OFFSET:
6313 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
6314 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
6315 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006316 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00006317 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00006318 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6319 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006320
6321 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6322 case ISD::READCYCLECOUNTER:
Gabor Greif1c80d112008-08-28 21:40:38 +00006323 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006324 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006325}
6326
Duncan Sandsac496a12008-07-04 11:47:58 +00006327/// ReplaceNodeResults - Replace a node with an illegal result type
6328/// with a new node built out of custom code.
6329SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006330 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00006331 default:
6332 return X86TargetLowering::LowerOperation(SDValue (N, 0), DAG).getNode();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006333 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6334 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesenbc187662008-08-28 02:44:49 +00006335 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00006336 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006337}
6338
6339const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6340 switch (Opcode) {
6341 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00006342 case X86ISD::BSF: return "X86ISD::BSF";
6343 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006344 case X86ISD::SHLD: return "X86ISD::SHLD";
6345 case X86ISD::SHRD: return "X86ISD::SHRD";
6346 case X86ISD::FAND: return "X86ISD::FAND";
6347 case X86ISD::FOR: return "X86ISD::FOR";
6348 case X86ISD::FXOR: return "X86ISD::FXOR";
6349 case X86ISD::FSRL: return "X86ISD::FSRL";
6350 case X86ISD::FILD: return "X86ISD::FILD";
6351 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
6352 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6353 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6354 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
6355 case X86ISD::FLD: return "X86ISD::FLD";
6356 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006357 case X86ISD::CALL: return "X86ISD::CALL";
6358 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6359 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6360 case X86ISD::CMP: return "X86ISD::CMP";
6361 case X86ISD::COMI: return "X86ISD::COMI";
6362 case X86ISD::UCOMI: return "X86ISD::UCOMI";
6363 case X86ISD::SETCC: return "X86ISD::SETCC";
6364 case X86ISD::CMOV: return "X86ISD::CMOV";
6365 case X86ISD::BRCOND: return "X86ISD::BRCOND";
6366 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
6367 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6368 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
6370 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begemand77e59e2008-02-11 04:19:36 +00006371 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00006373 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6374 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006375 case X86ISD::PINSRW: return "X86ISD::PINSRW";
6376 case X86ISD::FMAX: return "X86ISD::FMAX";
6377 case X86ISD::FMIN: return "X86ISD::FMIN";
6378 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6379 case X86ISD::FRCP: return "X86ISD::FRCP";
6380 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6381 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
6382 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00006383 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00006384 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00006385 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6386 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00006387 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6388 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6389 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6390 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6391 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6392 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00006393 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6394 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00006395 case X86ISD::VSHL: return "X86ISD::VSHL";
6396 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00006397 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6398 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6399 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6400 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6401 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6402 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6403 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6404 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6405 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6406 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006407 }
6408}
6409
6410// isLegalAddressingMode - Return true if the addressing mode represented
6411// by AM is legal for this target, for a load/store of the specified type.
6412bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6413 const Type *Ty) const {
6414 // X86 supports extremely general addressing modes.
6415
6416 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6417 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6418 return false;
6419
6420 if (AM.BaseGV) {
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006421 // We can only fold this if we don't need an extra load.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006422 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6423 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00006424
6425 // X86-64 only supports addr of globals in small code model.
6426 if (Subtarget->is64Bit()) {
6427 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6428 return false;
6429 // If lower 4G is not available, then we must use rip-relative addressing.
6430 if (AM.BaseOffs || AM.Scale > 1)
6431 return false;
6432 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006433 }
6434
6435 switch (AM.Scale) {
6436 case 0:
6437 case 1:
6438 case 2:
6439 case 4:
6440 case 8:
6441 // These scales always work.
6442 break;
6443 case 3:
6444 case 5:
6445 case 9:
6446 // These scales are formed with basereg+scalereg. Only accept if there is
6447 // no basereg yet.
6448 if (AM.HasBaseReg)
6449 return false;
6450 break;
6451 default: // Other stuff never works.
6452 return false;
6453 }
6454
6455 return true;
6456}
6457
6458
Evan Cheng27a820a2007-10-26 01:56:11 +00006459bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6460 if (!Ty1->isInteger() || !Ty2->isInteger())
6461 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00006462 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6463 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006464 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00006465 return false;
6466 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00006467}
6468
Duncan Sands92c43912008-06-06 12:08:01 +00006469bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6470 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00006471 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00006472 unsigned NumBits1 = VT1.getSizeInBits();
6473 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00006474 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00006475 return false;
6476 return Subtarget->is64Bit() || NumBits1 < 64;
6477}
Evan Cheng27a820a2007-10-26 01:56:11 +00006478
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479/// isShuffleMaskLegal - Targets can use this to indicate that they only
6480/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6481/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6482/// are assumed to be legal.
6483bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006484X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006485 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006486 if (VT.getSizeInBits() == 64) return false;
Gabor Greif1c80d112008-08-28 21:40:38 +00006487 return (Mask.getNode()->getNumOperands() <= 4 ||
6488 isIdentityMask(Mask.getNode()) ||
6489 isIdentityMask(Mask.getNode(), true) ||
6490 isSplatMask(Mask.getNode()) ||
6491 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6492 X86::isUNPCKLMask(Mask.getNode()) ||
6493 X86::isUNPCKHMask(Mask.getNode()) ||
6494 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6495 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496}
6497
Dan Gohman48d5f062008-04-09 20:09:42 +00006498bool
Dan Gohman8181bd12008-07-27 21:46:04 +00006499X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands92c43912008-06-06 12:08:01 +00006500 MVT EVT, SelectionDAG &DAG) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006501 unsigned NumElts = BVOps.size();
6502 // Only do shuffles on 128-bit vector types for now.
Duncan Sands92c43912008-06-06 12:08:01 +00006503 if (EVT.getSizeInBits() * NumElts == 64) return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504 if (NumElts == 2) return true;
6505 if (NumElts == 4) {
6506 return (isMOVLMask(&BVOps[0], 4) ||
6507 isCommutedMOVL(&BVOps[0], 4, true) ||
6508 isSHUFPMask(&BVOps[0], 4) ||
6509 isCommutedSHUFP(&BVOps[0], 4));
6510 }
6511 return false;
6512}
6513
6514//===----------------------------------------------------------------------===//
6515// X86 Scheduler Hooks
6516//===----------------------------------------------------------------------===//
6517
Mon P Wang078a62d2008-05-05 19:05:59 +00006518// private utility function
6519MachineBasicBlock *
6520X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6521 MachineBasicBlock *MBB,
6522 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006523 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00006524 unsigned LoadOpc,
6525 unsigned CXchgOpc,
6526 unsigned copyOpc,
6527 unsigned notOpc,
6528 unsigned EAXreg,
6529 TargetRegisterClass *RC,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006530 bool invSrc) {
Mon P Wang078a62d2008-05-05 19:05:59 +00006531 // For the atomic bitwise operator, we generate
6532 // thisMBB:
6533 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006534 // ld t1 = [bitinstr.addr]
6535 // op t2 = t1, [bitinstr.val]
6536 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006537 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6538 // bz newMBB
6539 // fallthrough -->nextMBB
6540 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6541 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006542 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006543 ++MBBIter;
6544
6545 /// First build the CFG
6546 MachineFunction *F = MBB->getParent();
6547 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006548 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6549 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6550 F->insert(MBBIter, newMBB);
6551 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006552
6553 // Move all successors to thisMBB to nextMBB
6554 nextMBB->transferSuccessors(thisMBB);
6555
6556 // Update thisMBB to fall through to newMBB
6557 thisMBB->addSuccessor(newMBB);
6558
6559 // newMBB jumps to itself and fall through to nextMBB
6560 newMBB->addSuccessor(nextMBB);
6561 newMBB->addSuccessor(newMBB);
6562
6563 // Insert instructions into newMBB based on incoming instruction
6564 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6565 MachineOperand& destOper = bInstr->getOperand(0);
6566 MachineOperand* argOpers[6];
6567 int numArgs = bInstr->getNumOperands() - 1;
6568 for (int i=0; i < numArgs; ++i)
6569 argOpers[i] = &bInstr->getOperand(i+1);
6570
6571 // x86 address has 4 operands: base, index, scale, and displacement
6572 int lastAddrIndx = 3; // [0,3]
6573 int valArgIndx = 4;
6574
Dale Johannesend20e4452008-08-19 18:47:28 +00006575 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6576 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006577 for (int i=0; i <= lastAddrIndx; ++i)
6578 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006579
Dale Johannesend20e4452008-08-19 18:47:28 +00006580 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006581 if (invSrc) {
Dale Johannesend20e4452008-08-19 18:47:28 +00006582 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006583 }
6584 else
6585 tt = t1;
6586
Dale Johannesend20e4452008-08-19 18:47:28 +00006587 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006588 assert((argOpers[valArgIndx]->isReg() ||
6589 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006590 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006591 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006592 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6593 else
6594 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006595 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00006596 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00006597
Dale Johannesend20e4452008-08-19 18:47:28 +00006598 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00006599 MIB.addReg(t1);
6600
Dale Johannesend20e4452008-08-19 18:47:28 +00006601 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00006602 for (int i=0; i <= lastAddrIndx; ++i)
6603 (*MIB).addOperand(*argOpers[i]);
6604 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00006605 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6606 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6607
Dale Johannesend20e4452008-08-19 18:47:28 +00006608 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6609 MIB.addReg(EAXreg);
Mon P Wang078a62d2008-05-05 19:05:59 +00006610
6611 // insert branch
6612 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6613
Dan Gohman221a4372008-07-07 23:14:23 +00006614 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006615 return nextMBB;
6616}
6617
Dale Johannesen44eb5372008-10-03 19:41:08 +00006618// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00006619MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00006620X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6621 MachineBasicBlock *MBB,
6622 unsigned regOpcL,
6623 unsigned regOpcH,
6624 unsigned immOpcL,
6625 unsigned immOpcH,
6626 bool invSrc) {
6627 // For the atomic bitwise operator, we generate
6628 // thisMBB (instructions are in pairs, except cmpxchg8b)
6629 // ld t1,t2 = [bitinstr.addr]
6630 // newMBB:
6631 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6632 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006633 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00006634 // mov ECX, EBX <- t5, t6
6635 // mov EAX, EDX <- t1, t2
6636 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6637 // mov t3, t4 <- EAX, EDX
6638 // bz newMBB
6639 // result in out1, out2
6640 // fallthrough -->nextMBB
6641
6642 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6643 const unsigned LoadOpc = X86::MOV32rm;
6644 const unsigned copyOpc = X86::MOV32rr;
6645 const unsigned NotOpc = X86::NOT32r;
6646 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6647 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6648 MachineFunction::iterator MBBIter = MBB;
6649 ++MBBIter;
6650
6651 /// First build the CFG
6652 MachineFunction *F = MBB->getParent();
6653 MachineBasicBlock *thisMBB = MBB;
6654 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6655 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6656 F->insert(MBBIter, newMBB);
6657 F->insert(MBBIter, nextMBB);
6658
6659 // Move all successors to thisMBB to nextMBB
6660 nextMBB->transferSuccessors(thisMBB);
6661
6662 // Update thisMBB to fall through to newMBB
6663 thisMBB->addSuccessor(newMBB);
6664
6665 // newMBB jumps to itself and fall through to nextMBB
6666 newMBB->addSuccessor(nextMBB);
6667 newMBB->addSuccessor(newMBB);
6668
6669 // Insert instructions into newMBB based on incoming instruction
6670 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6671 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6672 MachineOperand& dest1Oper = bInstr->getOperand(0);
6673 MachineOperand& dest2Oper = bInstr->getOperand(1);
6674 MachineOperand* argOpers[6];
6675 for (int i=0; i < 6; ++i)
6676 argOpers[i] = &bInstr->getOperand(i+2);
6677
6678 // x86 address has 4 operands: base, index, scale, and displacement
6679 int lastAddrIndx = 3; // [0,3]
6680
6681 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6682 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6683 for (int i=0; i <= lastAddrIndx; ++i)
6684 (*MIB).addOperand(*argOpers[i]);
6685 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6686 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006687 // add 4 to displacement.
Dale Johannesenf160d802008-10-02 18:53:47 +00006688 for (int i=0; i <= lastAddrIndx-1; ++i)
6689 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006690 MachineOperand newOp3 = *(argOpers[3]);
6691 if (newOp3.isImm())
6692 newOp3.setImm(newOp3.getImm()+4);
6693 else
6694 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00006695 (*MIB).addOperand(newOp3);
6696
6697 // t3/4 are defined later, at the bottom of the loop
6698 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6699 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6700 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6701 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6702 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6703 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6704
6705 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6706 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6707 if (invSrc) {
6708 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6709 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6710 } else {
6711 tt1 = t1;
6712 tt2 = t2;
6713 }
6714
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006715 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00006716 "invalid operand");
6717 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6718 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006719 if (argOpers[4]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006720 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6721 else
6722 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006723 if (regOpcL != X86::MOV32rr)
6724 MIB.addReg(tt1);
Dale Johannesenf160d802008-10-02 18:53:47 +00006725 (*MIB).addOperand(*argOpers[4]);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006726 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6727 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6728 if (argOpers[5]->isReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00006729 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6730 else
6731 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00006732 if (regOpcH != X86::MOV32rr)
6733 MIB.addReg(tt2);
Dale Johannesenf160d802008-10-02 18:53:47 +00006734 (*MIB).addOperand(*argOpers[5]);
6735
6736 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6737 MIB.addReg(t1);
6738 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6739 MIB.addReg(t2);
6740
6741 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6742 MIB.addReg(t5);
6743 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6744 MIB.addReg(t6);
6745
6746 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6747 for (int i=0; i <= lastAddrIndx; ++i)
6748 (*MIB).addOperand(*argOpers[i]);
6749
6750 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6751 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6752
6753 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6754 MIB.addReg(X86::EAX);
6755 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6756 MIB.addReg(X86::EDX);
6757
6758 // insert branch
6759 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6760
6761 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6762 return nextMBB;
6763}
6764
6765// private utility function
6766MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00006767X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6768 MachineBasicBlock *MBB,
6769 unsigned cmovOpc) {
6770 // For the atomic min/max operator, we generate
6771 // thisMBB:
6772 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00006773 // ld t1 = [min/max.addr]
Mon P Wang078a62d2008-05-05 19:05:59 +00006774 // mov t2 = [min/max.val]
6775 // cmp t1, t2
6776 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00006777 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00006778 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6779 // bz newMBB
6780 // fallthrough -->nextMBB
6781 //
6782 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6783 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006784 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00006785 ++MBBIter;
6786
6787 /// First build the CFG
6788 MachineFunction *F = MBB->getParent();
6789 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00006790 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6791 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6792 F->insert(MBBIter, newMBB);
6793 F->insert(MBBIter, nextMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006794
6795 // Move all successors to thisMBB to nextMBB
6796 nextMBB->transferSuccessors(thisMBB);
6797
6798 // Update thisMBB to fall through to newMBB
6799 thisMBB->addSuccessor(newMBB);
6800
6801 // newMBB jumps to newMBB and fall through to nextMBB
6802 newMBB->addSuccessor(nextMBB);
6803 newMBB->addSuccessor(newMBB);
6804
6805 // Insert instructions into newMBB based on incoming instruction
6806 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6807 MachineOperand& destOper = mInstr->getOperand(0);
6808 MachineOperand* argOpers[6];
6809 int numArgs = mInstr->getNumOperands() - 1;
6810 for (int i=0; i < numArgs; ++i)
6811 argOpers[i] = &mInstr->getOperand(i+1);
6812
6813 // x86 address has 4 operands: base, index, scale, and displacement
6814 int lastAddrIndx = 3; // [0,3]
6815 int valArgIndx = 4;
6816
Mon P Wang318b0372008-05-05 22:56:23 +00006817 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6818 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00006819 for (int i=0; i <= lastAddrIndx; ++i)
6820 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00006821
Mon P Wang078a62d2008-05-05 19:05:59 +00006822 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006823 assert((argOpers[valArgIndx]->isReg() ||
6824 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00006825 "invalid operand");
Mon P Wang078a62d2008-05-05 19:05:59 +00006826
6827 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006828 if (argOpers[valArgIndx]->isReg())
Mon P Wang078a62d2008-05-05 19:05:59 +00006829 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6830 else
6831 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6832 (*MIB).addOperand(*argOpers[valArgIndx]);
6833
Mon P Wang318b0372008-05-05 22:56:23 +00006834 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6835 MIB.addReg(t1);
6836
Mon P Wang078a62d2008-05-05 19:05:59 +00006837 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6838 MIB.addReg(t1);
6839 MIB.addReg(t2);
6840
6841 // Generate movc
6842 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6843 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6844 MIB.addReg(t2);
6845 MIB.addReg(t1);
6846
6847 // Cmp and exchange if none has modified the memory location
6848 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6849 for (int i=0; i <= lastAddrIndx; ++i)
6850 (*MIB).addOperand(*argOpers[i]);
6851 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00006852 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6853 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang078a62d2008-05-05 19:05:59 +00006854
6855 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6856 MIB.addReg(X86::EAX);
6857
6858 // insert branch
6859 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6860
Dan Gohman221a4372008-07-07 23:14:23 +00006861 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00006862 return nextMBB;
6863}
6864
6865
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006866MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00006867X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6868 MachineBasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006869 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6870 switch (MI->getOpcode()) {
6871 default: assert(false && "Unexpected instr type to insert");
6872 case X86::CMOV_FR32:
6873 case X86::CMOV_FR64:
6874 case X86::CMOV_V4F32:
6875 case X86::CMOV_V2F64:
Evan Cheng621216e2007-09-29 00:00:36 +00006876 case X86::CMOV_V2I64: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006877 // To "insert" a SELECT_CC instruction, we actually have to insert the
6878 // diamond control-flow pattern. The incoming instruction knows the
6879 // destination vreg to set, the condition code register to branch on, the
6880 // true/false values to select between, and a branch opcode to use.
6881 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00006882 MachineFunction::iterator It = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006883 ++It;
6884
6885 // thisMBB:
6886 // ...
6887 // TrueVal = ...
6888 // cmpTY ccX, r1, r2
6889 // bCC copy1MBB
6890 // fallthrough --> copy0MBB
6891 MachineBasicBlock *thisMBB = BB;
Dan Gohman221a4372008-07-07 23:14:23 +00006892 MachineFunction *F = BB->getParent();
6893 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6894 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006895 unsigned Opc =
6896 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
6897 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman221a4372008-07-07 23:14:23 +00006898 F->insert(It, copy0MBB);
6899 F->insert(It, sinkMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00006900 // Update machine-CFG edges by transferring all successors of the current
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006901 // block to the new block which will contain the Phi node for the select.
Mon P Wang078a62d2008-05-05 19:05:59 +00006902 sinkMBB->transferSuccessors(BB);
6903
6904 // Add the true and fallthrough blocks as its successors.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006905 BB->addSuccessor(copy0MBB);
6906 BB->addSuccessor(sinkMBB);
6907
6908 // copy0MBB:
6909 // %FalseValue = ...
6910 // # fallthrough to sinkMBB
6911 BB = copy0MBB;
6912
6913 // Update machine-CFG edges
6914 BB->addSuccessor(sinkMBB);
6915
6916 // sinkMBB:
6917 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6918 // ...
6919 BB = sinkMBB;
6920 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
6921 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6922 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6923
Dan Gohman221a4372008-07-07 23:14:23 +00006924 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006925 return BB;
6926 }
6927
6928 case X86::FP32_TO_INT16_IN_MEM:
6929 case X86::FP32_TO_INT32_IN_MEM:
6930 case X86::FP32_TO_INT64_IN_MEM:
6931 case X86::FP64_TO_INT16_IN_MEM:
6932 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006933 case X86::FP64_TO_INT64_IN_MEM:
6934 case X86::FP80_TO_INT16_IN_MEM:
6935 case X86::FP80_TO_INT32_IN_MEM:
6936 case X86::FP80_TO_INT64_IN_MEM: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006937 // Change the floating point control register to use "round towards zero"
6938 // mode when truncating to an integer value.
6939 MachineFunction *F = BB->getParent();
6940 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
6941 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
6942
6943 // Load the old value of the high byte of the control word...
6944 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00006945 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006946 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
6947
6948 // Set the high part to be round to zero...
6949 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6950 .addImm(0xC7F);
6951
6952 // Reload the modified control word now...
6953 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
6954
6955 // Restore the memory image of control word to original value
6956 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6957 .addReg(OldCW);
6958
6959 // Get the X86 opcode to use.
6960 unsigned Opc;
6961 switch (MI->getOpcode()) {
6962 default: assert(0 && "illegal opcode!");
6963 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6964 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6965 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6966 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6967 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6968 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00006969 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6970 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6971 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006972 }
6973
6974 X86AddressMode AM;
6975 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006976 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006977 AM.BaseType = X86AddressMode::RegBase;
6978 AM.Base.Reg = Op.getReg();
6979 } else {
6980 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00006981 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006982 }
6983 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006984 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006985 AM.Scale = Op.getImm();
6986 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006987 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006988 AM.IndexReg = Op.getImm();
6989 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00006990 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006991 AM.GV = Op.getGlobal();
6992 } else {
6993 AM.Disp = Op.getImm();
6994 }
6995 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6996 .addReg(MI->getOperand(4).getReg());
6997
6998 // Reload the original control word now.
6999 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
7000
Dan Gohman221a4372008-07-07 23:14:23 +00007001 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007002 return BB;
7003 }
Mon P Wang078a62d2008-05-05 19:05:59 +00007004 case X86::ATOMAND32:
7005 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007006 X86::AND32ri, X86::MOV32rm,
7007 X86::LCMPXCHG32, X86::MOV32rr,
7008 X86::NOT32r, X86::EAX,
7009 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007010 case X86::ATOMOR32:
7011 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007012 X86::OR32ri, X86::MOV32rm,
7013 X86::LCMPXCHG32, X86::MOV32rr,
7014 X86::NOT32r, X86::EAX,
7015 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00007016 case X86::ATOMXOR32:
7017 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007018 X86::XOR32ri, X86::MOV32rm,
7019 X86::LCMPXCHG32, X86::MOV32rr,
7020 X86::NOT32r, X86::EAX,
7021 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007022 case X86::ATOMNAND32:
7023 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00007024 X86::AND32ri, X86::MOV32rm,
7025 X86::LCMPXCHG32, X86::MOV32rr,
7026 X86::NOT32r, X86::EAX,
7027 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00007028 case X86::ATOMMIN32:
7029 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7030 case X86::ATOMMAX32:
7031 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7032 case X86::ATOMUMIN32:
7033 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7034 case X86::ATOMUMAX32:
7035 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00007036
7037 case X86::ATOMAND16:
7038 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7039 X86::AND16ri, X86::MOV16rm,
7040 X86::LCMPXCHG16, X86::MOV16rr,
7041 X86::NOT16r, X86::AX,
7042 X86::GR16RegisterClass);
7043 case X86::ATOMOR16:
7044 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7045 X86::OR16ri, X86::MOV16rm,
7046 X86::LCMPXCHG16, X86::MOV16rr,
7047 X86::NOT16r, X86::AX,
7048 X86::GR16RegisterClass);
7049 case X86::ATOMXOR16:
7050 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7051 X86::XOR16ri, X86::MOV16rm,
7052 X86::LCMPXCHG16, X86::MOV16rr,
7053 X86::NOT16r, X86::AX,
7054 X86::GR16RegisterClass);
7055 case X86::ATOMNAND16:
7056 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7057 X86::AND16ri, X86::MOV16rm,
7058 X86::LCMPXCHG16, X86::MOV16rr,
7059 X86::NOT16r, X86::AX,
7060 X86::GR16RegisterClass, true);
7061 case X86::ATOMMIN16:
7062 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7063 case X86::ATOMMAX16:
7064 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7065 case X86::ATOMUMIN16:
7066 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7067 case X86::ATOMUMAX16:
7068 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7069
7070 case X86::ATOMAND8:
7071 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7072 X86::AND8ri, X86::MOV8rm,
7073 X86::LCMPXCHG8, X86::MOV8rr,
7074 X86::NOT8r, X86::AL,
7075 X86::GR8RegisterClass);
7076 case X86::ATOMOR8:
7077 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7078 X86::OR8ri, X86::MOV8rm,
7079 X86::LCMPXCHG8, X86::MOV8rr,
7080 X86::NOT8r, X86::AL,
7081 X86::GR8RegisterClass);
7082 case X86::ATOMXOR8:
7083 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7084 X86::XOR8ri, X86::MOV8rm,
7085 X86::LCMPXCHG8, X86::MOV8rr,
7086 X86::NOT8r, X86::AL,
7087 X86::GR8RegisterClass);
7088 case X86::ATOMNAND8:
7089 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7090 X86::AND8ri, X86::MOV8rm,
7091 X86::LCMPXCHG8, X86::MOV8rr,
7092 X86::NOT8r, X86::AL,
7093 X86::GR8RegisterClass, true);
7094 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00007095 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00007096 case X86::ATOMAND64:
7097 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7098 X86::AND64ri32, X86::MOV64rm,
7099 X86::LCMPXCHG64, X86::MOV64rr,
7100 X86::NOT64r, X86::RAX,
7101 X86::GR64RegisterClass);
7102 case X86::ATOMOR64:
7103 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7104 X86::OR64ri32, X86::MOV64rm,
7105 X86::LCMPXCHG64, X86::MOV64rr,
7106 X86::NOT64r, X86::RAX,
7107 X86::GR64RegisterClass);
7108 case X86::ATOMXOR64:
7109 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7110 X86::XOR64ri32, X86::MOV64rm,
7111 X86::LCMPXCHG64, X86::MOV64rr,
7112 X86::NOT64r, X86::RAX,
7113 X86::GR64RegisterClass);
7114 case X86::ATOMNAND64:
7115 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7116 X86::AND64ri32, X86::MOV64rm,
7117 X86::LCMPXCHG64, X86::MOV64rr,
7118 X86::NOT64r, X86::RAX,
7119 X86::GR64RegisterClass, true);
7120 case X86::ATOMMIN64:
7121 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7122 case X86::ATOMMAX64:
7123 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7124 case X86::ATOMUMIN64:
7125 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7126 case X86::ATOMUMAX64:
7127 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00007128
7129 // This group does 64-bit operations on a 32-bit host.
7130 case X86::ATOMAND6432:
7131 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7132 X86::AND32rr, X86::AND32rr,
7133 X86::AND32ri, X86::AND32ri,
7134 false);
7135 case X86::ATOMOR6432:
7136 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7137 X86::OR32rr, X86::OR32rr,
7138 X86::OR32ri, X86::OR32ri,
7139 false);
7140 case X86::ATOMXOR6432:
7141 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7142 X86::XOR32rr, X86::XOR32rr,
7143 X86::XOR32ri, X86::XOR32ri,
7144 false);
7145 case X86::ATOMNAND6432:
7146 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7147 X86::AND32rr, X86::AND32rr,
7148 X86::AND32ri, X86::AND32ri,
7149 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00007150 case X86::ATOMADD6432:
7151 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7152 X86::ADD32rr, X86::ADC32rr,
7153 X86::ADD32ri, X86::ADC32ri,
7154 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00007155 case X86::ATOMSUB6432:
7156 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7157 X86::SUB32rr, X86::SBB32rr,
7158 X86::SUB32ri, X86::SBB32ri,
7159 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007160 case X86::ATOMSWAP6432:
7161 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7162 X86::MOV32rr, X86::MOV32rr,
7163 X86::MOV32ri, X86::MOV32ri,
7164 false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007165 }
7166}
7167
7168//===----------------------------------------------------------------------===//
7169// X86 Optimization Hooks
7170//===----------------------------------------------------------------------===//
7171
Dan Gohman8181bd12008-07-27 21:46:04 +00007172void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00007173 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00007174 APInt &KnownZero,
7175 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007176 const SelectionDAG &DAG,
7177 unsigned Depth) const {
7178 unsigned Opc = Op.getOpcode();
7179 assert((Opc >= ISD::BUILTIN_OP_END ||
7180 Opc == ISD::INTRINSIC_WO_CHAIN ||
7181 Opc == ISD::INTRINSIC_W_CHAIN ||
7182 Opc == ISD::INTRINSIC_VOID) &&
7183 "Should use MaskedValueIsZero if you don't know whether Op"
7184 " is a target node!");
7185
Dan Gohman1d79e432008-02-13 23:07:24 +00007186 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007187 switch (Opc) {
7188 default: break;
7189 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00007190 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7191 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007192 break;
7193 }
7194}
7195
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007196/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00007197/// node is a GlobalAddress + offset.
7198bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7199 GlobalValue* &GA, int64_t &Offset) const{
7200 if (N->getOpcode() == X86ISD::Wrapper) {
7201 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007202 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00007203 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007204 return true;
7205 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007206 }
Evan Chengef7be082008-05-12 19:56:52 +00007207 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007208}
7209
Evan Chengef7be082008-05-12 19:56:52 +00007210static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7211 const TargetLowering &TLI) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007212 GlobalValue *GV;
Nick Lewycky4bd3fca2008-02-02 08:29:58 +00007213 int64_t Offset = 0;
Evan Chengef7be082008-05-12 19:56:52 +00007214 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007215 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattner3834cf32008-01-26 20:07:42 +00007216 // DAG combine handles the stack object case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007217 return false;
7218}
7219
Dan Gohman8181bd12008-07-27 21:46:04 +00007220static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands92c43912008-06-06 12:08:01 +00007221 unsigned NumElems, MVT EVT,
Evan Chengef7be082008-05-12 19:56:52 +00007222 SDNode *&Base,
7223 SelectionDAG &DAG, MachineFrameInfo *MFI,
7224 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007225 Base = NULL;
7226 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007227 SDValue Idx = PermMask.getOperand(i);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007228 if (Idx.getOpcode() == ISD::UNDEF) {
7229 if (!Base)
7230 return false;
7231 continue;
7232 }
7233
Dan Gohman8181bd12008-07-27 21:46:04 +00007234 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00007235 if (!Elt.getNode() ||
7236 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007237 return false;
7238 if (!Base) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007239 Base = Elt.getNode();
Evan Cheng92ee6822008-05-10 06:46:49 +00007240 if (Base->getOpcode() == ISD::UNDEF)
7241 return false;
Evan Cheng40ee6e52008-05-08 00:57:18 +00007242 continue;
7243 }
7244 if (Elt.getOpcode() == ISD::UNDEF)
7245 continue;
7246
Gabor Greif1c80d112008-08-28 21:40:38 +00007247 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands92c43912008-06-06 12:08:01 +00007248 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng40ee6e52008-05-08 00:57:18 +00007249 return false;
7250 }
7251 return true;
7252}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007253
7254/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7255/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7256/// if the load addresses are consecutive, non-overlapping, and in the right
7257/// order.
Dan Gohman8181bd12008-07-27 21:46:04 +00007258static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengef7be082008-05-12 19:56:52 +00007259 const TargetLowering &TLI) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00007260 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands92c43912008-06-06 12:08:01 +00007261 MVT VT = N->getValueType(0);
7262 MVT EVT = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00007263 SDValue PermMask = N->getOperand(2);
Evan Chengbad18452008-05-05 22:12:23 +00007264 unsigned NumElems = PermMask.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007265 SDNode *Base = NULL;
Evan Chengef7be082008-05-12 19:56:52 +00007266 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7267 DAG, MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00007268 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007269
Dan Gohman11821702007-07-27 17:16:43 +00007270 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greif1c80d112008-08-28 21:40:38 +00007271 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007272 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohman11821702007-07-27 17:16:43 +00007273 LD->getSrcValueOffset(), LD->isVolatile());
Evan Chengbad18452008-05-05 22:12:23 +00007274 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7275 LD->getSrcValueOffset(), LD->isVolatile(),
7276 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007277}
7278
Evan Chengb6290462008-05-12 23:04:07 +00007279/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman8181bd12008-07-27 21:46:04 +00007280static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng6617eed2008-09-24 23:26:36 +00007281 const X86Subtarget *Subtarget,
7282 const TargetLowering &TLI) {
Evan Chengdea99362008-05-29 08:22:04 +00007283 unsigned NumOps = N->getNumOperands();
7284
Evan Chenge9b9c672008-05-09 21:53:03 +00007285 // Ignore single operand BUILD_VECTOR.
Evan Chengdea99362008-05-29 08:22:04 +00007286 if (NumOps == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00007287 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007288
Duncan Sands92c43912008-06-06 12:08:01 +00007289 MVT VT = N->getValueType(0);
7290 MVT EVT = VT.getVectorElementType();
Evan Chenge9b9c672008-05-09 21:53:03 +00007291 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7292 // We are looking for load i64 and zero extend. We want to transform
7293 // it before legalizer has a chance to expand it. Also look for i64
7294 // BUILD_PAIR bit casted to f64.
Dan Gohman8181bd12008-07-27 21:46:04 +00007295 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007296 // This must be an insertion into a zero vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00007297 SDValue HighElt = N->getOperand(1);
Evan Cheng5b0c30e2008-05-10 00:58:41 +00007298 if (!isZeroNode(HighElt))
Dan Gohman8181bd12008-07-27 21:46:04 +00007299 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007300
7301 // Value must be a load.
Gabor Greif1c80d112008-08-28 21:40:38 +00007302 SDNode *Base = N->getOperand(0).getNode();
Evan Chenge9b9c672008-05-09 21:53:03 +00007303 if (!isa<LoadSDNode>(Base)) {
Evan Chengb6290462008-05-12 23:04:07 +00007304 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman8181bd12008-07-27 21:46:04 +00007305 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00007306 Base = Base->getOperand(0).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00007307 if (!isa<LoadSDNode>(Base))
Dan Gohman8181bd12008-07-27 21:46:04 +00007308 return SDValue();
Evan Chenge9b9c672008-05-09 21:53:03 +00007309 }
Evan Chenge9b9c672008-05-09 21:53:03 +00007310
7311 // Transform it into VZEXT_LOAD addr.
Evan Chengb6290462008-05-12 23:04:07 +00007312 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begeman211c4742008-05-28 00:24:25 +00007313
7314 // Load must not be an extload.
7315 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman8181bd12008-07-27 21:46:04 +00007316 return SDValue();
Nate Begeman211c4742008-05-28 00:24:25 +00007317
Evan Cheng6617eed2008-09-24 23:26:36 +00007318 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7319 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7320 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7321 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7322 return ResNode;
Evan Chenge9b9c672008-05-09 21:53:03 +00007323}
7324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007325/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007326static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 const X86Subtarget *Subtarget) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007328 SDValue Cond = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007329
7330 // If we have SSE[12] support, try to form min/max nodes.
7331 if (Subtarget->hasSSE2() &&
7332 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7333 if (Cond.getOpcode() == ISD::SETCC) {
7334 // Get the LHS/RHS of the select.
Dan Gohman8181bd12008-07-27 21:46:04 +00007335 SDValue LHS = N->getOperand(1);
7336 SDValue RHS = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007337 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
7338
7339 unsigned Opcode = 0;
7340 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7341 switch (CC) {
7342 default: break;
7343 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7344 case ISD::SETULE:
7345 case ISD::SETLE:
7346 if (!UnsafeFPMath) break;
7347 // FALL THROUGH.
7348 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7349 case ISD::SETLT:
7350 Opcode = X86ISD::FMIN;
7351 break;
7352
7353 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7354 case ISD::SETUGT:
7355 case ISD::SETGT:
7356 if (!UnsafeFPMath) break;
7357 // FALL THROUGH.
7358 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7359 case ISD::SETGE:
7360 Opcode = X86ISD::FMAX;
7361 break;
7362 }
7363 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7364 switch (CC) {
7365 default: break;
7366 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7367 case ISD::SETUGT:
7368 case ISD::SETGT:
7369 if (!UnsafeFPMath) break;
7370 // FALL THROUGH.
7371 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7372 case ISD::SETGE:
7373 Opcode = X86ISD::FMIN;
7374 break;
7375
7376 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7377 case ISD::SETULE:
7378 case ISD::SETLE:
7379 if (!UnsafeFPMath) break;
7380 // FALL THROUGH.
7381 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7382 case ISD::SETLT:
7383 Opcode = X86ISD::FMAX;
7384 break;
7385 }
7386 }
7387
7388 if (Opcode)
7389 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
7390 }
7391
7392 }
7393
Dan Gohman8181bd12008-07-27 21:46:04 +00007394 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007395}
7396
Chris Lattnerce84ae42008-02-22 02:09:43 +00007397/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007398static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007399 const X86Subtarget *Subtarget) {
7400 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7401 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00007402 // A preferable solution to the general problem is to figure out the right
7403 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng40ee6e52008-05-08 00:57:18 +00007404 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands92c43912008-06-06 12:08:01 +00007405 if (St->getValue().getValueType().isVector() &&
7406 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesend112b802008-02-25 19:20:14 +00007407 isa<LoadSDNode>(St->getValue()) &&
7408 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7409 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007410 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007411 LoadSDNode *Ld = 0;
7412 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00007413 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00007414 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00007415 // Must be a store of a load. We currently handle two cases: the load
7416 // is a direct child, and it's under an intervening TokenFactor. It is
7417 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00007418 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00007419 Ld = cast<LoadSDNode>(St->getChain());
7420 else if (St->getValue().hasOneUse() &&
7421 ChainVal->getOpcode() == ISD::TokenFactor) {
7422 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00007423 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00007424 TokenFactorIndex = i;
7425 Ld = cast<LoadSDNode>(St->getValue());
7426 } else
7427 Ops.push_back(ChainVal->getOperand(i));
7428 }
7429 }
7430 if (Ld) {
7431 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7432 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007433 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesend112b802008-02-25 19:20:14 +00007434 Ld->getBasePtr(), Ld->getSrcValue(),
7435 Ld->getSrcValueOffset(), Ld->isVolatile(),
7436 Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007437 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007438 if (TokenFactorIndex != -1) {
Dan Gohman72032662008-03-28 23:45:16 +00007439 Ops.push_back(NewChain);
Dale Johannesend112b802008-02-25 19:20:14 +00007440 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7441 Ops.size());
7442 }
7443 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7444 St->getSrcValue(), St->getSrcValueOffset(),
7445 St->isVolatile(), St->getAlignment());
7446 }
7447
7448 // Otherwise, lower to two 32-bit copies.
Dan Gohman8181bd12008-07-27 21:46:04 +00007449 SDValue LoAddr = Ld->getBasePtr();
7450 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007451 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007452
Dan Gohman8181bd12008-07-27 21:46:04 +00007453 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007454 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7455 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007456 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesend112b802008-02-25 19:20:14 +00007457 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7458 Ld->isVolatile(),
7459 MinAlign(Ld->getAlignment(), 4));
7460
Dan Gohman8181bd12008-07-27 21:46:04 +00007461 SDValue NewChain = LoLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00007462 if (TokenFactorIndex != -1) {
7463 Ops.push_back(LoLd);
7464 Ops.push_back(HiLd);
7465 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7466 Ops.size());
7467 }
7468
7469 LoAddr = St->getBasePtr();
7470 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands92c43912008-06-06 12:08:01 +00007471 DAG.getConstant(4, MVT::i32));
Dale Johannesend112b802008-02-25 19:20:14 +00007472
Dan Gohman8181bd12008-07-27 21:46:04 +00007473 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattnerce84ae42008-02-22 02:09:43 +00007474 St->getSrcValue(), St->getSrcValueOffset(),
7475 St->isVolatile(), St->getAlignment());
Dan Gohman8181bd12008-07-27 21:46:04 +00007476 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif825aa892008-08-28 23:19:51 +00007477 St->getSrcValue(),
7478 St->getSrcValueOffset() + 4,
Dale Johannesend112b802008-02-25 19:20:14 +00007479 St->isVolatile(),
7480 MinAlign(St->getAlignment(), 4));
7481 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00007482 }
Chris Lattnerce84ae42008-02-22 02:09:43 +00007483 }
Dan Gohman8181bd12008-07-27 21:46:04 +00007484 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00007485}
7486
Chris Lattner470d5dc2008-01-25 06:14:17 +00007487/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7488/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007489static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00007490 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7491 // F[X]OR(0.0, x) -> x
7492 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00007493 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7494 if (C->getValueAPF().isPosZero())
7495 return N->getOperand(1);
7496 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7497 if (C->getValueAPF().isPosZero())
7498 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00007499 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007500}
7501
7502/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00007503static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00007504 // FAND(0.0, x) -> 0.0
7505 // FAND(x, 0.0) -> 0.0
7506 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7507 if (C->getValueAPF().isPosZero())
7508 return N->getOperand(0);
7509 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7510 if (C->getValueAPF().isPosZero())
7511 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00007512 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00007513}
7514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007515
Dan Gohman8181bd12008-07-27 21:46:04 +00007516SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00007517 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007518 SelectionDAG &DAG = DCI.DAG;
7519 switch (N->getOpcode()) {
7520 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00007521 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7522 case ISD::BUILD_VECTOR:
7523 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00007524 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00007525 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00007526 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00007527 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7528 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007529 }
7530
Dan Gohman8181bd12008-07-27 21:46:04 +00007531 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007532}
7533
7534//===----------------------------------------------------------------------===//
7535// X86 Inline Assembly Support
7536//===----------------------------------------------------------------------===//
7537
7538/// getConstraintType - Given a constraint letter, return the type of
7539/// constraint it is for this target.
7540X86TargetLowering::ConstraintType
7541X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7542 if (Constraint.size() == 1) {
7543 switch (Constraint[0]) {
7544 case 'A':
Chris Lattner267805f2008-03-11 19:06:29 +00007545 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007546 case 'r':
7547 case 'R':
7548 case 'l':
7549 case 'q':
7550 case 'Q':
7551 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00007552 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007553 case 'Y':
7554 return C_RegisterClass;
7555 default:
7556 break;
7557 }
7558 }
7559 return TargetLowering::getConstraintType(Constraint);
7560}
7561
Dale Johannesene99fc902008-01-29 02:21:21 +00007562/// LowerXConstraint - try to replace an X constraint, which matches anything,
7563/// with another that has more specific requirements based on the type of the
7564/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00007565const char *X86TargetLowering::
Duncan Sands92c43912008-06-06 12:08:01 +00007566LowerXConstraint(MVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00007567 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7568 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00007569 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00007570 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00007571 return "Y";
7572 if (Subtarget->hasSSE1())
7573 return "x";
7574 }
7575
7576 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00007577}
7578
Chris Lattnera531abc2007-08-25 00:47:38 +00007579/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7580/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00007581void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00007582 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00007583 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00007584 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00007585 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00007586 SDValue Result(0, 0);
Chris Lattnera531abc2007-08-25 00:47:38 +00007587
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007588 switch (Constraint) {
7589 default: break;
7590 case 'I':
7591 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007592 if (C->getZExtValue() <= 31) {
7593 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007594 break;
7595 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007596 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007597 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00007598 case 'J':
7599 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7600 if (C->getZExtValue() <= 63) {
7601 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7602 break;
7603 }
7604 }
7605 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007606 case 'N':
7607 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007608 if (C->getZExtValue() <= 255) {
7609 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007610 break;
7611 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007612 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007613 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007614 case 'i': {
7615 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00007616 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007617 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00007618 break;
7619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007620
7621 // If we are in non-pic codegen mode, we allow the address of a global (with
7622 // an optional displacement) to be used with 'i'.
7623 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7624 int64_t Offset = 0;
7625
7626 // Match either (GA) or (GA+C)
7627 if (GA) {
7628 Offset = GA->getOffset();
7629 } else if (Op.getOpcode() == ISD::ADD) {
7630 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7631 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7632 if (C && GA) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007633 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007634 } else {
7635 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7636 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7637 if (C && GA)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00007638 Offset = GA->getOffset()+C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007639 else
7640 C = 0, GA = 0;
7641 }
7642 }
7643
7644 if (GA) {
Evan Cheng7f250d62008-09-24 00:05:32 +00007645 if (hasMemory)
Dan Gohman36322c72008-10-18 02:06:02 +00007646 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00007647 else
7648 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7649 Offset);
Chris Lattnera531abc2007-08-25 00:47:38 +00007650 Result = Op;
7651 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007652 }
7653
7654 // Otherwise, not valid for this mode.
Chris Lattnera531abc2007-08-25 00:47:38 +00007655 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007656 }
7657 }
Chris Lattnera531abc2007-08-25 00:47:38 +00007658
Gabor Greif1c80d112008-08-28 21:40:38 +00007659 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00007660 Ops.push_back(Result);
7661 return;
7662 }
Evan Cheng7f250d62008-09-24 00:05:32 +00007663 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7664 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665}
7666
7667std::vector<unsigned> X86TargetLowering::
7668getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007669 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007670 if (Constraint.size() == 1) {
7671 // FIXME: not handling fp-stack yet!
7672 switch (Constraint[0]) { // GCC X86 Constraint Letters
7673 default: break; // Unknown constraint letter
7674 case 'A': // EAX/EDX
7675 if (VT == MVT::i32 || VT == MVT::i64)
7676 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7677 break;
7678 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7679 case 'Q': // Q_REGS
7680 if (VT == MVT::i32)
7681 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7682 else if (VT == MVT::i16)
7683 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7684 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00007685 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner35032592007-11-04 06:51:12 +00007686 else if (VT == MVT::i64)
7687 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7688 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007689 }
7690 }
7691
7692 return std::vector<unsigned>();
7693}
7694
7695std::pair<unsigned, const TargetRegisterClass*>
7696X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands92c43912008-06-06 12:08:01 +00007697 MVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007698 // First, see if this is a constraint that directly corresponds to an LLVM
7699 // register class.
7700 if (Constraint.size() == 1) {
7701 // GCC Constraint Letters
7702 switch (Constraint[0]) {
7703 default: break;
7704 case 'r': // GENERAL_REGS
7705 case 'R': // LEGACY_REGS
7706 case 'l': // INDEX_REGS
Chris Lattnerbbfea052008-10-17 18:15:05 +00007707 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007708 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00007709 if (VT == MVT::i16)
7710 return std::make_pair(0U, X86::GR16RegisterClass);
7711 if (VT == MVT::i32 || !Subtarget->is64Bit())
7712 return std::make_pair(0U, X86::GR32RegisterClass);
7713 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00007714 case 'f': // FP Stack registers.
7715 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7716 // value to the correct fpstack register class.
7717 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7718 return std::make_pair(0U, X86::RFP32RegisterClass);
7719 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7720 return std::make_pair(0U, X86::RFP64RegisterClass);
7721 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007722 case 'y': // MMX_REGS if MMX allowed.
7723 if (!Subtarget->hasMMX()) break;
7724 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007725 case 'Y': // SSE_REGS if SSE2 allowed
7726 if (!Subtarget->hasSSE2()) break;
7727 // FALL THROUGH.
7728 case 'x': // SSE_REGS if SSE1 allowed
7729 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00007730
7731 switch (VT.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007732 default: break;
7733 // Scalar SSE types.
7734 case MVT::f32:
7735 case MVT::i32:
7736 return std::make_pair(0U, X86::FR32RegisterClass);
7737 case MVT::f64:
7738 case MVT::i64:
7739 return std::make_pair(0U, X86::FR64RegisterClass);
7740 // Vector types.
7741 case MVT::v16i8:
7742 case MVT::v8i16:
7743 case MVT::v4i32:
7744 case MVT::v2i64:
7745 case MVT::v4f32:
7746 case MVT::v2f64:
7747 return std::make_pair(0U, X86::VR128RegisterClass);
7748 }
7749 break;
7750 }
7751 }
7752
7753 // Use the default implementation in TargetLowering to convert the register
7754 // constraint into a member of a register class.
7755 std::pair<unsigned, const TargetRegisterClass*> Res;
7756 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
7757
7758 // Not found as a standard register?
7759 if (Res.second == 0) {
7760 // GCC calls "st(0)" just plain "st".
7761 if (StringsEqualNoCase("{st}", Constraint)) {
7762 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00007763 Res.second = X86::RFP80RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007764 }
7765
7766 return Res;
7767 }
7768
7769 // Otherwise, check to see if this is a register class of the wrong value
7770 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7771 // turn into {ax},{dx}.
7772 if (Res.second->hasType(VT))
7773 return Res; // Correct type already, nothing to do.
7774
7775 // All of the single-register GCC register classes map their values onto
7776 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7777 // really want an 8-bit or 32-bit register, map to the appropriate register
7778 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +00007779 if (Res.second == X86::GR16RegisterClass) {
7780 if (VT == MVT::i8) {
7781 unsigned DestReg = 0;
7782 switch (Res.first) {
7783 default: break;
7784 case X86::AX: DestReg = X86::AL; break;
7785 case X86::DX: DestReg = X86::DL; break;
7786 case X86::CX: DestReg = X86::CL; break;
7787 case X86::BX: DestReg = X86::BL; break;
7788 }
7789 if (DestReg) {
7790 Res.first = DestReg;
7791 Res.second = Res.second = X86::GR8RegisterClass;
7792 }
7793 } else if (VT == MVT::i32) {
7794 unsigned DestReg = 0;
7795 switch (Res.first) {
7796 default: break;
7797 case X86::AX: DestReg = X86::EAX; break;
7798 case X86::DX: DestReg = X86::EDX; break;
7799 case X86::CX: DestReg = X86::ECX; break;
7800 case X86::BX: DestReg = X86::EBX; break;
7801 case X86::SI: DestReg = X86::ESI; break;
7802 case X86::DI: DestReg = X86::EDI; break;
7803 case X86::BP: DestReg = X86::EBP; break;
7804 case X86::SP: DestReg = X86::ESP; break;
7805 }
7806 if (DestReg) {
7807 Res.first = DestReg;
7808 Res.second = Res.second = X86::GR32RegisterClass;
7809 }
7810 } else if (VT == MVT::i64) {
7811 unsigned DestReg = 0;
7812 switch (Res.first) {
7813 default: break;
7814 case X86::AX: DestReg = X86::RAX; break;
7815 case X86::DX: DestReg = X86::RDX; break;
7816 case X86::CX: DestReg = X86::RCX; break;
7817 case X86::BX: DestReg = X86::RBX; break;
7818 case X86::SI: DestReg = X86::RSI; break;
7819 case X86::DI: DestReg = X86::RDI; break;
7820 case X86::BP: DestReg = X86::RBP; break;
7821 case X86::SP: DestReg = X86::RSP; break;
7822 }
7823 if (DestReg) {
7824 Res.first = DestReg;
7825 Res.second = Res.second = X86::GR64RegisterClass;
7826 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007827 }
Chris Lattnere9d7f792008-08-26 06:19:02 +00007828 } else if (Res.second == X86::FR32RegisterClass ||
7829 Res.second == X86::FR64RegisterClass ||
7830 Res.second == X86::VR128RegisterClass) {
7831 // Handle references to XMM physical registers that got mapped into the
7832 // wrong class. This can happen with constraints like {xmm0} where the
7833 // target independent register mapper will just pick the first match it can
7834 // find, ignoring the required type.
7835 if (VT == MVT::f32)
7836 Res.second = X86::FR32RegisterClass;
7837 else if (VT == MVT::f64)
7838 Res.second = X86::FR64RegisterClass;
7839 else if (X86::VR128RegisterClass->hasType(VT))
7840 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007841 }
7842
7843 return Res;
7844}
Mon P Wang1448aad2008-10-30 08:01:45 +00007845
7846//===----------------------------------------------------------------------===//
7847// X86 Widen vector type
7848//===----------------------------------------------------------------------===//
7849
7850/// getWidenVectorType: given a vector type, returns the type to widen
7851/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7852/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +00007853/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +00007854/// scalarizing vs using the wider vector type.
7855
7856MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7857 assert(VT.isVector());
7858 if (isTypeLegal(VT))
7859 return VT;
7860
7861 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7862 // type based on element type. This would speed up our search (though
7863 // it may not be worth it since the size of the list is relatively
7864 // small).
7865 MVT EltVT = VT.getVectorElementType();
7866 unsigned NElts = VT.getVectorNumElements();
7867
7868 // On X86, it make sense to widen any vector wider than 1
7869 if (NElts <= 1)
7870 return MVT::Other;
7871
7872 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7873 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7874 MVT SVT = (MVT::SimpleValueType)nVT;
7875
7876 if (isTypeLegal(SVT) &&
7877 SVT.getVectorElementType() == EltVT &&
7878 SVT.getVectorNumElements() > NElts)
7879 return SVT;
7880 }
7881 return MVT::Other;
7882}