blob: e53f6b7525bbb992bfc3e6f52866b7b82ad56ad7 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "X86TargetMachine.h"
Chris Lattner8886dc22009-09-16 01:46:41 +000019#include "X86TargetObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CallingConv.h"
21#include "llvm/Constants.h"
22#include "llvm/DerivedTypes.h"
Chris Lattnerec7cfd42009-06-26 21:20:29 +000023#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/GlobalVariable.h"
25#include "llvm/Function.h"
Chris Lattner7fce21c2009-07-20 17:51:36 +000026#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Intrinsics.h"
Owen Anderson6361f972009-07-15 21:51:10 +000028#include "llvm/LLVMContext.h"
Evan Cheng75184a92007-12-11 01:46:18 +000029#include "llvm/ADT/BitVector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/ADT/VectorExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner25525cd2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/MathExtras.h"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +000039#include "llvm/Support/Debug.h"
Edwin Török3cb88482009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041#include "llvm/Target/TargetOptions.h"
Evan Cheng75184a92007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043#include "llvm/ADT/StringExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000044#include "llvm/Support/CommandLine.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000045#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046using namespace llvm;
47
Mon P Wang1f292322008-11-23 04:37:22 +000048static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000049DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000050
Dan Gohmane84197b2009-09-03 17:18:51 +000051// Disable16Bit - 16-bit operations typically have a larger encoding than
52// corresponding 32-bit instructions, and 16-bit code is slow on some
53// processors. This is an experimental flag to disable 16-bit operations
54// (which forces them to be Legalized to 32-bit operations).
55static cl::opt<bool>
56Disable16Bit("disable-16bit", cl::Hidden,
57 cl::desc("Disable use of 16-bit instructions"));
58
Evan Cheng2aea0b42008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersonac9de032009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng2aea0b42008-04-25 19:11:04 +000062
Chris Lattnerc4c40a92009-07-28 03:13:23 +000063static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
64 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
65 default: llvm_unreachable("unknown subtarget type");
66 case X86Subtarget::isDarwin:
Chris Lattner8886dc22009-09-16 01:46:41 +000067 if (TM.getSubtarget<X86Subtarget>().is64Bit())
68 return new X8664_MachoTargetObjectFile();
Chris Lattnerf283fb22009-09-18 20:22:52 +000069 return new X8632_MachoTargetObjectFile();
Chris Lattnerc4c40a92009-07-28 03:13:23 +000070 case X86Subtarget::isELF:
71 return new TargetLoweringObjectFileELF();
72 case X86Subtarget::isMingw:
73 case X86Subtarget::isCygwin:
74 case X86Subtarget::isWindows:
75 return new TargetLoweringObjectFileCOFF();
76 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +000077
Chris Lattnerc4c40a92009-07-28 03:13:23 +000078}
79
Dan Gohmanb41dfba2008-05-14 01:58:56 +000080X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerc4c40a92009-07-28 03:13:23 +000081 : TargetLowering(TM, createTLOF(TM)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000083 X86ScalarSSEf64 = Subtarget->hasSSE2();
84 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000086
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000088 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
90 // Set up the TargetLowering object.
91
92 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000093 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +000094 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 setSchedulingPreference(SchedulingForRegPressure);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 setStackPointerRegisterToSaveRestore(X86StackPtr);
97
98 if (Subtarget->isTargetDarwin()) {
99 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
100 setUseUnderscoreSetJmp(false);
101 setUseUnderscoreLongJmp(false);
102 } else if (Subtarget->isTargetMingw()) {
103 // MS runtime is weird: it exports _setjmp, but longjmp!
104 setUseUnderscoreSetJmp(true);
105 setUseUnderscoreLongJmp(false);
106 } else {
107 setUseUnderscoreSetJmp(true);
108 setUseUnderscoreLongJmp(true);
109 }
Scott Michel91099d62009-02-17 22:15:04 +0000110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 // Set up the register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000112 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohmane84197b2009-09-03 17:18:51 +0000113 if (!Disable16Bit)
114 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000117 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000119 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
Scott Michel91099d62009-02-17 22:15:04 +0000121 // We don't accept any truncstore of integer registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000122 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000123 if (!Disable16Bit)
124 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000126 if (!Disable16Bit)
127 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000128 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
129 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng71343822008-10-15 02:05:31 +0000130
131 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000132 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
134 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
137 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
140 // operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000141 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
143 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144
145 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000146 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
147 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000148 } else if (!UseSoftFloat) {
149 if (X86ScalarSSEf64) {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000150 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000152 }
Eli Friedman8c3cb582009-05-23 09:59:16 +0000153 // We have an algorithm for SSE2, and we turn this into a 64-bit
154 // FILD for other targets.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000155 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157
158 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
159 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000160 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
161 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000162
Devang Patel3c233642009-06-05 18:48:29 +0000163 if (!UseSoftFloat) {
Bill Wendling6b42d012009-03-13 08:41:47 +0000164 // SSE has no i16 to fp conversion, only i32
165 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000167 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000169 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000172 }
Dale Johannesen2fc20782007-09-14 22:26:36 +0000173 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000174 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
175 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
177
Dale Johannesen958b08b2007-09-19 23:55:34 +0000178 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
179 // are Legal, f80 is custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
181 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
183 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
184 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000185 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
186 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000188 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000190 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000193 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
194 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 }
196
197 // Handle FP_TO_UINT by promoting the destination to a larger signed
198 // conversion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
200 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
201 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202
203 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000204 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
205 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000206 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000207 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 // Expand FP_TO_UINT into a select.
209 // FIXME: We would like to use a Custom expander here eventually to do
210 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 else
Eli Friedman8c3cb582009-05-23 09:59:16 +0000213 // With SSE3 we can use fisttpll to convert to a signed i64; without
214 // SSE, we're stuck with a fistpll.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 }
217
218 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000219 if (!X86ScalarSSEf64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000220 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
221 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 }
223
Dan Gohman8450d862008-02-18 19:34:53 +0000224 // Scalar integer divide and remainder are lowered to use operations that
225 // produce two results, to match the available instructions. This exposes
226 // the two-result form to trivial CSE, which is able to combine x/y and x%y
227 // into a single instruction.
228 //
229 // Scalar integer multiply-high is also lowered to use two-result
230 // operations, to match the available instructions. However, plain multiply
231 // (low) operations are left as Legal, as there are single-result
232 // instructions for this in x86. Using the two-result multiply instructions
233 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000234 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
235 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
236 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
237 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
238 setOperationAction(ISD::SREM , MVT::i8 , Expand);
239 setOperationAction(ISD::UREM , MVT::i8 , Expand);
240 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
241 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
242 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
243 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
244 setOperationAction(ISD::SREM , MVT::i16 , Expand);
245 setOperationAction(ISD::UREM , MVT::i16 , Expand);
246 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
247 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
248 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
249 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
250 setOperationAction(ISD::SREM , MVT::i32 , Expand);
251 setOperationAction(ISD::UREM , MVT::i32 , Expand);
252 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
253 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
254 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
255 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
256 setOperationAction(ISD::SREM , MVT::i64 , Expand);
257 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000258
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000259 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
260 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
261 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
262 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
268 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
269 setOperationAction(ISD::FREM , MVT::f32 , Expand);
270 setOperationAction(ISD::FREM , MVT::f64 , Expand);
271 setOperationAction(ISD::FREM , MVT::f80 , Expand);
272 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000273
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000274 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
275 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
276 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
277 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000278 if (Disable16Bit) {
279 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
280 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
281 } else {
282 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
284 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
290 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
291 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 }
293
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000294 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
295 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296
297 // These should be promoted to a larger select which is supported.
Dan Gohman29b998f2009-08-27 00:14:12 +0000298 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 // X86 wants to expand cmov itself.
Dan Gohman29b998f2009-08-27 00:14:12 +0000300 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000301 if (Disable16Bit)
302 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
303 else
304 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
307 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
308 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
309 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000310 if (Disable16Bit)
311 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
312 else
313 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
315 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
316 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
317 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000319 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000322 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
324 // Darwin ABI issue.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
326 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
327 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
328 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000329 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000330 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
331 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000332 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000334 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
335 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
336 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
337 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000338 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 }
340 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000341 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
342 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
343 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000344 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000345 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
346 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
347 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000348 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
Evan Cheng8d51ab32008-03-10 19:38:10 +0000350 if (Subtarget->hasSSE1())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000351 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000352
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000353 if (!Subtarget->hasSSE2())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000354 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000355
Mon P Wang078a62d2008-05-05 19:05:59 +0000356 // Expand certain atomics
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000361
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000366
Dale Johannesenf160d802008-10-02 18:53:47 +0000367 if (!Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000368 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000375 }
376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // FIXME - use subtarget debug flags
378 if (!Subtarget->isTargetDarwin() &&
379 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000380 !Subtarget->isTargetCygMing()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000381 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000382 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
386 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
387 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 setExceptionPointerRegister(X86::RAX);
390 setExceptionSelectorRegister(X86::RDX);
391 } else {
392 setExceptionPointerRegister(X86::EAX);
393 setExceptionSelectorRegister(X86::EDX);
394 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000395 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
396 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000397
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000399
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000400 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000403 setOperationAction(ISD::VASTART , MVT::Other, Custom);
404 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000405 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000406 setOperationAction(ISD::VAARG , MVT::Other, Custom);
407 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000408 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VAARG , MVT::Other, Expand);
410 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000411 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000413 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
414 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 if (Subtarget->isTargetCygMing())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421
Evan Cheng0b84fe12009-02-13 22:36:38 +0000422 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000423 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
426 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427
428 // Use ANDPD to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FABS , MVT::f64, Custom);
430 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431
432 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FNEG , MVT::f64, Custom);
434 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435
436 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000437 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
438 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000439
440 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000441 setOperationAction(ISD::FSIN , MVT::f64, Expand);
442 setOperationAction(ISD::FCOS , MVT::f64, Expand);
443 setOperationAction(ISD::FSIN , MVT::f32, Expand);
444 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 // Expand FP immediates into loads from the stack, except for the special
447 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000448 addLegalFPImmediate(APFloat(+0.0)); // xorpd
449 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Cheng0b84fe12009-02-13 22:36:38 +0000450 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000451 // Use SSE for f32, x87 for f64.
452 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000453 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
454 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455
456 // Use ANDPS to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000458
459 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000460 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000461
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000462 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000463
464 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000465 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
466 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000467
468 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000469 setOperationAction(ISD::FSIN , MVT::f32, Expand);
470 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000471
Nate Begemane2ba64f2008-02-14 08:57:00 +0000472 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000473 addLegalFPImmediate(APFloat(+0.0f)); // xorps
474 addLegalFPImmediate(APFloat(+0.0)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
478
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000479 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000480 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
481 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000482 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000483 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000484 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000486 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
487 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000489 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
490 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
492 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000493
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000495 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
496 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000498 addLegalFPImmediate(APFloat(+0.0)); // FLD0
499 addLegalFPImmediate(APFloat(+1.0)); // FLD1
500 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
501 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000502 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
503 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
504 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
505 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 }
507
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000508 // Long double always uses X87.
Evan Chenge738dc32009-03-26 23:06:32 +0000509 if (!UseSoftFloat) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000510 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
511 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
512 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000513 {
514 bool ignored;
515 APFloat TmpFlt(+0.0);
516 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
517 &ignored);
518 addLegalFPImmediate(TmpFlt); // FLD0
519 TmpFlt.changeSign();
520 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
521 APFloat TmpFlt2(+1.0);
522 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
523 &ignored);
524 addLegalFPImmediate(TmpFlt2); // FLD1
525 TmpFlt2.changeSign();
526 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
527 }
Scott Michel91099d62009-02-17 22:15:04 +0000528
Evan Cheng0b84fe12009-02-13 22:36:38 +0000529 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
531 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000532 }
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000533 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000534
Dan Gohman2f7b1982007-10-11 23:21:31 +0000535 // Always use a library call for pow.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000536 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
537 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000539
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FLOG, MVT::f80, Expand);
541 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
543 setOperationAction(ISD::FEXP, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000545
Mon P Wanga5a239f2008-11-06 05:31:54 +0000546 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000547 // (for widening) or expand (for scalarization). Then we will selectively
548 // turn on ones that can be effectively codegen'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000549 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
550 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
551 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
566 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman9d501bd2009-12-11 21:31:27 +0000599 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohmanc6cfdd32009-12-14 23:40:38 +0000600 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
605 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
606 setTruncStoreAction((MVT::SimpleValueType)VT,
607 (MVT::SimpleValueType)InnerVT, Expand);
608 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
609 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 }
612
Evan Cheng0b84fe12009-02-13 22:36:38 +0000613 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
614 // with -msoft-float, disable use of MMX as well.
Evan Chenge738dc32009-03-26 23:06:32 +0000615 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
617 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
618 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
619 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
620 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000621
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000622 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
623 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
624 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
625 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000627 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
628 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
629 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
630 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000632 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
633 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000635 setOperationAction(ISD::AND, MVT::v8i8, Promote);
636 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
637 setOperationAction(ISD::AND, MVT::v4i16, Promote);
638 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
639 setOperationAction(ISD::AND, MVT::v2i32, Promote);
640 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
641 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000643 setOperationAction(ISD::OR, MVT::v8i8, Promote);
644 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
645 setOperationAction(ISD::OR, MVT::v4i16, Promote);
646 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
647 setOperationAction(ISD::OR, MVT::v2i32, Promote);
648 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
649 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000651 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
652 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
653 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
654 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
655 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
656 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
657 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000658
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000659 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
660 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
661 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
662 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
663 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
664 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
665 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
666 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
667 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000669 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
670 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
671 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000680 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
681 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000684
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000686
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
688 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
689 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
690 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
691 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
692 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
693 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000694 }
695
Evan Chenge738dc32009-03-26 23:06:32 +0000696 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000697 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000699 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
700 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
701 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
702 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
704 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
705 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
706 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
707 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
709 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
710 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 }
712
Evan Chenge738dc32009-03-26 23:06:32 +0000713 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000714 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000715
Bill Wendling042eda32009-03-11 22:30:01 +0000716 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
717 // registers cannot be used even for integer operations.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000718 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
719 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
720 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
721 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000723 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
724 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
725 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
726 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
727 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
728 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
729 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
730 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
731 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
732 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
733 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
734 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
735 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
736 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
738 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000740 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
741 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
742 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
743 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000744
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
746 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750
Mon P Wanga8ff0dd2010-01-24 00:05:03 +0000751 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
752 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
753 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
756
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000758 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
759 EVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000760 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000761 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000762 continue;
David Greenea5acb6e2009-06-29 16:47:10 +0000763 // Do not attempt to custom lower non-128-bit vectors
764 if (!VT.is128BitVector())
765 continue;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000766 setOperationAction(ISD::BUILD_VECTOR,
767 VT.getSimpleVT().SimpleTy, Custom);
768 setOperationAction(ISD::VECTOR_SHUFFLE,
769 VT.getSimpleVT().SimpleTy, Custom);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
771 VT.getSimpleVT().SimpleTy, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000772 }
Bill Wendling042eda32009-03-11 22:30:01 +0000773
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000774 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
775 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
776 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
777 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendling042eda32009-03-11 22:30:01 +0000780
Nate Begeman4294c1f2008-02-12 22:51:28 +0000781 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000784 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785
786 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000787 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
788 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersonac9de032009-08-10 22:56:29 +0000789 EVT VT = SVT;
David Greenea5acb6e2009-06-29 16:47:10 +0000790
791 // Do not attempt to promote non-128-bit vectors
792 if (!VT.is128BitVector()) {
793 continue;
794 }
Owen Andersona0c69eb2009-08-10 20:46:15 +0000795 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000796 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000797 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000798 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000799 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000800 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000801 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000802 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000803 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000804 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000805 }
806
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000807 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000808
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 // Custom lower v2i64 and v2f64 selects.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000810 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
811 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
812 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
813 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000814
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000815 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
816 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000817 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000818 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
819 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000820 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000821 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000822
Nate Begemand77e59e2008-02-11 04:19:36 +0000823 if (Subtarget->hasSSE41()) {
824 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000825 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000826
827 // i8 and i16 vectors are custom , because the source register and source
828 // source memory operand types are not the same width. f32 vectors are
829 // custom since the immediate controlling the insert encodes additional
830 // information.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000835
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
837 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
839 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000840
841 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
843 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000844 }
845 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846
Nate Begeman03605a02008-07-17 16:51:19 +0000847 if (Subtarget->hasSSE42()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000848 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000849 }
Scott Michel91099d62009-02-17 22:15:04 +0000850
David Greenea5acb6e2009-06-29 16:47:10 +0000851 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000852 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
853 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
854 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
855 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greeneed1b3db2009-06-29 22:50:51 +0000856
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000857 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
858 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
859 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
860 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
861 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
862 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
863 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
864 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
865 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
866 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
867 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
868 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
869 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
870 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
871 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000872
873 // Operations to consider commented out -v16i16 v32i8
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000874 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
875 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
876 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
877 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
878 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
879 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
880 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
881 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
882 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
883 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
884 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
885 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
886 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
887 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000888
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000889 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
890 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
891 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
892 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000893
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000894 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
895 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
896 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000899
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000900 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
901 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
902 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
903 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000906
907#if 0
908 // Not sure we want to do this since there are no 256-bit integer
909 // operations in AVX
910
911 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
912 // This includes 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000913 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
914 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000915
916 // Do not attempt to custom lower non-power-of-2 vectors
917 if (!isPowerOf2_32(VT.getVectorNumElements()))
918 continue;
919
920 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
921 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
923 }
924
925 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000926 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopher3d82bbd2009-08-27 18:07:15 +0000928 }
David Greenea5acb6e2009-06-29 16:47:10 +0000929#endif
930
931#if 0
932 // Not sure we want to do this since there are no 256-bit integer
933 // operations in AVX
934
935 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
936 // Including 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000937 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
938 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000939
940 if (!VT.is256BitVector()) {
941 continue;
942 }
943 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000944 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000945 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000946 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000947 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000948 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000949 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000950 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000951 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000952 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000953 }
954
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000955 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greenea5acb6e2009-06-29 16:47:10 +0000956#endif
957 }
958
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 // We want to custom lower some of our intrinsics.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000960 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000961
Bill Wendling7e04be62008-12-09 22:08:41 +0000962 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000963 setOperationAction(ISD::SADDO, MVT::i32, Custom);
964 setOperationAction(ISD::SADDO, MVT::i64, Custom);
965 setOperationAction(ISD::UADDO, MVT::i32, Custom);
966 setOperationAction(ISD::UADDO, MVT::i64, Custom);
967 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
968 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
969 setOperationAction(ISD::USUBO, MVT::i32, Custom);
970 setOperationAction(ISD::USUBO, MVT::i64, Custom);
971 setOperationAction(ISD::SMULO, MVT::i32, Custom);
972 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000973
Evan Cheng9c215602009-03-31 19:38:51 +0000974 if (!Subtarget->is64Bit()) {
975 // These libcalls are not available in 32-bit.
976 setLibcallName(RTLIB::SHL_I128, 0);
977 setLibcallName(RTLIB::SRL_I128, 0);
978 setLibcallName(RTLIB::SRA_I128, 0);
979 }
980
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 // We have target-specific dag combine patterns for the following nodes:
982 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000983 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000985 setTargetDAGCombine(ISD::SHL);
986 setTargetDAGCombine(ISD::SRA);
987 setTargetDAGCombine(ISD::SRL);
Evan Cheng10957b82010-01-04 21:22:48 +0000988 setTargetDAGCombine(ISD::OR);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000989 setTargetDAGCombine(ISD::STORE);
Owen Anderson58155b22009-06-29 18:04:45 +0000990 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Chengedeb1692009-12-16 00:53:11 +0000991 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng04ecee12009-03-28 05:57:29 +0000992 if (Subtarget->is64Bit())
993 setTargetDAGCombine(ISD::MUL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994
995 computeRegisterProperties();
996
Mon P Wangc707f3f2009-11-30 02:42:02 +0000997 // Divide and reminder operations have no vector equivalent and can
998 // trap. Do a custom widening for these operations in which we never
999 // generate more divides/remainder than the original vector width.
1000 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1002 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1003 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1004 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1005 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1006 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1007 }
1008 }
1009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 // FIXME: These should be based on subtarget info. Plus, the values should
1011 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +00001012 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1013 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1014 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Cheng45c1edb2008-02-28 00:43:03 +00001015 setPrefLoopAlignment(16);
Evan Cheng79566822009-05-13 21:42:09 +00001016 benefitFromCodePlacementOpt = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017}
1018
Scott Michel502151f2008-03-10 15:42:14 +00001019
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001020MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1021 return MVT::i8;
Scott Michel502151f2008-03-10 15:42:14 +00001022}
1023
1024
Evan Cheng5a67b812008-01-23 23:17:41 +00001025/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1026/// the desired ByVal argument alignment.
1027static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1028 if (MaxAlign == 16)
1029 return;
1030 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1031 if (VTy->getBitWidth() == 128)
1032 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +00001033 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1034 unsigned EltAlign = 0;
1035 getMaxByValAlign(ATy->getElementType(), EltAlign);
1036 if (EltAlign > MaxAlign)
1037 MaxAlign = EltAlign;
1038 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1039 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1040 unsigned EltAlign = 0;
1041 getMaxByValAlign(STy->getElementType(i), EltAlign);
1042 if (EltAlign > MaxAlign)
1043 MaxAlign = EltAlign;
1044 if (MaxAlign == 16)
1045 break;
1046 }
1047 }
1048 return;
1049}
1050
1051/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1052/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +00001053/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1054/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +00001055unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001056 if (Subtarget->is64Bit()) {
1057 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001058 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001059 if (TyAlign > 8)
1060 return TyAlign;
1061 return 8;
1062 }
1063
Evan Cheng5a67b812008-01-23 23:17:41 +00001064 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +00001065 if (Subtarget->hasSSE1())
1066 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +00001067 return Align;
1068}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069
Evan Cheng8c590372008-05-15 08:39:06 +00001070/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +00001071/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001072/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +00001073/// determining it.
Owen Andersonac9de032009-08-10 22:56:29 +00001074EVT
Evan Cheng8c590372008-05-15 08:39:06 +00001075X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patelc386c842009-06-05 21:57:13 +00001076 bool isSrcConst, bool isSrcStr,
1077 SelectionDAG &DAG) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001078 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1079 // linux. This is because the stack realignment code can't handle certain
1080 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patelc386c842009-06-05 21:57:13 +00001081 const Function *F = DAG.getMachineFunction().getFunction();
1082 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1083 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001084 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001085 return MVT::v4i32;
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001086 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001087 return MVT::v4f32;
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001088 }
Evan Cheng8c590372008-05-15 08:39:06 +00001089 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001090 return MVT::i64;
1091 return MVT::i32;
Evan Cheng8c590372008-05-15 08:39:06 +00001092}
1093
Chris Lattner25525cd2010-01-25 23:38:14 +00001094/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1095/// current function. The returned value is a member of the
1096/// MachineJumpTableInfo::JTEntryKind enum.
1097unsigned X86TargetLowering::getJumpTableEncoding() const {
1098 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1099 // symbol.
1100 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1101 Subtarget->isPICStyleGOT())
1102 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
1103
1104 // Otherwise, use the normal jump table encoding heuristics.
1105 return TargetLowering::getJumpTableEncoding();
1106}
1107
Evan Cheng6fb06762007-11-09 01:32:10 +00001108/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1109/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +00001110SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Cheng6fb06762007-11-09 01:32:10 +00001111 SelectionDAG &DAG) const {
Chris Lattneraa7c6d22009-07-09 03:15:51 +00001112 if (!Subtarget->is64Bit())
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001113 // This doesn't have DebugLoc associated with it, but is not really the
1114 // same as a Register.
1115 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1116 getPointerTy());
Evan Cheng6fb06762007-11-09 01:32:10 +00001117 return Table;
1118}
1119
Bill Wendling045f2632009-07-01 18:50:55 +00001120/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling25a8ae32009-06-30 22:38:32 +00001121unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman4f6b95c2009-08-18 00:20:06 +00001122 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling25a8ae32009-06-30 22:38:32 +00001123}
1124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001125//===----------------------------------------------------------------------===//
1126// Return Value Calling Convention Implementation
1127//===----------------------------------------------------------------------===//
1128
1129#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001130
Kenneth Uildriks87d04262009-11-07 02:11:54 +00001131bool
1132X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1133 const SmallVectorImpl<EVT> &OutTys,
1134 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1135 SelectionDAG &DAG) {
1136 SmallVector<CCValAssign, 16> RVLocs;
1137 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1138 RVLocs, *DAG.getContext());
1139 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1140}
1141
Dan Gohman9178de12009-08-05 01:29:28 +00001142SDValue
1143X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001144 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001145 const SmallVectorImpl<ISD::OutputArg> &Outs,
1146 DebugLoc dl, SelectionDAG &DAG) {
Scott Michel91099d62009-02-17 22:15:04 +00001147
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001148 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001149 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1150 RVLocs, *DAG.getContext());
1151 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001153 // If this is the first return lowered for this function, add the regs to the
1154 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +00001155 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 for (unsigned i = 0; i != RVLocs.size(); ++i)
1157 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +00001158 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001159 }
Scott Michel91099d62009-02-17 22:15:04 +00001160
Dan Gohman8181bd12008-07-27 21:46:04 +00001161 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001162
Dan Gohman8181bd12008-07-27 21:46:04 +00001163 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +00001164 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1165 // Operand #1 = Bytes To Pop
Dan Gohmane84197b2009-09-03 17:18:51 +00001166 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michel91099d62009-02-17 22:15:04 +00001167
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001169 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1170 CCValAssign &VA = RVLocs[i];
1171 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman9178de12009-08-05 01:29:28 +00001172 SDValue ValToCopy = Outs[i].Val;
Scott Michel91099d62009-02-17 22:15:04 +00001173
Chris Lattnerb56cc342008-03-11 03:23:40 +00001174 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1175 // the RET instruction and handled by the FP Stackifier.
Dan Gohman6c4be722009-02-04 17:28:58 +00001176 if (VA.getLocReg() == X86::ST0 ||
1177 VA.getLocReg() == X86::ST1) {
Chris Lattnerb56cc342008-03-11 03:23:40 +00001178 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1179 // change the value to the FP stack register class.
Dan Gohman6c4be722009-02-04 17:28:58 +00001180 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001181 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +00001182 RetOps.push_back(ValToCopy);
1183 // Don't emit a copytoreg.
1184 continue;
1185 }
Dale Johannesena585daf2008-06-24 22:01:44 +00001186
Evan Chengef356282009-02-23 09:03:22 +00001187 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1188 // which is returned in RAX / RDX.
Evan Chenge8db6e02009-02-22 08:05:12 +00001189 if (Subtarget->is64Bit()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001190 EVT ValVT = ValToCopy.getValueType();
Evan Chengef356282009-02-23 09:03:22 +00001191 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001192 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001193 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001194 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001195 }
Evan Chenge8db6e02009-02-22 08:05:12 +00001196 }
1197
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001198 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001199 Flag = Chain.getValue(1);
1200 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001201
1202 // The x86-64 ABI for returning structs by value requires that we copy
1203 // the sret argument into %rax for the return. We saved the argument into
1204 // a virtual register in the entry block, so now we copy the value out
1205 // and into %rax.
1206 if (Subtarget->is64Bit() &&
1207 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1208 MachineFunction &MF = DAG.getMachineFunction();
1209 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1210 unsigned Reg = FuncInfo->getSRetReturnReg();
1211 if (!Reg) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001212 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001213 FuncInfo->setSRetReturnReg(Reg);
1214 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001215 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001216
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001217 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001218 Flag = Chain.getValue(1);
Dan Gohman1c738f52009-10-12 16:36:12 +00001219
1220 // RAX now acts like a return value.
1221 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001222 }
Scott Michel91099d62009-02-17 22:15:04 +00001223
Chris Lattnerb56cc342008-03-11 03:23:40 +00001224 RetOps[0] = Chain; // Update chain.
1225
1226 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001227 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001228 RetOps.push_back(Flag);
Scott Michel91099d62009-02-17 22:15:04 +00001229
1230 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001231 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232}
1233
Dan Gohman9178de12009-08-05 01:29:28 +00001234/// LowerCallResult - Lower the result values of a call into the
1235/// appropriate copies out of appropriate physical registers.
1236///
1237SDValue
1238X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001239 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001240 const SmallVectorImpl<ISD::InputArg> &Ins,
1241 DebugLoc dl, SelectionDAG &DAG,
1242 SmallVectorImpl<SDValue> &InVals) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 // Assign locations to each value returned by this call.
1245 SmallVector<CCValAssign, 16> RVLocs;
Edwin Törökaf8e1332009-02-01 18:15:56 +00001246 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman9178de12009-08-05 01:29:28 +00001247 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Anderson175b6542009-07-22 00:24:57 +00001248 RVLocs, *DAG.getContext());
Dan Gohman9178de12009-08-05 01:29:28 +00001249 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001250
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001252 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman6c4be722009-02-04 17:28:58 +00001253 CCValAssign &VA = RVLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001254 EVT CopyVT = VA.getValVT();
Scott Michel91099d62009-02-17 22:15:04 +00001255
Edwin Törökaf8e1332009-02-01 18:15:56 +00001256 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001257 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman9178de12009-08-05 01:29:28 +00001258 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Edwin Török2b331342009-07-08 19:04:27 +00001259 llvm_report_error("SSE register return with SSE disabled");
Edwin Törökaf8e1332009-02-01 18:15:56 +00001260 }
1261
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001262 // If this is a call to a function that returns an fp value on the floating
1263 // point stack, but where we prefer to use the value in xmm registers, copy
1264 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman6c4be722009-02-04 17:28:58 +00001265 if ((VA.getLocReg() == X86::ST0 ||
1266 VA.getLocReg() == X86::ST1) &&
1267 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001268 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001269 }
Scott Michel91099d62009-02-17 22:15:04 +00001270
Evan Cheng9cc600e2009-02-20 20:43:02 +00001271 SDValue Val;
1272 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Chengef356282009-02-23 09:03:22 +00001273 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1274 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1275 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001276 MVT::v2i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001277 Val = Chain.getValue(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001278 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1279 Val, DAG.getConstant(0, MVT::i64));
Evan Chengef356282009-02-23 09:03:22 +00001280 } else {
1281 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001282 MVT::i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001283 Val = Chain.getValue(0);
1284 }
Evan Cheng9cc600e2009-02-20 20:43:02 +00001285 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1286 } else {
1287 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1288 CopyVT, InFlag).getValue(1);
1289 Val = Chain.getValue(0);
1290 }
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001291 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001292
Dan Gohman6c4be722009-02-04 17:28:58 +00001293 if (CopyVT != VA.getValVT()) {
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001294 // Round the F80 the right size, which also moves to the appropriate xmm
1295 // register.
Dan Gohman6c4be722009-02-04 17:28:58 +00001296 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001297 // This truncation won't change the value.
1298 DAG.getIntPtrConstant(1));
1299 }
Scott Michel91099d62009-02-17 22:15:04 +00001300
Dan Gohman9178de12009-08-05 01:29:28 +00001301 InVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001302 }
Duncan Sands698842f2008-07-02 17:40:58 +00001303
Dan Gohman9178de12009-08-05 01:29:28 +00001304 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001305}
1306
1307
1308//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001309// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310//===----------------------------------------------------------------------===//
1311// StdCall calling convention seems to be standard for many Windows' API
1312// routines and around. It differs from C calling convention just a little:
1313// callee should clean up the stack, not caller. Symbols should be also
1314// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001315// For info on fast calling convention see Fast Calling Convention (tail call)
1316// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317
Dan Gohman9178de12009-08-05 01:29:28 +00001318/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001319/// semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001320static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1321 if (Outs.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001322 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001323
Dan Gohman9178de12009-08-05 01:29:28 +00001324 return Outs[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001325}
1326
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001327/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001328/// return semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001329static bool
1330ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1331 if (Ins.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001332 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001333
Dan Gohman9178de12009-08-05 01:29:28 +00001334 return Ins[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001335}
1336
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001337/// IsCalleePop - Determines whether the callee is required to pop its
1338/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001339bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen18ace102008-01-05 16:56:59 +00001340 if (IsVarArg)
1341 return false;
1342
Dan Gohman705e3f72008-09-13 01:54:27 +00001343 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001344 default:
1345 return false;
1346 case CallingConv::X86_StdCall:
1347 return !Subtarget->is64Bit();
1348 case CallingConv::X86_FastCall:
1349 return !Subtarget->is64Bit();
1350 case CallingConv::Fast:
1351 return PerformTailCallOpt;
1352 }
1353}
1354
Dan Gohman705e3f72008-09-13 01:54:27 +00001355/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1356/// given CallingConvention value.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001357CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001358 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001359 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001360 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001361 else
1362 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001363 }
1364
Gordon Henriksen18ace102008-01-05 16:56:59 +00001365 if (CC == CallingConv::X86_FastCall)
1366 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001367 else if (CC == CallingConv::Fast)
1368 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001369 else
1370 return CC_X86_32_C;
1371}
1372
Dan Gohman9178de12009-08-05 01:29:28 +00001373/// NameDecorationForCallConv - Selects the appropriate decoration to
1374/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001375NameDecorationStyle
Sandeep Patel5838baa2009-09-02 08:44:58 +00001376X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman9178de12009-08-05 01:29:28 +00001377 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001378 return FastCall;
Dan Gohman9178de12009-08-05 01:29:28 +00001379 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380 return StdCall;
1381 return None;
1382}
1383
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001384
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001385/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1386/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001387/// the specific parameter attribute. The copy will be passed as a byval
1388/// function parameter.
Scott Michel91099d62009-02-17 22:15:04 +00001389static SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00001390CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001391 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1392 DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001393 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001394 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001395 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001396}
1397
Dan Gohman9178de12009-08-05 01:29:28 +00001398SDValue
1399X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001400 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001401 const SmallVectorImpl<ISD::InputArg> &Ins,
1402 DebugLoc dl, SelectionDAG &DAG,
1403 const CCValAssign &VA,
1404 MachineFrameInfo *MFI,
1405 unsigned i) {
1406
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001407 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001408 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1409 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001410 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001411 EVT ValVT;
1412
1413 // If value is passed by pointer we have address passed instead of the value
1414 // itself.
1415 if (VA.getLocInfo() == CCValAssign::Indirect)
1416 ValVT = VA.getLocVT();
1417 else
1418 ValVT = VA.getValVT();
Evan Cheng3e42a522008-01-10 02:24:25 +00001419
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001420 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michel91099d62009-02-17 22:15:04 +00001421 // changed with more analysis.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001422 // In case of tail call optimization mark all arguments mutable. Since they
1423 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001424 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene6424ab92009-11-12 20:49:22 +00001425 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00001426 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001427 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001428 return FIN;
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001429 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng1f996572009-10-17 07:53:04 +00001430 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001431}
1432
Dan Gohman8181bd12008-07-27 21:46:04 +00001433SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001434X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001435 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001436 bool isVarArg,
1437 const SmallVectorImpl<ISD::InputArg> &Ins,
1438 DebugLoc dl,
1439 SelectionDAG &DAG,
1440 SmallVectorImpl<SDValue> &InVals) {
1441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001443 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michel91099d62009-02-17 22:15:04 +00001444
Gordon Henriksen18ace102008-01-05 16:56:59 +00001445 const Function* Fn = MF.getFunction();
1446 if (Fn->hasExternalLinkage() &&
1447 Subtarget->isTargetCygMing() &&
1448 Fn->getName() == "main")
1449 FuncInfo->setForceFramePointer(true);
1450
1451 // Decorate the function name.
Dan Gohman9178de12009-08-05 01:29:28 +00001452 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001453
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001454 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001455 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001456 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001457
Dan Gohman9178de12009-08-05 01:29:28 +00001458 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001459 "Var args not supported with calling convention fastcc");
1460
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001461 // Assign locations to all of the incoming arguments.
1462 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001463 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1464 ArgLocs, *DAG.getContext());
1465 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001466
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 unsigned LastVal = ~0U;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001468 SDValue ArgValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1470 CCValAssign &VA = ArgLocs[i];
1471 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1472 // places.
1473 assert(VA.getValNo() != LastVal &&
1474 "Don't support value assigned to multiple locs yet");
1475 LastVal = VA.getValNo();
Scott Michel91099d62009-02-17 22:15:04 +00001476
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 if (VA.isRegLoc()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001478 EVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001479 TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001480 if (RegVT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 RC = X86::GR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001482 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001483 RC = X86::GR64RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001484 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001485 RC = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001486 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001487 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001488 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001489 RC = X86::VR128RegisterClass;
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001490 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1491 RC = X86::VR64RegisterClass;
1492 else
Edwin Törökbd448e32009-07-14 16:55:14 +00001493 llvm_unreachable("Unknown argument type!");
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001494
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001495 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman9178de12009-08-05 01:29:28 +00001496 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michel91099d62009-02-17 22:15:04 +00001497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001498 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1499 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1500 // right size.
1501 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001502 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 DAG.getValueType(VA.getValVT()));
1504 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001505 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001506 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001507 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikova6ad5be2009-08-03 08:14:14 +00001508 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michel91099d62009-02-17 22:15:04 +00001509
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001510 if (VA.isExtInLoc()) {
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001511 // Handle MMX values passed in XMM regs.
1512 if (RegVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001513 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1514 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001515 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1516 } else
1517 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001518 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 } else {
1520 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00001521 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001522 }
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001523
1524 // If value is passed via pointer - do a load.
1525 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman9178de12009-08-05 01:29:28 +00001526 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001527
Dan Gohman9178de12009-08-05 01:29:28 +00001528 InVals.push_back(ArgValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001530
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001531 // The x86-64 ABI for returning structs by value requires that we copy
1532 // the sret argument into %rax for the return. Save the argument into
1533 // a virtual register so that we can access it from the return points.
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001534 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001535 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1536 unsigned Reg = FuncInfo->getSRetReturnReg();
1537 if (!Reg) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001538 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001539 FuncInfo->setSRetReturnReg(Reg);
1540 }
Dan Gohman9178de12009-08-05 01:29:28 +00001541 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001542 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001543 }
1544
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001546 // align stack specially for tail calls
Dan Gohman9178de12009-08-05 01:29:28 +00001547 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001548 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549
1550 // If the function takes variable number of arguments, make a frame index for
1551 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001552 if (isVarArg) {
Dan Gohman9178de12009-08-05 01:29:28 +00001553 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene6424ab92009-11-12 20:49:22 +00001554 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001555 }
1556 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001557 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1558
1559 // FIXME: We should really autogenerate these arrays
1560 static const unsigned GPR64ArgRegsWin64[] = {
1561 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001562 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001563 static const unsigned XMMArgRegsWin64[] = {
1564 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1565 };
1566 static const unsigned GPR64ArgRegs64Bit[] = {
1567 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1568 };
1569 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001570 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1571 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1572 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001573 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1574
1575 if (IsWin64) {
1576 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1577 GPR64ArgRegs = GPR64ArgRegsWin64;
1578 XMMArgRegs = XMMArgRegsWin64;
1579 } else {
1580 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1581 GPR64ArgRegs = GPR64ArgRegs64Bit;
1582 XMMArgRegs = XMMArgRegs64Bit;
1583 }
1584 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1585 TotalNumIntRegs);
1586 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1587 TotalNumXMMRegs);
1588
Devang Patelc386c842009-06-05 21:57:13 +00001589 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Cheng0b84fe12009-02-13 22:36:38 +00001590 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Edwin Törökaf8e1332009-02-01 18:15:56 +00001591 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001592 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Cheng0b84fe12009-02-13 22:36:38 +00001593 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001594 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Edwin Törökaf8e1332009-02-01 18:15:56 +00001595 // Kernel mode asks for SSE to be disabled, so don't push them
1596 // on the stack.
1597 TotalNumXMMRegs = 0;
Bill Wendling042eda32009-03-11 22:30:01 +00001598
Gordon Henriksen18ace102008-01-05 16:56:59 +00001599 // For X86-64, if there are vararg parameters that are passed via
1600 // registers, then we must store them to their spots on the stack so they
1601 // may be loaded by deferencing the result of va_next.
1602 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001603 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1604 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene6424ab92009-11-12 20:49:22 +00001605 TotalNumXMMRegs * 16, 16,
1606 false);
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001607
Gordon Henriksen18ace102008-01-05 16:56:59 +00001608 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001609 SmallVector<SDValue, 8> MemOps;
1610 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman34228bf2009-08-15 01:38:56 +00001611 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001612 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohman34228bf2009-08-15 01:38:56 +00001613 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1614 DAG.getIntPtrConstant(Offset));
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001615 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1616 X86::GR64RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001617 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +00001618 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001619 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Cheng174e2cf2009-10-18 18:16:27 +00001620 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohman34228bf2009-08-15 01:38:56 +00001621 Offset);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001622 MemOps.push_back(Store);
Dan Gohman34228bf2009-08-15 01:38:56 +00001623 Offset += 8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001624 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001625
Dan Gohmanb9f06832009-08-16 21:24:25 +00001626 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1627 // Now store the XMM (fp + vector) parameter registers.
1628 SmallVector<SDValue, 11> SaveXMMOps;
1629 SaveXMMOps.push_back(Chain);
Dan Gohman34228bf2009-08-15 01:38:56 +00001630
Dan Gohmanb9f06832009-08-16 21:24:25 +00001631 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1632 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1633 SaveXMMOps.push_back(ALVal);
Dan Gohman34228bf2009-08-15 01:38:56 +00001634
Dan Gohmanb9f06832009-08-16 21:24:25 +00001635 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1636 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohman34228bf2009-08-15 01:38:56 +00001637
Dan Gohmanb9f06832009-08-16 21:24:25 +00001638 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1639 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1640 X86::VR128RegisterClass);
1641 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1642 SaveXMMOps.push_back(Val);
1643 }
1644 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1645 MVT::Other,
1646 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001647 }
Dan Gohmanb9f06832009-08-16 21:24:25 +00001648
1649 if (!MemOps.empty())
1650 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1651 &MemOps[0], MemOps.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001652 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001653 }
Scott Michel91099d62009-02-17 22:15:04 +00001654
Gordon Henriksen18ace102008-01-05 16:56:59 +00001655 // Some CCs need callee pop.
Dan Gohman9178de12009-08-05 01:29:28 +00001656 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001657 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 BytesCallerReserves = 0;
1659 } else {
1660 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman9178de12009-08-05 01:29:28 +00001662 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michel91099d62009-02-17 22:15:04 +00001663 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001664 BytesCallerReserves = StackSize;
1665 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001666
Gordon Henriksen18ace102008-01-05 16:56:59 +00001667 if (!Is64Bit) {
1668 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman9178de12009-08-05 01:29:28 +00001669 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001670 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1671 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001672
Anton Korobeynikove844e472007-08-15 17:12:32 +00001673 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674
Dan Gohman9178de12009-08-05 01:29:28 +00001675 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001676}
1677
Dan Gohman8181bd12008-07-27 21:46:04 +00001678SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001679X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1680 SDValue StackPtr, SDValue Arg,
1681 DebugLoc dl, SelectionDAG &DAG,
Evan Chengbc077bf2008-01-10 00:09:10 +00001682 const CCValAssign &VA,
Dan Gohman9178de12009-08-05 01:29:28 +00001683 ISD::ArgFlagsTy Flags) {
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001684 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001685 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001687 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001688 if (Flags.isByVal()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001689 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengbc077bf2008-01-10 00:09:10 +00001690 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001691 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001692 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001693}
1694
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001695/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001696/// optimization is performed and it is required.
Scott Michel91099d62009-02-17 22:15:04 +00001697SDValue
1698X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00001699 SDValue &OutRetAddr,
Scott Michel91099d62009-02-17 22:15:04 +00001700 SDValue Chain,
1701 bool IsTailCall,
1702 bool Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001703 int FPDiff,
1704 DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001705 if (!IsTailCall || FPDiff==0) return Chain;
1706
1707 // Adjust the Return address stack slot.
Owen Andersonac9de032009-08-10 22:56:29 +00001708 EVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001709 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001710
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001711 // Load the "old" Return address.
Dale Johannesence0805b2009-02-03 19:33:06 +00001712 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001713 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001714}
1715
1716/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1717/// optimization is performed and it is required (FPDiff!=0).
Scott Michel91099d62009-02-17 22:15:04 +00001718static SDValue
1719EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001720 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001721 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001722 // Store the return address to the appropriate stack slot.
1723 if (!FPDiff) return Chain;
1724 // Calculate the new stack slot for the return address.
1725 int SlotSize = Is64Bit ? 8 : 4;
Scott Michel91099d62009-02-17 22:15:04 +00001726 int NewReturnAddrFI =
David Greene6424ab92009-11-12 20:49:22 +00001727 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1728 true, false);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001729 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001730 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001731 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng1f996572009-10-17 07:53:04 +00001732 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001733 return Chain;
1734}
1735
Dan Gohman9178de12009-08-05 01:29:28 +00001736SDValue
1737X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001738 CallingConv::ID CallConv, bool isVarArg,
1739 bool isTailCall,
Dan Gohman9178de12009-08-05 01:29:28 +00001740 const SmallVectorImpl<ISD::OutputArg> &Outs,
1741 const SmallVectorImpl<ISD::InputArg> &Ins,
1742 DebugLoc dl, SelectionDAG &DAG,
1743 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001744
Dan Gohman9178de12009-08-05 01:29:28 +00001745 MachineFunction &MF = DAG.getMachineFunction();
1746 bool Is64Bit = Subtarget->is64Bit();
1747 bool IsStructRet = CallIsStructReturn(Outs);
1748
1749 assert((!isTailCall ||
1750 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1751 "IsEligibleForTailCallOptimization missed a case!");
1752 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001753 "Var args not supported with calling convention fastcc");
1754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 // Analyze operands of the call, assigning locations to each operand.
1756 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001757 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1758 ArgLocs, *DAG.getContext());
1759 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001760
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001761 // Get a count of how many bytes are to be pushed on the stack.
1762 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman9178de12009-08-05 01:29:28 +00001763 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001764 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001765
Gordon Henriksen18ace102008-01-05 16:56:59 +00001766 int FPDiff = 0;
Dan Gohman9178de12009-08-05 01:29:28 +00001767 if (isTailCall) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001768 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michel91099d62009-02-17 22:15:04 +00001769 unsigned NumBytesCallerPushed =
Gordon Henriksen18ace102008-01-05 16:56:59 +00001770 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1771 FPDiff = NumBytesCallerPushed - NumBytes;
1772
1773 // Set the delta of movement of the returnaddr stackslot.
1774 // But only set if delta is greater than previous delta.
1775 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1776 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1777 }
1778
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001779 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001780
Dan Gohman8181bd12008-07-27 21:46:04 +00001781 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001782 // Load return adress for tail calls.
Dan Gohman9178de12009-08-05 01:29:28 +00001783 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001784 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001785
Dan Gohman8181bd12008-07-27 21:46:04 +00001786 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1787 SmallVector<SDValue, 8> MemOpChains;
1788 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001790 // Walk the register/memloc assignments, inserting copies/loads. In the case
1791 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001792 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1793 CCValAssign &VA = ArgLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001794 EVT RegVT = VA.getLocVT();
Dan Gohman9178de12009-08-05 01:29:28 +00001795 SDValue Arg = Outs[i].Val;
1796 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman705e3f72008-09-13 01:54:27 +00001797 bool isByVal = Flags.isByVal();
Scott Michel91099d62009-02-17 22:15:04 +00001798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799 // Promote the value if needed.
1800 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001801 default: llvm_unreachable("Unknown loc info!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001802 case CCValAssign::Full: break;
1803 case CCValAssign::SExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001804 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001805 break;
1806 case CCValAssign::ZExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001807 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001808 break;
1809 case CCValAssign::AExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001810 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1811 // Special case: passing MMX values in XMM registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001812 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1813 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1814 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001815 } else
1816 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1817 break;
1818 case CCValAssign::BCvt:
1819 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 break;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001821 case CCValAssign::Indirect: {
1822 // Store the argument.
1823 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Cheng174e2cf2009-10-18 18:16:27 +00001824 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001825 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00001826 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001827 Arg = SpillSlot;
1828 break;
1829 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001830 }
Scott Michel91099d62009-02-17 22:15:04 +00001831
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001832 if (VA.isRegLoc()) {
1833 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1834 } else {
Dan Gohman9178de12009-08-05 01:29:28 +00001835 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001836 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001837 if (StackPtr.getNode() == 0)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001838 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michel91099d62009-02-17 22:15:04 +00001839
Dan Gohman9178de12009-08-05 01:29:28 +00001840 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1841 dl, DAG, VA, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001842 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001843 }
1844 }
Scott Michel91099d62009-02-17 22:15:04 +00001845
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001846 if (!MemOpChains.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001847 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001848 &MemOpChains[0], MemOpChains.size());
1849
1850 // Build a sequence of copy-to-reg nodes chained together with token chain
1851 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001852 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001853 // Tail call byval lowering might overwrite argument registers so in case of
1854 // tail call optimization the copies to registers are lowered later.
Dan Gohman9178de12009-08-05 01:29:28 +00001855 if (!isTailCall)
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001856 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001857 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001858 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001859 InFlag = Chain.getValue(1);
1860 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001861
Eric Christopher3d82bbd2009-08-27 18:07:15 +00001862
Chris Lattnerf165d342009-07-09 04:24:46 +00001863 if (Subtarget->isPICStyleGOT()) {
Chris Lattner679cad52009-07-09 02:55:47 +00001864 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1865 // GOT pointer.
Dan Gohman9178de12009-08-05 01:29:28 +00001866 if (!isTailCall) {
Chris Lattner679cad52009-07-09 02:55:47 +00001867 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1868 DAG.getNode(X86ISD::GlobalBaseReg,
1869 DebugLoc::getUnknownLoc(),
1870 getPointerTy()),
1871 InFlag);
1872 InFlag = Chain.getValue(1);
1873 } else {
1874 // If we are tail calling and generating PIC/GOT style code load the
1875 // address of the callee into ECX. The value in ecx is used as target of
1876 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1877 // for tail calls on PIC/GOT architectures. Normally we would just put the
1878 // address of GOT into ebx and then call target@PLT. But for tail calls
1879 // ebx would be restored (since ebx is callee saved) before jumping to the
1880 // target@PLT.
1881
1882 // Note: The actual moving to ECX is done further down.
1883 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1884 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1885 !G->getGlobal()->hasProtectedVisibility())
1886 Callee = LowerGlobalAddress(Callee, DAG);
1887 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner5d1f2572009-07-09 04:39:06 +00001888 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattner679cad52009-07-09 02:55:47 +00001889 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001890 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001891
Gordon Henriksen18ace102008-01-05 16:56:59 +00001892 if (Is64Bit && isVarArg) {
1893 // From AMD64 ABI document:
1894 // For calls that may call functions that use varargs or stdargs
1895 // (prototype-less calls or calls to functions containing ellipsis (...) in
1896 // the declaration) %al is used as hidden argument to specify the number
1897 // of SSE registers used. The contents of %al do not need to match exactly
1898 // the number of registers, but must be an ubound on the number of SSE
1899 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001900
1901 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001902 // Count the number of XMM registers allocated.
1903 static const unsigned XMMArgRegs[] = {
1904 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1905 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1906 };
1907 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michel91099d62009-02-17 22:15:04 +00001908 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Edwin Törökaf8e1332009-02-01 18:15:56 +00001909 && "SSE registers cannot be used when SSE is disabled");
Scott Michel91099d62009-02-17 22:15:04 +00001910
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001911 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001912 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001913 InFlag = Chain.getValue(1);
1914 }
1915
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001916
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001917 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001918 if (isTailCall) {
1919 // Force all the incoming stack arguments to be loaded from the stack
1920 // before any new outgoing arguments are stored to the stack, because the
1921 // outgoing stack slots may alias the incoming argument stack slots, and
1922 // the alias isn't otherwise explicit. This is slightly more conservative
1923 // than necessary, because it means that each store effectively depends
1924 // on every argument instead of just those arguments it would clobber.
1925 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1926
Dan Gohman8181bd12008-07-27 21:46:04 +00001927 SmallVector<SDValue, 8> MemOpChains2;
1928 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001929 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001930 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001931 InFlag = SDValue();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001932 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1933 CCValAssign &VA = ArgLocs[i];
1934 if (!VA.isRegLoc()) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001935 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00001936 SDValue Arg = Outs[i].Val;
1937 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001938 // Create frame index.
1939 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001940 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene6424ab92009-11-12 20:49:22 +00001941 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001942 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001943
Duncan Sandsc93fae32008-03-21 09:14:45 +00001944 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001945 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001946 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00001947 if (StackPtr.getNode() == 0)
Scott Michel91099d62009-02-17 22:15:04 +00001948 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001949 getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00001950 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001951
Dan Gohman9178de12009-08-05 01:29:28 +00001952 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1953 ArgChain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001954 Flags, DAG, dl));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001955 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001956 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00001957 MemOpChains2.push_back(
Dan Gohman9178de12009-08-05 01:29:28 +00001958 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng1f996572009-10-17 07:53:04 +00001959 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michel91099d62009-02-17 22:15:04 +00001960 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001961 }
1962 }
1963
1964 if (!MemOpChains2.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001965 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00001966 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001967
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001968 // Copy arguments to their registers.
1969 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001970 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001971 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001972 InFlag = Chain.getValue(1);
1973 }
Dan Gohman8181bd12008-07-27 21:46:04 +00001974 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001975
Gordon Henriksen18ace102008-01-05 16:56:59 +00001976 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001977 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001978 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001979 }
1980
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00001981 bool WasGlobalOrExternal = false;
1982 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
1983 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
1984 // In the 64-bit large code model, we have to make all calls
1985 // through a register, since the call instruction's 32-bit
1986 // pc-relative offset may not be large enough to hold the whole
1987 // address.
1988 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1989 WasGlobalOrExternal = true;
1990 // If the callee is a GlobalAddress node (quite common, every direct call
1991 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
1992 // it.
1993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 // We should use extra load for direct calls to dllimported functions in
1995 // non-JIT mode.
Chris Lattner48837612009-07-09 05:27:35 +00001996 GlobalValue *GV = G->getGlobal();
Chris Lattner180a7ee2009-07-10 05:48:03 +00001997 if (!GV->hasDLLImportLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00001998 unsigned char OpFlags = 0;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00001999
Chris Lattner8e8afe42009-07-09 05:02:21 +00002000 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2001 // external symbols most go through the PLT in PIC mode. If the symbol
2002 // has hidden or protected visibility, or if it is static or local, then
2003 // we don't need to use the PLT - we can directly call it.
2004 if (Subtarget->isTargetELF() &&
2005 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner48837612009-07-09 05:27:35 +00002006 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002007 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002008 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002009 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2010 Subtarget->getDarwinVers() < 9) {
2011 // PC-relative references to external symbols should go through $stub,
2012 // unless we're building with the leopard linker or later, which
2013 // automatically synthesizes these stubs.
2014 OpFlags = X86II::MO_DARWIN_STUB;
2015 }
Chris Lattner8e8afe42009-07-09 05:02:21 +00002016
Chris Lattner48837612009-07-09 05:27:35 +00002017 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner8e8afe42009-07-09 05:02:21 +00002018 G->getOffset(), OpFlags);
2019 }
Bill Wendlingfef06052008-09-16 21:48:12 +00002020 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002021 WasGlobalOrExternal = true;
Chris Lattner8e8afe42009-07-09 05:02:21 +00002022 unsigned char OpFlags = 0;
2023
2024 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2025 // symbols should go through the PLT.
2026 if (Subtarget->isTargetELF() &&
Chris Lattner48837612009-07-09 05:27:35 +00002027 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002028 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002029 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002030 Subtarget->getDarwinVers() < 9) {
2031 // PC-relative references to external symbols should go through $stub,
2032 // unless we're building with the leopard linker or later, which
2033 // automatically synthesizes these stubs.
2034 OpFlags = X86II::MO_DARWIN_STUB;
2035 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002036
Chris Lattner8e8afe42009-07-09 05:02:21 +00002037 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2038 OpFlags);
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002039 }
2040
2041 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighofera8726f02009-06-12 16:26:57 +00002042 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002043
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002044 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michel91099d62009-02-17 22:15:04 +00002045 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00002046 Callee,InFlag);
2047 Callee = DAG.getRegister(Opc, getPointerTy());
2048 // Add register as live out.
Dan Gohmanc21d06a2009-08-01 19:14:37 +00002049 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002050 }
Scott Michel91099d62009-02-17 22:15:04 +00002051
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002052 // Returns a chain & a flag for retval copy to use.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002053 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00002054 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002055
Dan Gohman9178de12009-08-05 01:29:28 +00002056 if (isTailCall) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002057 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2058 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002059 InFlag = Chain.getValue(1);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002060 }
Scott Michel91099d62009-02-17 22:15:04 +00002061
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062 Ops.push_back(Chain);
2063 Ops.push_back(Callee);
2064
Dan Gohman9178de12009-08-05 01:29:28 +00002065 if (isTailCall)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002066 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067
Gordon Henriksen18ace102008-01-05 16:56:59 +00002068 // Add argument registers to the end of the list so that they are known live
2069 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00002070 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2071 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2072 RegsToPass[i].second.getValueType()));
Scott Michel91099d62009-02-17 22:15:04 +00002073
Evan Cheng8ba45e62008-03-18 23:36:35 +00002074 // Add an implicit use GOT pointer in EBX.
Dan Gohman9178de12009-08-05 01:29:28 +00002075 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng8ba45e62008-03-18 23:36:35 +00002076 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2077
2078 // Add an implicit use of AL for x86 vararg functions.
2079 if (Is64Bit && isVarArg)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002080 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng8ba45e62008-03-18 23:36:35 +00002081
Gabor Greif1c80d112008-08-28 21:40:38 +00002082 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002083 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002084
Dan Gohman9178de12009-08-05 01:29:28 +00002085 if (isTailCall) {
2086 // If this is the first return lowered for this function, add the regs
2087 // to the liveout set for the function.
2088 if (MF.getRegInfo().liveout_empty()) {
2089 SmallVector<CCValAssign, 16> RVLocs;
2090 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2091 *DAG.getContext());
2092 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2093 for (unsigned i = 0; i != RVLocs.size(); ++i)
2094 if (RVLocs[i].isRegLoc())
2095 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2096 }
Scott Michel91099d62009-02-17 22:15:04 +00002097
Dan Gohman9178de12009-08-05 01:29:28 +00002098 assert(((Callee.getOpcode() == ISD::Register &&
2099 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskin846123d2010-01-09 18:56:43 +00002100 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman9178de12009-08-05 01:29:28 +00002101 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2102 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskin846123d2010-01-09 18:56:43 +00002103 "Expecting a global address, external symbol, or scratch register");
Dan Gohman9178de12009-08-05 01:29:28 +00002104
2105 return DAG.getNode(X86ISD::TC_RETURN, dl,
2106 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00002107 }
2108
Dale Johannesence0805b2009-02-03 19:33:06 +00002109 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002110 InFlag = Chain.getValue(1);
2111
2112 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00002113 unsigned NumBytesForCalleeToPush;
Dan Gohman9178de12009-08-05 01:29:28 +00002114 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen18ace102008-01-05 16:56:59 +00002115 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman9178de12009-08-05 01:29:28 +00002116 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 // If this is is a call to a struct-return function, the callee
2118 // pops the hidden struct pointer, so we have to push it back.
2119 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002120 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002121 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002122 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michel91099d62009-02-17 22:15:04 +00002123
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002124 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00002125 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00002126 DAG.getIntPtrConstant(NumBytes, true),
2127 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2128 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00002129 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002130 InFlag = Chain.getValue(1);
2131
2132 // Handle result values, copying them out of physregs into vregs that we
2133 // return.
Dan Gohman9178de12009-08-05 01:29:28 +00002134 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2135 Ins, dl, DAG, InVals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136}
2137
2138
2139//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002140// Fast Calling Convention (tail call) implementation
2141//===----------------------------------------------------------------------===//
2142
2143// Like std call, callee cleans arguments, convention except that ECX is
2144// reserved for storing the tail called function address. Only 2 registers are
2145// free for argument passing (inreg). Tail call optimization is performed
2146// provided:
2147// * tailcallopt is enabled
2148// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00002149// On X86_64 architecture with GOT-style position independent code only local
2150// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002151// To keep the stack aligned according to platform abi the function
2152// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2153// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002154// If a tail called function callee has more arguments than the caller the
2155// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002156// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002157// original REtADDR, but before the saved framepointer or the spilled registers
2158// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2159// stack layout:
2160// arg1
2161// arg2
2162// RETADDR
Scott Michel91099d62009-02-17 22:15:04 +00002163// [ new RETADDR
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002164// move area ]
2165// (possible EBP)
2166// ESI
2167// EDI
2168// local1 ..
2169
2170/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2171/// for a 16 byte align requirement.
Scott Michel91099d62009-02-17 22:15:04 +00002172unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002173 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00002174 MachineFunction &MF = DAG.getMachineFunction();
2175 const TargetMachine &TM = MF.getTarget();
2176 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2177 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michel91099d62009-02-17 22:15:04 +00002178 uint64_t AlignMask = StackAlignment - 1;
Evan Chengded8f902008-09-07 09:07:23 +00002179 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00002180 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00002181 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2182 // Number smaller than 12 so just add the difference.
2183 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2184 } else {
2185 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michel91099d62009-02-17 22:15:04 +00002186 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chengded8f902008-09-07 09:07:23 +00002187 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002188 }
Evan Chengded8f902008-09-07 09:07:23 +00002189 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002190}
2191
Dan Gohman9178de12009-08-05 01:29:28 +00002192/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2193/// for tail call optimization. Targets which want to do tail call
2194/// optimization should implement this function.
2195bool
2196X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel5838baa2009-09-02 08:44:58 +00002197 CallingConv::ID CalleeCC,
Dan Gohman9178de12009-08-05 01:29:28 +00002198 bool isVarArg,
2199 const SmallVectorImpl<ISD::InputArg> &Ins,
2200 SelectionDAG& DAG) const {
2201 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel5838baa2009-09-02 08:44:58 +00002202 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman9178de12009-08-05 01:29:28 +00002203 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002204}
2205
Dan Gohmanca4857a2008-09-03 23:12:08 +00002206FastISel *
2207X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohman76dd96e2008-09-23 21:53:34 +00002208 MachineModuleInfo *mmo,
Devang Patelfcf1c752009-01-13 00:35:13 +00002209 DwarfWriter *dw,
Dan Gohmanca4857a2008-09-03 23:12:08 +00002210 DenseMap<const Value *, unsigned> &vm,
2211 DenseMap<const BasicBlock *,
Dan Gohmand6211a72008-09-10 20:11:02 +00002212 MachineBasicBlock *> &bm,
Dan Gohman9dd43582008-10-14 23:54:11 +00002213 DenseMap<const AllocaInst *, int> &am
2214#ifndef NDEBUG
2215 , SmallSet<Instruction*, 8> &cil
2216#endif
2217 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00002218 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00002219#ifndef NDEBUG
2220 , cil
2221#endif
2222 );
Dan Gohman97805ee2008-08-19 21:32:53 +00002223}
2224
2225
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002226//===----------------------------------------------------------------------===//
2227// Other Lowering Hooks
2228//===----------------------------------------------------------------------===//
2229
2230
Dan Gohman8181bd12008-07-27 21:46:04 +00002231SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002232 MachineFunction &MF = DAG.getMachineFunction();
2233 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2234 int ReturnAddrIndex = FuncInfo->getRAIndex();
2235
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002236 if (ReturnAddrIndex == 0) {
2237 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00002238 uint64_t SlotSize = TD->getPointerSize();
David Greene6424ab92009-11-12 20:49:22 +00002239 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2240 true, false);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002241 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002242 }
2243
2244 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2245}
2246
2247
Anton Korobeynikovc283e152009-08-05 23:01:26 +00002248bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2249 bool hasSymbolicDisplacement) {
2250 // Offset should fit into 32 bit immediate field.
2251 if (!isInt32(Offset))
2252 return false;
2253
2254 // If we don't have a symbolic displacement - we don't have any extra
2255 // restrictions.
2256 if (!hasSymbolicDisplacement)
2257 return true;
2258
2259 // FIXME: Some tweaks might be needed for medium code model.
2260 if (M != CodeModel::Small && M != CodeModel::Kernel)
2261 return false;
2262
2263 // For small code model we assume that latest object is 16MB before end of 31
2264 // bits boundary. We may also accept pretty large negative constants knowing
2265 // that all objects are in the positive half of address space.
2266 if (M == CodeModel::Small && Offset < 16*1024*1024)
2267 return true;
2268
2269 // For kernel code model we know that all object resist in the negative half
2270 // of 32bits address space. We may not accept negative offsets, since they may
2271 // be just off and we may accept pretty large positive ones.
2272 if (M == CodeModel::Kernel && Offset > 0)
2273 return true;
2274
2275 return false;
2276}
2277
Chris Lattnerebb91142008-12-24 23:53:05 +00002278/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2279/// specific condition code, returning the condition code and the LHS/RHS of the
2280/// comparison to make.
2281static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2282 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002283 if (!isFP) {
2284 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2285 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2286 // X > -1 -> X == 0, jump !sign.
2287 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002288 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002289 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2290 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002291 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002292 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002293 // X < 1 -> X <= 0
2294 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002295 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002296 }
2297 }
2298
2299 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002300 default: llvm_unreachable("Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002301 case ISD::SETEQ: return X86::COND_E;
2302 case ISD::SETGT: return X86::COND_G;
2303 case ISD::SETGE: return X86::COND_GE;
2304 case ISD::SETLT: return X86::COND_L;
2305 case ISD::SETLE: return X86::COND_LE;
2306 case ISD::SETNE: return X86::COND_NE;
2307 case ISD::SETULT: return X86::COND_B;
2308 case ISD::SETUGT: return X86::COND_A;
2309 case ISD::SETULE: return X86::COND_BE;
2310 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002312 }
Scott Michel91099d62009-02-17 22:15:04 +00002313
Chris Lattnerb8397512008-12-23 23:42:27 +00002314 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002315
Chris Lattnerb8397512008-12-23 23:42:27 +00002316 // If LHS is a foldable load, but RHS is not, flip the condition.
2317 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2318 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2319 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2320 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002321 }
2322
Chris Lattnerb8397512008-12-23 23:42:27 +00002323 switch (SetCCOpcode) {
2324 default: break;
2325 case ISD::SETOLT:
2326 case ISD::SETOLE:
2327 case ISD::SETUGT:
2328 case ISD::SETUGE:
2329 std::swap(LHS, RHS);
2330 break;
2331 }
2332
2333 // On a floating point condition, the flags are set as follows:
2334 // ZF PF CF op
2335 // 0 | 0 | 0 | X > Y
2336 // 0 | 0 | 1 | X < Y
2337 // 1 | 0 | 0 | X == Y
2338 // 1 | 1 | 1 | unordered
2339 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002340 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002341 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002342 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002343 case ISD::SETOLT: // flipped
2344 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002345 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002346 case ISD::SETOLE: // flipped
2347 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002348 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002349 case ISD::SETUGT: // flipped
2350 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002351 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002352 case ISD::SETUGE: // flipped
2353 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002354 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002355 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002356 case ISD::SETNE: return X86::COND_NE;
2357 case ISD::SETUO: return X86::COND_P;
2358 case ISD::SETO: return X86::COND_NP;
Dan Gohman8ab7dd02009-10-20 16:22:37 +00002359 case ISD::SETOEQ:
2360 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattnerb8397512008-12-23 23:42:27 +00002361 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362}
2363
2364/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2365/// code. Current x86 isa includes the following FP cmov instructions:
2366/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2367static bool hasFPCMov(unsigned X86CC) {
2368 switch (X86CC) {
2369 default:
2370 return false;
2371 case X86::COND_B:
2372 case X86::COND_BE:
2373 case X86::COND_E:
2374 case X86::COND_P:
2375 case X86::COND_A:
2376 case X86::COND_AE:
2377 case X86::COND_NE:
2378 case X86::COND_NP:
2379 return true;
2380 }
2381}
2382
Evan Cheng6337b552009-10-27 19:56:55 +00002383/// isFPImmLegal - Returns true if the target can instruction select the
2384/// specified FP immediate natively. If false, the legalizer will
2385/// materialize the FP immediate as a load from a constant pool.
Evan Chenga0e67782009-10-28 01:43:28 +00002386bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Cheng6337b552009-10-27 19:56:55 +00002387 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2388 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2389 return true;
2390 }
2391 return false;
2392}
2393
Nate Begeman543d2142009-04-27 18:41:29 +00002394/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2395/// the specified range (L, H].
2396static bool isUndefOrInRange(int Val, int Low, int Hi) {
2397 return (Val < 0) || (Val >= Low && Val < Hi);
2398}
2399
2400/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2401/// specified value.
2402static bool isUndefOrEqual(int Val, int CmpVal) {
2403 if (Val < 0 || Val == CmpVal)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002404 return true;
Nate Begeman543d2142009-04-27 18:41:29 +00002405 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002406}
2407
Nate Begeman543d2142009-04-27 18:41:29 +00002408/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2409/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2410/// the second operand.
Owen Andersonac9de032009-08-10 22:56:29 +00002411static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002412 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman543d2142009-04-27 18:41:29 +00002413 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002414 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman543d2142009-04-27 18:41:29 +00002415 return (Mask[0] < 2 && Mask[1] < 2);
2416 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002417}
2418
Nate Begeman543d2142009-04-27 18:41:29 +00002419bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002420 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002421 N->getMask(M);
2422 return ::isPSHUFDMask(M, N->getValueType(0));
2423}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002424
Nate Begeman543d2142009-04-27 18:41:29 +00002425/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2426/// is suitable for input to PSHUFHW.
Owen Andersonac9de032009-08-10 22:56:29 +00002427static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002428 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002429 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002430
Nate Begeman543d2142009-04-27 18:41:29 +00002431 // Lower quadword copied in order or undef.
2432 for (int i = 0; i != 4; ++i)
2433 if (Mask[i] >= 0 && Mask[i] != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002434 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002436 // Upper quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002437 for (int i = 4; i != 8; ++i)
2438 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002439 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002440
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002441 return true;
2442}
2443
Nate Begeman543d2142009-04-27 18:41:29 +00002444bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002445 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002446 N->getMask(M);
2447 return ::isPSHUFHWMask(M, N->getValueType(0));
2448}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002449
Nate Begeman543d2142009-04-27 18:41:29 +00002450/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2451/// is suitable for input to PSHUFLW.
Owen Andersonac9de032009-08-10 22:56:29 +00002452static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002453 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002454 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002455
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002456 // Upper quadword copied in order.
Nate Begeman543d2142009-04-27 18:41:29 +00002457 for (int i = 4; i != 8; ++i)
2458 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002459 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002460
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002461 // Lower quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002462 for (int i = 0; i != 4; ++i)
2463 if (Mask[i] >= 4)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002464 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002465
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002466 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002467}
2468
Nate Begeman543d2142009-04-27 18:41:29 +00002469bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002470 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002471 N->getMask(M);
2472 return ::isPSHUFLWMask(M, N->getValueType(0));
2473}
2474
Nate Begeman080f8e22009-10-19 02:17:23 +00002475/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2476/// is suitable for input to PALIGNR.
2477static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2478 bool hasSSSE3) {
2479 int i, e = VT.getVectorNumElements();
2480
2481 // Do not handle v2i64 / v2f64 shuffles with palignr.
2482 if (e < 4 || !hasSSSE3)
2483 return false;
2484
2485 for (i = 0; i != e; ++i)
2486 if (Mask[i] >= 0)
2487 break;
2488
2489 // All undef, not a palignr.
2490 if (i == e)
2491 return false;
2492
2493 // Determine if it's ok to perform a palignr with only the LHS, since we
2494 // don't have access to the actual shuffle elements to see if RHS is undef.
2495 bool Unary = Mask[i] < (int)e;
2496 bool NeedsUnary = false;
2497
2498 int s = Mask[i] - i;
2499
2500 // Check the rest of the elements to see if they are consecutive.
2501 for (++i; i != e; ++i) {
2502 int m = Mask[i];
2503 if (m < 0)
2504 continue;
2505
2506 Unary = Unary && (m < (int)e);
2507 NeedsUnary = NeedsUnary || (m < s);
2508
2509 if (NeedsUnary && !Unary)
2510 return false;
2511 if (Unary && m != ((s+i) & (e-1)))
2512 return false;
2513 if (!Unary && m != (s+i))
2514 return false;
2515 }
2516 return true;
2517}
2518
2519bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2520 SmallVector<int, 8> M;
2521 N->getMask(M);
2522 return ::isPALIGNRMask(M, N->getValueType(0), true);
2523}
2524
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002525/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2526/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersonac9de032009-08-10 22:56:29 +00002527static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002528 int NumElems = VT.getVectorNumElements();
2529 if (NumElems != 2 && NumElems != 4)
2530 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002531
Nate Begeman543d2142009-04-27 18:41:29 +00002532 int Half = NumElems / 2;
2533 for (int i = 0; i < Half; ++i)
2534 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002535 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002536 for (int i = Half; i < NumElems; ++i)
2537 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002538 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002539
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002540 return true;
2541}
2542
Nate Begeman543d2142009-04-27 18:41:29 +00002543bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2544 SmallVector<int, 8> M;
2545 N->getMask(M);
2546 return ::isSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547}
2548
2549/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2550/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2551/// half elements to come from vector 1 (which would equal the dest.) and
2552/// the upper half to come from vector 2.
Owen Andersonac9de032009-08-10 22:56:29 +00002553static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002554 int NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002555
2556 if (NumElems != 2 && NumElems != 4)
Nate Begeman543d2142009-04-27 18:41:29 +00002557 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002558
Nate Begeman543d2142009-04-27 18:41:29 +00002559 int Half = NumElems / 2;
2560 for (int i = 0; i < Half; ++i)
2561 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002562 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002563 for (int i = Half; i < NumElems; ++i)
2564 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002565 return false;
2566 return true;
2567}
2568
Nate Begeman543d2142009-04-27 18:41:29 +00002569static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2570 SmallVector<int, 8> M;
2571 N->getMask(M);
2572 return isCommutedSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002573}
2574
2575/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2576/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman543d2142009-04-27 18:41:29 +00002577bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2578 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002579 return false;
2580
2581 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman543d2142009-04-27 18:41:29 +00002582 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2583 isUndefOrEqual(N->getMaskElt(1), 7) &&
2584 isUndefOrEqual(N->getMaskElt(2), 2) &&
2585 isUndefOrEqual(N->getMaskElt(3), 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002586}
2587
Nate Begemanb13034d2009-11-07 23:17:15 +00002588/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2589/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2590/// <2, 3, 2, 3>
2591bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2592 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2593
2594 if (NumElems != 4)
2595 return false;
2596
2597 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2598 isUndefOrEqual(N->getMaskElt(1), 3) &&
2599 isUndefOrEqual(N->getMaskElt(2), 2) &&
2600 isUndefOrEqual(N->getMaskElt(3), 3);
2601}
2602
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002603/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2604/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman543d2142009-04-27 18:41:29 +00002605bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2606 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002608 if (NumElems != 2 && NumElems != 4)
2609 return false;
2610
2611 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002612 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002613 return false;
2614
2615 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002616 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002617 return false;
2618
2619 return true;
2620}
2621
Nate Begemanb13034d2009-11-07 23:17:15 +00002622/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2623/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2624bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002625 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002626
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002627 if (NumElems != 2 && NumElems != 4)
2628 return false;
2629
2630 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002631 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002632 return false;
2633
Nate Begeman543d2142009-04-27 18:41:29 +00002634 for (unsigned i = 0; i < NumElems/2; ++i)
2635 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002636 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002637
2638 return true;
2639}
2640
2641/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2642/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersonac9de032009-08-10 22:56:29 +00002643static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002644 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002645 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002646 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2647 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002648
Nate Begeman543d2142009-04-27 18:41:29 +00002649 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2650 int BitI = Mask[i];
2651 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652 if (!isUndefOrEqual(BitI, j))
2653 return false;
2654 if (V2IsSplat) {
Mon P Wang56d91642009-02-04 01:16:59 +00002655 if (!isUndefOrEqual(BitI1, NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002656 return false;
2657 } else {
2658 if (!isUndefOrEqual(BitI1, j + NumElts))
2659 return false;
2660 }
2661 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 return true;
2663}
2664
Nate Begeman543d2142009-04-27 18:41:29 +00002665bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2666 SmallVector<int, 8> M;
2667 N->getMask(M);
2668 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669}
2670
2671/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2672/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002673static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002674 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002675 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002676 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2677 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002678
Nate Begeman543d2142009-04-27 18:41:29 +00002679 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2680 int BitI = Mask[i];
2681 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002682 if (!isUndefOrEqual(BitI, j + NumElts/2))
2683 return false;
2684 if (V2IsSplat) {
2685 if (isUndefOrEqual(BitI1, NumElts))
2686 return false;
2687 } else {
2688 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2689 return false;
2690 }
2691 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 return true;
2693}
2694
Nate Begeman543d2142009-04-27 18:41:29 +00002695bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2696 SmallVector<int, 8> M;
2697 N->getMask(M);
2698 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699}
2700
2701/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2702/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2703/// <0, 0, 1, 1>
Owen Andersonac9de032009-08-10 22:56:29 +00002704static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002705 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2707 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002708
Nate Begeman543d2142009-04-27 18:41:29 +00002709 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2710 int BitI = Mask[i];
2711 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002712 if (!isUndefOrEqual(BitI, j))
2713 return false;
2714 if (!isUndefOrEqual(BitI1, j))
2715 return false;
2716 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002717 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002718}
2719
Nate Begeman543d2142009-04-27 18:41:29 +00002720bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2721 SmallVector<int, 8> M;
2722 N->getMask(M);
2723 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2724}
2725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2727/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2728/// <2, 2, 3, 3>
Owen Andersonac9de032009-08-10 22:56:29 +00002729static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002730 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002731 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2732 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002733
Nate Begeman543d2142009-04-27 18:41:29 +00002734 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2735 int BitI = Mask[i];
2736 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002737 if (!isUndefOrEqual(BitI, j))
2738 return false;
2739 if (!isUndefOrEqual(BitI1, j))
2740 return false;
2741 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002742 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002743}
2744
Nate Begeman543d2142009-04-27 18:41:29 +00002745bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2746 SmallVector<int, 8> M;
2747 N->getMask(M);
2748 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2749}
2750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2752/// specifies a shuffle of elements that is suitable for input to MOVSS,
2753/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersonac9de032009-08-10 22:56:29 +00002754static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedmand49401f2009-06-06 06:05:10 +00002755 if (VT.getVectorElementType().getSizeInBits() < 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002756 return false;
Eli Friedmand49401f2009-06-06 06:05:10 +00002757
2758 int NumElts = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002759
Nate Begeman543d2142009-04-27 18:41:29 +00002760 if (!isUndefOrEqual(Mask[0], NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002761 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002762
Nate Begeman543d2142009-04-27 18:41:29 +00002763 for (int i = 1; i < NumElts; ++i)
2764 if (!isUndefOrEqual(Mask[i], i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002765 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002766
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002767 return true;
2768}
2769
Nate Begeman543d2142009-04-27 18:41:29 +00002770bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2771 SmallVector<int, 8> M;
2772 N->getMask(M);
2773 return ::isMOVLMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774}
2775
2776/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2777/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2778/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersonac9de032009-08-10 22:56:29 +00002779static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman543d2142009-04-27 18:41:29 +00002780 bool V2IsSplat = false, bool V2IsUndef = false) {
2781 int NumOps = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002782 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2783 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002784
Nate Begeman543d2142009-04-27 18:41:29 +00002785 if (!isUndefOrEqual(Mask[0], 0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002786 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002787
Nate Begeman543d2142009-04-27 18:41:29 +00002788 for (int i = 1; i < NumOps; ++i)
2789 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2790 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2791 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002792 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002793
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002794 return true;
2795}
2796
Nate Begeman543d2142009-04-27 18:41:29 +00002797static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002798 bool V2IsUndef = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002799 SmallVector<int, 8> M;
2800 N->getMask(M);
2801 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002802}
2803
2804/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2805/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002806bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2807 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002808 return false;
2809
2810 // Expect 1, 1, 3, 3
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002811 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002812 int Elt = N->getMaskElt(i);
2813 if (Elt >= 0 && Elt != 1)
2814 return false;
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002815 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002816
2817 bool HasHi = false;
2818 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002819 int Elt = N->getMaskElt(i);
2820 if (Elt >= 0 && Elt != 3)
2821 return false;
2822 if (Elt == 3)
2823 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002824 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002825 // Don't use movshdup if it can be done with a shufps.
Nate Begeman543d2142009-04-27 18:41:29 +00002826 // FIXME: verify that matching u, u, 3, 3 is what we want.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002827 return HasHi;
2828}
2829
2830/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2831/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002832bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2833 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002834 return false;
2835
2836 // Expect 0, 0, 2, 2
Nate Begeman543d2142009-04-27 18:41:29 +00002837 for (unsigned i = 0; i < 2; ++i)
2838 if (N->getMaskElt(i) > 0)
2839 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002840
2841 bool HasHi = false;
2842 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002843 int Elt = N->getMaskElt(i);
2844 if (Elt >= 0 && Elt != 2)
2845 return false;
2846 if (Elt == 2)
2847 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002848 }
Nate Begeman543d2142009-04-27 18:41:29 +00002849 // Don't use movsldup if it can be done with a shufps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002850 return HasHi;
2851}
2852
Evan Chenga2497eb2008-09-25 20:50:48 +00002853/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2854/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002855bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2856 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002857
Nate Begeman543d2142009-04-27 18:41:29 +00002858 for (int i = 0; i < e; ++i)
2859 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002860 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002861 for (int i = 0; i < e; ++i)
2862 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002863 return false;
2864 return true;
2865}
2866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002867/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00002868/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002869unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002870 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2871 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2872
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002873 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2874 unsigned Mask = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00002875 for (int i = 0; i < NumOperands; ++i) {
2876 int Val = SVOp->getMaskElt(NumOperands-i-1);
2877 if (Val < 0) Val = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 if (Val >= NumOperands) Val -= NumOperands;
2879 Mask |= Val;
2880 if (i != NumOperands - 1)
2881 Mask <<= Shift;
2882 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 return Mask;
2884}
2885
2886/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00002887/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002889 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002890 unsigned Mask = 0;
2891 // 8 nodes, but we only care about the last 4.
2892 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002893 int Val = SVOp->getMaskElt(i);
2894 if (Val >= 0)
Mon P Wang56d91642009-02-04 01:16:59 +00002895 Mask |= (Val - 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002896 if (i != 4)
2897 Mask <<= 2;
2898 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002899 return Mask;
2900}
2901
2902/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00002903/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002905 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906 unsigned Mask = 0;
2907 // 8 nodes, but we only care about the first 4.
2908 for (int i = 3; i >= 0; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002909 int Val = SVOp->getMaskElt(i);
2910 if (Val >= 0)
2911 Mask |= Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912 if (i != 0)
2913 Mask <<= 2;
2914 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002915 return Mask;
2916}
2917
Nate Begeman080f8e22009-10-19 02:17:23 +00002918/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2919/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2920unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2921 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2922 EVT VVT = N->getValueType(0);
2923 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2924 int Val = 0;
2925
2926 unsigned i, e;
2927 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2928 Val = SVOp->getMaskElt(i);
2929 if (Val >= 0)
2930 break;
2931 }
2932 return (Val - i) * EltSize;
2933}
2934
Evan Chengb723fb52009-07-30 08:33:02 +00002935/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2936/// constant +0.0.
2937bool X86::isZeroNode(SDValue Elt) {
2938 return ((isa<ConstantSDNode>(Elt) &&
2939 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2940 (isa<ConstantFPSDNode>(Elt) &&
2941 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2942}
2943
Nate Begeman543d2142009-04-27 18:41:29 +00002944/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2945/// their permute mask.
2946static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2947 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00002948 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002949 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00002950 SmallVector<int, 8> MaskVec;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002951
Nate Begemane8f61cb2009-04-29 05:20:52 +00002952 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002953 int idx = SVOp->getMaskElt(i);
2954 if (idx < 0)
2955 MaskVec.push_back(idx);
Nate Begemane8f61cb2009-04-29 05:20:52 +00002956 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00002957 MaskVec.push_back(idx + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002958 else
Nate Begeman543d2142009-04-27 18:41:29 +00002959 MaskVec.push_back(idx - NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002960 }
Nate Begeman543d2142009-04-27 18:41:29 +00002961 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2962 SVOp->getOperand(0), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002963}
2964
Evan Chenga6769df2007-12-07 21:30:01 +00002965/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2966/// the two vector operands have swapped position.
Owen Andersonac9de032009-08-10 22:56:29 +00002967static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00002968 unsigned NumElems = VT.getVectorNumElements();
2969 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002970 int idx = Mask[i];
2971 if (idx < 0)
Evan Chengfca29242007-12-07 08:07:39 +00002972 continue;
Nate Begemane8f61cb2009-04-29 05:20:52 +00002973 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00002974 Mask[i] = idx + NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00002975 else
Nate Begeman543d2142009-04-27 18:41:29 +00002976 Mask[i] = idx - NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00002977 }
Evan Chengfca29242007-12-07 08:07:39 +00002978}
2979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2981/// match movhlps. The lower half elements should come from upper half of
2982/// V1 (and in order), and the upper half elements should come from the upper
2983/// half of V2 (and in order).
Nate Begeman543d2142009-04-27 18:41:29 +00002984static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2985 if (Op->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 return false;
2987 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002988 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002989 return false;
2990 for (unsigned i = 2; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002991 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 return false;
2993 return true;
2994}
2995
2996/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00002997/// is promoted to a vector. It also returns the LoadSDNode by reference if
2998/// required.
2999static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00003000 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3001 return false;
3002 N = N->getOperand(0).getNode();
3003 if (!ISD::isNON_EXTLoad(N))
3004 return false;
3005 if (LD)
3006 *LD = cast<LoadSDNode>(N);
3007 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008}
3009
3010/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3011/// match movlp{s|d}. The lower half elements should come from lower half of
3012/// V1 (and in order), and the upper half elements should come from the upper
3013/// half of V2 (and in order). And since V1 will become the source of the
3014/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003015static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3016 ShuffleVectorSDNode *Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3018 return false;
3019 // Is V2 is a vector load, don't do this transformation. We will try to use
3020 // load folding shufps op.
3021 if (ISD::isNON_EXTLoad(V2))
3022 return false;
3023
Nate Begemane8f61cb2009-04-29 05:20:52 +00003024 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003025
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 if (NumElems != 2 && NumElems != 4)
3027 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003028 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003029 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003030 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003031 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003032 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033 return false;
3034 return true;
3035}
3036
3037/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3038/// all the same.
3039static bool isSplatVector(SDNode *N) {
3040 if (N->getOpcode() != ISD::BUILD_VECTOR)
3041 return false;
3042
Dan Gohman8181bd12008-07-27 21:46:04 +00003043 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003044 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3045 if (N->getOperand(i) != SplatValue)
3046 return false;
3047 return true;
3048}
3049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003050/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003051/// to an zero vector.
Nate Begemane8f61cb2009-04-29 05:20:52 +00003052/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman543d2142009-04-27 18:41:29 +00003053static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003054 SDValue V1 = N->getOperand(0);
3055 SDValue V2 = N->getOperand(1);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003056 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3057 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003058 int Idx = N->getMaskElt(i);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003059 if (Idx >= (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003060 unsigned Opc = V2.getOpcode();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003061 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3062 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003063 if (Opc != ISD::BUILD_VECTOR ||
3064 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman543d2142009-04-27 18:41:29 +00003065 return false;
3066 } else if (Idx >= 0) {
3067 unsigned Opc = V1.getOpcode();
3068 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3069 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003070 if (Opc != ISD::BUILD_VECTOR ||
3071 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003072 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003073 }
3074 }
3075 return true;
3076}
3077
3078/// getZeroVector - Returns a vector of specified type with all zero elements.
3079///
Owen Andersonac9de032009-08-10 22:56:29 +00003080static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003081 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003082 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003083
Chris Lattnere6aa3862007-11-25 00:24:49 +00003084 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3085 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003086 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003087 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003088 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3089 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003090 } else if (HasSSE2) { // SSE2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003091 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3092 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003093 } else { // SSE1
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003094 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3095 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003096 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003097 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098}
3099
Chris Lattnere6aa3862007-11-25 00:24:49 +00003100/// getOnesVector - Returns a vector of specified type with all bits set.
3101///
Owen Andersonac9de032009-08-10 22:56:29 +00003102static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003103 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003104
Chris Lattnere6aa3862007-11-25 00:24:49 +00003105 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3106 // type. This ensures they get CSE'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003107 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +00003108 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003109 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003110 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003111 else // SSE
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003112 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesence0805b2009-02-03 19:33:06 +00003113 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003114}
3115
3116
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003117/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3118/// that point to V2 points to its first element.
Nate Begeman543d2142009-04-27 18:41:29 +00003119static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003120 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003121 unsigned NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003122
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 bool Changed = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003124 SmallVector<int, 8> MaskVec;
3125 SVOp->getMask(MaskVec);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003126
Nate Begemane8f61cb2009-04-29 05:20:52 +00003127 for (unsigned i = 0; i != NumElems; ++i) {
3128 if (MaskVec[i] > (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003129 MaskVec[i] = NumElems;
3130 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003132 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003133 if (Changed)
Nate Begeman543d2142009-04-27 18:41:29 +00003134 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3135 SVOp->getOperand(1), &MaskVec[0]);
3136 return SDValue(SVOp, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137}
3138
3139/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3140/// operation of specified width.
Owen Andersonac9de032009-08-10 22:56:29 +00003141static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003142 SDValue V2) {
3143 unsigned NumElems = VT.getVectorNumElements();
3144 SmallVector<int, 8> Mask;
3145 Mask.push_back(NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003147 Mask.push_back(i);
3148 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003149}
3150
Nate Begeman543d2142009-04-27 18:41:29 +00003151/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003152static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003153 SDValue V2) {
3154 unsigned NumElems = VT.getVectorNumElements();
3155 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003156 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003157 Mask.push_back(i);
3158 Mask.push_back(i + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003159 }
Nate Begeman543d2142009-04-27 18:41:29 +00003160 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003161}
3162
Nate Begeman543d2142009-04-27 18:41:29 +00003163/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003164static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003165 SDValue V2) {
3166 unsigned NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003167 unsigned Half = NumElems/2;
Nate Begeman543d2142009-04-27 18:41:29 +00003168 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003169 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003170 Mask.push_back(i + Half);
3171 Mask.push_back(i + NumElems + Half);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003172 }
Nate Begeman543d2142009-04-27 18:41:29 +00003173 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003174}
3175
Evan Chengbf8b2c52008-04-05 00:30:36 +00003176/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003177static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00003178 bool HasSSE2) {
3179 if (SV->getValueType(0).getVectorNumElements() <= 4)
3180 return SDValue(SV, 0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003181
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003182 EVT PVT = MVT::v4f32;
Owen Andersonac9de032009-08-10 22:56:29 +00003183 EVT VT = SV->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00003184 DebugLoc dl = SV->getDebugLoc();
3185 SDValue V1 = SV->getOperand(0);
3186 int NumElems = VT.getVectorNumElements();
3187 int EltNo = SV->getSplatIndex();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003188
Nate Begeman543d2142009-04-27 18:41:29 +00003189 // unpack elements to the correct location
3190 while (NumElems > 4) {
3191 if (EltNo < NumElems/2) {
3192 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3193 } else {
3194 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3195 EltNo -= NumElems/2;
3196 }
3197 NumElems >>= 1;
3198 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003199
Nate Begeman543d2142009-04-27 18:41:29 +00003200 // Perform the splat.
3201 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesence0805b2009-02-03 19:33:06 +00003202 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman543d2142009-04-27 18:41:29 +00003203 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3204 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003205}
3206
3207/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003208/// vector of zero or undef vector. This produces a shuffle where the low
3209/// element of V2 is swizzled into the zero/undef vector, landing at element
3210/// 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 +00003211static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003212 bool isZero, bool HasSSE2,
3213 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003214 EVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003215 SDValue V1 = isZero
Nate Begeman543d2142009-04-27 18:41:29 +00003216 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3217 unsigned NumElems = VT.getVectorNumElements();
3218 SmallVector<int, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003219 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003220 // If this is the insertion idx, put the low elt of V2 here.
3221 MaskVec.push_back(i == Idx ? NumElems : i);
3222 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003223}
3224
Evan Chengdea99362008-05-29 08:22:04 +00003225/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3226/// a shuffle that is zero.
3227static
Nate Begeman543d2142009-04-27 18:41:29 +00003228unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3229 bool Low, SelectionDAG &DAG) {
Evan Chengdea99362008-05-29 08:22:04 +00003230 unsigned NumZeros = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00003231 for (int i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003232 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman543d2142009-04-27 18:41:29 +00003233 int Idx = SVOp->getMaskElt(Index);
3234 if (Idx < 0) {
Evan Chengdea99362008-05-29 08:22:04 +00003235 ++NumZeros;
3236 continue;
3237 }
Nate Begeman543d2142009-04-27 18:41:29 +00003238 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Chengb723fb52009-07-30 08:33:02 +00003239 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003240 ++NumZeros;
3241 else
3242 break;
3243 }
3244 return NumZeros;
3245}
3246
3247/// isVectorShift - Returns true if the shuffle can be implemented as a
3248/// logical left or right shift of a vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003249/// FIXME: split into pslldqi, psrldqi, palignr variants.
3250static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00003251 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman543d2142009-04-27 18:41:29 +00003252 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengdea99362008-05-29 08:22:04 +00003253
3254 isLeft = true;
Nate Begeman543d2142009-04-27 18:41:29 +00003255 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003256 if (!NumZeros) {
3257 isLeft = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003258 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003259 if (!NumZeros)
3260 return false;
3261 }
Evan Chengdea99362008-05-29 08:22:04 +00003262 bool SeenV1 = false;
3263 bool SeenV2 = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003264 for (int i = NumZeros; i < NumElems; ++i) {
3265 int Val = isLeft ? (i - NumZeros) : i;
3266 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3267 if (Idx < 0)
Evan Chengdea99362008-05-29 08:22:04 +00003268 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00003269 if (Idx < NumElems)
Evan Chengdea99362008-05-29 08:22:04 +00003270 SeenV1 = true;
3271 else {
Nate Begeman543d2142009-04-27 18:41:29 +00003272 Idx -= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00003273 SeenV2 = true;
3274 }
Nate Begeman543d2142009-04-27 18:41:29 +00003275 if (Idx != Val)
Evan Chengdea99362008-05-29 08:22:04 +00003276 return false;
3277 }
3278 if (SeenV1 && SeenV2)
3279 return false;
3280
Nate Begeman543d2142009-04-27 18:41:29 +00003281 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengdea99362008-05-29 08:22:04 +00003282 ShAmt = NumZeros;
3283 return true;
3284}
3285
3286
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003287/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3288///
Dan Gohman8181bd12008-07-27 21:46:04 +00003289static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003290 unsigned NumNonZero, unsigned NumZero,
3291 SelectionDAG &DAG, TargetLowering &TLI) {
3292 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003293 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003295 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003296 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003297 bool First = true;
3298 for (unsigned i = 0; i < 16; ++i) {
3299 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3300 if (ThisIsNonZero && First) {
3301 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003302 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003303 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003304 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003305 First = false;
3306 }
3307
3308 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003309 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003310 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3311 if (LastIsNonZero) {
Scott Michel91099d62009-02-17 22:15:04 +00003312 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003313 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003314 }
3315 if (ThisIsNonZero) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003316 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3317 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3318 ThisElt, DAG.getConstant(8, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003319 if (LastIsNonZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003320 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003321 } else
3322 ThisElt = LastElt;
3323
Gabor Greif1c80d112008-08-28 21:40:38 +00003324 if (ThisElt.getNode())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003325 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003326 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003327 }
3328 }
3329
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003330 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331}
3332
3333/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3334///
Dan Gohman8181bd12008-07-27 21:46:04 +00003335static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003336 unsigned NumNonZero, unsigned NumZero,
3337 SelectionDAG &DAG, TargetLowering &TLI) {
3338 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003340
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003341 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003343 bool First = true;
3344 for (unsigned i = 0; i < 8; ++i) {
3345 bool isNonZero = (NonZeros & (1 << i)) != 0;
3346 if (isNonZero) {
3347 if (First) {
3348 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003349 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003351 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003352 First = false;
3353 }
Scott Michel91099d62009-02-17 22:15:04 +00003354 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003355 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003356 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003357 }
3358 }
3359
3360 return V;
3361}
3362
Evan Chengdea99362008-05-29 08:22:04 +00003363/// getVShift - Return a vector logical shift node.
3364///
Owen Andersonac9de032009-08-10 22:56:29 +00003365static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman543d2142009-04-27 18:41:29 +00003366 unsigned NumBits, SelectionDAG &DAG,
3367 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003368 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003369 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003370 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003371 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3372 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3373 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003374 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003375}
3376
Dan Gohman8181bd12008-07-27 21:46:04 +00003377SDValue
Evan Chenge31a26a2009-12-09 21:00:30 +00003378X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3379 SelectionDAG &DAG) {
3380
3381 // Check if the scalar load can be widened into a vector load. And if
3382 // the address is "base + cst" see if the cst can be "absorbed" into
3383 // the shuffle mask.
3384 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3385 SDValue Ptr = LD->getBasePtr();
3386 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3387 return SDValue();
3388 EVT PVT = LD->getValueType(0);
3389 if (PVT != MVT::i32 && PVT != MVT::f32)
3390 return SDValue();
3391
3392 int FI = -1;
3393 int64_t Offset = 0;
3394 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3395 FI = FINode->getIndex();
3396 Offset = 0;
3397 } else if (Ptr.getOpcode() == ISD::ADD &&
3398 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3399 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3400 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3401 Offset = Ptr.getConstantOperandVal(1);
3402 Ptr = Ptr.getOperand(0);
3403 } else {
3404 return SDValue();
3405 }
3406
3407 SDValue Chain = LD->getChain();
3408 // Make sure the stack object alignment is at least 16.
3409 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3410 if (DAG.InferPtrAlignment(Ptr) < 16) {
3411 if (MFI->isFixedObjectIndex(FI)) {
Eric Christopherc21aa852010-01-23 06:02:43 +00003412 // Can't change the alignment. FIXME: It's possible to compute
3413 // the exact stack offset and reference FI + adjust offset instead.
3414 // If someone *really* cares about this. That's the way to implement it.
3415 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003416 } else {
3417 MFI->setObjectAlignment(FI, 16);
3418 }
3419 }
3420
3421 // (Offset % 16) must be multiple of 4. Then address is then
3422 // Ptr + (Offset & ~15).
3423 if (Offset < 0)
3424 return SDValue();
3425 if ((Offset % 16) & 3)
3426 return SDValue();
3427 int64_t StartOffset = Offset & ~15;
3428 if (StartOffset)
3429 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3430 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3431
3432 int EltNo = (Offset - StartOffset) >> 2;
3433 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3434 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3435 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3436 // Canonicalize it to a v4i32 shuffle.
3437 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3438 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3439 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3440 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3441 }
3442
3443 return SDValue();
3444}
3445
3446SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00003447X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003448 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003449 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003450 if (ISD::isBuildVectorAllZeros(Op.getNode())
3451 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003452 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3453 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3454 // eliminated on x86-32 hosts.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003455 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattnere6aa3862007-11-25 00:24:49 +00003456 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457
Gabor Greif1c80d112008-08-28 21:40:38 +00003458 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003459 return getOnesVector(Op.getValueType(), DAG, dl);
3460 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003461 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462
Owen Andersonac9de032009-08-10 22:56:29 +00003463 EVT VT = Op.getValueType();
3464 EVT ExtVT = VT.getVectorElementType();
3465 unsigned EVTBits = ExtVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003466
3467 unsigned NumElems = Op.getNumOperands();
3468 unsigned NumZero = 0;
3469 unsigned NumNonZero = 0;
3470 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003471 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003472 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003473 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003474 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003475 if (Elt.getOpcode() == ISD::UNDEF)
3476 continue;
3477 Values.insert(Elt);
3478 if (Elt.getOpcode() != ISD::Constant &&
3479 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003480 IsAllConstants = false;
Evan Chengb723fb52009-07-30 08:33:02 +00003481 if (X86::isZeroNode(Elt))
Evan Chengc1073492007-12-12 06:45:40 +00003482 NumZero++;
3483 else {
3484 NonZeros |= (1 << i);
3485 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003486 }
3487 }
3488
3489 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003490 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003491 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 }
3493
Chris Lattner66a4dda2008-03-09 05:42:06 +00003494 // Special case for single non-zero, non-undef, element.
Eli Friedmand49401f2009-06-06 06:05:10 +00003495 if (NumNonZero == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003496 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003497 SDValue Item = Op.getOperand(Idx);
Scott Michel91099d62009-02-17 22:15:04 +00003498
Chris Lattner2d91b962008-03-09 01:05:04 +00003499 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3500 // the value are obviously zero, truncate the value to i32 and do the
3501 // insertion that way. Only do this if the value is non-constant or if the
3502 // value is a constant being inserted into element 0. It is cheaper to do
3503 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003504 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner2d91b962008-03-09 01:05:04 +00003505 (!IsAllConstants || Idx == 0)) {
3506 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3507 // Handle MMX and SSE both.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003508 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3509 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michel91099d62009-02-17 22:15:04 +00003510
Chris Lattner2d91b962008-03-09 01:05:04 +00003511 // Truncate the value (which may itself be a constant) to i32, and
3512 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003513 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesence0805b2009-02-03 19:33:06 +00003514 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003515 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3516 Subtarget->hasSSE2(), DAG);
Scott Michel91099d62009-02-17 22:15:04 +00003517
Chris Lattner2d91b962008-03-09 01:05:04 +00003518 // Now we have our 32-bit value zero extended in the low element of
3519 // a vector. If Idx != 0, swizzle it into place.
3520 if (Idx != 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003521 SmallVector<int, 4> Mask;
3522 Mask.push_back(Idx);
3523 for (unsigned i = 1; i != VecElts; ++i)
3524 Mask.push_back(i);
3525 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003526 DAG.getUNDEF(Item.getValueType()),
Nate Begeman543d2142009-04-27 18:41:29 +00003527 &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003528 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003529 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003530 }
3531 }
Scott Michel91099d62009-02-17 22:15:04 +00003532
Chris Lattnerac914892008-03-08 22:59:52 +00003533 // If we have a constant or non-constant insertion into the low element of
3534 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3535 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedmand49401f2009-06-06 06:05:10 +00003536 // depending on what the source datatype is.
3537 if (Idx == 0) {
3538 if (NumZero == 0) {
3539 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003540 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3541 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedmand49401f2009-06-06 06:05:10 +00003542 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3543 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3544 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3545 DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003546 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3547 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3548 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedmand49401f2009-06-06 06:05:10 +00003549 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3550 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3551 Subtarget->hasSSE2(), DAG);
3552 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3553 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003554 }
Evan Chengdea99362008-05-29 08:22:04 +00003555
3556 // Is it a vector logical left shift?
3557 if (NumElems == 2 && Idx == 1 &&
Evan Chengb723fb52009-07-30 08:33:02 +00003558 X86::isZeroNode(Op.getOperand(0)) &&
3559 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003560 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003561 return getVShift(true, VT,
Scott Michel91099d62009-02-17 22:15:04 +00003562 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00003563 VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003564 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003565 }
Scott Michel91099d62009-02-17 22:15:04 +00003566
Chris Lattner92bdcb52008-03-08 22:48:29 +00003567 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003568 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003569
Chris Lattnerac914892008-03-08 22:59:52 +00003570 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3571 // is a non-constant being inserted into an element other than the low one,
3572 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3573 // movd/movss) to move this into the low element, then shuffle it into
3574 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003576 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michel91099d62009-02-17 22:15:04 +00003577
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003578 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003579 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3580 Subtarget->hasSSE2(), DAG);
Nate Begeman543d2142009-04-27 18:41:29 +00003581 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003582 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman543d2142009-04-27 18:41:29 +00003583 MaskVec.push_back(i == Idx ? 0 : 1);
3584 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003585 }
3586 }
3587
Chris Lattner66a4dda2008-03-09 05:42:06 +00003588 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chenge31a26a2009-12-09 21:00:30 +00003589 if (Values.size() == 1) {
3590 if (EVTBits == 32) {
3591 // Instead of a shuffle like this:
3592 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3593 // Check if it's possible to issue this instead.
3594 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3595 unsigned Idx = CountTrailingZeros_32(NonZeros);
3596 SDValue Item = Op.getOperand(Idx);
3597 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3598 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3599 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003600 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003601 }
Scott Michel91099d62009-02-17 22:15:04 +00003602
Dan Gohman21463242007-07-24 22:55:08 +00003603 // A vector full of immediates; various special cases are already
3604 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003605 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003607
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003608 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003609 if (EVTBits == 64) {
3610 if (NumNonZero == 1) {
3611 // One half is zero or undef.
3612 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003613 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003614 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003615 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3616 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003617 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003618 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003619 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003620
3621 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3622 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003623 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003624 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003625 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003626 }
3627
3628 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003629 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003631 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003632 }
3633
3634 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003635 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003636 V.resize(NumElems);
3637 if (NumElems == 4 && NumZero > 0) {
3638 for (unsigned i = 0; i < 4; ++i) {
3639 bool isZero = !(NonZeros & (1 << i));
3640 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003641 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003642 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003643 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 }
3645
3646 for (unsigned i = 0; i < 2; ++i) {
3647 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3648 default: break;
3649 case 0:
3650 V[i] = V[i*2]; // Must be a zero vector.
3651 break;
3652 case 1:
Nate Begeman543d2142009-04-27 18:41:29 +00003653 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003654 break;
3655 case 2:
Nate Begeman543d2142009-04-27 18:41:29 +00003656 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003657 break;
3658 case 3:
Nate Begeman543d2142009-04-27 18:41:29 +00003659 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003660 break;
3661 }
3662 }
3663
Nate Begeman543d2142009-04-27 18:41:29 +00003664 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 bool Reverse = (NonZeros & 0x3) == 2;
3666 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003667 MaskVec.push_back(Reverse ? 1-i : i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003668 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3669 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003670 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3671 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003672 }
3673
3674 if (Values.size() > 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00003675 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3676 // values to be inserted is equal to the number of elements, in which case
3677 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003678 // load merge pattern for shuffles.
Nate Begeman543d2142009-04-27 18:41:29 +00003679 // FIXME: We could probably just check that here directly.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003680 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman543d2142009-04-27 18:41:29 +00003681 getSubtarget()->hasSSE41()) {
3682 V[0] = DAG.getUNDEF(VT);
3683 for (unsigned i = 0; i < NumElems; ++i)
3684 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3685 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3686 Op.getOperand(i), DAG.getIntPtrConstant(i));
3687 return V[0];
3688 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003689 // Expand into a number of unpckl*.
3690 // e.g. for v4f32
3691 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3692 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3693 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003694 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003695 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696 NumElems >>= 1;
3697 while (NumElems != 0) {
3698 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003699 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003700 NumElems >>= 1;
3701 }
3702 return V[0];
3703 }
3704
Dan Gohman8181bd12008-07-27 21:46:04 +00003705 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003706}
3707
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00003708SDValue
3709X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3710 // We support concatenate two MMX registers and place them in a MMX
3711 // register. This is better than doing a stack convert.
3712 DebugLoc dl = Op.getDebugLoc();
3713 EVT ResVT = Op.getValueType();
3714 assert(Op.getNumOperands() == 2);
3715 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3716 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3717 int Mask[2];
3718 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3719 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3720 InVec = Op.getOperand(1);
3721 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3722 unsigned NumElts = ResVT.getVectorNumElements();
3723 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3724 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3725 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3726 } else {
3727 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3728 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3729 Mask[0] = 0; Mask[1] = 2;
3730 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3731 }
3732 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3733}
3734
Nate Begeman2c87c422009-02-23 08:49:38 +00003735// v8i16 shuffles - Prefer shuffles in the following order:
3736// 1. [all] pshuflw, pshufhw, optional move
3737// 2. [ssse3] 1 x pshufb
3738// 3. [ssse3] 2 x pshufb + 1 x por
3739// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Chengfca29242007-12-07 08:07:39 +00003740static
Nate Begeman543d2142009-04-27 18:41:29 +00003741SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3742 SelectionDAG &DAG, X86TargetLowering &TLI) {
3743 SDValue V1 = SVOp->getOperand(0);
3744 SDValue V2 = SVOp->getOperand(1);
3745 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00003746 SmallVector<int, 8> MaskVals;
Evan Cheng75184a92007-12-11 01:46:18 +00003747
Nate Begeman2c87c422009-02-23 08:49:38 +00003748 // Determine if more than 1 of the words in each of the low and high quadwords
3749 // of the result come from the same quadword of one of the two inputs. Undef
3750 // mask values count as coming from any quadword, for better codegen.
3751 SmallVector<unsigned, 4> LoQuad(4);
3752 SmallVector<unsigned, 4> HiQuad(4);
3753 BitVector InputQuads(4);
3754 for (unsigned i = 0; i < 8; ++i) {
3755 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman543d2142009-04-27 18:41:29 +00003756 int EltIdx = SVOp->getMaskElt(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00003757 MaskVals.push_back(EltIdx);
3758 if (EltIdx < 0) {
3759 ++Quad[0];
3760 ++Quad[1];
3761 ++Quad[2];
3762 ++Quad[3];
Evan Cheng75184a92007-12-11 01:46:18 +00003763 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003764 }
3765 ++Quad[EltIdx / 4];
3766 InputQuads.set(EltIdx / 4);
Evan Cheng75184a92007-12-11 01:46:18 +00003767 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003768
Nate Begeman2c87c422009-02-23 08:49:38 +00003769 int BestLoQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003770 unsigned MaxQuad = 1;
3771 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003772 if (LoQuad[i] > MaxQuad) {
3773 BestLoQuad = i;
3774 MaxQuad = LoQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003775 }
Evan Chengfca29242007-12-07 08:07:39 +00003776 }
3777
Nate Begeman2c87c422009-02-23 08:49:38 +00003778 int BestHiQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003779 MaxQuad = 1;
3780 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003781 if (HiQuad[i] > MaxQuad) {
3782 BestHiQuad = i;
3783 MaxQuad = HiQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003784 }
3785 }
3786
Nate Begeman2c87c422009-02-23 08:49:38 +00003787 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003788 // of the two input vectors, shuffle them into one input vector so only a
Nate Begeman2c87c422009-02-23 08:49:38 +00003789 // single pshufb instruction is necessary. If There are more than 2 input
3790 // quads, disable the next transformation since it does not help SSSE3.
3791 bool V1Used = InputQuads[0] || InputQuads[1];
3792 bool V2Used = InputQuads[2] || InputQuads[3];
3793 if (TLI.getSubtarget()->hasSSSE3()) {
3794 if (InputQuads.count() == 2 && V1Used && V2Used) {
3795 BestLoQuad = InputQuads.find_first();
3796 BestHiQuad = InputQuads.find_next(BestLoQuad);
3797 }
3798 if (InputQuads.count() > 2) {
3799 BestLoQuad = -1;
3800 BestHiQuad = -1;
3801 }
3802 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003803
Nate Begeman2c87c422009-02-23 08:49:38 +00003804 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3805 // the shuffle mask. If a quad is scored as -1, that means that it contains
3806 // words from all 4 input quadwords.
3807 SDValue NewV;
3808 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003809 SmallVector<int, 8> MaskV;
3810 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3811 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003812 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003813 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3814 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3815 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003816
Nate Begeman2c87c422009-02-23 08:49:38 +00003817 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3818 // source words for the shuffle, to aid later transformations.
3819 bool AllWordsInNewV = true;
Mon P Wangb1db1202009-03-11 06:35:11 +00003820 bool InOrder[2] = { true, true };
Evan Cheng75184a92007-12-11 01:46:18 +00003821 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003822 int idx = MaskVals[i];
Mon P Wangb1db1202009-03-11 06:35:11 +00003823 if (idx != (int)i)
3824 InOrder[i/4] = false;
Nate Begeman2c87c422009-02-23 08:49:38 +00003825 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng75184a92007-12-11 01:46:18 +00003826 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003827 AllWordsInNewV = false;
3828 break;
Evan Cheng75184a92007-12-11 01:46:18 +00003829 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003830
Nate Begeman2c87c422009-02-23 08:49:38 +00003831 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3832 if (AllWordsInNewV) {
3833 for (int i = 0; i != 8; ++i) {
3834 int idx = MaskVals[i];
3835 if (idx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00003836 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003837 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begeman2c87c422009-02-23 08:49:38 +00003838 if ((idx != i) && idx < 4)
3839 pshufhw = false;
3840 if ((idx != i) && idx > 3)
3841 pshuflw = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003842 }
Nate Begeman2c87c422009-02-23 08:49:38 +00003843 V1 = NewV;
3844 V2Used = false;
3845 BestLoQuad = 0;
3846 BestHiQuad = 1;
Evan Chengfca29242007-12-07 08:07:39 +00003847 }
Evan Cheng75184a92007-12-11 01:46:18 +00003848
Nate Begeman2c87c422009-02-23 08:49:38 +00003849 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3850 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wangb1db1202009-03-11 06:35:11 +00003851 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003852 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003853 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng75184a92007-12-11 01:46:18 +00003854 }
Evan Cheng75184a92007-12-11 01:46:18 +00003855 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003856
Nate Begeman2c87c422009-02-23 08:49:38 +00003857 // If we have SSSE3, and all words of the result are from 1 input vector,
3858 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3859 // is present, fall back to case 4.
3860 if (TLI.getSubtarget()->hasSSSE3()) {
3861 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003862
Nate Begeman2c87c422009-02-23 08:49:38 +00003863 // If we have elements from both input vectors, set the high bit of the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003864 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begeman2c87c422009-02-23 08:49:38 +00003865 // mask, and elements that come from V1 in the V2 mask, so that the two
3866 // results can be OR'd together.
3867 bool TwoInputs = V1Used && V2Used;
3868 for (unsigned i = 0; i != 8; ++i) {
3869 int EltIdx = MaskVals[i] * 2;
3870 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003871 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3872 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00003873 continue;
3874 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003875 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3876 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00003877 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003878 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003879 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00003880 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003881 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00003882 if (!TwoInputs)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003883 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003884
Nate Begeman2c87c422009-02-23 08:49:38 +00003885 // Calculate the shuffle mask for the second input, shuffle it, and
3886 // OR it with the first shuffled input.
3887 pshufbMask.clear();
3888 for (unsigned i = 0; i != 8; ++i) {
3889 int EltIdx = MaskVals[i] * 2;
3890 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003891 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3892 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00003893 continue;
3894 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003895 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3896 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00003897 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003898 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003899 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00003900 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003901 MVT::v16i8, &pshufbMask[0], 16));
3902 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3903 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003904 }
3905
3906 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3907 // and update MaskVals with new element order.
3908 BitVector InOrder(8);
3909 if (BestLoQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003910 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00003911 for (int i = 0; i != 4; ++i) {
3912 int idx = MaskVals[i];
3913 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003914 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003915 InOrder.set(i);
3916 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00003917 MaskV.push_back(idx & 3);
Nate Begeman2c87c422009-02-23 08:49:38 +00003918 InOrder.set(i);
3919 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003920 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003921 }
3922 }
3923 for (unsigned i = 4; i != 8; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003924 MaskV.push_back(i);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003925 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00003926 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00003927 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003928
Nate Begeman2c87c422009-02-23 08:49:38 +00003929 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3930 // and update MaskVals with the new element order.
3931 if (BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003932 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00003933 for (unsigned i = 0; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003934 MaskV.push_back(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00003935 for (unsigned i = 4; i != 8; ++i) {
3936 int idx = MaskVals[i];
3937 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003938 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003939 InOrder.set(i);
3940 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00003941 MaskV.push_back((idx & 3) + 4);
Nate Begeman2c87c422009-02-23 08:49:38 +00003942 InOrder.set(i);
3943 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00003944 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00003945 }
3946 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003947 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00003948 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00003949 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003950
Nate Begeman2c87c422009-02-23 08:49:38 +00003951 // In case BestHi & BestLo were both -1, which means each quadword has a word
3952 // from each of the four input quadwords, calculate the InOrder bitvector now
3953 // before falling through to the insert/extract cleanup.
3954 if (BestLoQuad == -1 && BestHiQuad == -1) {
3955 NewV = V1;
3956 for (int i = 0; i != 8; ++i)
3957 if (MaskVals[i] < 0 || MaskVals[i] == i)
3958 InOrder.set(i);
3959 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003960
Nate Begeman2c87c422009-02-23 08:49:38 +00003961 // The other elements are put in the right place using pextrw and pinsrw.
3962 for (unsigned i = 0; i != 8; ++i) {
3963 if (InOrder[i])
3964 continue;
3965 int EltIdx = MaskVals[i];
3966 if (EltIdx < 0)
3967 continue;
3968 SDValue ExtOp = (EltIdx < 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003969 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begeman2c87c422009-02-23 08:49:38 +00003970 DAG.getIntPtrConstant(EltIdx))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003971 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begeman2c87c422009-02-23 08:49:38 +00003972 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003973 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begeman2c87c422009-02-23 08:49:38 +00003974 DAG.getIntPtrConstant(i));
3975 }
3976 return NewV;
3977}
3978
3979// v16i8 shuffles - Prefer shuffles in the following order:
3980// 1. [ssse3] 1 x pshufb
3981// 2. [ssse3] 2 x pshufb + 1 x por
3982// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3983static
Nate Begeman543d2142009-04-27 18:41:29 +00003984SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3985 SelectionDAG &DAG, X86TargetLowering &TLI) {
3986 SDValue V1 = SVOp->getOperand(0);
3987 SDValue V2 = SVOp->getOperand(1);
3988 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00003989 SmallVector<int, 16> MaskVals;
Nate Begeman543d2142009-04-27 18:41:29 +00003990 SVOp->getMask(MaskVals);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003991
Nate Begeman2c87c422009-02-23 08:49:38 +00003992 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003993 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begeman2c87c422009-02-23 08:49:38 +00003994 // present, fall back to case 3.
3995 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3996 bool V1Only = true;
3997 bool V2Only = true;
3998 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003999 int EltIdx = MaskVals[i];
Nate Begeman2c87c422009-02-23 08:49:38 +00004000 if (EltIdx < 0)
4001 continue;
4002 if (EltIdx < 16)
4003 V2Only = false;
4004 else
4005 V1Only = false;
4006 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004007
Nate Begeman2c87c422009-02-23 08:49:38 +00004008 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4009 if (TLI.getSubtarget()->hasSSSE3()) {
4010 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004011
Nate Begeman2c87c422009-02-23 08:49:38 +00004012 // If all result elements are from one input vector, then only translate
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004013 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begeman2c87c422009-02-23 08:49:38 +00004014 //
4015 // Otherwise, we have elements from both input vectors, and must zero out
4016 // elements that come from V2 in the first mask, and V1 in the second mask
4017 // so that we can OR them together.
4018 bool TwoInputs = !(V1Only || V2Only);
4019 for (unsigned i = 0; i != 16; ++i) {
4020 int EltIdx = MaskVals[i];
4021 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004022 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004023 continue;
4024 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004025 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004026 }
4027 // If all the elements are from V2, assign it to V1 and return after
4028 // building the first pshufb.
4029 if (V2Only)
4030 V1 = V2;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004031 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00004032 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004033 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004034 if (!TwoInputs)
4035 return V1;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004036
Nate Begeman2c87c422009-02-23 08:49:38 +00004037 // Calculate the shuffle mask for the second input, shuffle it, and
4038 // OR it with the first shuffled input.
4039 pshufbMask.clear();
4040 for (unsigned i = 0; i != 16; ++i) {
4041 int EltIdx = MaskVals[i];
4042 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004043 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004044 continue;
4045 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004046 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004047 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004048 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00004049 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004050 MVT::v16i8, &pshufbMask[0], 16));
4051 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004052 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004053
Nate Begeman2c87c422009-02-23 08:49:38 +00004054 // No SSSE3 - Calculate in place words and then fix all out of place words
4055 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4056 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004057 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4058 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004059 SDValue NewV = V2Only ? V2 : V1;
4060 for (int i = 0; i != 8; ++i) {
4061 int Elt0 = MaskVals[i*2];
4062 int Elt1 = MaskVals[i*2+1];
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004063
Nate Begeman2c87c422009-02-23 08:49:38 +00004064 // This word of the result is all undef, skip it.
4065 if (Elt0 < 0 && Elt1 < 0)
4066 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004067
Nate Begeman2c87c422009-02-23 08:49:38 +00004068 // This word of the result is already in the correct place, skip it.
4069 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4070 continue;
4071 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4072 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004073
Nate Begeman2c87c422009-02-23 08:49:38 +00004074 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4075 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4076 SDValue InsElt;
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004077
4078 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4079 // using a single extract together, load it and store it.
4080 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004081 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004082 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004083 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004084 DAG.getIntPtrConstant(i));
4085 continue;
4086 }
4087
Nate Begeman2c87c422009-02-23 08:49:38 +00004088 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004089 // source byte is not also odd, shift the extracted word left 8 bits
4090 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begeman2c87c422009-02-23 08:49:38 +00004091 if (Elt1 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004092 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begeman2c87c422009-02-23 08:49:38 +00004093 DAG.getIntPtrConstant(Elt1 / 2));
4094 if ((Elt1 & 1) == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004095 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004096 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004097 else if (Elt0 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004098 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4099 DAG.getConstant(0xFF00, MVT::i16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004100 }
4101 // If Elt0 is defined, extract it from the appropriate source. If the
4102 // source byte is not also even, shift the extracted word right 8 bits. If
4103 // Elt1 was also defined, OR the extracted values together before
4104 // inserting them in the result.
4105 if (Elt0 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004106 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begeman2c87c422009-02-23 08:49:38 +00004107 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4108 if ((Elt0 & 1) != 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004109 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begeman2c87c422009-02-23 08:49:38 +00004110 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004111 else if (Elt1 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004112 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4113 DAG.getConstant(0x00FF, MVT::i16));
4114 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begeman2c87c422009-02-23 08:49:38 +00004115 : InsElt0;
4116 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004117 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004118 DAG.getIntPtrConstant(i));
4119 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004120 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00004121}
4122
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004123/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4124/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4125/// done when every pair / quad of shuffle mask elements point to elements in
4126/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00004127/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4128static
Nate Begeman543d2142009-04-27 18:41:29 +00004129SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4130 SelectionDAG &DAG,
4131 TargetLowering &TLI, DebugLoc dl) {
Owen Andersonac9de032009-08-10 22:56:29 +00004132 EVT VT = SVOp->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00004133 SDValue V1 = SVOp->getOperand(0);
4134 SDValue V2 = SVOp->getOperand(1);
4135 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004136 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004137 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersonac9de032009-08-10 22:56:29 +00004138 EVT MaskEltVT = MaskVT.getVectorElementType();
4139 EVT NewVT = MaskVT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004140 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00004141 default: assert(false && "Unexpected!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004142 case MVT::v4f32: NewVT = MVT::v2f64; break;
4143 case MVT::v4i32: NewVT = MVT::v2i64; break;
4144 case MVT::v8i16: NewVT = MVT::v4i32; break;
4145 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004146 }
4147
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004148 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00004149 if (VT.isInteger())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004150 NewVT = MVT::v2i64;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004151 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004152 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004153 }
Nate Begeman543d2142009-04-27 18:41:29 +00004154 int Scale = NumElems / NewWidth;
4155 SmallVector<int, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00004156 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman543d2142009-04-27 18:41:29 +00004157 int StartIdx = -1;
4158 for (int j = 0; j < Scale; ++j) {
4159 int EltIdx = SVOp->getMaskElt(i+j);
4160 if (EltIdx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00004161 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00004162 if (StartIdx == -1)
Evan Cheng75184a92007-12-11 01:46:18 +00004163 StartIdx = EltIdx - (EltIdx % Scale);
4164 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00004165 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004166 }
Nate Begeman543d2142009-04-27 18:41:29 +00004167 if (StartIdx == -1)
4168 MaskVec.push_back(-1);
Evan Cheng75184a92007-12-11 01:46:18 +00004169 else
Nate Begeman543d2142009-04-27 18:41:29 +00004170 MaskVec.push_back(StartIdx / Scale);
Evan Chengfca29242007-12-07 08:07:39 +00004171 }
4172
Dale Johannesence0805b2009-02-03 19:33:06 +00004173 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4174 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman543d2142009-04-27 18:41:29 +00004175 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Chengfca29242007-12-07 08:07:39 +00004176}
4177
Evan Chenge9b9c672008-05-09 21:53:03 +00004178/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00004179///
Owen Andersonac9de032009-08-10 22:56:29 +00004180static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman543d2142009-04-27 18:41:29 +00004181 SDValue SrcOp, SelectionDAG &DAG,
4182 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004183 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004184 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00004185 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00004186 LD = dyn_cast<LoadSDNode>(SrcOp);
4187 if (!LD) {
4188 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4189 // instead.
Owen Anderson2dd68a22009-08-11 21:59:30 +00004190 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4191 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00004192 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4193 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson2dd68a22009-08-11 21:59:30 +00004194 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004195 // PR2108
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004196 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004197 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4198 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4199 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4200 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00004201 SrcOp.getOperand(0)
4202 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004203 }
4204 }
4205 }
4206
Dale Johannesence0805b2009-02-03 19:33:06 +00004207 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4208 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michel91099d62009-02-17 22:15:04 +00004209 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00004210 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004211}
4212
Evan Chengf50554e2008-07-22 21:13:36 +00004213/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4214/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00004215static SDValue
Nate Begeman543d2142009-04-27 18:41:29 +00004216LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4217 SDValue V1 = SVOp->getOperand(0);
4218 SDValue V2 = SVOp->getOperand(1);
4219 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00004220 EVT VT = SVOp->getValueType(0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004221
Evan Chengf50554e2008-07-22 21:13:36 +00004222 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00004223 Locs.resize(4);
Nate Begeman543d2142009-04-27 18:41:29 +00004224 SmallVector<int, 8> Mask1(4U, -1);
4225 SmallVector<int, 8> PermMask;
4226 SVOp->getMask(PermMask);
4227
Evan Chengf50554e2008-07-22 21:13:36 +00004228 unsigned NumHi = 0;
4229 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00004230 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00004231 int Idx = PermMask[i];
4232 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004233 Locs[i] = std::make_pair(-1, -1);
4234 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004235 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4236 if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004237 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman543d2142009-04-27 18:41:29 +00004238 Mask1[NumLo] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004239 NumLo++;
4240 } else {
4241 Locs[i] = std::make_pair(1, NumHi);
4242 if (2+NumHi < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004243 Mask1[2+NumHi] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004244 NumHi++;
4245 }
4246 }
4247 }
Evan Cheng3cae0332008-07-23 00:22:17 +00004248
Evan Chengf50554e2008-07-22 21:13:36 +00004249 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00004250 // If no more than two elements come from either vector. This can be
4251 // implemented with two shuffles. First shuffle gather the elements.
4252 // The second shuffle, which takes the first shuffle as both of its
4253 // vector operands, put the elements into the right order.
Nate Begeman543d2142009-04-27 18:41:29 +00004254 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004255
Nate Begeman543d2142009-04-27 18:41:29 +00004256 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004257
Evan Chengf50554e2008-07-22 21:13:36 +00004258 for (unsigned i = 0; i != 4; ++i) {
4259 if (Locs[i].first == -1)
4260 continue;
4261 else {
4262 unsigned Idx = (i < 2) ? 0 : 4;
4263 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004264 Mask2[i] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004265 }
4266 }
4267
Nate Begeman543d2142009-04-27 18:41:29 +00004268 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004269 } else if (NumLo == 3 || NumHi == 3) {
4270 // Otherwise, we must have three elements from one vector, call it X, and
4271 // one element from the other, call it Y. First, use a shufps to build an
4272 // intermediate vector with the one element from Y and the element from X
4273 // that will be in the same half in the final destination (the indexes don't
4274 // matter). Then, use a shufps to build the final vector, taking the half
4275 // containing the element from Y from the intermediate, and the other half
4276 // from X.
4277 if (NumHi == 3) {
4278 // Normalize it so the 3 elements come from V1.
Nate Begeman543d2142009-04-27 18:41:29 +00004279 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng3cae0332008-07-23 00:22:17 +00004280 std::swap(V1, V2);
4281 }
4282
4283 // Find the element from V2.
4284 unsigned HiIndex;
4285 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman543d2142009-04-27 18:41:29 +00004286 int Val = PermMask[HiIndex];
4287 if (Val < 0)
Evan Cheng3cae0332008-07-23 00:22:17 +00004288 continue;
Evan Cheng3cae0332008-07-23 00:22:17 +00004289 if (Val >= 4)
4290 break;
4291 }
4292
Nate Begeman543d2142009-04-27 18:41:29 +00004293 Mask1[0] = PermMask[HiIndex];
4294 Mask1[1] = -1;
4295 Mask1[2] = PermMask[HiIndex^1];
4296 Mask1[3] = -1;
4297 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004298
4299 if (HiIndex >= 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00004300 Mask1[0] = PermMask[0];
4301 Mask1[1] = PermMask[1];
4302 Mask1[2] = HiIndex & 1 ? 6 : 4;
4303 Mask1[3] = HiIndex & 1 ? 4 : 6;
4304 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004305 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004306 Mask1[0] = HiIndex & 1 ? 2 : 0;
4307 Mask1[1] = HiIndex & 1 ? 0 : 2;
4308 Mask1[2] = PermMask[2];
4309 Mask1[3] = PermMask[3];
4310 if (Mask1[2] >= 0)
4311 Mask1[2] += 4;
4312 if (Mask1[3] >= 0)
4313 Mask1[3] += 4;
4314 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004315 }
Evan Chengf50554e2008-07-22 21:13:36 +00004316 }
4317
4318 // Break it into (shuffle shuffle_hi, shuffle_lo).
4319 Locs.clear();
Nate Begeman543d2142009-04-27 18:41:29 +00004320 SmallVector<int,8> LoMask(4U, -1);
4321 SmallVector<int,8> HiMask(4U, -1);
4322
4323 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004324 unsigned MaskIdx = 0;
4325 unsigned LoIdx = 0;
4326 unsigned HiIdx = 2;
4327 for (unsigned i = 0; i != 4; ++i) {
4328 if (i == 2) {
4329 MaskPtr = &HiMask;
4330 MaskIdx = 1;
4331 LoIdx = 0;
4332 HiIdx = 2;
4333 }
Nate Begeman543d2142009-04-27 18:41:29 +00004334 int Idx = PermMask[i];
4335 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004336 Locs[i] = std::make_pair(-1, -1);
Nate Begeman543d2142009-04-27 18:41:29 +00004337 } else if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004338 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004339 (*MaskPtr)[LoIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004340 LoIdx++;
4341 } else {
4342 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004343 (*MaskPtr)[HiIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004344 HiIdx++;
4345 }
4346 }
4347
Nate Begeman543d2142009-04-27 18:41:29 +00004348 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4349 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4350 SmallVector<int, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004351 for (unsigned i = 0; i != 4; ++i) {
4352 if (Locs[i].first == -1) {
Nate Begeman543d2142009-04-27 18:41:29 +00004353 MaskOps.push_back(-1);
Evan Chengf50554e2008-07-22 21:13:36 +00004354 } else {
4355 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004356 MaskOps.push_back(Idx);
Evan Chengf50554e2008-07-22 21:13:36 +00004357 }
4358 }
Nate Begeman543d2142009-04-27 18:41:29 +00004359 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengf50554e2008-07-22 21:13:36 +00004360}
4361
Dan Gohman8181bd12008-07-27 21:46:04 +00004362SDValue
4363X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman543d2142009-04-27 18:41:29 +00004364 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004365 SDValue V1 = Op.getOperand(0);
4366 SDValue V2 = Op.getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00004367 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004368 DebugLoc dl = Op.getDebugLoc();
Nate Begeman543d2142009-04-27 18:41:29 +00004369 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands92c43912008-06-06 12:08:01 +00004370 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4372 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4373 bool V1IsSplat = false;
4374 bool V2IsSplat = false;
4375
Nate Begeman543d2142009-04-27 18:41:29 +00004376 if (isZeroShuffle(SVOp))
Dale Johannesence0805b2009-02-03 19:33:06 +00004377 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004378
Nate Begeman543d2142009-04-27 18:41:29 +00004379 // Promote splats to v4f32.
4380 if (SVOp->isSplat()) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004381 if (isMMX || NumElems < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004382 return Op;
4383 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384 }
4385
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004386 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4387 // do it!
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004388 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004389 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004390 if (NewOp.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00004391 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesence0805b2009-02-03 19:33:06 +00004392 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004393 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004394 // FIXME: Figure out a cleaner way to do this.
4395 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004396 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004397 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004398 if (NewOp.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00004399 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4400 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4401 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004402 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004403 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004404 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4405 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chenge9b9c672008-05-09 21:53:03 +00004406 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman543d2142009-04-27 18:41:29 +00004407 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004408 }
4409 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004410
Nate Begeman543d2142009-04-27 18:41:29 +00004411 if (X86::isPSHUFDMask(SVOp))
4412 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004413
Evan Chengdea99362008-05-29 08:22:04 +00004414 // Check if this can be converted into a logical shift.
4415 bool isLeft = false;
4416 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004417 SDValue ShVal;
Nate Begeman543d2142009-04-27 18:41:29 +00004418 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chenge31a26a2009-12-09 21:00:30 +00004419 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengdea99362008-05-29 08:22:04 +00004420 if (isShift && ShVal.hasOneUse()) {
Scott Michel91099d62009-02-17 22:15:04 +00004421 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengdea99362008-05-29 08:22:04 +00004422 // v_set0 + movlhps or movhlps, etc.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004423 EVT EltVT = VT.getVectorElementType();
4424 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004425 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004426 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004427
Nate Begeman543d2142009-04-27 18:41:29 +00004428 if (X86::isMOVLMask(SVOp)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004429 if (V1IsUndef)
4430 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004431 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004432 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004433 if (!isMMX)
4434 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004435 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004436
Nate Begeman543d2142009-04-27 18:41:29 +00004437 // FIXME: fold these into legal mask.
4438 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4439 X86::isMOVSLDUPMask(SVOp) ||
4440 X86::isMOVHLPSMask(SVOp) ||
Nate Begemanb13034d2009-11-07 23:17:15 +00004441 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman543d2142009-04-27 18:41:29 +00004442 X86::isMOVLPMask(SVOp)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004443 return Op;
4444
Nate Begeman543d2142009-04-27 18:41:29 +00004445 if (ShouldXformToMOVHLPS(SVOp) ||
4446 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4447 return CommuteVectorShuffle(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448
Evan Chengdea99362008-05-29 08:22:04 +00004449 if (isShift) {
4450 // No better options. Use a vshl / vsrl.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004451 EVT EltVT = VT.getVectorElementType();
4452 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004453 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004454 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004455
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004457 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4458 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004459 V1IsSplat = isSplatVector(V1.getNode());
4460 V2IsSplat = isSplatVector(V2.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00004461
Chris Lattnere6aa3862007-11-25 00:24:49 +00004462 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004463 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman543d2142009-04-27 18:41:29 +00004464 Op = CommuteVectorShuffle(SVOp, DAG);
4465 SVOp = cast<ShuffleVectorSDNode>(Op);
4466 V1 = SVOp->getOperand(0);
4467 V2 = SVOp->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004468 std::swap(V1IsSplat, V2IsSplat);
4469 std::swap(V1IsUndef, V2IsUndef);
4470 Commuted = true;
4471 }
4472
Nate Begeman543d2142009-04-27 18:41:29 +00004473 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4474 // Shuffling low element of v1 into undef, just return v1.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004475 if (V2IsUndef)
Nate Begeman543d2142009-04-27 18:41:29 +00004476 return V1;
4477 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4478 // the instruction selector will not match, so get a canonical MOVL with
4479 // swapped operands to undo the commute.
4480 return getMOVL(DAG, dl, VT, V2, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004481 }
4482
Nate Begeman543d2142009-04-27 18:41:29 +00004483 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4484 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4485 X86::isUNPCKLMask(SVOp) ||
4486 X86::isUNPCKHMask(SVOp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004487 return Op;
4488
4489 if (V2IsSplat) {
4490 // Normalize mask so all entries that point to V2 points to its first
4491 // element then try to match unpck{h|l} again. If match, return a
4492 // new vector_shuffle with the corrected mask.
Nate Begeman543d2142009-04-27 18:41:29 +00004493 SDValue NewMask = NormalizeMask(SVOp, DAG);
4494 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4495 if (NSVOp != SVOp) {
4496 if (X86::isUNPCKLMask(NSVOp, true)) {
4497 return NewMask;
4498 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4499 return NewMask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004500 }
4501 }
4502 }
4503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004504 if (Commuted) {
4505 // Commute is back and try unpck* again.
Nate Begeman543d2142009-04-27 18:41:29 +00004506 // FIXME: this seems wrong.
4507 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4508 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4509 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4510 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4511 X86::isUNPCKLMask(NewSVOp) ||
4512 X86::isUNPCKHMask(NewSVOp))
4513 return NewOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514 }
4515
Nate Begeman2c87c422009-02-23 08:49:38 +00004516 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman543d2142009-04-27 18:41:29 +00004517
4518 // Normalize the node to match x86 shuffle ops if needed
4519 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4520 return CommuteVectorShuffle(SVOp, DAG);
4521
4522 // Check for legal shuffle and return?
4523 SmallVector<int, 16> PermMask;
4524 SVOp->getMask(PermMask);
4525 if (isShuffleMaskLegal(PermMask, VT))
Evan Chengbf8b2c52008-04-05 00:30:36 +00004526 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004527
Evan Cheng75184a92007-12-11 01:46:18 +00004528 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004529 if (VT == MVT::v8i16) {
Nate Begeman543d2142009-04-27 18:41:29 +00004530 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004531 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004532 return NewOp;
4533 }
4534
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004535 if (VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004536 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begeman2c87c422009-02-23 08:49:38 +00004537 if (NewOp.getNode())
4538 return NewOp;
4539 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004540
Evan Chengf50554e2008-07-22 21:13:36 +00004541 // Handle all 4 wide cases with a number of shuffles except for MMX.
4542 if (NumElems == 4 && !isMMX)
Nate Begeman543d2142009-04-27 18:41:29 +00004543 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004544
Dan Gohman8181bd12008-07-27 21:46:04 +00004545 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546}
4547
Dan Gohman8181bd12008-07-27 21:46:04 +00004548SDValue
4549X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004550 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004551 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004552 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004553 if (VT.getSizeInBits() == 8) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004554 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004555 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004556 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004557 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004558 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004559 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004560 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4561 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4562 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004563 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4564 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004565 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004566 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004567 Op.getOperand(0)),
4568 Op.getOperand(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004569 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004570 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004571 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004572 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004573 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004574 } else if (VT == MVT::f32) {
Evan Cheng6c249332008-03-24 21:52:23 +00004575 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4576 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004577 // result has a single use which is a store or a bitcast to i32. And in
4578 // the case of a store, it's not worth it if the index is a constant 0,
4579 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004580 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004581 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004582 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004583 if ((User->getOpcode() != ISD::STORE ||
4584 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4585 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004586 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004587 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004588 return SDValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004589 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4590 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004591 Op.getOperand(0)),
4592 Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004593 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4594 } else if (VT == MVT::i32) {
Mon P Wangac2a3c52009-01-15 21:10:20 +00004595 // ExtractPS works with constant index.
4596 if (isa<ConstantSDNode>(Op.getOperand(1)))
4597 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004598 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004599 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004600}
4601
4602
Dan Gohman8181bd12008-07-27 21:46:04 +00004603SDValue
4604X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004605 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004606 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004607
Evan Cheng6c249332008-03-24 21:52:23 +00004608 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004609 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004610 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004611 return Res;
4612 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004613
Owen Andersonac9de032009-08-10 22:56:29 +00004614 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004615 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004616 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004617 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004618 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004619 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004620 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004621 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4622 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michel91099d62009-02-17 22:15:04 +00004623 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004624 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004625 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004626 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck5d3fa642009-12-17 15:31:52 +00004627 EVT EltVT = MVT::i32;
Dan Gohman3bab1f72009-09-23 21:02:20 +00004628 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004629 Op.getOperand(0), Op.getOperand(1));
Dan Gohman3bab1f72009-09-23 21:02:20 +00004630 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004632 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004633 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004634 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004635 if (Idx == 0)
4636 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004638 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman543d2142009-04-27 18:41:29 +00004639 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004640 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004641 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004642 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004643 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004644 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004645 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004646 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4647 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4648 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004649 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004650 if (Idx == 0)
4651 return Op;
4652
4653 // UNPCKHPD the element to the lowest double word, then movsd.
4654 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4655 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman543d2142009-04-27 18:41:29 +00004656 int Mask[2] = { 1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004657 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004658 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004659 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004660 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004661 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 }
4663
Dan Gohman8181bd12008-07-27 21:46:04 +00004664 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004665}
4666
Dan Gohman8181bd12008-07-27 21:46:04 +00004667SDValue
4668X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersonac9de032009-08-10 22:56:29 +00004669 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004670 EVT EltVT = VT.getVectorElementType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004671 DebugLoc dl = Op.getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004672
Dan Gohman8181bd12008-07-27 21:46:04 +00004673 SDValue N0 = Op.getOperand(0);
4674 SDValue N1 = Op.getOperand(1);
4675 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004676
Dan Gohman3bab1f72009-09-23 21:02:20 +00004677 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohman5a7af042008-08-14 22:53:18 +00004678 isa<ConstantSDNode>(N2)) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00004679 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4680 : X86ISD::PINSRW;
Nate Begemand77e59e2008-02-11 04:19:36 +00004681 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4682 // argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004683 if (N1.getValueType() != MVT::i32)
4684 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4685 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004686 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004687 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004688 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004689 // Bits [7:6] of the constant are the source select. This will always be
4690 // zero here. The DAG Combiner may combine an extract_elt index into these
4691 // bits. For example (insert (extract, 3), 2) could be matched by putting
4692 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michel91099d62009-02-17 22:15:04 +00004693 // Bits [5:4] of the constant are the destination select. This is the
Nate Begemand77e59e2008-02-11 04:19:36 +00004694 // value of the incoming immediate.
Scott Michel91099d62009-02-17 22:15:04 +00004695 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begemand77e59e2008-02-11 04:19:36 +00004696 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004697 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherefb657e2009-07-24 00:33:09 +00004698 // Create this as a scalar to vector..
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004699 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004700 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004701 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherefb657e2009-07-24 00:33:09 +00004702 // PINSR* works with constant index.
4703 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004704 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004705 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004706}
4707
Dan Gohman8181bd12008-07-27 21:46:04 +00004708SDValue
4709X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004710 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004711 EVT EltVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004712
4713 if (Subtarget->hasSSE41())
4714 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4715
Dan Gohman3bab1f72009-09-23 21:02:20 +00004716 if (EltVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004717 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004718
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004719 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004720 SDValue N0 = Op.getOperand(0);
4721 SDValue N1 = Op.getOperand(1);
4722 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004723
Dan Gohman3bab1f72009-09-23 21:02:20 +00004724 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004725 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4726 // as its second argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004727 if (N1.getValueType() != MVT::i32)
4728 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4729 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004730 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004731 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734}
4735
Dan Gohman8181bd12008-07-27 21:46:04 +00004736SDValue
4737X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004738 DebugLoc dl = Op.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004739 if (Op.getValueType() == MVT::v2f32)
4740 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4741 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4742 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004743 Op.getOperand(0))));
4744
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004745 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4746 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindolafe2a3972009-08-03 02:45:34 +00004747
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004748 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4749 EVT VT = MVT::v2i32;
4750 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengd1045a62008-02-18 23:04:32 +00004751 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004752 case MVT::v16i8:
4753 case MVT::v8i16:
4754 VT = MVT::v4i32;
Evan Chengd1045a62008-02-18 23:04:32 +00004755 break;
4756 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004757 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4758 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759}
4760
Bill Wendlingfef06052008-09-16 21:48:12 +00004761// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4762// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4763// one of the above mentioned nodes. It has to be wrapped because otherwise
4764// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4765// be used to form addressing mode. These wrapped nodes will be selected
4766// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004767SDValue
4768X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004769 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004770
Chris Lattner5062b3b2009-06-26 19:22:52 +00004771 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4772 // global base reg.
4773 unsigned char OpFlag = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004774 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004775 CodeModel::Model M = getTargetMachine().getCodeModel();
4776
Chris Lattner28d40c62009-07-11 20:29:19 +00004777 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004778 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004779 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004780 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004781 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004782 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004783 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004784
Evan Cheng68c18682009-03-13 07:51:59 +00004785 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner5062b3b2009-06-26 19:22:52 +00004786 CP->getAlignment(),
4787 CP->getOffset(), OpFlag);
4788 DebugLoc DL = CP->getDebugLoc();
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004789 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004790 // With PIC, the address is actually $g + Offset.
Chris Lattner5062b3b2009-06-26 19:22:52 +00004791 if (OpFlag) {
4792 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004793 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner5062b3b2009-06-26 19:22:52 +00004794 DebugLoc::getUnknownLoc(), getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004795 Result);
4796 }
4797
4798 return Result;
4799}
4800
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004801SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4802 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004803
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004804 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4805 // global base reg.
4806 unsigned char OpFlag = 0;
4807 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004808 CodeModel::Model M = getTargetMachine().getCodeModel();
4809
Chris Lattner28d40c62009-07-11 20:29:19 +00004810 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004811 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004812 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004813 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004814 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004815 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004816 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004817
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004818 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4819 OpFlag);
4820 DebugLoc DL = JT->getDebugLoc();
4821 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004822
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004823 // With PIC, the address is actually $g + Offset.
4824 if (OpFlag) {
4825 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4826 DAG.getNode(X86ISD::GlobalBaseReg,
4827 DebugLoc::getUnknownLoc(), getPointerTy()),
4828 Result);
4829 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004830
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004831 return Result;
4832}
4833
4834SDValue
4835X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4836 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004837
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004838 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4839 // global base reg.
4840 unsigned char OpFlag = 0;
4841 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004842 CodeModel::Model M = getTargetMachine().getCodeModel();
4843
Chris Lattner28d40c62009-07-11 20:29:19 +00004844 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004845 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004846 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004847 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004848 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004849 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004850 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004851
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004852 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004853
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004854 DebugLoc DL = Op.getDebugLoc();
4855 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004856
4857
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004858 // With PIC, the address is actually $g + Offset.
4859 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004860 !Subtarget->is64Bit()) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004861 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4862 DAG.getNode(X86ISD::GlobalBaseReg,
4863 DebugLoc::getUnknownLoc(),
4864 getPointerTy()),
4865 Result);
4866 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004867
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004868 return Result;
4869}
4870
Dan Gohman8181bd12008-07-27 21:46:04 +00004871SDValue
Dan Gohman064403e2009-10-30 01:28:02 +00004872X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman885793b2009-11-20 23:18:13 +00004873 // Create the TargetBlockAddressAddress node.
4874 unsigned char OpFlags =
4875 Subtarget->ClassifyBlockAddressReference();
Dan Gohman064403e2009-10-30 01:28:02 +00004876 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman885793b2009-11-20 23:18:13 +00004877 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4878 DebugLoc dl = Op.getDebugLoc();
4879 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4880 /*isTarget=*/true, OpFlags);
4881
Dan Gohman064403e2009-10-30 01:28:02 +00004882 if (Subtarget->isPICStyleRIPRel() &&
4883 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman885793b2009-11-20 23:18:13 +00004884 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4885 else
4886 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman064403e2009-10-30 01:28:02 +00004887
Dan Gohman885793b2009-11-20 23:18:13 +00004888 // With PIC, the address is actually $g + Offset.
4889 if (isGlobalRelativeToPICBase(OpFlags)) {
4890 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4891 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4892 Result);
4893 }
Dan Gohman064403e2009-10-30 01:28:02 +00004894
4895 return Result;
4896}
4897
4898SDValue
Dale Johannesenea996922009-02-04 20:06:27 +00004899X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman36322c72008-10-18 02:06:02 +00004900 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00004901 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00004902 // Create the TargetGlobalAddress node, folding in the constant
4903 // offset if it is legal.
Chris Lattner505aa6c2009-07-10 07:20:05 +00004904 unsigned char OpFlags =
4905 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004906 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman36322c72008-10-18 02:06:02 +00004907 SDValue Result;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004908 if (OpFlags == X86II::MO_NO_FLAG &&
4909 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner9ab4e662009-07-09 00:58:53 +00004910 // A direct static reference to a global.
Dale Johannesenf97110c2009-07-21 00:12:29 +00004911 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman36322c72008-10-18 02:06:02 +00004912 Offset = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004913 } else {
Chris Lattner5bdaa522009-06-27 05:39:56 +00004914 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004915 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004916
Chris Lattner28d40c62009-07-11 20:29:19 +00004917 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004918 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004919 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4920 else
4921 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00004922
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004923 // With PIC, the address is actually $g + Offset.
Chris Lattner054532c2009-07-10 07:34:39 +00004924 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesenea996922009-02-04 20:06:27 +00004925 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4926 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 Result);
4928 }
Scott Michel91099d62009-02-17 22:15:04 +00004929
Chris Lattner054532c2009-07-10 07:34:39 +00004930 // For globals that require a load from a stub to get the address, emit the
4931 // load.
4932 if (isGlobalStubReference(OpFlags))
Dale Johannesenea996922009-02-04 20:06:27 +00004933 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00004934 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004935
Dan Gohman36322c72008-10-18 02:06:02 +00004936 // If there was a non-zero offset that we didn't fold, create an explicit
4937 // addition for it.
4938 if (Offset != 0)
Dale Johannesenea996922009-02-04 20:06:27 +00004939 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman36322c72008-10-18 02:06:02 +00004940 DAG.getConstant(Offset, getPointerTy()));
4941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004942 return Result;
4943}
4944
Evan Cheng7f250d62008-09-24 00:05:32 +00004945SDValue
4946X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4947 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00004948 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004949 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00004950}
4951
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004952static SDValue
4953GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersonac9de032009-08-10 22:56:29 +00004954 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerec7cfd42009-06-26 21:20:29 +00004955 unsigned char OperandFlags) {
Anton Korobeynikov7767af52009-12-11 19:39:55 +00004956 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004957 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004958 DebugLoc dl = GA->getDebugLoc();
4959 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4960 GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00004961 GA->getOffset(),
4962 OperandFlags);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004963 if (InFlag) {
4964 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004965 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004966 } else {
4967 SDValue Ops[] = { Chain, TGA };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004968 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004969 }
Anton Korobeynikov7767af52009-12-11 19:39:55 +00004970
4971 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
4972 MFI->setHasCalls(true);
4973
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00004974 SDValue Flag = Chain.getValue(1);
4975 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00004976}
4977
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004978// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004979static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004980LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00004981 const EVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004982 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00004983 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4984 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004985 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004986 DebugLoc::getUnknownLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004987 PtrVT), InFlag);
4988 InFlag = Chain.getValue(1);
4989
Chris Lattnerec7cfd42009-06-26 21:20:29 +00004990 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991}
4992
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004993// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00004994static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004995LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00004996 const EVT PtrVT) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00004997 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4998 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00004999}
5000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5002// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00005003static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005004 const EVT PtrVT, TLSModel::Model model,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005005 bool is64Bit) {
Dale Johannesenea996922009-02-04 20:06:27 +00005006 DebugLoc dl = GA->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005007 // Get the Thread Pointer
Rafael Espindolabca99f72009-04-08 21:14:34 +00005008 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5009 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005010 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005011 MVT::i32));
Rafael Espindolabca99f72009-04-08 21:14:34 +00005012
5013 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5014 NULL, 0);
5015
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005016 unsigned char OperandFlags = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005017 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5018 // initialexec.
5019 unsigned WrapperKind = X86ISD::Wrapper;
5020 if (model == TLSModel::LocalExec) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005021 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005022 } else if (is64Bit) {
5023 assert(model == TLSModel::InitialExec);
5024 OperandFlags = X86II::MO_GOTTPOFF;
5025 WrapperKind = X86ISD::WrapperRIP;
5026 } else {
5027 assert(model == TLSModel::InitialExec);
5028 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005029 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005030
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005031 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5032 // exec)
Chris Lattner3207f8b2009-06-21 02:22:34 +00005033 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005034 GA->getOffset(), OperandFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005035 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005036
Rafael Espindola7b620af2009-02-27 13:37:18 +00005037 if (model == TLSModel::InitialExec)
Dale Johannesenea996922009-02-04 20:06:27 +00005038 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005039 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040
5041 // The address of the thread local variable is the add of the thread
5042 // pointer with the offset of the variable.
Dale Johannesenea996922009-02-04 20:06:27 +00005043 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005044}
5045
Dan Gohman8181bd12008-07-27 21:46:04 +00005046SDValue
5047X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005048 // TODO: implement the "local dynamic" model
5049 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005050 assert(Subtarget->isTargetELF() &&
5051 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005052 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005053 const GlobalValue *GV = GA->getGlobal();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005054
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005055 // If GV is an alias then use the aliasee for determining
5056 // thread-localness.
5057 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5058 GV = GA->resolveAliasedGlobal(false);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005059
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005060 TLSModel::Model model = getTLSModel(GV,
5061 getTargetMachine().getRelocationModel());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005062
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005063 switch (model) {
5064 case TLSModel::GeneralDynamic:
5065 case TLSModel::LocalDynamic: // not implemented
5066 if (Subtarget->is64Bit())
Rafael Espindola7b620af2009-02-27 13:37:18 +00005067 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005068 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005069
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005070 case TLSModel::InitialExec:
5071 case TLSModel::LocalExec:
5072 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5073 Subtarget->is64Bit());
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005074 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005075
Edwin Törökbd448e32009-07-14 16:55:14 +00005076 llvm_unreachable("Unreachable");
Chris Lattnerda028df2009-04-01 22:14:45 +00005077 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005078}
5079
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005080
Chris Lattner62814a32007-10-17 06:02:13 +00005081/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michel91099d62009-02-17 22:15:04 +00005082/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00005083SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00005084 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersonac9de032009-08-10 22:56:29 +00005085 EVT VT = Op.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00005086 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005087 DebugLoc dl = Op.getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00005088 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00005089 SDValue ShOpLo = Op.getOperand(0);
5090 SDValue ShOpHi = Op.getOperand(1);
5091 SDValue ShAmt = Op.getOperand(2);
Chris Lattner996d9e52009-07-29 05:48:09 +00005092 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005093 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner996d9e52009-07-29 05:48:09 +00005094 : DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005095
Dan Gohman8181bd12008-07-27 21:46:04 +00005096 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00005097 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005098 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5099 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005100 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005101 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5102 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005103 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005105 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5106 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00005107 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005108 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005109
Dan Gohman8181bd12008-07-27 21:46:04 +00005110 SDValue Hi, Lo;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005111 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman8181bd12008-07-27 21:46:04 +00005112 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5113 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00005114
Chris Lattner62814a32007-10-17 06:02:13 +00005115 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005116 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5117 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005118 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005119 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5120 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005121 }
5122
Dan Gohman8181bd12008-07-27 21:46:04 +00005123 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00005124 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005125}
5126
Dan Gohman8181bd12008-07-27 21:46:04 +00005127SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00005128 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005129
5130 if (SrcVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005131 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005132 return Op;
5133 }
5134 return SDValue();
5135 }
5136
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005137 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00005138 "Unknown SINT_TO_FP to lower!");
Scott Michel91099d62009-02-17 22:15:04 +00005139
Eli Friedman9d77ac32009-05-27 00:47:34 +00005140 // These are really Legal; return the operand so the caller accepts it as
5141 // Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005142 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman9d77ac32009-05-27 00:47:34 +00005143 return Op;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005144 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005145 Subtarget->is64Bit()) {
5146 return Op;
5147 }
Scott Michel91099d62009-02-17 22:15:04 +00005148
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005149 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005150 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005151 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005152 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005153 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00005154 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling6b42d012009-03-13 08:41:47 +00005155 StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005156 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005157 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5158}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005159
Owen Andersonac9de032009-08-10 22:56:29 +00005160SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman8c3cb582009-05-23 09:59:16 +00005161 SDValue StackSlot,
5162 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163 // Build the FILD
Eli Friedman8c3cb582009-05-23 09:59:16 +00005164 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005165 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00005166 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005167 if (useSSE)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005168 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005169 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005170 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005171 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesence0805b2009-02-03 19:33:06 +00005172 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005173 Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005174
Dale Johannesen2fc20782007-09-14 22:26:36 +00005175 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005176 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00005177 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005178
5179 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5180 // shouldn't be necessary except that RFP cannot be live across
5181 // multiple blocks. When stackifier is fixed, they can be uncoupled.
5182 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005183 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005184 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005185 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005186 SDValue Ops[] = {
5187 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5188 };
5189 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesence0805b2009-02-03 19:33:06 +00005190 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005191 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005192 }
5193
5194 return Result;
5195}
5196
Bill Wendling14a30ef2009-01-17 03:56:04 +00005197// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5198SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5199 // This algorithm is not obvious. Here it is in C code, more or less:
5200 /*
5201 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5202 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5203 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00005204
Bill Wendling14a30ef2009-01-17 03:56:04 +00005205 // Copy ints to xmm registers.
5206 __m128i xh = _mm_cvtsi32_si128( hi );
5207 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005208
Bill Wendling14a30ef2009-01-17 03:56:04 +00005209 // Combine into low half of a single xmm register.
5210 __m128i x = _mm_unpacklo_epi32( xh, xl );
5211 __m128d d;
5212 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00005213
Bill Wendling14a30ef2009-01-17 03:56:04 +00005214 // Merge in appropriate exponents to give the integer bits the right
5215 // magnitude.
5216 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005217
Bill Wendling14a30ef2009-01-17 03:56:04 +00005218 // Subtract away the biases to deal with the IEEE-754 double precision
5219 // implicit 1.
5220 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005221
Bill Wendling14a30ef2009-01-17 03:56:04 +00005222 // All conversions up to here are exact. The correctly rounded result is
5223 // calculated using the current rounding mode using the following
5224 // horizontal add.
5225 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5226 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5227 // store doesn't really need to be here (except
5228 // maybe to zero the other double)
5229 return sd;
5230 }
5231 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00005232
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005233 DebugLoc dl = Op.getDebugLoc();
Owen Anderson6361f972009-07-15 21:51:10 +00005234 LLVMContext *Context = DAG.getContext();
Dale Johannesence0805b2009-02-03 19:33:06 +00005235
Dale Johannesena359b8b2008-10-21 20:50:01 +00005236 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00005237 std::vector<Constant*> CV0;
Owen Andersoneacb44d2009-07-24 23:12:02 +00005238 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5239 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5240 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5241 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Anderson2f422e02009-07-28 21:19:26 +00005242 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng68c18682009-03-13 07:51:59 +00005243 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005244
Bill Wendling14a30ef2009-01-17 03:56:04 +00005245 std::vector<Constant*> CV1;
Owen Anderson6361f972009-07-15 21:51:10 +00005246 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005247 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Anderson6361f972009-07-15 21:51:10 +00005248 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005249 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Anderson2f422e02009-07-28 21:19:26 +00005250 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng68c18682009-03-13 07:51:59 +00005251 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005252
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005253 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5254 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005255 Op.getOperand(0),
5256 DAG.getIntPtrConstant(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005257 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5258 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005259 Op.getOperand(0),
5260 DAG.getIntPtrConstant(0)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005261 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5262 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005263 PseudoSourceValue::getConstantPool(), 0,
5264 false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005265 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5266 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5267 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005268 PseudoSourceValue::getConstantPool(), 0,
5269 false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005270 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005271
Dale Johannesena359b8b2008-10-21 20:50:01 +00005272 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman543d2142009-04-27 18:41:29 +00005273 int ShufMask[2] = { 1, -1 };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005274 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5275 DAG.getUNDEF(MVT::v2f64), ShufMask);
5276 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5277 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00005278 DAG.getIntPtrConstant(0));
5279}
5280
Bill Wendling14a30ef2009-01-17 03:56:04 +00005281// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5282SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005283 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005284 // FP constant to bias correct the final result.
5285 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005286 MVT::f64);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005287
5288 // Load the 32-bit value into an XMM register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005289 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5290 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005291 Op.getOperand(0),
5292 DAG.getIntPtrConstant(0)));
5293
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005294 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5295 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005296 DAG.getIntPtrConstant(0));
5297
5298 // Or the load with the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005299 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5300 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005301 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005302 MVT::v2f64, Load)),
5303 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005304 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005305 MVT::v2f64, Bias)));
5306 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5307 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005308 DAG.getIntPtrConstant(0));
5309
5310 // Subtract the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005311 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005312
5313 // Handle final rounding.
Owen Andersonac9de032009-08-10 22:56:29 +00005314 EVT DestVT = Op.getValueType();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005315
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005316 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005317 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00005318 DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005319 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005320 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00005321 }
5322
5323 // Handle final rounding.
5324 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00005325}
5326
5327SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00005328 SDValue N0 = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005329 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005330
Evan Cheng44fd2392009-01-19 08:08:22 +00005331 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5332 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5333 // the optimization here.
5334 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00005335 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00005336
Owen Andersonac9de032009-08-10 22:56:29 +00005337 EVT SrcVT = N0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005338 if (SrcVT == MVT::i64) {
Eli Friedman9d77ac32009-05-27 00:47:34 +00005339 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005340 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar00261df2009-05-26 21:27:02 +00005341 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005342
Bill Wendling14a30ef2009-01-17 03:56:04 +00005343 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005344 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00005345 return LowerUINT_TO_FP_i32(Op, DAG);
5346 }
5347
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005348 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman8c3cb582009-05-23 09:59:16 +00005349
5350 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005351 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005352 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5353 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5354 getPointerTy(), StackSlot, WordOff);
5355 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5356 StackSlot, NULL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005357 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman8c3cb582009-05-23 09:59:16 +00005358 OffsetSlot, NULL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005359 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005360}
5361
Dan Gohman8181bd12008-07-27 21:46:04 +00005362std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman8c3cb582009-05-23 09:59:16 +00005363FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005364 DebugLoc dl = Op.getDebugLoc();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005365
Owen Andersonac9de032009-08-10 22:56:29 +00005366 EVT DstTy = Op.getValueType();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005367
5368 if (!IsSigned) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005369 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5370 DstTy = MVT::i64;
Eli Friedman8c3cb582009-05-23 09:59:16 +00005371 }
5372
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005373 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5374 DstTy.getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005375 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005376
Dale Johannesen2fc20782007-09-14 22:26:36 +00005377 // These are really Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005378 if (DstTy == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005379 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005380 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005381 if (Subtarget->is64Bit() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005382 DstTy == MVT::i64 &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005383 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005384 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005385
Evan Cheng05441e62007-10-15 20:11:21 +00005386 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5387 // stack slot.
5388 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005389 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene6424ab92009-11-12 20:49:22 +00005390 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005391 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005392
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005393 unsigned Opc;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005394 switch (DstTy.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005395 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005396 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5397 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5398 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005399 }
5400
Dan Gohman8181bd12008-07-27 21:46:04 +00005401 SDValue Chain = DAG.getEntryNode();
5402 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005403 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005404 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005405 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005406 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005407 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005408 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005409 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5410 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005411 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005412 Chain = Value.getValue(1);
David Greene6424ab92009-11-12 20:49:22 +00005413 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005414 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5415 }
5416
5417 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005418 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005419 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005420
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005421 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005422}
5423
Dan Gohman8181bd12008-07-27 21:46:04 +00005424SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005425 if (Op.getValueType().isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005426 if (Op.getValueType() == MVT::v2i32 &&
5427 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005428 return Op;
5429 }
5430 return SDValue();
5431 }
5432
Eli Friedman8c3cb582009-05-23 09:59:16 +00005433 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman8181bd12008-07-27 21:46:04 +00005434 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman9d77ac32009-05-27 00:47:34 +00005435 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5436 if (FIST.getNode() == 0) return Op;
Scott Michel91099d62009-02-17 22:15:04 +00005437
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005438 // Load the result.
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005439 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesence0805b2009-02-03 19:33:06 +00005440 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005441}
5442
Eli Friedman8c3cb582009-05-23 09:59:16 +00005443SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5444 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5445 SDValue FIST = Vals.first, StackSlot = Vals.second;
5446 assert(FIST.getNode() && "Unexpected failure");
5447
5448 // Load the result.
5449 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5450 FIST, StackSlot, NULL, 0);
5451}
5452
Dan Gohman8181bd12008-07-27 21:46:04 +00005453SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005454 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005455 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005456 EVT VT = Op.getValueType();
5457 EVT EltVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00005458 if (VT.isVector())
5459 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005460 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005461 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005462 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005463 CV.push_back(C);
5464 CV.push_back(C);
5465 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005466 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005467 CV.push_back(C);
5468 CV.push_back(C);
5469 CV.push_back(C);
5470 CV.push_back(C);
5471 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005472 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005473 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005474 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005475 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005476 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005477 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005478}
5479
Dan Gohman8181bd12008-07-27 21:46:04 +00005480SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005481 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005482 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005483 EVT VT = Op.getValueType();
5484 EVT EltVT = VT;
Duncan Sands831102e2009-09-06 19:29:07 +00005485 if (VT.isVector())
Duncan Sands92c43912008-06-06 12:08:01 +00005486 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005487 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005488 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005489 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005490 CV.push_back(C);
5491 CV.push_back(C);
5492 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005493 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 CV.push_back(C);
5495 CV.push_back(C);
5496 CV.push_back(C);
5497 CV.push_back(C);
5498 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005499 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005500 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005501 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005502 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005503 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005504 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005505 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005506 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5507 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005508 Op.getOperand(0)),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005509 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005510 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005511 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005513}
5514
Dan Gohman8181bd12008-07-27 21:46:04 +00005515SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005516 LLVMContext *Context = DAG.getContext();
Dan Gohman8181bd12008-07-27 21:46:04 +00005517 SDValue Op0 = Op.getOperand(0);
5518 SDValue Op1 = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005519 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005520 EVT VT = Op.getValueType();
5521 EVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005522
5523 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005524 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005525 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526 SrcVT = VT;
5527 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005528 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005529 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005530 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005531 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005532 }
5533
5534 // At this point the operands and the result should have the same
5535 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536
5537 // First get the sign bit of second operand.
5538 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005539 if (SrcVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005540 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5541 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005542 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005543 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5544 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5545 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5546 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005548 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005549 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005550 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005551 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005552 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005553 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005554
5555 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005556 if (SrcVT.bitsGT(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005557 // Op0 is MVT::f32, Op1 is MVT::f64.
5558 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5559 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5560 DAG.getConstant(32, MVT::i32));
5561 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5562 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005563 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005564 }
5565
5566 // Clear first operand sign bit.
5567 CV.clear();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005568 if (VT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005569 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5570 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005571 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005572 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5573 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5574 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5575 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005576 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005577 C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005578 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005579 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005580 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005581 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005582 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005583
5584 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005585 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586}
5587
Dan Gohman99a12192009-03-04 19:44:21 +00005588/// Emit nodes that will be selected as "test Op0,Op0", or something
5589/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005590SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5591 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005592 DebugLoc dl = Op.getDebugLoc();
5593
Dan Gohmanc8b47852009-03-07 01:58:32 +00005594 // CF and OF aren't always set the way we want. Determine which
5595 // of these we need.
5596 bool NeedCF = false;
5597 bool NeedOF = false;
5598 switch (X86CC) {
5599 case X86::COND_A: case X86::COND_AE:
5600 case X86::COND_B: case X86::COND_BE:
5601 NeedCF = true;
5602 break;
5603 case X86::COND_G: case X86::COND_GE:
5604 case X86::COND_L: case X86::COND_LE:
5605 case X86::COND_O: case X86::COND_NO:
5606 NeedOF = true;
5607 break;
5608 default: break;
5609 }
5610
Dan Gohman99a12192009-03-04 19:44:21 +00005611 // See if we can use the EFLAGS value from the operand instead of
Dan Gohmanc8b47852009-03-07 01:58:32 +00005612 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5613 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5614 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman99a12192009-03-04 19:44:21 +00005615 unsigned Opcode = 0;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005616 unsigned NumOperands = 0;
Dan Gohman99a12192009-03-04 19:44:21 +00005617 switch (Op.getNode()->getOpcode()) {
5618 case ISD::ADD:
5619 // Due to an isel shortcoming, be conservative if this add is likely to
5620 // be selected as part of a load-modify-store instruction. When the root
5621 // node in a match is a store, isel doesn't know how to remap non-chain
5622 // non-flag uses of other nodes in the match, such as the ADD in this
5623 // case. This leads to the ADD being left around and reselected, with
5624 // the result being two adds in the output.
5625 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5626 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5627 if (UI->getOpcode() == ISD::STORE)
5628 goto default_case;
Dan Gohman99a12192009-03-04 19:44:21 +00005629 if (ConstantSDNode *C =
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005630 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5631 // An add of one will be selected as an INC.
Dan Gohman99a12192009-03-04 19:44:21 +00005632 if (C->getAPIntValue() == 1) {
5633 Opcode = X86ISD::INC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005634 NumOperands = 1;
Dan Gohman99a12192009-03-04 19:44:21 +00005635 break;
5636 }
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005637 // An add of negative one (subtract of one) will be selected as a DEC.
5638 if (C->getAPIntValue().isAllOnesValue()) {
5639 Opcode = X86ISD::DEC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005640 NumOperands = 1;
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005641 break;
5642 }
5643 }
Dan Gohman99a12192009-03-04 19:44:21 +00005644 // Otherwise use a regular EFLAGS-setting add.
5645 Opcode = X86ISD::ADD;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005646 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005647 break;
Dan Gohman12e03292009-09-18 19:59:53 +00005648 case ISD::AND: {
5649 // If the primary and result isn't used, don't bother using X86ISD::AND,
5650 // because a TEST instruction will be better.
5651 bool NonFlagUse = false;
5652 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Chengc429ff52010-01-07 00:54:06 +00005653 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5654 SDNode *User = *UI;
5655 unsigned UOpNo = UI.getOperandNo();
5656 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5657 // Look pass truncate.
5658 UOpNo = User->use_begin().getOperandNo();
5659 User = *User->use_begin();
5660 }
5661 if (User->getOpcode() != ISD::BRCOND &&
5662 User->getOpcode() != ISD::SETCC &&
5663 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohman12e03292009-09-18 19:59:53 +00005664 NonFlagUse = true;
5665 break;
5666 }
Evan Chengc429ff52010-01-07 00:54:06 +00005667 }
Dan Gohman12e03292009-09-18 19:59:53 +00005668 if (!NonFlagUse)
5669 break;
5670 }
5671 // FALL THROUGH
Dan Gohman99a12192009-03-04 19:44:21 +00005672 case ISD::SUB:
Dan Gohman12e03292009-09-18 19:59:53 +00005673 case ISD::OR:
5674 case ISD::XOR:
5675 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman99a12192009-03-04 19:44:21 +00005676 // likely to be selected as part of a load-modify-store instruction.
5677 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5678 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5679 if (UI->getOpcode() == ISD::STORE)
5680 goto default_case;
Dan Gohman12e03292009-09-18 19:59:53 +00005681 // Otherwise use a regular EFLAGS-setting instruction.
5682 switch (Op.getNode()->getOpcode()) {
5683 case ISD::SUB: Opcode = X86ISD::SUB; break;
5684 case ISD::OR: Opcode = X86ISD::OR; break;
5685 case ISD::XOR: Opcode = X86ISD::XOR; break;
5686 case ISD::AND: Opcode = X86ISD::AND; break;
5687 default: llvm_unreachable("unexpected operator!");
5688 }
Dan Gohman8c8a8022009-03-05 21:29:28 +00005689 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005690 break;
5691 case X86ISD::ADD:
5692 case X86ISD::SUB:
5693 case X86ISD::INC:
5694 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00005695 case X86ISD::OR:
5696 case X86ISD::XOR:
5697 case X86ISD::AND:
Dan Gohman99a12192009-03-04 19:44:21 +00005698 return SDValue(Op.getNode(), 1);
5699 default:
5700 default_case:
5701 break;
5702 }
5703 if (Opcode != 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005704 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman99a12192009-03-04 19:44:21 +00005705 SmallVector<SDValue, 4> Ops;
Dan Gohmanc8b47852009-03-07 01:58:32 +00005706 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman99a12192009-03-04 19:44:21 +00005707 Ops.push_back(Op.getOperand(i));
Dan Gohmanee036282009-04-09 23:54:40 +00005708 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman99a12192009-03-04 19:44:21 +00005709 DAG.ReplaceAllUsesWith(Op, New);
5710 return SDValue(New.getNode(), 1);
5711 }
5712 }
5713
5714 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005715 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman99a12192009-03-04 19:44:21 +00005716 DAG.getConstant(0, Op.getValueType()));
5717}
5718
5719/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5720/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005721SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5722 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005723 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5724 if (C->getAPIntValue() == 0)
Dan Gohmanc8b47852009-03-07 01:58:32 +00005725 return EmitTest(Op0, X86CC, DAG);
Dan Gohman99a12192009-03-04 19:44:21 +00005726
5727 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005728 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman99a12192009-03-04 19:44:21 +00005729}
5730
Evan Cheng095dac22010-01-06 19:38:29 +00005731/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5732/// if it's possible.
5733static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Chengc621d452010-01-05 06:52:31 +00005734 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng095dac22010-01-06 19:38:29 +00005735 SDValue LHS, RHS;
5736 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5737 if (ConstantSDNode *Op010C =
5738 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5739 if (Op010C->getZExtValue() == 1) {
5740 LHS = Op0.getOperand(0);
5741 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00005742 }
Evan Cheng095dac22010-01-06 19:38:29 +00005743 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5744 if (ConstantSDNode *Op000C =
5745 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5746 if (Op000C->getZExtValue() == 1) {
5747 LHS = Op0.getOperand(1);
5748 RHS = Op0.getOperand(0).getOperand(1);
5749 }
5750 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5751 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5752 SDValue AndLHS = Op0.getOperand(0);
5753 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5754 LHS = AndLHS.getOperand(0);
5755 RHS = AndLHS.getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00005756 }
Evan Cheng095dac22010-01-06 19:38:29 +00005757 }
Evan Cheng950aac02007-09-25 01:57:46 +00005758
Evan Cheng095dac22010-01-06 19:38:29 +00005759 if (LHS.getNode()) {
5760 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5761 // instruction. Since the shift amount is in-range-or-undefined, we know
5762 // that doing a bittest on the i16 value is ok. We extend to i32 because
5763 // the encoding for the i16 version is larger than the i32 version.
5764 if (LHS.getValueType() == MVT::i8)
5765 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005766
Evan Cheng095dac22010-01-06 19:38:29 +00005767 // If the operand types disagree, extend the shift amount to match. Since
5768 // BT ignores high bits (like shifts) we can use anyextend.
5769 if (LHS.getValueType() != RHS.getValueType())
5770 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005771
Evan Cheng095dac22010-01-06 19:38:29 +00005772 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5773 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5774 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5775 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattner77a62312008-12-25 05:34:37 +00005776 }
5777
Evan Chengc621d452010-01-05 06:52:31 +00005778 return SDValue();
5779}
5780
5781SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5782 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5783 SDValue Op0 = Op.getOperand(0);
5784 SDValue Op1 = Op.getOperand(1);
5785 DebugLoc dl = Op.getDebugLoc();
5786 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5787
5788 // Optimize to BT if possible.
Evan Cheng095dac22010-01-06 19:38:29 +00005789 // Lower (X & (1 << N)) == 0 to BT(X, N).
5790 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5791 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5792 if (Op0.getOpcode() == ISD::AND &&
5793 Op0.hasOneUse() &&
5794 Op1.getOpcode() == ISD::Constant &&
5795 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5796 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5797 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5798 if (NewSetCC.getNode())
5799 return NewSetCC;
5800 }
Evan Chengc621d452010-01-05 06:52:31 +00005801
Chris Lattner77a62312008-12-25 05:34:37 +00005802 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5803 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00005804 if (X86CC == X86::COND_INVALID)
5805 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00005806
Dan Gohmanc8b47852009-03-07 01:58:32 +00005807 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Cheng834ae6b2009-12-15 00:53:42 +00005808
5809 // Use sbb x, x to materialize carry bit into a GPR.
Evan Chengedeb1692009-12-16 00:53:11 +00005810 if (X86CC == X86::COND_B)
Evan Cheng834ae6b2009-12-15 00:53:42 +00005811 return DAG.getNode(ISD::AND, dl, MVT::i8,
5812 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5813 DAG.getConstant(X86CC, MVT::i8), Cond),
5814 DAG.getConstant(1, MVT::i8));
Evan Cheng834ae6b2009-12-15 00:53:42 +00005815
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005816 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5817 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005818}
5819
Dan Gohman8181bd12008-07-27 21:46:04 +00005820SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5821 SDValue Cond;
5822 SDValue Op0 = Op.getOperand(0);
5823 SDValue Op1 = Op.getOperand(1);
5824 SDValue CC = Op.getOperand(2);
Owen Andersonac9de032009-08-10 22:56:29 +00005825 EVT VT = Op.getValueType();
Nate Begeman03605a02008-07-17 16:51:19 +00005826 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5827 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005828 DebugLoc dl = Op.getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005829
5830 if (isFP) {
5831 unsigned SSECC = 8;
Owen Andersonac9de032009-08-10 22:56:29 +00005832 EVT VT0 = Op0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005833 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5834 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005835 bool Swap = false;
5836
5837 switch (SetCCOpcode) {
5838 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005839 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005840 case ISD::SETEQ: SSECC = 0; break;
Scott Michel91099d62009-02-17 22:15:04 +00005841 case ISD::SETOGT:
Nate Begeman03605a02008-07-17 16:51:19 +00005842 case ISD::SETGT: Swap = true; // Fallthrough
5843 case ISD::SETLT:
5844 case ISD::SETOLT: SSECC = 1; break;
5845 case ISD::SETOGE:
5846 case ISD::SETGE: Swap = true; // Fallthrough
5847 case ISD::SETLE:
5848 case ISD::SETOLE: SSECC = 2; break;
5849 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005850 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005851 case ISD::SETNE: SSECC = 4; break;
5852 case ISD::SETULE: Swap = true;
5853 case ISD::SETUGE: SSECC = 5; break;
5854 case ISD::SETULT: Swap = true;
5855 case ISD::SETUGT: SSECC = 6; break;
5856 case ISD::SETO: SSECC = 7; break;
5857 }
5858 if (Swap)
5859 std::swap(Op0, Op1);
5860
Nate Begeman6357f9d2008-07-25 19:05:58 +00005861 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005862 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005863 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005864 SDValue UNORD, EQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005865 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5866 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00005867 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005868 }
5869 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005870 SDValue ORD, NEQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005871 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5872 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00005873 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005874 }
Edwin Törökbd448e32009-07-14 16:55:14 +00005875 llvm_unreachable("Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00005876 }
5877 // Handle all other FP comparisons here.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005878 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00005879 }
Scott Michel91099d62009-02-17 22:15:04 +00005880
Nate Begeman03605a02008-07-17 16:51:19 +00005881 // We are handling one of the integer comparisons here. Since SSE only has
5882 // GT and EQ comparisons for integer, swapping operands and multiple
5883 // operations may be required for some comparisons.
5884 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5885 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michel91099d62009-02-17 22:15:04 +00005886
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005887 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman03605a02008-07-17 16:51:19 +00005888 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005889 case MVT::v8i8:
5890 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5891 case MVT::v4i16:
5892 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5893 case MVT::v2i32:
5894 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5895 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman03605a02008-07-17 16:51:19 +00005896 }
Scott Michel91099d62009-02-17 22:15:04 +00005897
Nate Begeman03605a02008-07-17 16:51:19 +00005898 switch (SetCCOpcode) {
5899 default: break;
5900 case ISD::SETNE: Invert = true;
5901 case ISD::SETEQ: Opc = EQOpc; break;
5902 case ISD::SETLT: Swap = true;
5903 case ISD::SETGT: Opc = GTOpc; break;
5904 case ISD::SETGE: Swap = true;
5905 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5906 case ISD::SETULT: Swap = true;
5907 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5908 case ISD::SETUGE: Swap = true;
5909 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5910 }
5911 if (Swap)
5912 std::swap(Op0, Op1);
Scott Michel91099d62009-02-17 22:15:04 +00005913
Nate Begeman03605a02008-07-17 16:51:19 +00005914 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5915 // bits of the inputs before performing those operations.
5916 if (FlipSigns) {
Owen Andersonac9de032009-08-10 22:56:29 +00005917 EVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00005918 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5919 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00005920 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Cheng907a2d22009-02-25 22:49:59 +00005921 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5922 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00005923 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5924 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00005925 }
Scott Michel91099d62009-02-17 22:15:04 +00005926
Dale Johannesence0805b2009-02-03 19:33:06 +00005927 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00005928
5929 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00005930 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00005931 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00005932
Nate Begeman03605a02008-07-17 16:51:19 +00005933 return Result;
5934}
Evan Cheng950aac02007-09-25 01:57:46 +00005935
Evan Chengd580f022008-12-03 08:38:43 +00005936// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman99a12192009-03-04 19:44:21 +00005937static bool isX86LogicalCmp(SDValue Op) {
5938 unsigned Opc = Op.getNode()->getOpcode();
5939 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5940 return true;
5941 if (Op.getResNo() == 1 &&
5942 (Opc == X86ISD::ADD ||
5943 Opc == X86ISD::SUB ||
5944 Opc == X86ISD::SMUL ||
5945 Opc == X86ISD::UMUL ||
5946 Opc == X86ISD::INC ||
Dan Gohman12e03292009-09-18 19:59:53 +00005947 Opc == X86ISD::DEC ||
5948 Opc == X86ISD::OR ||
5949 Opc == X86ISD::XOR ||
5950 Opc == X86ISD::AND))
Dan Gohman99a12192009-03-04 19:44:21 +00005951 return true;
5952
5953 return false;
Evan Chengd580f022008-12-03 08:38:43 +00005954}
5955
Dan Gohman8181bd12008-07-27 21:46:04 +00005956SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005957 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00005958 SDValue Cond = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005959 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005960 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005961
Dan Gohman8ab7dd02009-10-20 16:22:37 +00005962 if (Cond.getOpcode() == ISD::SETCC) {
5963 SDValue NewCond = LowerSETCC(Cond, DAG);
5964 if (NewCond.getNode())
5965 Cond = NewCond;
5966 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967
Evan Cheng506f6f02010-01-26 02:00:44 +00005968 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
5969 SDValue Op1 = Op.getOperand(1);
5970 SDValue Op2 = Op.getOperand(2);
5971 if (Cond.getOpcode() == X86ISD::SETCC &&
5972 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
5973 SDValue Cmp = Cond.getOperand(1);
5974 if (Cmp.getOpcode() == X86ISD::CMP) {
5975 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
5976 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
5977 ConstantSDNode *RHSC =
5978 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
5979 if (N1C && N1C->isAllOnesValue() &&
5980 N2C && N2C->isNullValue() &&
5981 RHSC && RHSC->isNullValue()) {
5982 SDValue CmpOp0 = Cmp.getOperand(0);
5983 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
5984 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
5985 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
5986 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
5987 }
5988 }
5989 }
5990
Evan Cheng834ae6b2009-12-15 00:53:42 +00005991 // Look pass (and (setcc_carry (cmp ...)), 1).
5992 if (Cond.getOpcode() == ISD::AND &&
5993 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
5994 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
5995 if (C && C->getAPIntValue() == 1)
5996 Cond = Cond.getOperand(0);
5997 }
5998
Evan Cheng50d37ab2007-10-08 22:16:29 +00005999 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6000 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006001 if (Cond.getOpcode() == X86ISD::SETCC ||
6002 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006003 CC = Cond.getOperand(0);
6004
Dan Gohman8181bd12008-07-27 21:46:04 +00006005 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006006 unsigned Opc = Cmp.getOpcode();
Owen Andersonac9de032009-08-10 22:56:29 +00006007 EVT VT = Op.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006008
Evan Cheng50d37ab2007-10-08 22:16:29 +00006009 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00006010 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00006011 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00006012 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michel91099d62009-02-17 22:15:04 +00006013
Chris Lattnere4577dc2009-03-12 06:52:53 +00006014 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6015 Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00006016 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006017 addTest = false;
6018 }
6019 }
6020
6021 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006022 // Look pass the truncate.
6023 if (Cond.getOpcode() == ISD::TRUNCATE)
6024 Cond = Cond.getOperand(0);
6025
6026 // We know the result of AND is compared against zero. Try to match
6027 // it to BT.
6028 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6029 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6030 if (NewSetCC.getNode()) {
6031 CC = NewSetCC.getOperand(0);
6032 Cond = NewSetCC.getOperand(1);
6033 addTest = false;
6034 }
6035 }
6036 }
6037
6038 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006039 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006040 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006041 }
6042
Evan Cheng950aac02007-09-25 01:57:46 +00006043 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6044 // condition is true.
Evan Cheng506f6f02010-01-26 02:00:44 +00006045 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6046 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006047 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng950aac02007-09-25 01:57:46 +00006048}
6049
Evan Chengd580f022008-12-03 08:38:43 +00006050// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6051// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6052// from the AND / OR.
6053static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6054 Opc = Op.getOpcode();
6055 if (Opc != ISD::OR && Opc != ISD::AND)
6056 return false;
6057 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6058 Op.getOperand(0).hasOneUse() &&
6059 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6060 Op.getOperand(1).hasOneUse());
6061}
6062
Evan Cheng67f98b12009-02-02 08:19:07 +00006063// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6064// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006065static bool isXor1OfSetCC(SDValue Op) {
6066 if (Op.getOpcode() != ISD::XOR)
6067 return false;
6068 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6069 if (N1C && N1C->getAPIntValue() == 1) {
6070 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6071 Op.getOperand(0).hasOneUse();
6072 }
6073 return false;
6074}
6075
Dan Gohman8181bd12008-07-27 21:46:04 +00006076SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006077 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00006078 SDValue Chain = Op.getOperand(0);
6079 SDValue Cond = Op.getOperand(1);
6080 SDValue Dest = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006081 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00006082 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006083
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006084 if (Cond.getOpcode() == ISD::SETCC) {
6085 SDValue NewCond = LowerSETCC(Cond, DAG);
6086 if (NewCond.getNode())
6087 Cond = NewCond;
6088 }
Chris Lattner77a62312008-12-25 05:34:37 +00006089#if 0
6090 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00006091 else if (Cond.getOpcode() == X86ISD::ADD ||
6092 Cond.getOpcode() == X86ISD::SUB ||
6093 Cond.getOpcode() == X86ISD::SMUL ||
6094 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00006095 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00006096#endif
Scott Michel91099d62009-02-17 22:15:04 +00006097
Evan Cheng834ae6b2009-12-15 00:53:42 +00006098 // Look pass (and (setcc_carry (cmp ...)), 1).
6099 if (Cond.getOpcode() == ISD::AND &&
6100 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6101 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6102 if (C && C->getAPIntValue() == 1)
6103 Cond = Cond.getOperand(0);
6104 }
6105
Evan Cheng50d37ab2007-10-08 22:16:29 +00006106 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6107 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006108 if (Cond.getOpcode() == X86ISD::SETCC ||
6109 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006110 CC = Cond.getOperand(0);
6111
Dan Gohman8181bd12008-07-27 21:46:04 +00006112 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006113 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00006114 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman99a12192009-03-04 19:44:21 +00006115 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00006116 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006117 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00006118 } else {
Evan Chengd580f022008-12-03 08:38:43 +00006119 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00006120 default: break;
6121 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006122 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00006123 // These can only come from an arithmetic instruction with overflow,
6124 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00006125 Cond = Cond.getNode()->getOperand(1);
6126 addTest = false;
6127 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006128 }
Evan Cheng950aac02007-09-25 01:57:46 +00006129 }
Evan Chengd580f022008-12-03 08:38:43 +00006130 } else {
6131 unsigned CondOpc;
6132 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6133 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Chengd580f022008-12-03 08:38:43 +00006134 if (CondOpc == ISD::OR) {
6135 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6136 // two branches instead of an explicit OR instruction with a
6137 // separate test.
6138 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006139 isX86LogicalCmp(Cmp)) {
Evan Chengd580f022008-12-03 08:38:43 +00006140 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006141 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006142 Chain, Dest, CC, Cmp);
6143 CC = Cond.getOperand(1).getOperand(0);
6144 Cond = Cmp;
6145 addTest = false;
6146 }
6147 } else { // ISD::AND
6148 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6149 // two branches instead of an explicit AND instruction with a
6150 // separate test. However, we only do this if this block doesn't
6151 // have a fall-through edge, because this requires an explicit
6152 // jmp when the condition is false.
6153 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006154 isX86LogicalCmp(Cmp) &&
Evan Chengd580f022008-12-03 08:38:43 +00006155 Op.getNode()->hasOneUse()) {
6156 X86::CondCode CCode =
6157 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6158 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006159 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006160 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6161 // Look for an unconditional branch following this conditional branch.
6162 // We need this because we need to reverse the successors in order
6163 // to implement FCMP_OEQ.
6164 if (User.getOpcode() == ISD::BR) {
6165 SDValue FalseBB = User.getOperand(1);
6166 SDValue NewBR =
6167 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6168 assert(NewBR == User);
6169 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006170
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006171 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006172 Chain, Dest, CC, Cmp);
6173 X86::CondCode CCode =
6174 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6175 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006176 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006177 Cond = Cmp;
6178 addTest = false;
6179 }
6180 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006181 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006182 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6183 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6184 // It should be transformed during dag combiner except when the condition
6185 // is set by a arithmetics with overflow node.
6186 X86::CondCode CCode =
6187 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6188 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006189 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006190 Cond = Cond.getOperand(0).getOperand(1);
6191 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006192 }
Evan Cheng950aac02007-09-25 01:57:46 +00006193 }
6194
6195 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006196 // Look pass the truncate.
6197 if (Cond.getOpcode() == ISD::TRUNCATE)
6198 Cond = Cond.getOperand(0);
6199
6200 // We know the result of AND is compared against zero. Try to match
6201 // it to BT.
6202 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6203 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6204 if (NewSetCC.getNode()) {
6205 CC = NewSetCC.getOperand(0);
6206 Cond = NewSetCC.getOperand(1);
6207 addTest = false;
6208 }
6209 }
6210 }
6211
6212 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006213 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006214 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006215 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006216 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006217 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00006218}
6219
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006220
6221// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6222// Calls to _alloca is needed to probe the stack when allocating more than 4k
6223// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6224// that the guard pages used by the OS virtual memory manager are allocated in
6225// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00006226SDValue
6227X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006228 SelectionDAG &DAG) {
6229 assert(Subtarget->isTargetCygMing() &&
6230 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006231 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006232
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006233 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00006234 SDValue Chain = Op.getOperand(0);
6235 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006236 // FIXME: Ensure alignment here
6237
Dan Gohman8181bd12008-07-27 21:46:04 +00006238 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006239
Owen Andersonac9de032009-08-10 22:56:29 +00006240 EVT IntPtr = getPointerTy();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006241 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006242
Chris Lattnerfe5d4022008-10-11 22:08:30 +00006243 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006244
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006245 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246 Flag = Chain.getValue(1);
6247
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006248 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006249 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00006250 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006251 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006252 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006253 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006254 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006255 Flag = Chain.getValue(1);
6256
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006257 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00006258 DAG.getIntPtrConstant(0, true),
6259 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006260 Flag);
6261
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006262 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006263
Dan Gohman8181bd12008-07-27 21:46:04 +00006264 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006265 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006266}
6267
Dan Gohman8181bd12008-07-27 21:46:04 +00006268SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006269X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006270 SDValue Chain,
6271 SDValue Dst, SDValue Src,
6272 SDValue Size, unsigned Align,
6273 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00006274 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006275 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006277 // If not DWORD aligned or size is more than the threshold, call the library.
6278 // The libc version is likely to be faster for these cases. It can use the
6279 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006280 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00006281 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006282 ConstantSize->getZExtValue() >
6283 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006284 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006285
6286 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00006287 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006288
Bill Wendling4b2e3782008-10-01 00:59:58 +00006289 if (const char *bzeroEntry = V &&
6290 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00006291 EVT IntPtr = getPointerTy();
Owen Anderson35b47072009-08-13 21:58:54 +00006292 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michel91099d62009-02-17 22:15:04 +00006293 TargetLowering::ArgListTy Args;
Bill Wendling4b2e3782008-10-01 00:59:58 +00006294 TargetLowering::ArgListEntry Entry;
6295 Entry.Node = Dst;
6296 Entry.Ty = IntPtrTy;
6297 Args.push_back(Entry);
6298 Entry.Node = Size;
6299 Args.push_back(Entry);
6300 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00006301 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6302 false, false, false, false,
Dan Gohman9178de12009-08-05 01:29:28 +00006303 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendlingaa181762009-12-22 02:10:19 +00006304 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6305 DAG.GetOrdering(Chain.getNode()));
Bill Wendling4b2e3782008-10-01 00:59:58 +00006306 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006307 }
6308
Dan Gohmane8b391e2008-04-12 04:36:06 +00006309 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00006310 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006311 }
6312
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006313 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006314 SDValue InFlag(0, 0);
Owen Andersonac9de032009-08-10 22:56:29 +00006315 EVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00006316 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006317 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006318 unsigned BytesLeft = 0;
6319 bool TwoRepStos = false;
6320 if (ValC) {
6321 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006322 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006323
6324 // If the value is a constant, then we can potentially use larger sets.
6325 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006326 case 2: // WORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006327 AVT = MVT::i16;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006328 ValReg = X86::AX;
6329 Val = (Val << 8) | Val;
6330 break;
6331 case 0: // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006332 AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006333 ValReg = X86::EAX;
6334 Val = (Val << 8) | Val;
6335 Val = (Val << 16) | Val;
6336 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006337 AVT = MVT::i64;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006338 ValReg = X86::RAX;
6339 Val = (Val << 32) | Val;
6340 }
6341 break;
6342 default: // Byte aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006343 AVT = MVT::i8;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006344 ValReg = X86::AL;
6345 Count = DAG.getIntPtrConstant(SizeVal);
6346 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006347 }
6348
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006349 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00006350 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006351 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6352 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006353 }
6354
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006355 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006356 InFlag);
6357 InFlag = Chain.getValue(1);
6358 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006359 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00006360 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006361 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006362 InFlag = Chain.getValue(1);
6363 }
6364
Scott Michel91099d62009-02-17 22:15:04 +00006365 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006366 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006367 Count, InFlag);
6368 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006369 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006370 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006371 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372 InFlag = Chain.getValue(1);
6373
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006374 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006375 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6376 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006377
6378 if (TwoRepStos) {
6379 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00006380 Count = Size;
Owen Andersonac9de032009-08-10 22:56:29 +00006381 EVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006382 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006383 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6384 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006385 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006386 Left, InFlag);
6387 InFlag = Chain.getValue(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006388 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006389 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6390 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006391 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006392 // Handle the last 1 - 7 bytes.
6393 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006394 EVT AddrVT = Dst.getValueType();
6395 EVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006396
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006397 Chain = DAG.getMemset(Chain, dl,
6398 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006399 DAG.getConstant(Offset, AddrVT)),
6400 Src,
6401 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00006402 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403 }
6404
Dan Gohmane8b391e2008-04-12 04:36:06 +00006405 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006406 return Chain;
6407}
6408
Dan Gohman8181bd12008-07-27 21:46:04 +00006409SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006410X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006411 SDValue Chain, SDValue Dst, SDValue Src,
6412 SDValue Size, unsigned Align,
6413 bool AlwaysInline,
6414 const Value *DstSV, uint64_t DstSVOff,
Scott Michel91099d62009-02-17 22:15:04 +00006415 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006416 // This requires the copy size to be a constant, preferrably
6417 // within a subtarget-specific limit.
6418 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6419 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00006420 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006421 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006422 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00006423 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006424
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006425 /// If not DWORD aligned, call the library.
6426 if ((Align & 3) != 0)
6427 return SDValue();
6428
6429 // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006430 EVT AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006431 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006432 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006433
Duncan Sands92c43912008-06-06 12:08:01 +00006434 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006435 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00006436 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006437 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438
Dan Gohman8181bd12008-07-27 21:46:04 +00006439 SDValue InFlag(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00006440 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006441 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006442 Count, InFlag);
6443 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006444 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006445 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006446 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006447 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006448 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006449 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006450 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006451 InFlag = Chain.getValue(1);
6452
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006453 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006454 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6455 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6456 array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006457
Dan Gohman8181bd12008-07-27 21:46:04 +00006458 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00006459 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00006460 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006461 // Handle the last 1 - 7 bytes.
6462 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006463 EVT DstVT = Dst.getValueType();
6464 EVT SrcVT = Src.getValueType();
6465 EVT SizeVT = Size.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006466 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006467 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00006468 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006469 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00006470 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00006471 DAG.getConstant(BytesLeft, SizeVT),
6472 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00006473 DstSV, DstSVOff + Offset,
6474 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006475 }
6476
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006477 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006478 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006479}
6480
Dan Gohman8181bd12008-07-27 21:46:04 +00006481SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00006482 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006483 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006484
6485 if (!Subtarget->is64Bit()) {
6486 // vastart just stores the address of the VarArgsFrameIndex slot into the
6487 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00006488 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006489 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006490 }
6491
6492 // __va_list_tag:
6493 // gp_offset (0 - 6 * 8)
6494 // fp_offset (48 - 48 + 8 * 16)
6495 // overflow_arg_area (point to parameters coming in memory).
6496 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00006497 SmallVector<SDValue, 8> MemOps;
6498 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006499 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006500 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006501 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00006502 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006503 MemOps.push_back(Store);
6504
6505 // Store fp_offset
Scott Michel91099d62009-02-17 22:15:04 +00006506 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006507 FIN, DAG.getIntPtrConstant(4));
6508 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006509 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00006510 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511 MemOps.push_back(Store);
6512
6513 // Store ptr to overflow_arg_area
Scott Michel91099d62009-02-17 22:15:04 +00006514 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006515 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00006516 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006517 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 MemOps.push_back(Store);
6519
6520 // Store ptr to reg_save_area.
Scott Michel91099d62009-02-17 22:15:04 +00006521 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006522 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006523 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006524 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525 MemOps.push_back(Store);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006526 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006527 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006528}
6529
Dan Gohman8181bd12008-07-27 21:46:04 +00006530SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00006531 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6532 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006533 SDValue Chain = Op.getOperand(0);
6534 SDValue SrcPtr = Op.getOperand(1);
6535 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006536
Edwin Török4d9756a2009-07-08 20:53:28 +00006537 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006538 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00006539}
6540
Dan Gohman8181bd12008-07-27 21:46:04 +00006541SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006542 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00006543 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006544 SDValue Chain = Op.getOperand(0);
6545 SDValue DstPtr = Op.getOperand(1);
6546 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00006547 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6548 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006549 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006550
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006551 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman840ff5c2008-04-18 20:55:41 +00006552 DAG.getIntPtrConstant(24), 8, false,
6553 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006554}
6555
Dan Gohman8181bd12008-07-27 21:46:04 +00006556SDValue
6557X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006558 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006559 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006560 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006561 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006562 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006563 case Intrinsic::x86_sse_comieq_ss:
6564 case Intrinsic::x86_sse_comilt_ss:
6565 case Intrinsic::x86_sse_comile_ss:
6566 case Intrinsic::x86_sse_comigt_ss:
6567 case Intrinsic::x86_sse_comige_ss:
6568 case Intrinsic::x86_sse_comineq_ss:
6569 case Intrinsic::x86_sse_ucomieq_ss:
6570 case Intrinsic::x86_sse_ucomilt_ss:
6571 case Intrinsic::x86_sse_ucomile_ss:
6572 case Intrinsic::x86_sse_ucomigt_ss:
6573 case Intrinsic::x86_sse_ucomige_ss:
6574 case Intrinsic::x86_sse_ucomineq_ss:
6575 case Intrinsic::x86_sse2_comieq_sd:
6576 case Intrinsic::x86_sse2_comilt_sd:
6577 case Intrinsic::x86_sse2_comile_sd:
6578 case Intrinsic::x86_sse2_comigt_sd:
6579 case Intrinsic::x86_sse2_comige_sd:
6580 case Intrinsic::x86_sse2_comineq_sd:
6581 case Intrinsic::x86_sse2_ucomieq_sd:
6582 case Intrinsic::x86_sse2_ucomilt_sd:
6583 case Intrinsic::x86_sse2_ucomile_sd:
6584 case Intrinsic::x86_sse2_ucomigt_sd:
6585 case Intrinsic::x86_sse2_ucomige_sd:
6586 case Intrinsic::x86_sse2_ucomineq_sd: {
6587 unsigned Opc = 0;
6588 ISD::CondCode CC = ISD::SETCC_INVALID;
6589 switch (IntNo) {
6590 default: break;
6591 case Intrinsic::x86_sse_comieq_ss:
6592 case Intrinsic::x86_sse2_comieq_sd:
6593 Opc = X86ISD::COMI;
6594 CC = ISD::SETEQ;
6595 break;
6596 case Intrinsic::x86_sse_comilt_ss:
6597 case Intrinsic::x86_sse2_comilt_sd:
6598 Opc = X86ISD::COMI;
6599 CC = ISD::SETLT;
6600 break;
6601 case Intrinsic::x86_sse_comile_ss:
6602 case Intrinsic::x86_sse2_comile_sd:
6603 Opc = X86ISD::COMI;
6604 CC = ISD::SETLE;
6605 break;
6606 case Intrinsic::x86_sse_comigt_ss:
6607 case Intrinsic::x86_sse2_comigt_sd:
6608 Opc = X86ISD::COMI;
6609 CC = ISD::SETGT;
6610 break;
6611 case Intrinsic::x86_sse_comige_ss:
6612 case Intrinsic::x86_sse2_comige_sd:
6613 Opc = X86ISD::COMI;
6614 CC = ISD::SETGE;
6615 break;
6616 case Intrinsic::x86_sse_comineq_ss:
6617 case Intrinsic::x86_sse2_comineq_sd:
6618 Opc = X86ISD::COMI;
6619 CC = ISD::SETNE;
6620 break;
6621 case Intrinsic::x86_sse_ucomieq_ss:
6622 case Intrinsic::x86_sse2_ucomieq_sd:
6623 Opc = X86ISD::UCOMI;
6624 CC = ISD::SETEQ;
6625 break;
6626 case Intrinsic::x86_sse_ucomilt_ss:
6627 case Intrinsic::x86_sse2_ucomilt_sd:
6628 Opc = X86ISD::UCOMI;
6629 CC = ISD::SETLT;
6630 break;
6631 case Intrinsic::x86_sse_ucomile_ss:
6632 case Intrinsic::x86_sse2_ucomile_sd:
6633 Opc = X86ISD::UCOMI;
6634 CC = ISD::SETLE;
6635 break;
6636 case Intrinsic::x86_sse_ucomigt_ss:
6637 case Intrinsic::x86_sse2_ucomigt_sd:
6638 Opc = X86ISD::UCOMI;
6639 CC = ISD::SETGT;
6640 break;
6641 case Intrinsic::x86_sse_ucomige_ss:
6642 case Intrinsic::x86_sse2_ucomige_sd:
6643 Opc = X86ISD::UCOMI;
6644 CC = ISD::SETGE;
6645 break;
6646 case Intrinsic::x86_sse_ucomineq_ss:
6647 case Intrinsic::x86_sse2_ucomineq_sd:
6648 Opc = X86ISD::UCOMI;
6649 CC = ISD::SETNE;
6650 break;
6651 }
6652
Dan Gohman8181bd12008-07-27 21:46:04 +00006653 SDValue LHS = Op.getOperand(1);
6654 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006655 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006656 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006657 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6658 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6659 DAG.getConstant(X86CC, MVT::i8), Cond);
6660 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006661 }
Eric Christopher95d79262009-07-29 00:28:05 +00006662 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher79e0e8b2009-07-29 01:01:19 +00006663 // an integer value, not just an instruction so lower it to the ptest
6664 // pattern and a setcc for the result.
Eric Christopher95d79262009-07-29 00:28:05 +00006665 case Intrinsic::x86_sse41_ptestz:
6666 case Intrinsic::x86_sse41_ptestc:
6667 case Intrinsic::x86_sse41_ptestnzc:{
6668 unsigned X86CC = 0;
6669 switch (IntNo) {
Eric Christopher6612b082009-07-29 18:14:04 +00006670 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher95d79262009-07-29 00:28:05 +00006671 case Intrinsic::x86_sse41_ptestz:
6672 // ZF = 1
6673 X86CC = X86::COND_E;
6674 break;
6675 case Intrinsic::x86_sse41_ptestc:
6676 // CF = 1
6677 X86CC = X86::COND_B;
6678 break;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006679 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher95d79262009-07-29 00:28:05 +00006680 // ZF and CF = 0
6681 X86CC = X86::COND_A;
6682 break;
6683 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006684
Eric Christopher95d79262009-07-29 00:28:05 +00006685 SDValue LHS = Op.getOperand(1);
6686 SDValue RHS = Op.getOperand(2);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006687 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6688 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6689 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6690 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher95d79262009-07-29 00:28:05 +00006691 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006692
6693 // Fix vector shift instructions where the last operand is a non-immediate
6694 // i32 value.
6695 case Intrinsic::x86_sse2_pslli_w:
6696 case Intrinsic::x86_sse2_pslli_d:
6697 case Intrinsic::x86_sse2_pslli_q:
6698 case Intrinsic::x86_sse2_psrli_w:
6699 case Intrinsic::x86_sse2_psrli_d:
6700 case Intrinsic::x86_sse2_psrli_q:
6701 case Intrinsic::x86_sse2_psrai_w:
6702 case Intrinsic::x86_sse2_psrai_d:
6703 case Intrinsic::x86_mmx_pslli_w:
6704 case Intrinsic::x86_mmx_pslli_d:
6705 case Intrinsic::x86_mmx_pslli_q:
6706 case Intrinsic::x86_mmx_psrli_w:
6707 case Intrinsic::x86_mmx_psrli_d:
6708 case Intrinsic::x86_mmx_psrli_q:
6709 case Intrinsic::x86_mmx_psrai_w:
6710 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006711 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006712 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006713 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006714
6715 unsigned NewIntNo = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006716 EVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006717 switch (IntNo) {
6718 case Intrinsic::x86_sse2_pslli_w:
6719 NewIntNo = Intrinsic::x86_sse2_psll_w;
6720 break;
6721 case Intrinsic::x86_sse2_pslli_d:
6722 NewIntNo = Intrinsic::x86_sse2_psll_d;
6723 break;
6724 case Intrinsic::x86_sse2_pslli_q:
6725 NewIntNo = Intrinsic::x86_sse2_psll_q;
6726 break;
6727 case Intrinsic::x86_sse2_psrli_w:
6728 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6729 break;
6730 case Intrinsic::x86_sse2_psrli_d:
6731 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6732 break;
6733 case Intrinsic::x86_sse2_psrli_q:
6734 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6735 break;
6736 case Intrinsic::x86_sse2_psrai_w:
6737 NewIntNo = Intrinsic::x86_sse2_psra_w;
6738 break;
6739 case Intrinsic::x86_sse2_psrai_d:
6740 NewIntNo = Intrinsic::x86_sse2_psra_d;
6741 break;
6742 default: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006743 ShAmtVT = MVT::v2i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006744 switch (IntNo) {
6745 case Intrinsic::x86_mmx_pslli_w:
6746 NewIntNo = Intrinsic::x86_mmx_psll_w;
6747 break;
6748 case Intrinsic::x86_mmx_pslli_d:
6749 NewIntNo = Intrinsic::x86_mmx_psll_d;
6750 break;
6751 case Intrinsic::x86_mmx_pslli_q:
6752 NewIntNo = Intrinsic::x86_mmx_psll_q;
6753 break;
6754 case Intrinsic::x86_mmx_psrli_w:
6755 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6756 break;
6757 case Intrinsic::x86_mmx_psrli_d:
6758 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6759 break;
6760 case Intrinsic::x86_mmx_psrli_q:
6761 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6762 break;
6763 case Intrinsic::x86_mmx_psrai_w:
6764 NewIntNo = Intrinsic::x86_mmx_psra_w;
6765 break;
6766 case Intrinsic::x86_mmx_psrai_d:
6767 NewIntNo = Intrinsic::x86_mmx_psra_d;
6768 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00006769 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006770 }
6771 break;
6772 }
6773 }
Mon P Wang04c767e2009-09-03 19:56:25 +00006774
6775 // The vector shift intrinsics with scalars uses 32b shift amounts but
6776 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6777 // to be zero.
6778 SDValue ShOps[4];
6779 ShOps[0] = ShAmt;
6780 ShOps[1] = DAG.getConstant(0, MVT::i32);
6781 if (ShAmtVT == MVT::v4i32) {
6782 ShOps[2] = DAG.getUNDEF(MVT::i32);
6783 ShOps[3] = DAG.getUNDEF(MVT::i32);
6784 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6785 } else {
6786 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6787 }
6788
Owen Andersonac9de032009-08-10 22:56:29 +00006789 EVT VT = Op.getValueType();
Mon P Wang04c767e2009-09-03 19:56:25 +00006790 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006791 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006792 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006793 Op.getOperand(1), ShAmt);
6794 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006795 }
6796}
6797
Dan Gohman8181bd12008-07-27 21:46:04 +00006798SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006799 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006800 DebugLoc dl = Op.getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006801
6802 if (Depth > 0) {
6803 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6804 SDValue Offset =
6805 DAG.getConstant(TD->getPointerSize(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006806 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006807 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michel91099d62009-02-17 22:15:04 +00006808 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006809 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006810 NULL, 0);
6811 }
6812
6813 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006814 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michel91099d62009-02-17 22:15:04 +00006815 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006816 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006817}
6818
Dan Gohman8181bd12008-07-27 21:46:04 +00006819SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006820 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6821 MFI->setFrameAddressIsTaken(true);
Owen Andersonac9de032009-08-10 22:56:29 +00006822 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006823 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00006824 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6825 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006826 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00006827 while (Depth--)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006828 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng33633672008-09-27 01:56:22 +00006829 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006830}
6831
Dan Gohman8181bd12008-07-27 21:46:04 +00006832SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006833 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006834 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006835}
6836
Dan Gohman8181bd12008-07-27 21:46:04 +00006837SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006838{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006839 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006840 SDValue Chain = Op.getOperand(0);
6841 SDValue Offset = Op.getOperand(1);
6842 SDValue Handler = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006843 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006844
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006845 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6846 getPointerTy());
6847 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006848
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006849 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006850 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006851 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6852 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006853 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006854 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006855
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006856 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006857 MVT::Other,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006858 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006859}
6860
Dan Gohman8181bd12008-07-27 21:46:04 +00006861SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006862 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006863 SDValue Root = Op.getOperand(0);
6864 SDValue Trmp = Op.getOperand(1); // trampoline
6865 SDValue FPtr = Op.getOperand(2); // nested function
6866 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006867 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006868
Dan Gohman12a9c082008-02-06 22:27:42 +00006869 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006870
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006871 const X86InstrInfo *TII =
6872 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6873
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006874 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006875 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006876
6877 // Large code-model.
6878
6879 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6880 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6881
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006882 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6883 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006884
6885 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6886
6887 // Load the pointer to the nested function into R11.
6888 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00006889 SDValue Addr = Trmp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006890 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006891 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006892
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006893 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6894 DAG.getConstant(2, MVT::i64));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006895 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006896
6897 // Load the 'nest' parameter value into R10.
6898 // R10 is specified in X86CallingConv.td
6899 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006900 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6901 DAG.getConstant(10, MVT::i64));
6902 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006903 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006904
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006905 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6906 DAG.getConstant(12, MVT::i64));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006907 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006908
6909 // Jump to the nested function.
6910 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006911 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6912 DAG.getConstant(20, MVT::i64));
6913 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006914 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006915
6916 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006917 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6918 DAG.getConstant(22, MVT::i64));
6919 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006920 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006921
Dan Gohman8181bd12008-07-27 21:46:04 +00006922 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006923 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006924 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006925 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00006926 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006927 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel5838baa2009-09-02 08:44:58 +00006928 CallingConv::ID CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00006929 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006930
6931 switch (CC) {
6932 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00006933 llvm_unreachable("Unsupported calling convention");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006934 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006935 case CallingConv::X86_StdCall: {
6936 // Pass 'nest' parameter in ECX.
6937 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006938 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006939
6940 // Check that ECX wasn't needed by an 'inreg' parameter.
6941 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00006942 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006943
Chris Lattner1c8733e2008-03-12 17:45:29 +00006944 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006945 unsigned InRegCount = 0;
6946 unsigned Idx = 1;
6947
6948 for (FunctionType::param_iterator I = FTy->param_begin(),
6949 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00006950 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006951 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006952 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006953
6954 if (InRegCount > 2) {
Edwin Török3cb88482009-07-08 18:01:40 +00006955 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006956 }
6957 }
6958 break;
6959 }
6960 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00006961 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006962 // Pass 'nest' parameter in EAX.
6963 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00006964 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006965 break;
6966 }
6967
Dan Gohman8181bd12008-07-27 21:46:04 +00006968 SDValue OutChains[4];
6969 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006970
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006971 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6972 DAG.getConstant(10, MVT::i32));
6973 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006974
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006975 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00006976 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michel91099d62009-02-17 22:15:04 +00006977 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006978 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00006979 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006980
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006981 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6982 DAG.getConstant(1, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006983 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006984
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006985 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006986 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6987 DAG.getConstant(5, MVT::i32));
6988 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00006989 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006990
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006991 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6992 DAG.getConstant(6, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006993 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006994
Dan Gohman8181bd12008-07-27 21:46:04 +00006995 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006996 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006997 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006998 }
6999}
7000
Dan Gohman8181bd12008-07-27 21:46:04 +00007001SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007002 /*
7003 The rounding mode is in bits 11:10 of FPSR, and has the following
7004 settings:
7005 00 Round to nearest
7006 01 Round to -inf
7007 10 Round to +inf
7008 11 Round to 0
7009
7010 FLT_ROUNDS, on the other hand, expects the following:
7011 -1 Undefined
7012 0 Round to 0
7013 1 Round to nearest
7014 2 Round to +inf
7015 3 Round to -inf
7016
7017 To perform the conversion, we do:
7018 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7019 */
7020
7021 MachineFunction &MF = DAG.getMachineFunction();
7022 const TargetMachine &TM = MF.getTarget();
7023 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7024 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersonac9de032009-08-10 22:56:29 +00007025 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007026 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007027
7028 // Save FP Control Word to stack slot
David Greene6424ab92009-11-12 20:49:22 +00007029 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00007030 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007031
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007032 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00007033 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007034
7035 // Load FP Control Word from stack slot
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007036 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007037
7038 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00007039 SDValue CWD1 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007040 DAG.getNode(ISD::SRL, dl, MVT::i16,
7041 DAG.getNode(ISD::AND, dl, MVT::i16,
7042 CWD, DAG.getConstant(0x800, MVT::i16)),
7043 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00007044 SDValue CWD2 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007045 DAG.getNode(ISD::SRL, dl, MVT::i16,
7046 DAG.getNode(ISD::AND, dl, MVT::i16,
7047 CWD, DAG.getConstant(0x400, MVT::i16)),
7048 DAG.getConstant(9, MVT::i8));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007049
Dan Gohman8181bd12008-07-27 21:46:04 +00007050 SDValue RetVal =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007051 DAG.getNode(ISD::AND, dl, MVT::i16,
7052 DAG.getNode(ISD::ADD, dl, MVT::i16,
7053 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7054 DAG.getConstant(1, MVT::i16)),
7055 DAG.getConstant(3, MVT::i16));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007056
7057
Duncan Sands92c43912008-06-06 12:08:01 +00007058 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen24dd9a52009-02-07 00:55:49 +00007059 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007060}
7061
Dan Gohman8181bd12008-07-27 21:46:04 +00007062SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007063 EVT VT = Op.getValueType();
7064 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007065 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007066 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007067
7068 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007069 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007070 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007071 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007072 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007073 }
Evan Cheng48679f42007-12-14 02:13:44 +00007074
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007075 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007076 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007077 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007078
7079 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007080 SDValue Ops[] = {
7081 Op,
7082 DAG.getConstant(NumBits+NumBits-1, OpVT),
7083 DAG.getConstant(X86::COND_E, MVT::i8),
7084 Op.getValue(1)
7085 };
7086 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007087
7088 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007089 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007090
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007091 if (VT == MVT::i8)
7092 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007093 return Op;
7094}
7095
Dan Gohman8181bd12008-07-27 21:46:04 +00007096SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007097 EVT VT = Op.getValueType();
7098 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007099 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007100 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007101
7102 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007103 if (VT == MVT::i8) {
7104 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007105 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007106 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007107
7108 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007109 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007110 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007111
7112 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007113 SDValue Ops[] = {
7114 Op,
7115 DAG.getConstant(NumBits, OpVT),
7116 DAG.getConstant(X86::COND_E, MVT::i8),
7117 Op.getValue(1)
7118 };
7119 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007120
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007121 if (VT == MVT::i8)
7122 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007123 return Op;
7124}
7125
Mon P Wang14edb092008-12-18 21:42:19 +00007126SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007127 EVT VT = Op.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007128 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007129 DebugLoc dl = Op.getDebugLoc();
Scott Michel91099d62009-02-17 22:15:04 +00007130
Mon P Wang14edb092008-12-18 21:42:19 +00007131 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7132 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7133 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7134 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7135 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7136 //
7137 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7138 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7139 // return AloBlo + AloBhi + AhiBlo;
7140
7141 SDValue A = Op.getOperand(0);
7142 SDValue B = Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +00007143
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007144 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007145 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7146 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007147 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007148 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7149 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007150 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007151 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007152 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007153 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007154 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007155 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007156 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007157 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007158 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007159 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007160 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7161 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007162 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007163 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7164 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007165 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7166 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00007167 return Res;
7168}
7169
7170
Bill Wendling7e04be62008-12-09 22:08:41 +00007171SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7172 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7173 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00007174 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7175 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00007176 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00007177 SDValue LHS = N->getOperand(0);
7178 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00007179 unsigned BaseOp = 0;
7180 unsigned Cond = 0;
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007181 DebugLoc dl = Op.getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00007182
7183 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007184 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling7e04be62008-12-09 22:08:41 +00007185 case ISD::SADDO:
Dan Gohman99a12192009-03-04 19:44:21 +00007186 // A subtract of one will be selected as a INC. Note that INC doesn't
7187 // set CF, so we can't do this for UADDO.
7188 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7189 if (C->getAPIntValue() == 1) {
7190 BaseOp = X86ISD::INC;
7191 Cond = X86::COND_O;
7192 break;
7193 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007194 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00007195 Cond = X86::COND_O;
7196 break;
7197 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007198 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007199 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007200 break;
7201 case ISD::SSUBO:
Dan Gohman99a12192009-03-04 19:44:21 +00007202 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7203 // set CF, so we can't do this for USUBO.
7204 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7205 if (C->getAPIntValue() == 1) {
7206 BaseOp = X86ISD::DEC;
7207 Cond = X86::COND_O;
7208 break;
7209 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007210 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00007211 Cond = X86::COND_O;
7212 break;
7213 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007214 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007215 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007216 break;
7217 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007218 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00007219 Cond = X86::COND_O;
7220 break;
7221 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007222 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007223 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007224 break;
7225 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00007226
Bill Wendlingd3511522008-12-02 01:06:39 +00007227 // Also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007228 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007229 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00007230
Bill Wendlingd3511522008-12-02 01:06:39 +00007231 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007232 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007233 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00007234
Bill Wendlingd3511522008-12-02 01:06:39 +00007235 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7236 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00007237}
7238
Dan Gohman8181bd12008-07-27 21:46:04 +00007239SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007240 EVT T = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007241 DebugLoc dl = Op.getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00007242 unsigned Reg = 0;
7243 unsigned size = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007244 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00007245 default:
7246 assert(false && "Invalid value type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007247 case MVT::i8: Reg = X86::AL; size = 1; break;
7248 case MVT::i16: Reg = X86::AX; size = 2; break;
7249 case MVT::i32: Reg = X86::EAX; size = 4; break;
7250 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007251 assert(Subtarget->is64Bit() && "Node not type legal!");
7252 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00007253 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00007254 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007255 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00007256 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00007257 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00007258 Op.getOperand(1),
7259 Op.getOperand(3),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007260 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng6617eed2008-09-24 23:26:36 +00007261 cpIn.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007262 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007263 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michel91099d62009-02-17 22:15:04 +00007264 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007265 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00007266 return cpOut;
7267}
7268
Duncan Sands7d9834b2008-12-01 11:39:25 +00007269SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00007270 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00007271 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007272 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007273 SDValue TheChain = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007274 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007275 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007276 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7277 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007278 rax.getValue(2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007279 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7280 DAG.getConstant(32, MVT::i8));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007281 SDValue Ops[] = {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007282 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00007283 rdx.getValue(1)
7284 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007285 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00007286}
7287
Dale Johannesen9011d872008-09-29 22:25:26 +00007288SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7289 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007290 DebugLoc dl = Node->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00007291 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007292 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Chengef356282009-02-23 09:03:22 +00007293 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007294 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007295 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00007296 Node->getOperand(0),
7297 Node->getOperand(1), negOp,
7298 cast<AtomicSDNode>(Node)->getSrcValue(),
7299 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00007300}
7301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007302/// LowerOperation - Provide custom lowering hooks for some operations.
7303///
Dan Gohman8181bd12008-07-27 21:46:04 +00007304SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007305 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007306 default: llvm_unreachable("Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007307 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7308 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007309 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00007310 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007311 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7312 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7313 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7314 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7315 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7316 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
7317 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00007318 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohman064403e2009-10-30 01:28:02 +00007319 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007320 case ISD::SHL_PARTS:
7321 case ISD::SRA_PARTS:
7322 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7323 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00007324 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007325 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman8c3cb582009-05-23 09:59:16 +00007326 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007327 case ISD::FABS: return LowerFABS(Op, DAG);
7328 case ISD::FNEG: return LowerFNEG(Op, DAG);
7329 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007330 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00007331 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007332 case ISD::SELECT: return LowerSELECT(Op, DAG);
7333 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007334 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007335 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00007336 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007337 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
7338 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
7339 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7340 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
7341 case ISD::FRAME_TO_ARGS_OFFSET:
7342 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
7343 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
7344 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007345 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00007346 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00007347 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7348 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00007349 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00007350 case ISD::SADDO:
7351 case ISD::UADDO:
7352 case ISD::SSUBO:
7353 case ISD::USUBO:
7354 case ISD::SMULO:
7355 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007356 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007357 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007358}
7359
Duncan Sands7d9834b2008-12-01 11:39:25 +00007360void X86TargetLowering::
7361ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7362 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersonac9de032009-08-10 22:56:29 +00007363 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007364 DebugLoc dl = Node->getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007365 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007366
7367 SDValue Chain = Node->getOperand(0);
7368 SDValue In1 = Node->getOperand(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007369 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007370 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007371 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007372 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007373 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007374 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007375 SDValue Result =
7376 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7377 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands7d9834b2008-12-01 11:39:25 +00007378 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007379 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007380 Results.push_back(Result.getValue(2));
7381}
7382
Duncan Sandsac496a12008-07-04 11:47:58 +00007383/// ReplaceNodeResults - Replace a node with an illegal result type
7384/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00007385void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7386 SmallVectorImpl<SDValue>&Results,
7387 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007388 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007389 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00007390 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007391 assert(false && "Do not know how to custom type legalize this operation!");
7392 return;
7393 case ISD::FP_TO_SINT: {
Eli Friedman8c3cb582009-05-23 09:59:16 +00007394 std::pair<SDValue,SDValue> Vals =
7395 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007396 SDValue FIST = Vals.first, StackSlot = Vals.second;
7397 if (FIST.getNode() != 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00007398 EVT VT = N->getValueType(0);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007399 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007400 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007401 }
7402 return;
7403 }
7404 case ISD::READCYCLECOUNTER: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007405 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007406 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007407 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007408 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007409 rd.getValue(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007410 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007411 eax.getValue(2));
7412 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7413 SDValue Ops[] = { eax, edx };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007414 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007415 Results.push_back(edx.getValue(1));
7416 return;
7417 }
Mon P Wangc707f3f2009-11-30 02:42:02 +00007418 case ISD::SDIV:
7419 case ISD::UDIV:
7420 case ISD::SREM:
7421 case ISD::UREM: {
7422 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7423 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7424 return;
7425 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007426 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersonac9de032009-08-10 22:56:29 +00007427 EVT T = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007428 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007429 SDValue cpInL, cpInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007430 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7431 DAG.getConstant(0, MVT::i32));
7432 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7433 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007434 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7435 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007436 cpInL.getValue(1));
7437 SDValue swapInL, swapInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007438 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7439 DAG.getConstant(0, MVT::i32));
7440 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7441 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007442 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007443 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007444 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007445 swapInL.getValue(1));
7446 SDValue Ops[] = { swapInH.getValue(0),
7447 N->getOperand(1),
7448 swapInH.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007449 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007450 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007451 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007452 MVT::i32, Result.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007453 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007454 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007455 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007456 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007457 Results.push_back(cpOutH.getValue(1));
7458 return;
7459 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007460 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007461 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7462 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007463 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007464 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7465 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007466 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007467 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7468 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007469 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007470 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7471 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007472 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007473 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7474 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007475 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007476 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7477 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007478 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007479 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7480 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007481 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007482}
7483
7484const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7485 switch (Opcode) {
7486 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00007487 case X86ISD::BSF: return "X86ISD::BSF";
7488 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007489 case X86ISD::SHLD: return "X86ISD::SHLD";
7490 case X86ISD::SHRD: return "X86ISD::SHRD";
7491 case X86ISD::FAND: return "X86ISD::FAND";
7492 case X86ISD::FOR: return "X86ISD::FOR";
7493 case X86ISD::FXOR: return "X86ISD::FXOR";
7494 case X86ISD::FSRL: return "X86ISD::FSRL";
7495 case X86ISD::FILD: return "X86ISD::FILD";
7496 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
7497 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7498 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7499 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
7500 case X86ISD::FLD: return "X86ISD::FLD";
7501 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007502 case X86ISD::CALL: return "X86ISD::CALL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007503 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00007504 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007505 case X86ISD::CMP: return "X86ISD::CMP";
7506 case X86ISD::COMI: return "X86ISD::COMI";
7507 case X86ISD::UCOMI: return "X86ISD::UCOMI";
7508 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng834ae6b2009-12-15 00:53:42 +00007509 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007510 case X86ISD::CMOV: return "X86ISD::CMOV";
7511 case X86ISD::BRCOND: return "X86ISD::BRCOND";
7512 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
7513 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7514 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007515 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
7516 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattnerdc6fc472009-06-27 04:16:01 +00007517 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begemand77e59e2008-02-11 04:19:36 +00007518 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007519 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00007520 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7521 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007522 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begeman2c87c422009-02-23 08:49:38 +00007523 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007524 case X86ISD::FMAX: return "X86ISD::FMAX";
7525 case X86ISD::FMIN: return "X86ISD::FMIN";
7526 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7527 case X86ISD::FRCP: return "X86ISD::FRCP";
7528 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindolabca99f72009-04-08 21:14:34 +00007529 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007530 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00007531 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007532 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00007533 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7534 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00007535 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7536 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7537 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7538 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7539 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7540 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00007541 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7542 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00007543 case X86ISD::VSHL: return "X86ISD::VSHL";
7544 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00007545 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7546 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7547 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7548 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7549 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7550 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7551 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7552 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7553 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7554 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00007555 case X86ISD::ADD: return "X86ISD::ADD";
7556 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00007557 case X86ISD::SMUL: return "X86ISD::SMUL";
7558 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman99a12192009-03-04 19:44:21 +00007559 case X86ISD::INC: return "X86ISD::INC";
7560 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohman12e03292009-09-18 19:59:53 +00007561 case X86ISD::OR: return "X86ISD::OR";
7562 case X86ISD::XOR: return "X86ISD::XOR";
7563 case X86ISD::AND: return "X86ISD::AND";
Evan Chengc3495762009-03-30 21:36:47 +00007564 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher95d79262009-07-29 00:28:05 +00007565 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohman34228bf2009-08-15 01:38:56 +00007566 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007567 }
7568}
7569
7570// isLegalAddressingMode - Return true if the addressing mode represented
7571// by AM is legal for this target, for a load/store of the specified type.
Scott Michel91099d62009-02-17 22:15:04 +00007572bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007573 const Type *Ty) const {
7574 // X86 supports extremely general addressing modes.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007575 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michel91099d62009-02-17 22:15:04 +00007576
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007577 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007578 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007579 return false;
Scott Michel91099d62009-02-17 22:15:04 +00007580
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007581 if (AM.BaseGV) {
Chris Lattner01e39942009-07-10 07:38:24 +00007582 unsigned GVFlags =
7583 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007584
Chris Lattner01e39942009-07-10 07:38:24 +00007585 // If a reference to this global requires an extra load, we can't fold it.
7586 if (isGlobalStubReference(GVFlags))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007587 return false;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007588
Chris Lattner01e39942009-07-10 07:38:24 +00007589 // If BaseGV requires a register for the PIC base, we cannot also have a
7590 // BaseReg specified.
7591 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen64660e92008-12-05 21:47:27 +00007592 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00007593
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007594 // If lower 4G is not available, then we must use rip-relative addressing.
7595 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7596 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007597 }
Scott Michel91099d62009-02-17 22:15:04 +00007598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007599 switch (AM.Scale) {
7600 case 0:
7601 case 1:
7602 case 2:
7603 case 4:
7604 case 8:
7605 // These scales always work.
7606 break;
7607 case 3:
7608 case 5:
7609 case 9:
7610 // These scales are formed with basereg+scalereg. Only accept if there is
7611 // no basereg yet.
7612 if (AM.HasBaseReg)
7613 return false;
7614 break;
7615 default: // Other stuff never works.
7616 return false;
7617 }
Scott Michel91099d62009-02-17 22:15:04 +00007618
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007619 return true;
7620}
7621
7622
Evan Cheng27a820a2007-10-26 01:56:11 +00007623bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7624 if (!Ty1->isInteger() || !Ty2->isInteger())
7625 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00007626 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7627 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007628 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00007629 return false;
7630 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00007631}
7632
Owen Andersonac9de032009-08-10 22:56:29 +00007633bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands92c43912008-06-06 12:08:01 +00007634 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00007635 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00007636 unsigned NumBits1 = VT1.getSizeInBits();
7637 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007638 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00007639 return false;
7640 return Subtarget->is64Bit() || NumBits1 < 64;
7641}
Evan Cheng27a820a2007-10-26 01:56:11 +00007642
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007643bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007644 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman65054cc2010-01-15 22:18:15 +00007645 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007646}
7647
Owen Andersonac9de032009-08-10 22:56:29 +00007648bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007649 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007650 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007651}
7652
Owen Andersonac9de032009-08-10 22:56:29 +00007653bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007654 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007655 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007656}
7657
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007658/// isShuffleMaskLegal - Targets can use this to indicate that they only
7659/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7660/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7661/// are assumed to be legal.
7662bool
Eric Christopher3d82bbd2009-08-27 18:07:15 +00007663X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersonac9de032009-08-10 22:56:29 +00007664 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007665 // Only do shuffles on 128-bit vector types for now.
Nate Begeman543d2142009-04-27 18:41:29 +00007666 if (VT.getSizeInBits() == 64)
7667 return false;
7668
Nate Begeman080f8e22009-10-19 02:17:23 +00007669 // FIXME: pshufb, blends, shifts.
Nate Begeman543d2142009-04-27 18:41:29 +00007670 return (VT.getVectorNumElements() == 2 ||
7671 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7672 isMOVLMask(M, VT) ||
7673 isSHUFPMask(M, VT) ||
7674 isPSHUFDMask(M, VT) ||
7675 isPSHUFHWMask(M, VT) ||
7676 isPSHUFLWMask(M, VT) ||
Nate Begeman080f8e22009-10-19 02:17:23 +00007677 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman543d2142009-04-27 18:41:29 +00007678 isUNPCKLMask(M, VT) ||
7679 isUNPCKHMask(M, VT) ||
7680 isUNPCKL_v_undef_Mask(M, VT) ||
7681 isUNPCKH_v_undef_Mask(M, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007682}
7683
Dan Gohman48d5f062008-04-09 20:09:42 +00007684bool
Nate Begemane8f61cb2009-04-29 05:20:52 +00007685X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersonac9de032009-08-10 22:56:29 +00007686 EVT VT) const {
Nate Begeman543d2142009-04-27 18:41:29 +00007687 unsigned NumElts = VT.getVectorNumElements();
7688 // FIXME: This collection of masks seems suspect.
7689 if (NumElts == 2)
7690 return true;
7691 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7692 return (isMOVLMask(Mask, VT) ||
7693 isCommutedMOVLMask(Mask, VT, true) ||
7694 isSHUFPMask(Mask, VT) ||
7695 isCommutedSHUFPMask(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007696 }
7697 return false;
7698}
7699
7700//===----------------------------------------------------------------------===//
7701// X86 Scheduler Hooks
7702//===----------------------------------------------------------------------===//
7703
Mon P Wang078a62d2008-05-05 19:05:59 +00007704// private utility function
7705MachineBasicBlock *
7706X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7707 MachineBasicBlock *MBB,
7708 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007709 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00007710 unsigned LoadOpc,
7711 unsigned CXchgOpc,
7712 unsigned copyOpc,
7713 unsigned notOpc,
7714 unsigned EAXreg,
7715 TargetRegisterClass *RC,
Dan Gohman96d60922009-02-07 16:15:20 +00007716 bool invSrc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00007717 // For the atomic bitwise operator, we generate
7718 // thisMBB:
7719 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007720 // ld t1 = [bitinstr.addr]
7721 // op t2 = t1, [bitinstr.val]
7722 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007723 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7724 // bz newMBB
7725 // fallthrough -->nextMBB
7726 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7727 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007728 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007729 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007730
Mon P Wang078a62d2008-05-05 19:05:59 +00007731 /// First build the CFG
7732 MachineFunction *F = MBB->getParent();
7733 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007734 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7735 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7736 F->insert(MBBIter, newMBB);
7737 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007738
Mon P Wang078a62d2008-05-05 19:05:59 +00007739 // Move all successors to thisMBB to nextMBB
7740 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007741
Mon P Wang078a62d2008-05-05 19:05:59 +00007742 // Update thisMBB to fall through to newMBB
7743 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007744
Mon P Wang078a62d2008-05-05 19:05:59 +00007745 // newMBB jumps to itself and fall through to nextMBB
7746 newMBB->addSuccessor(nextMBB);
7747 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007748
Mon P Wang078a62d2008-05-05 19:05:59 +00007749 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007750 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00007751 "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007752 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007753 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007754 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00007755 int numArgs = bInstr->getNumOperands() - 1;
7756 for (int i=0; i < numArgs; ++i)
7757 argOpers[i] = &bInstr->getOperand(i+1);
7758
7759 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007760 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7761 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00007762
Dale Johannesend20e4452008-08-19 18:47:28 +00007763 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007764 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007765 for (int i=0; i <= lastAddrIndx; ++i)
7766 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007767
Dale Johannesend20e4452008-08-19 18:47:28 +00007768 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007769 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007770 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007771 }
Scott Michel91099d62009-02-17 22:15:04 +00007772 else
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007773 tt = t1;
7774
Dale Johannesend20e4452008-08-19 18:47:28 +00007775 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007776 assert((argOpers[valArgIndx]->isReg() ||
7777 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007778 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007779 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007780 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007781 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007782 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007783 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007784 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007785
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007786 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007787 MIB.addReg(t1);
Scott Michel91099d62009-02-17 22:15:04 +00007788
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007789 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007790 for (int i=0; i <= lastAddrIndx; ++i)
7791 (*MIB).addOperand(*argOpers[i]);
7792 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007793 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007794 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7795 bInstr->memoperands_end());
Mon P Wang50584a62008-07-17 04:54:06 +00007796
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007797 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007798 MIB.addReg(EAXreg);
Scott Michel91099d62009-02-17 22:15:04 +00007799
Mon P Wang078a62d2008-05-05 19:05:59 +00007800 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007801 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007802
Dan Gohman221a4372008-07-07 23:14:23 +00007803 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007804 return nextMBB;
7805}
7806
Dale Johannesen44eb5372008-10-03 19:41:08 +00007807// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007808MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007809X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7810 MachineBasicBlock *MBB,
7811 unsigned regOpcL,
7812 unsigned regOpcH,
7813 unsigned immOpcL,
7814 unsigned immOpcH,
Dan Gohman96d60922009-02-07 16:15:20 +00007815 bool invSrc) const {
Dale Johannesenf160d802008-10-02 18:53:47 +00007816 // For the atomic bitwise operator, we generate
7817 // thisMBB (instructions are in pairs, except cmpxchg8b)
7818 // ld t1,t2 = [bitinstr.addr]
7819 // newMBB:
7820 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7821 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007822 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007823 // mov ECX, EBX <- t5, t6
7824 // mov EAX, EDX <- t1, t2
7825 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7826 // mov t3, t4 <- EAX, EDX
7827 // bz newMBB
7828 // result in out1, out2
7829 // fallthrough -->nextMBB
7830
7831 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7832 const unsigned LoadOpc = X86::MOV32rm;
7833 const unsigned copyOpc = X86::MOV32rr;
7834 const unsigned NotOpc = X86::NOT32r;
7835 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7836 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7837 MachineFunction::iterator MBBIter = MBB;
7838 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007839
Dale Johannesenf160d802008-10-02 18:53:47 +00007840 /// First build the CFG
7841 MachineFunction *F = MBB->getParent();
7842 MachineBasicBlock *thisMBB = MBB;
7843 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7844 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7845 F->insert(MBBIter, newMBB);
7846 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007847
Dale Johannesenf160d802008-10-02 18:53:47 +00007848 // Move all successors to thisMBB to nextMBB
7849 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007850
Dale Johannesenf160d802008-10-02 18:53:47 +00007851 // Update thisMBB to fall through to newMBB
7852 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007853
Dale Johannesenf160d802008-10-02 18:53:47 +00007854 // newMBB jumps to itself and fall through to nextMBB
7855 newMBB->addSuccessor(nextMBB);
7856 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007857
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007858 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007859 // Insert instructions into newMBB based on incoming instruction
7860 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007861 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00007862 "unexpected number of operands");
Dale Johannesenf160d802008-10-02 18:53:47 +00007863 MachineOperand& dest1Oper = bInstr->getOperand(0);
7864 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007865 MachineOperand* argOpers[2 + X86AddrNumOperands];
7866 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00007867 argOpers[i] = &bInstr->getOperand(i+2);
7868
Evan Cheng4460e1b2010-01-08 19:14:57 +00007869 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007870 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michel91099d62009-02-17 22:15:04 +00007871
Dale Johannesenf160d802008-10-02 18:53:47 +00007872 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007873 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00007874 for (int i=0; i <= lastAddrIndx; ++i)
7875 (*MIB).addOperand(*argOpers[i]);
7876 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007877 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007878 // add 4 to displacement.
Rafael Espindolabca99f72009-04-08 21:14:34 +00007879 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00007880 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007881 MachineOperand newOp3 = *(argOpers[3]);
7882 if (newOp3.isImm())
7883 newOp3.setImm(newOp3.getImm()+4);
7884 else
7885 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007886 (*MIB).addOperand(newOp3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00007887 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesenf160d802008-10-02 18:53:47 +00007888
7889 // t3/4 are defined later, at the bottom of the loop
7890 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7891 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007892 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007893 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007894 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00007895 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7896
Evan Chengcdd58c32010-01-08 23:41:50 +00007897 // The subsequent operations should be using the destination registers of
7898 //the PHI instructions.
Scott Michel91099d62009-02-17 22:15:04 +00007899 if (invSrc) {
Evan Chengcdd58c32010-01-08 23:41:50 +00007900 t1 = F->getRegInfo().createVirtualRegister(RC);
7901 t2 = F->getRegInfo().createVirtualRegister(RC);
7902 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7903 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesenf160d802008-10-02 18:53:47 +00007904 } else {
Evan Chengcdd58c32010-01-08 23:41:50 +00007905 t1 = dest1Oper.getReg();
7906 t2 = dest2Oper.getReg();
Dale Johannesenf160d802008-10-02 18:53:47 +00007907 }
7908
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007909 int valArgIndx = lastAddrIndx + 1;
7910 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendlingc1946742009-05-30 01:09:53 +00007911 argOpers[valArgIndx]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00007912 "invalid operand");
7913 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7914 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007915 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007916 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00007917 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007918 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007919 if (regOpcL != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00007920 MIB.addReg(t1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007921 (*MIB).addOperand(*argOpers[valArgIndx]);
7922 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00007923 argOpers[valArgIndx]->isReg());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007924 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00007925 argOpers[valArgIndx]->isImm());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007926 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007927 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00007928 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007929 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007930 if (regOpcH != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00007931 MIB.addReg(t2);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007932 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesenf160d802008-10-02 18:53:47 +00007933
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007934 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007935 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007936 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007937 MIB.addReg(t2);
7938
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007939 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007940 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007941 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00007942 MIB.addReg(t6);
Scott Michel91099d62009-02-17 22:15:04 +00007943
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007944 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00007945 for (int i=0; i <= lastAddrIndx; ++i)
7946 (*MIB).addOperand(*argOpers[i]);
7947
7948 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007949 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7950 bInstr->memoperands_end());
Dale Johannesenf160d802008-10-02 18:53:47 +00007951
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007952 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00007953 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007954 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00007955 MIB.addReg(X86::EDX);
Scott Michel91099d62009-02-17 22:15:04 +00007956
Dale Johannesenf160d802008-10-02 18:53:47 +00007957 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007958 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00007959
7960 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7961 return nextMBB;
7962}
7963
7964// private utility function
7965MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00007966X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7967 MachineBasicBlock *MBB,
Dan Gohman96d60922009-02-07 16:15:20 +00007968 unsigned cmovOpc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00007969 // For the atomic min/max operator, we generate
7970 // thisMBB:
7971 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007972 // ld t1 = [min/max.addr]
Scott Michel91099d62009-02-17 22:15:04 +00007973 // mov t2 = [min/max.val]
Mon P Wang078a62d2008-05-05 19:05:59 +00007974 // cmp t1, t2
7975 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00007976 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007977 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7978 // bz newMBB
7979 // fallthrough -->nextMBB
7980 //
7981 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7982 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007983 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007984 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007985
Mon P Wang078a62d2008-05-05 19:05:59 +00007986 /// First build the CFG
7987 MachineFunction *F = MBB->getParent();
7988 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007989 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7990 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7991 F->insert(MBBIter, newMBB);
7992 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007993
Dan Gohman34228bf2009-08-15 01:38:56 +00007994 // Move all successors of thisMBB to nextMBB
Mon P Wang078a62d2008-05-05 19:05:59 +00007995 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007996
Mon P Wang078a62d2008-05-05 19:05:59 +00007997 // Update thisMBB to fall through to newMBB
7998 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007999
Mon P Wang078a62d2008-05-05 19:05:59 +00008000 // newMBB jumps to newMBB and fall through to nextMBB
8001 newMBB->addSuccessor(nextMBB);
8002 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008003
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008004 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00008005 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008006 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00008007 "unexpected number of operands");
Mon P Wang078a62d2008-05-05 19:05:59 +00008008 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008009 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00008010 int numArgs = mInstr->getNumOperands() - 1;
8011 for (int i=0; i < numArgs; ++i)
8012 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michel91099d62009-02-17 22:15:04 +00008013
Mon P Wang078a62d2008-05-05 19:05:59 +00008014 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008015 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8016 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00008017
Mon P Wang318b0372008-05-05 22:56:23 +00008018 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008019 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00008020 for (int i=0; i <= lastAddrIndx; ++i)
8021 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00008022
Mon P Wang078a62d2008-05-05 19:05:59 +00008023 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008024 assert((argOpers[valArgIndx]->isReg() ||
8025 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00008026 "invalid operand");
Scott Michel91099d62009-02-17 22:15:04 +00008027
8028 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008029 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008030 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michel91099d62009-02-17 22:15:04 +00008031 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008032 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00008033 (*MIB).addOperand(*argOpers[valArgIndx]);
8034
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008035 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00008036 MIB.addReg(t1);
8037
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008038 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00008039 MIB.addReg(t1);
8040 MIB.addReg(t2);
8041
8042 // Generate movc
8043 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008044 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00008045 MIB.addReg(t2);
8046 MIB.addReg(t1);
8047
8048 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008049 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00008050 for (int i=0; i <= lastAddrIndx; ++i)
8051 (*MIB).addOperand(*argOpers[i]);
8052 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00008053 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008054 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8055 mInstr->memoperands_end());
Scott Michel91099d62009-02-17 22:15:04 +00008056
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008057 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00008058 MIB.addReg(X86::EAX);
Scott Michel91099d62009-02-17 22:15:04 +00008059
Mon P Wang078a62d2008-05-05 19:05:59 +00008060 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008061 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00008062
Dan Gohman221a4372008-07-07 23:14:23 +00008063 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00008064 return nextMBB;
8065}
8066
Eric Christopher20391ca62009-08-27 18:08:16 +00008067// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8068// all of this code can be replaced with that in the .td file.
Dan Gohman34228bf2009-08-15 01:38:56 +00008069MachineBasicBlock *
Eric Christopher22a39402009-08-18 22:50:32 +00008070X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008071 unsigned numArgs, bool memArg) const {
Eric Christopher22a39402009-08-18 22:50:32 +00008072
8073 MachineFunction *F = BB->getParent();
8074 DebugLoc dl = MI->getDebugLoc();
8075 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8076
8077 unsigned Opc;
Evan Cheng5f3a5402009-09-19 09:51:03 +00008078 if (memArg)
8079 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8080 else
8081 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopher22a39402009-08-18 22:50:32 +00008082
8083 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8084
8085 for (unsigned i = 0; i < numArgs; ++i) {
8086 MachineOperand &Op = MI->getOperand(i+1);
8087
8088 if (!(Op.isReg() && Op.isImplicit()))
8089 MIB.addOperand(Op);
8090 }
8091
8092 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8093 .addReg(X86::XMM0);
8094
8095 F->DeleteMachineInstr(MI);
8096
8097 return BB;
8098}
8099
8100MachineBasicBlock *
Dan Gohman34228bf2009-08-15 01:38:56 +00008101X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8102 MachineInstr *MI,
8103 MachineBasicBlock *MBB) const {
8104 // Emit code to save XMM registers to the stack. The ABI says that the
8105 // number of registers to save is given in %al, so it's theoretically
8106 // possible to do an indirect jump trick to avoid saving all of them,
8107 // however this code takes a simpler approach and just executes all
8108 // of the stores if %al is non-zero. It's less code, and it's probably
8109 // easier on the hardware branch predictor, and stores aren't all that
8110 // expensive anyway.
8111
8112 // Create the new basic blocks. One block contains all the XMM stores,
8113 // and one block is the final destination regardless of whether any
8114 // stores were performed.
8115 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8116 MachineFunction *F = MBB->getParent();
8117 MachineFunction::iterator MBBIter = MBB;
8118 ++MBBIter;
8119 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8120 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8121 F->insert(MBBIter, XMMSaveMBB);
8122 F->insert(MBBIter, EndMBB);
8123
8124 // Set up the CFG.
8125 // Move any original successors of MBB to the end block.
8126 EndMBB->transferSuccessors(MBB);
8127 // The original block will now fall through to the XMM save block.
8128 MBB->addSuccessor(XMMSaveMBB);
8129 // The XMMSaveMBB will fall through to the end block.
8130 XMMSaveMBB->addSuccessor(EndMBB);
8131
8132 // Now add the instructions.
8133 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8134 DebugLoc DL = MI->getDebugLoc();
8135
8136 unsigned CountReg = MI->getOperand(0).getReg();
8137 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8138 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8139
8140 if (!Subtarget->isTargetWin64()) {
8141 // If %al is 0, branch around the XMM save block.
8142 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8143 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8144 MBB->addSuccessor(EndMBB);
8145 }
8146
8147 // In the XMM save block, save all the XMM argument registers.
8148 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8149 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008150 MachineMemOperand *MMO =
Evan Cheng174e2cf2009-10-18 18:16:27 +00008151 F->getMachineMemOperand(
8152 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8153 MachineMemOperand::MOStore, Offset,
8154 /*Size=*/16, /*Align=*/16);
Dan Gohman34228bf2009-08-15 01:38:56 +00008155 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8156 .addFrameIndex(RegSaveFrameIndex)
8157 .addImm(/*Scale=*/1)
8158 .addReg(/*IndexReg=*/0)
8159 .addImm(/*Disp=*/Offset)
8160 .addReg(/*Segment=*/0)
8161 .addReg(MI->getOperand(i).getReg())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008162 .addMemOperand(MMO);
Dan Gohman34228bf2009-08-15 01:38:56 +00008163 }
8164
8165 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8166
8167 return EndMBB;
8168}
Mon P Wang078a62d2008-05-05 19:05:59 +00008169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008170MachineBasicBlock *
Chris Lattner84a67202009-09-02 05:57:00 +00008171X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Cheng5f3a5402009-09-19 09:51:03 +00008172 MachineBasicBlock *BB,
8173 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner84a67202009-09-02 05:57:00 +00008174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8175 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008176
Chris Lattner84a67202009-09-02 05:57:00 +00008177 // To "insert" a SELECT_CC instruction, we actually have to insert the
8178 // diamond control-flow pattern. The incoming instruction knows the
8179 // destination vreg to set, the condition code register to branch on, the
8180 // true/false values to select between, and a branch opcode to use.
8181 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8182 MachineFunction::iterator It = BB;
8183 ++It;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008184
Chris Lattner84a67202009-09-02 05:57:00 +00008185 // thisMBB:
8186 // ...
8187 // TrueVal = ...
8188 // cmpTY ccX, r1, r2
8189 // bCC copy1MBB
8190 // fallthrough --> copy0MBB
8191 MachineBasicBlock *thisMBB = BB;
8192 MachineFunction *F = BB->getParent();
8193 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8194 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8195 unsigned Opc =
8196 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8197 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8198 F->insert(It, copy0MBB);
8199 F->insert(It, sinkMBB);
Evan Cheng5f3a5402009-09-19 09:51:03 +00008200 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner84a67202009-09-02 05:57:00 +00008201 // block to the new block which will contain the Phi node for the select.
Evan Cheng5f3a5402009-09-19 09:51:03 +00008202 // Also inform sdisel of the edge changes.
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008203 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Cheng5f3a5402009-09-19 09:51:03 +00008204 E = BB->succ_end(); I != E; ++I) {
8205 EM->insert(std::make_pair(*I, sinkMBB));
8206 sinkMBB->addSuccessor(*I);
8207 }
8208 // Next, remove all successors of the current block, and add the true
8209 // and fallthrough blocks as its successors.
8210 while (!BB->succ_empty())
8211 BB->removeSuccessor(BB->succ_begin());
Chris Lattner84a67202009-09-02 05:57:00 +00008212 // Add the true and fallthrough blocks as its successors.
8213 BB->addSuccessor(copy0MBB);
8214 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008215
Chris Lattner84a67202009-09-02 05:57:00 +00008216 // copy0MBB:
8217 // %FalseValue = ...
8218 // # fallthrough to sinkMBB
8219 BB = copy0MBB;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008220
Chris Lattner84a67202009-09-02 05:57:00 +00008221 // Update machine-CFG edges
8222 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008223
Chris Lattner84a67202009-09-02 05:57:00 +00008224 // sinkMBB:
8225 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8226 // ...
8227 BB = sinkMBB;
8228 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8229 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8230 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8231
8232 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8233 return BB;
8234}
8235
8236
8237MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00008238X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengd7dc9832009-09-18 21:02:19 +00008239 MachineBasicBlock *BB,
8240 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008241 switch (MI->getOpcode()) {
8242 default: assert(false && "Unexpected instr type to insert");
Dan Gohman29b998f2009-08-27 00:14:12 +00008243 case X86::CMOV_GR8:
Mon P Wang83edba52008-12-12 01:25:51 +00008244 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008245 case X86::CMOV_FR32:
8246 case X86::CMOV_FR64:
8247 case X86::CMOV_V4F32:
8248 case X86::CMOV_V2F64:
Chris Lattner84a67202009-09-02 05:57:00 +00008249 case X86::CMOV_V2I64:
Evan Cheng5f3a5402009-09-19 09:51:03 +00008250 return EmitLoweredSelect(MI, BB, EM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008251
8252 case X86::FP32_TO_INT16_IN_MEM:
8253 case X86::FP32_TO_INT32_IN_MEM:
8254 case X86::FP32_TO_INT64_IN_MEM:
8255 case X86::FP64_TO_INT16_IN_MEM:
8256 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008257 case X86::FP64_TO_INT64_IN_MEM:
8258 case X86::FP80_TO_INT16_IN_MEM:
8259 case X86::FP80_TO_INT32_IN_MEM:
8260 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner84a67202009-09-02 05:57:00 +00008261 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8262 DebugLoc DL = MI->getDebugLoc();
8263
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008264 // Change the floating point control register to use "round towards zero"
8265 // mode when truncating to an integer value.
8266 MachineFunction *F = BB->getParent();
David Greene6424ab92009-11-12 20:49:22 +00008267 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner84a67202009-09-02 05:57:00 +00008268 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008269
8270 // Load the old value of the high byte of the control word...
8271 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00008272 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner84a67202009-09-02 05:57:00 +00008273 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008274 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008275
8276 // Set the high part to be round to zero...
Chris Lattner84a67202009-09-02 05:57:00 +00008277 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008278 .addImm(0xC7F);
8279
8280 // Reload the modified control word now...
Chris Lattner84a67202009-09-02 05:57:00 +00008281 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008282
8283 // Restore the memory image of control word to original value
Chris Lattner84a67202009-09-02 05:57:00 +00008284 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008285 .addReg(OldCW);
8286
8287 // Get the X86 opcode to use.
8288 unsigned Opc;
8289 switch (MI->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00008290 default: llvm_unreachable("illegal opcode!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008291 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8292 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8293 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8294 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8295 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8296 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008297 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8298 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8299 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008300 }
8301
8302 X86AddressMode AM;
8303 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008304 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008305 AM.BaseType = X86AddressMode::RegBase;
8306 AM.Base.Reg = Op.getReg();
8307 } else {
8308 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00008309 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008310 }
8311 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008312 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008313 AM.Scale = Op.getImm();
8314 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008315 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008316 AM.IndexReg = Op.getImm();
8317 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008318 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008319 AM.GV = Op.getGlobal();
8320 } else {
8321 AM.Disp = Op.getImm();
8322 }
Chris Lattner84a67202009-09-02 05:57:00 +00008323 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindolafee9c0f2009-04-08 08:09:33 +00008324 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008325
8326 // Reload the original control word now.
Chris Lattner84a67202009-09-02 05:57:00 +00008327 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008328
Dan Gohman221a4372008-07-07 23:14:23 +00008329 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008330 return BB;
8331 }
Eric Christopher22a39402009-08-18 22:50:32 +00008332 // String/text processing lowering.
8333 case X86::PCMPISTRM128REG:
8334 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8335 case X86::PCMPISTRM128MEM:
8336 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8337 case X86::PCMPESTRM128REG:
8338 return EmitPCMP(MI, BB, 5, false /* in mem */);
8339 case X86::PCMPESTRM128MEM:
8340 return EmitPCMP(MI, BB, 5, true /* in mem */);
8341
8342 // Atomic Lowering.
Mon P Wang078a62d2008-05-05 19:05:59 +00008343 case X86::ATOMAND32:
8344 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008345 X86::AND32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008346 X86::LCMPXCHG32, X86::MOV32rr,
8347 X86::NOT32r, X86::EAX,
8348 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008349 case X86::ATOMOR32:
Scott Michel91099d62009-02-17 22:15:04 +00008350 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8351 X86::OR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008352 X86::LCMPXCHG32, X86::MOV32rr,
8353 X86::NOT32r, X86::EAX,
8354 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008355 case X86::ATOMXOR32:
8356 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008357 X86::XOR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008358 X86::LCMPXCHG32, X86::MOV32rr,
8359 X86::NOT32r, X86::EAX,
8360 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008361 case X86::ATOMNAND32:
8362 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008363 X86::AND32ri, X86::MOV32rm,
8364 X86::LCMPXCHG32, X86::MOV32rr,
8365 X86::NOT32r, X86::EAX,
8366 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00008367 case X86::ATOMMIN32:
8368 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8369 case X86::ATOMMAX32:
8370 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8371 case X86::ATOMUMIN32:
8372 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8373 case X86::ATOMUMAX32:
8374 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00008375
8376 case X86::ATOMAND16:
8377 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8378 X86::AND16ri, X86::MOV16rm,
8379 X86::LCMPXCHG16, X86::MOV16rr,
8380 X86::NOT16r, X86::AX,
8381 X86::GR16RegisterClass);
8382 case X86::ATOMOR16:
Scott Michel91099d62009-02-17 22:15:04 +00008383 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008384 X86::OR16ri, X86::MOV16rm,
8385 X86::LCMPXCHG16, X86::MOV16rr,
8386 X86::NOT16r, X86::AX,
8387 X86::GR16RegisterClass);
8388 case X86::ATOMXOR16:
8389 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8390 X86::XOR16ri, X86::MOV16rm,
8391 X86::LCMPXCHG16, X86::MOV16rr,
8392 X86::NOT16r, X86::AX,
8393 X86::GR16RegisterClass);
8394 case X86::ATOMNAND16:
8395 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8396 X86::AND16ri, X86::MOV16rm,
8397 X86::LCMPXCHG16, X86::MOV16rr,
8398 X86::NOT16r, X86::AX,
8399 X86::GR16RegisterClass, true);
8400 case X86::ATOMMIN16:
8401 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8402 case X86::ATOMMAX16:
8403 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8404 case X86::ATOMUMIN16:
8405 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8406 case X86::ATOMUMAX16:
8407 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8408
8409 case X86::ATOMAND8:
8410 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8411 X86::AND8ri, X86::MOV8rm,
8412 X86::LCMPXCHG8, X86::MOV8rr,
8413 X86::NOT8r, X86::AL,
8414 X86::GR8RegisterClass);
8415 case X86::ATOMOR8:
Scott Michel91099d62009-02-17 22:15:04 +00008416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008417 X86::OR8ri, X86::MOV8rm,
8418 X86::LCMPXCHG8, X86::MOV8rr,
8419 X86::NOT8r, X86::AL,
8420 X86::GR8RegisterClass);
8421 case X86::ATOMXOR8:
8422 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8423 X86::XOR8ri, X86::MOV8rm,
8424 X86::LCMPXCHG8, X86::MOV8rr,
8425 X86::NOT8r, X86::AL,
8426 X86::GR8RegisterClass);
8427 case X86::ATOMNAND8:
8428 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8429 X86::AND8ri, X86::MOV8rm,
8430 X86::LCMPXCHG8, X86::MOV8rr,
8431 X86::NOT8r, X86::AL,
8432 X86::GR8RegisterClass, true);
8433 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00008434 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008435 case X86::ATOMAND64:
8436 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008437 X86::AND64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008438 X86::LCMPXCHG64, X86::MOV64rr,
8439 X86::NOT64r, X86::RAX,
8440 X86::GR64RegisterClass);
8441 case X86::ATOMOR64:
Scott Michel91099d62009-02-17 22:15:04 +00008442 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8443 X86::OR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008444 X86::LCMPXCHG64, X86::MOV64rr,
8445 X86::NOT64r, X86::RAX,
8446 X86::GR64RegisterClass);
8447 case X86::ATOMXOR64:
8448 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008449 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008450 X86::LCMPXCHG64, X86::MOV64rr,
8451 X86::NOT64r, X86::RAX,
8452 X86::GR64RegisterClass);
8453 case X86::ATOMNAND64:
8454 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8455 X86::AND64ri32, X86::MOV64rm,
8456 X86::LCMPXCHG64, X86::MOV64rr,
8457 X86::NOT64r, X86::RAX,
8458 X86::GR64RegisterClass, true);
8459 case X86::ATOMMIN64:
8460 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8461 case X86::ATOMMAX64:
8462 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8463 case X86::ATOMUMIN64:
8464 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8465 case X86::ATOMUMAX64:
8466 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00008467
8468 // This group does 64-bit operations on a 32-bit host.
8469 case X86::ATOMAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008470 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008471 X86::AND32rr, X86::AND32rr,
8472 X86::AND32ri, X86::AND32ri,
8473 false);
8474 case X86::ATOMOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008475 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008476 X86::OR32rr, X86::OR32rr,
8477 X86::OR32ri, X86::OR32ri,
8478 false);
8479 case X86::ATOMXOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008480 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008481 X86::XOR32rr, X86::XOR32rr,
8482 X86::XOR32ri, X86::XOR32ri,
8483 false);
8484 case X86::ATOMNAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008485 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008486 X86::AND32rr, X86::AND32rr,
8487 X86::AND32ri, X86::AND32ri,
8488 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00008489 case X86::ATOMADD6432:
Scott Michel91099d62009-02-17 22:15:04 +00008490 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008491 X86::ADD32rr, X86::ADC32rr,
8492 X86::ADD32ri, X86::ADC32ri,
8493 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00008494 case X86::ATOMSUB6432:
Scott Michel91099d62009-02-17 22:15:04 +00008495 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008496 X86::SUB32rr, X86::SBB32rr,
8497 X86::SUB32ri, X86::SBB32ri,
8498 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008499 case X86::ATOMSWAP6432:
Scott Michel91099d62009-02-17 22:15:04 +00008500 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008501 X86::MOV32rr, X86::MOV32rr,
8502 X86::MOV32ri, X86::MOV32ri,
8503 false);
Dan Gohman34228bf2009-08-15 01:38:56 +00008504 case X86::VASTART_SAVE_XMM_REGS:
8505 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008506 }
8507}
8508
8509//===----------------------------------------------------------------------===//
8510// X86 Optimization Hooks
8511//===----------------------------------------------------------------------===//
8512
Dan Gohman8181bd12008-07-27 21:46:04 +00008513void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00008514 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00008515 APInt &KnownZero,
8516 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008517 const SelectionDAG &DAG,
8518 unsigned Depth) const {
8519 unsigned Opc = Op.getOpcode();
8520 assert((Opc >= ISD::BUILTIN_OP_END ||
8521 Opc == ISD::INTRINSIC_WO_CHAIN ||
8522 Opc == ISD::INTRINSIC_W_CHAIN ||
8523 Opc == ISD::INTRINSIC_VOID) &&
8524 "Should use MaskedValueIsZero if you don't know whether Op"
8525 " is a target node!");
8526
Dan Gohman1d79e432008-02-13 23:07:24 +00008527 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008528 switch (Opc) {
8529 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008530 case X86ISD::ADD:
8531 case X86ISD::SUB:
8532 case X86ISD::SMUL:
8533 case X86ISD::UMUL:
Dan Gohman99a12192009-03-04 19:44:21 +00008534 case X86ISD::INC:
8535 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00008536 case X86ISD::OR:
8537 case X86ISD::XOR:
8538 case X86ISD::AND:
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008539 // These nodes' second result is a boolean.
8540 if (Op.getResNo() == 0)
8541 break;
8542 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008543 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00008544 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8545 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008546 break;
8547 }
8548}
8549
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008550/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00008551/// node is a GlobalAddress + offset.
8552bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8553 GlobalValue* &GA, int64_t &Offset) const{
8554 if (N->getOpcode() == X86ISD::Wrapper) {
8555 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008556 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00008557 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008558 return true;
8559 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008560 }
Evan Chengef7be082008-05-12 19:56:52 +00008561 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008562}
8563
Nate Begeman543d2142009-04-27 18:41:29 +00008564static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman3bab1f72009-09-23 21:02:20 +00008565 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008566 unsigned &LastLoadedElt,
Evan Chengef7be082008-05-12 19:56:52 +00008567 SelectionDAG &DAG, MachineFrameInfo *MFI,
8568 const TargetLowering &TLI) {
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008569 LDBase = NULL;
Anton Korobeynikova99a2862009-06-09 23:00:39 +00008570 LastLoadedElt = -1U;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008571 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00008572 if (N->getMaskElt(i) < 0) {
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008573 if (!LDBase)
Evan Cheng40ee6e52008-05-08 00:57:18 +00008574 return false;
8575 continue;
8576 }
8577
Dan Gohman8181bd12008-07-27 21:46:04 +00008578 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00008579 if (!Elt.getNode() ||
8580 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00008581 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008582 if (!LDBase) {
8583 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng92ee6822008-05-10 06:46:49 +00008584 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008585 LDBase = cast<LoadSDNode>(Elt.getNode());
8586 LastLoadedElt = i;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008587 continue;
8588 }
8589 if (Elt.getOpcode() == ISD::UNDEF)
8590 continue;
8591
Nate Begeman65e80032009-06-05 21:37:30 +00008592 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng1a029cb2009-12-09 01:36:00 +00008593 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng40ee6e52008-05-08 00:57:18 +00008594 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008595 LastLoadedElt = i;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008596 }
8597 return true;
8598}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008599
8600/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8601/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8602/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang6e30ad02009-04-03 02:43:30 +00008603/// order. In the case of v2i64, it will see if it can rewrite the
8604/// shuffle to be an appropriate build vector so it can take advantage of
8605// performBuildVectorCombine.
Dan Gohman8181bd12008-07-27 21:46:04 +00008606static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00008607 const TargetLowering &TLI) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008608 DebugLoc dl = N->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00008609 EVT VT = N->getValueType(0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00008610 EVT EltVT = VT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00008611 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8612 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang6e30ad02009-04-03 02:43:30 +00008613
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008614 if (VT.getSizeInBits() != 128)
8615 return SDValue();
8616
Mon P Wang6e30ad02009-04-03 02:43:30 +00008617 // Try to combine a vector_shuffle into a 128-bit load.
8618 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008619 LoadSDNode *LD = NULL;
8620 unsigned LastLoadedElt;
Dan Gohman3bab1f72009-09-23 21:02:20 +00008621 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008622 MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00008623 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008624
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008625 if (LastLoadedElt == NumElems - 1) {
Evan Cheng76ebe862009-12-09 01:53:58 +00008626 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008627 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8628 LD->getSrcValue(), LD->getSrcValueOffset(),
8629 LD->isVolatile());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008630 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michel91099d62009-02-17 22:15:04 +00008631 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008632 LD->isVolatile(), LD->getAlignment());
8633 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008634 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begeman65e80032009-06-05 21:37:30 +00008635 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8636 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begeman65e80032009-06-05 21:37:30 +00008637 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8638 }
8639 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00008640}
Evan Chenge9b9c672008-05-09 21:53:03 +00008641
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008642/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008643static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner472f1d52009-03-11 05:48:52 +00008644 const X86Subtarget *Subtarget) {
8645 DebugLoc DL = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00008646 SDValue Cond = N->getOperand(0);
Chris Lattner472f1d52009-03-11 05:48:52 +00008647 // Get the LHS/RHS of the select.
8648 SDValue LHS = N->getOperand(1);
8649 SDValue RHS = N->getOperand(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008650
Dan Gohman19488552009-09-21 18:03:22 +00008651 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8652 // instructions have the peculiarity that if either operand is a NaN,
8653 // they chose what we call the RHS operand (and as such are not symmetric).
8654 // It happens that this matches the semantics of the common C idiom
8655 // x<y?x:y and related forms, so we can recognize these cases.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008656 if (Subtarget->hasSSE2() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008657 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner472f1d52009-03-11 05:48:52 +00008658 Cond.getOpcode() == ISD::SETCC) {
8659 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008660
Chris Lattner472f1d52009-03-11 05:48:52 +00008661 unsigned Opcode = 0;
Dan Gohman19488552009-09-21 18:03:22 +00008662 // Check for x CC y ? x : y.
Chris Lattner472f1d52009-03-11 05:48:52 +00008663 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8664 switch (CC) {
8665 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00008666 case ISD::SETULT:
8667 // This can be a min if we can prove that at least one of the operands
8668 // is not a nan.
8669 if (!FiniteOnlyFPMath()) {
8670 if (DAG.isKnownNeverNaN(RHS)) {
8671 // Put the potential NaN in the RHS so that SSE will preserve it.
8672 std::swap(LHS, RHS);
8673 } else if (!DAG.isKnownNeverNaN(LHS))
8674 break;
8675 }
8676 Opcode = X86ISD::FMIN;
8677 break;
8678 case ISD::SETOLE:
8679 // This can be a min if we can prove that at least one of the operands
8680 // is not a nan.
8681 if (!FiniteOnlyFPMath()) {
8682 if (DAG.isKnownNeverNaN(LHS)) {
8683 // Put the potential NaN in the RHS so that SSE will preserve it.
8684 std::swap(LHS, RHS);
8685 } else if (!DAG.isKnownNeverNaN(RHS))
8686 break;
8687 }
8688 Opcode = X86ISD::FMIN;
8689 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00008690 case ISD::SETULE:
Dan Gohman19488552009-09-21 18:03:22 +00008691 // This can be a min, but if either operand is a NaN we need it to
8692 // preserve the original LHS.
8693 std::swap(LHS, RHS);
8694 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008695 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00008696 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00008697 Opcode = X86ISD::FMIN;
8698 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008699
Dan Gohman19488552009-09-21 18:03:22 +00008700 case ISD::SETOGE:
8701 // This can be a max if we can prove that at least one of the operands
8702 // is not a nan.
8703 if (!FiniteOnlyFPMath()) {
8704 if (DAG.isKnownNeverNaN(LHS)) {
8705 // Put the potential NaN in the RHS so that SSE will preserve it.
8706 std::swap(LHS, RHS);
8707 } else if (!DAG.isKnownNeverNaN(RHS))
8708 break;
8709 }
8710 Opcode = X86ISD::FMAX;
8711 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00008712 case ISD::SETUGT:
Dan Gohman19488552009-09-21 18:03:22 +00008713 // This can be a max if we can prove that at least one of the operands
8714 // is not a nan.
8715 if (!FiniteOnlyFPMath()) {
8716 if (DAG.isKnownNeverNaN(RHS)) {
8717 // Put the potential NaN in the RHS so that SSE will preserve it.
8718 std::swap(LHS, RHS);
8719 } else if (!DAG.isKnownNeverNaN(LHS))
8720 break;
8721 }
8722 Opcode = X86ISD::FMAX;
8723 break;
8724 case ISD::SETUGE:
8725 // This can be a max, but if either operand is a NaN we need it to
8726 // preserve the original LHS.
8727 std::swap(LHS, RHS);
8728 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008729 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008730 case ISD::SETGE:
8731 Opcode = X86ISD::FMAX;
8732 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008733 }
Dan Gohman19488552009-09-21 18:03:22 +00008734 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner472f1d52009-03-11 05:48:52 +00008735 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8736 switch (CC) {
8737 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00008738 case ISD::SETOGE:
8739 // This can be a min if we can prove that at least one of the operands
8740 // is not a nan.
8741 if (!FiniteOnlyFPMath()) {
8742 if (DAG.isKnownNeverNaN(RHS)) {
8743 // Put the potential NaN in the RHS so that SSE will preserve it.
8744 std::swap(LHS, RHS);
8745 } else if (!DAG.isKnownNeverNaN(LHS))
8746 break;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008747 }
Dan Gohman19488552009-09-21 18:03:22 +00008748 Opcode = X86ISD::FMIN;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008749 break;
Dan Gohman19488552009-09-21 18:03:22 +00008750 case ISD::SETUGT:
8751 // This can be a min if we can prove that at least one of the operands
8752 // is not a nan.
8753 if (!FiniteOnlyFPMath()) {
8754 if (DAG.isKnownNeverNaN(LHS)) {
8755 // Put the potential NaN in the RHS so that SSE will preserve it.
8756 std::swap(LHS, RHS);
8757 } else if (!DAG.isKnownNeverNaN(RHS))
8758 break;
8759 }
8760 Opcode = X86ISD::FMIN;
8761 break;
8762 case ISD::SETUGE:
8763 // This can be a min, but if either operand is a NaN we need it to
8764 // preserve the original LHS.
8765 std::swap(LHS, RHS);
8766 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008767 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008768 case ISD::SETGE:
8769 Opcode = X86ISD::FMIN;
8770 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008771
Dan Gohman19488552009-09-21 18:03:22 +00008772 case ISD::SETULT:
8773 // This can be a max if we can prove that at least one of the operands
8774 // is not a nan.
8775 if (!FiniteOnlyFPMath()) {
8776 if (DAG.isKnownNeverNaN(LHS)) {
8777 // Put the potential NaN in the RHS so that SSE will preserve it.
8778 std::swap(LHS, RHS);
8779 } else if (!DAG.isKnownNeverNaN(RHS))
8780 break;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008781 }
Dan Gohman19488552009-09-21 18:03:22 +00008782 Opcode = X86ISD::FMAX;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008783 break;
Dan Gohman19488552009-09-21 18:03:22 +00008784 case ISD::SETOLE:
8785 // This can be a max if we can prove that at least one of the operands
8786 // is not a nan.
8787 if (!FiniteOnlyFPMath()) {
8788 if (DAG.isKnownNeverNaN(RHS)) {
8789 // Put the potential NaN in the RHS so that SSE will preserve it.
8790 std::swap(LHS, RHS);
8791 } else if (!DAG.isKnownNeverNaN(LHS))
8792 break;
8793 }
8794 Opcode = X86ISD::FMAX;
8795 break;
8796 case ISD::SETULE:
8797 // This can be a max, but if either operand is a NaN we need it to
8798 // preserve the original LHS.
8799 std::swap(LHS, RHS);
8800 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008801 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00008802 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00008803 Opcode = X86ISD::FMAX;
8804 break;
8805 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008806 }
8807
Chris Lattner472f1d52009-03-11 05:48:52 +00008808 if (Opcode)
8809 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008810 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008811
Chris Lattnere4577dc2009-03-12 06:52:53 +00008812 // If this is a select between two integer constants, try to do some
8813 // optimizations.
Chris Lattnera054e842009-03-13 05:53:31 +00008814 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8815 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnere4577dc2009-03-12 06:52:53 +00008816 // Don't do this for crazy integer types.
8817 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8818 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnera054e842009-03-13 05:53:31 +00008819 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnere4577dc2009-03-12 06:52:53 +00008820 bool NeedsCondInvert = false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008821
Chris Lattnera054e842009-03-13 05:53:31 +00008822 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnere4577dc2009-03-12 06:52:53 +00008823 // Efficiently invertible.
8824 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8825 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8826 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8827 NeedsCondInvert = true;
Chris Lattnera054e842009-03-13 05:53:31 +00008828 std::swap(TrueC, FalseC);
Chris Lattnere4577dc2009-03-12 06:52:53 +00008829 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008830
Chris Lattnere4577dc2009-03-12 06:52:53 +00008831 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00008832 if (FalseC->getAPIntValue() == 0 &&
8833 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnere4577dc2009-03-12 06:52:53 +00008834 if (NeedsCondInvert) // Invert the condition if needed.
8835 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8836 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008837
Chris Lattnere4577dc2009-03-12 06:52:53 +00008838 // Zero extend the condition if needed.
8839 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008840
Chris Lattnera054e842009-03-13 05:53:31 +00008841 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnere4577dc2009-03-12 06:52:53 +00008842 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008843 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00008844 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008845
Chris Lattner938d6652009-03-13 05:22:11 +00008846 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnera054e842009-03-13 05:53:31 +00008847 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner938d6652009-03-13 05:22:11 +00008848 if (NeedsCondInvert) // Invert the condition if needed.
8849 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8850 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008851
Chris Lattner938d6652009-03-13 05:22:11 +00008852 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00008853 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8854 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00008855 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnera054e842009-03-13 05:53:31 +00008856 SDValue(FalseC, 0));
Chris Lattner938d6652009-03-13 05:22:11 +00008857 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008858
Chris Lattnera054e842009-03-13 05:53:31 +00008859 // Optimize cases that will turn into an LEA instruction. This requires
8860 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008861 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00008862 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008863 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008864
Chris Lattnera054e842009-03-13 05:53:31 +00008865 bool isFastMultiplier = false;
8866 if (Diff < 10) {
8867 switch ((unsigned char)Diff) {
8868 default: break;
8869 case 1: // result = add base, cond
8870 case 2: // result = lea base( , cond*2)
8871 case 3: // result = lea base(cond, cond*2)
8872 case 4: // result = lea base( , cond*4)
8873 case 5: // result = lea base(cond, cond*4)
8874 case 8: // result = lea base( , cond*8)
8875 case 9: // result = lea base(cond, cond*8)
8876 isFastMultiplier = true;
8877 break;
8878 }
8879 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008880
Chris Lattnera054e842009-03-13 05:53:31 +00008881 if (isFastMultiplier) {
8882 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8883 if (NeedsCondInvert) // Invert the condition if needed.
8884 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8885 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008886
Chris Lattnera054e842009-03-13 05:53:31 +00008887 // Zero extend the condition if needed.
8888 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8889 Cond);
8890 // Scale the condition by the difference.
8891 if (Diff != 1)
8892 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8893 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008894
Chris Lattnera054e842009-03-13 05:53:31 +00008895 // Add the base if non-zero.
8896 if (FalseC->getAPIntValue() != 0)
8897 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8898 SDValue(FalseC, 0));
8899 return Cond;
8900 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008901 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00008902 }
8903 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008904
Dan Gohman8181bd12008-07-27 21:46:04 +00008905 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008906}
8907
Chris Lattnere4577dc2009-03-12 06:52:53 +00008908/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8909static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8910 TargetLowering::DAGCombinerInfo &DCI) {
8911 DebugLoc DL = N->getDebugLoc();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008912
Chris Lattnere4577dc2009-03-12 06:52:53 +00008913 // If the flag operand isn't dead, don't touch this CMOV.
8914 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8915 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008916
Chris Lattnere4577dc2009-03-12 06:52:53 +00008917 // If this is a select between two integer constants, try to do some
8918 // optimizations. Note that the operands are ordered the opposite of SELECT
8919 // operands.
8920 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8921 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8922 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8923 // larger than FalseC (the false value).
8924 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008925
Chris Lattnere4577dc2009-03-12 06:52:53 +00008926 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8927 CC = X86::GetOppositeBranchCondition(CC);
8928 std::swap(TrueC, FalseC);
8929 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008930
Chris Lattnere4577dc2009-03-12 06:52:53 +00008931 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00008932 // This is efficient for any integer data type (including i8/i16) and
8933 // shift amount.
Chris Lattnere4577dc2009-03-12 06:52:53 +00008934 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8935 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008936 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8937 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008938
Chris Lattnere4577dc2009-03-12 06:52:53 +00008939 // Zero extend the condition if needed.
8940 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008941
Chris Lattnere4577dc2009-03-12 06:52:53 +00008942 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8943 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008944 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00008945 if (N->getNumValues() == 2) // Dead flag value?
8946 return DCI.CombineTo(N, Cond, SDValue());
8947 return Cond;
8948 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008949
Chris Lattnera054e842009-03-13 05:53:31 +00008950 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8951 // for any integer data type, including i8/i16.
Chris Lattner938d6652009-03-13 05:22:11 +00008952 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8953 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008954 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8955 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008956
Chris Lattner938d6652009-03-13 05:22:11 +00008957 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00008958 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8959 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00008960 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8961 SDValue(FalseC, 0));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008962
Chris Lattner938d6652009-03-13 05:22:11 +00008963 if (N->getNumValues() == 2) // Dead flag value?
8964 return DCI.CombineTo(N, Cond, SDValue());
8965 return Cond;
8966 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008967
Chris Lattnera054e842009-03-13 05:53:31 +00008968 // Optimize cases that will turn into an LEA instruction. This requires
8969 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008970 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00008971 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008972 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008973
Chris Lattnera054e842009-03-13 05:53:31 +00008974 bool isFastMultiplier = false;
8975 if (Diff < 10) {
8976 switch ((unsigned char)Diff) {
8977 default: break;
8978 case 1: // result = add base, cond
8979 case 2: // result = lea base( , cond*2)
8980 case 3: // result = lea base(cond, cond*2)
8981 case 4: // result = lea base( , cond*4)
8982 case 5: // result = lea base(cond, cond*4)
8983 case 8: // result = lea base( , cond*8)
8984 case 9: // result = lea base(cond, cond*8)
8985 isFastMultiplier = true;
8986 break;
8987 }
8988 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008989
Chris Lattnera054e842009-03-13 05:53:31 +00008990 if (isFastMultiplier) {
8991 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8992 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008993 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8994 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnera054e842009-03-13 05:53:31 +00008995 // Zero extend the condition if needed.
8996 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8997 Cond);
8998 // Scale the condition by the difference.
8999 if (Diff != 1)
9000 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9001 DAG.getConstant(Diff, Cond.getValueType()));
9002
9003 // Add the base if non-zero.
9004 if (FalseC->getAPIntValue() != 0)
9005 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9006 SDValue(FalseC, 0));
9007 if (N->getNumValues() == 2) // Dead flag value?
9008 return DCI.CombineTo(N, Cond, SDValue());
9009 return Cond;
9010 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009011 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00009012 }
9013 }
9014 return SDValue();
9015}
9016
9017
Evan Cheng04ecee12009-03-28 05:57:29 +00009018/// PerformMulCombine - Optimize a single multiply with constant into two
9019/// in order to implement it with two cheaper instructions, e.g.
9020/// LEA + SHL, LEA + LEA.
9021static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9022 TargetLowering::DAGCombinerInfo &DCI) {
9023 if (DAG.getMachineFunction().
9024 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9025 return SDValue();
9026
9027 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9028 return SDValue();
9029
Owen Andersonac9de032009-08-10 22:56:29 +00009030 EVT VT = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009031 if (VT != MVT::i64)
Evan Cheng04ecee12009-03-28 05:57:29 +00009032 return SDValue();
9033
9034 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9035 if (!C)
9036 return SDValue();
9037 uint64_t MulAmt = C->getZExtValue();
9038 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9039 return SDValue();
9040
9041 uint64_t MulAmt1 = 0;
9042 uint64_t MulAmt2 = 0;
9043 if ((MulAmt % 9) == 0) {
9044 MulAmt1 = 9;
9045 MulAmt2 = MulAmt / 9;
9046 } else if ((MulAmt % 5) == 0) {
9047 MulAmt1 = 5;
9048 MulAmt2 = MulAmt / 5;
9049 } else if ((MulAmt % 3) == 0) {
9050 MulAmt1 = 3;
9051 MulAmt2 = MulAmt / 3;
9052 }
9053 if (MulAmt2 &&
9054 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9055 DebugLoc DL = N->getDebugLoc();
9056
9057 if (isPowerOf2_64(MulAmt2) &&
9058 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9059 // If second multiplifer is pow2, issue it first. We want the multiply by
9060 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9061 // is an add.
9062 std::swap(MulAmt1, MulAmt2);
9063
9064 SDValue NewMul;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009065 if (isPowerOf2_64(MulAmt1))
Evan Cheng04ecee12009-03-28 05:57:29 +00009066 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009067 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng04ecee12009-03-28 05:57:29 +00009068 else
Evan Chengc3495762009-03-30 21:36:47 +00009069 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng04ecee12009-03-28 05:57:29 +00009070 DAG.getConstant(MulAmt1, VT));
9071
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009072 if (isPowerOf2_64(MulAmt2))
Evan Cheng04ecee12009-03-28 05:57:29 +00009073 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009074 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009075 else
Evan Chengc3495762009-03-30 21:36:47 +00009076 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng04ecee12009-03-28 05:57:29 +00009077 DAG.getConstant(MulAmt2, VT));
9078
9079 // Do not add new nodes to DAG combiner worklist.
9080 DCI.CombineTo(N, NewMul, false);
9081 }
9082 return SDValue();
9083}
9084
Evan Cheng834ae6b2009-12-15 00:53:42 +00009085static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9086 SDValue N0 = N->getOperand(0);
9087 SDValue N1 = N->getOperand(1);
9088 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9089 EVT VT = N0.getValueType();
9090
9091 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9092 // since the result of setcc_c is all zero's or all ones.
9093 if (N1C && N0.getOpcode() == ISD::AND &&
9094 N0.getOperand(1).getOpcode() == ISD::Constant) {
9095 SDValue N00 = N0.getOperand(0);
9096 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9097 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9098 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9099 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9100 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9101 APInt ShAmt = N1C->getAPIntValue();
9102 Mask = Mask.shl(ShAmt);
9103 if (Mask != 0)
9104 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9105 N00, DAG.getConstant(Mask, VT));
9106 }
9107 }
9108
9109 return SDValue();
9110}
Evan Cheng04ecee12009-03-28 05:57:29 +00009111
sampo025b75c2009-01-26 00:52:55 +00009112/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9113/// when possible.
9114static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9115 const X86Subtarget *Subtarget) {
Evan Cheng834ae6b2009-12-15 00:53:42 +00009116 EVT VT = N->getValueType(0);
9117 if (!VT.isVector() && VT.isInteger() &&
9118 N->getOpcode() == ISD::SHL)
9119 return PerformSHLCombine(N, DAG);
9120
sampo025b75c2009-01-26 00:52:55 +00009121 // On X86 with SSE2 support, we can transform this to a vector shift if
9122 // all elements are shifted by the same amount. We can't do this in legalize
9123 // because the a constant vector is typically transformed to a constant pool
9124 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00009125 if (!Subtarget->hasSSE2())
9126 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009127
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009128 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
sampo087d53c2009-01-26 03:15:31 +00009129 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009130
Mon P Wanga91e9642009-01-28 08:12:05 +00009131 SDValue ShAmtOp = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00009132 EVT EltVT = VT.getVectorElementType();
Chris Lattner472f1d52009-03-11 05:48:52 +00009133 DebugLoc DL = N->getDebugLoc();
Mon P Wang04c767e2009-09-03 19:56:25 +00009134 SDValue BaseShAmt = SDValue();
Mon P Wanga91e9642009-01-28 08:12:05 +00009135 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9136 unsigned NumElts = VT.getVectorNumElements();
9137 unsigned i = 0;
9138 for (; i != NumElts; ++i) {
9139 SDValue Arg = ShAmtOp.getOperand(i);
9140 if (Arg.getOpcode() == ISD::UNDEF) continue;
9141 BaseShAmt = Arg;
9142 break;
9143 }
9144 for (; i != NumElts; ++i) {
9145 SDValue Arg = ShAmtOp.getOperand(i);
9146 if (Arg.getOpcode() == ISD::UNDEF) continue;
9147 if (Arg != BaseShAmt) {
9148 return SDValue();
9149 }
9150 }
9151 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman543d2142009-04-27 18:41:29 +00009152 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wang04c767e2009-09-03 19:56:25 +00009153 SDValue InVec = ShAmtOp.getOperand(0);
9154 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9155 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9156 unsigned i = 0;
9157 for (; i != NumElts; ++i) {
9158 SDValue Arg = InVec.getOperand(i);
9159 if (Arg.getOpcode() == ISD::UNDEF) continue;
9160 BaseShAmt = Arg;
9161 break;
9162 }
9163 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9164 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9165 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9166 if (C->getZExtValue() == SplatIdx)
9167 BaseShAmt = InVec.getOperand(1);
9168 }
9169 }
9170 if (BaseShAmt.getNode() == 0)
9171 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9172 DAG.getIntPtrConstant(0));
Mon P Wanga91e9642009-01-28 08:12:05 +00009173 } else
sampo087d53c2009-01-26 03:15:31 +00009174 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00009175
Mon P Wang04c767e2009-09-03 19:56:25 +00009176 // The shift amount is an i32.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009177 if (EltVT.bitsGT(MVT::i32))
9178 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9179 else if (EltVT.bitsLT(MVT::i32))
Mon P Wang04c767e2009-09-03 19:56:25 +00009180 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00009181
sampo087d53c2009-01-26 03:15:31 +00009182 // The shift amount is identical so we can do a vector shift.
9183 SDValue ValOp = N->getOperand(0);
9184 switch (N->getOpcode()) {
9185 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00009186 llvm_unreachable("Unknown shift opcode!");
sampo087d53c2009-01-26 03:15:31 +00009187 break;
9188 case ISD::SHL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009189 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009190 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009191 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009192 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009193 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009194 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009195 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009196 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009197 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009198 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009199 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009200 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009201 break;
9202 case ISD::SRA:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009203 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009204 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009205 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009206 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009207 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009208 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009209 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009210 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009211 break;
9212 case ISD::SRL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009213 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009215 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009216 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009217 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009218 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009219 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009220 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009221 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009222 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009223 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009224 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009225 break;
sampo025b75c2009-01-26 00:52:55 +00009226 }
9227 return SDValue();
9228}
9229
Evan Cheng10957b82010-01-04 21:22:48 +00009230static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9231 const X86Subtarget *Subtarget) {
9232 EVT VT = N->getValueType(0);
9233 if (VT != MVT::i64 || !Subtarget->is64Bit())
9234 return SDValue();
9235
9236 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9237 SDValue N0 = N->getOperand(0);
9238 SDValue N1 = N->getOperand(1);
9239 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9240 std::swap(N0, N1);
9241 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9242 return SDValue();
9243
9244 SDValue ShAmt0 = N0.getOperand(1);
9245 if (ShAmt0.getValueType() != MVT::i8)
9246 return SDValue();
9247 SDValue ShAmt1 = N1.getOperand(1);
9248 if (ShAmt1.getValueType() != MVT::i8)
9249 return SDValue();
9250 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9251 ShAmt0 = ShAmt0.getOperand(0);
9252 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9253 ShAmt1 = ShAmt1.getOperand(0);
9254
9255 DebugLoc DL = N->getDebugLoc();
9256 unsigned Opc = X86ISD::SHLD;
9257 SDValue Op0 = N0.getOperand(0);
9258 SDValue Op1 = N1.getOperand(0);
9259 if (ShAmt0.getOpcode() == ISD::SUB) {
9260 Opc = X86ISD::SHRD;
9261 std::swap(Op0, Op1);
9262 std::swap(ShAmt0, ShAmt1);
9263 }
9264
9265 if (ShAmt1.getOpcode() == ISD::SUB) {
9266 SDValue Sum = ShAmt1.getOperand(0);
9267 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9268 if (SumC->getSExtValue() == 64 &&
9269 ShAmt1.getOperand(1) == ShAmt0)
9270 return DAG.getNode(Opc, DL, VT,
9271 Op0, Op1,
9272 DAG.getNode(ISD::TRUNCATE, DL,
9273 MVT::i8, ShAmt0));
9274 }
9275 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9276 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9277 if (ShAmt0C &&
9278 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9279 return DAG.getNode(Opc, DL, VT,
9280 N0.getOperand(0), N1.getOperand(0),
9281 DAG.getNode(ISD::TRUNCATE, DL,
9282 MVT::i8, ShAmt0));
9283 }
9284
9285 return SDValue();
9286}
9287
Chris Lattnerce84ae42008-02-22 02:09:43 +00009288/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009289static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Chengc944c5d2009-03-12 05:59:15 +00009290 const X86Subtarget *Subtarget) {
Chris Lattnerce84ae42008-02-22 02:09:43 +00009291 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9292 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00009293 // A preferable solution to the general problem is to figure out the right
9294 // places to insert EMMS. This qualifies as a quick hack.
Evan Chengc944c5d2009-03-12 05:59:15 +00009295
9296 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng40ee6e52008-05-08 00:57:18 +00009297 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersonac9de032009-08-10 22:56:29 +00009298 EVT VT = St->getValue().getValueType();
Evan Chengc944c5d2009-03-12 05:59:15 +00009299 if (VT.getSizeInBits() != 64)
9300 return SDValue();
9301
Devang Patelc386c842009-06-05 21:57:13 +00009302 const Function *F = DAG.getMachineFunction().getFunction();
9303 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009304 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patelc386c842009-06-05 21:57:13 +00009305 && Subtarget->hasSSE2();
Evan Chengc944c5d2009-03-12 05:59:15 +00009306 if ((VT.isVector() ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009307 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesend112b802008-02-25 19:20:14 +00009308 isa<LoadSDNode>(St->getValue()) &&
9309 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9310 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009311 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009312 LoadSDNode *Ld = 0;
9313 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00009314 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00009315 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009316 // Must be a store of a load. We currently handle two cases: the load
9317 // is a direct child, and it's under an intervening TokenFactor. It is
9318 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00009319 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00009320 Ld = cast<LoadSDNode>(St->getChain());
9321 else if (St->getValue().hasOneUse() &&
9322 ChainVal->getOpcode() == ISD::TokenFactor) {
9323 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009324 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00009325 TokenFactorIndex = i;
9326 Ld = cast<LoadSDNode>(St->getValue());
9327 } else
9328 Ops.push_back(ChainVal->getOperand(i));
9329 }
9330 }
Dale Johannesend112b802008-02-25 19:20:14 +00009331
Evan Chengc944c5d2009-03-12 05:59:15 +00009332 if (!Ld || !ISD::isNormalLoad(Ld))
9333 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009334
Evan Chengc944c5d2009-03-12 05:59:15 +00009335 // If this is not the MMX case, i.e. we are just turning i64 load/store
9336 // into f64 load/store, avoid the transformation if there are multiple
9337 // uses of the loaded value.
9338 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9339 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009340
Evan Chengc944c5d2009-03-12 05:59:15 +00009341 DebugLoc LdDL = Ld->getDebugLoc();
9342 DebugLoc StDL = N->getDebugLoc();
9343 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9344 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9345 // pair instead.
9346 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009347 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Chengc944c5d2009-03-12 05:59:15 +00009348 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9349 Ld->getBasePtr(), Ld->getSrcValue(),
9350 Ld->getSrcValueOffset(), Ld->isVolatile(),
9351 Ld->getAlignment());
9352 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00009353 if (TokenFactorIndex != -1) {
Evan Chengc944c5d2009-03-12 05:59:15 +00009354 Ops.push_back(NewChain);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009355 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00009356 Ops.size());
9357 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009358 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattnerce84ae42008-02-22 02:09:43 +00009359 St->getSrcValue(), St->getSrcValueOffset(),
9360 St->isVolatile(), St->getAlignment());
9361 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009362
9363 // Otherwise, lower to two pairs of 32-bit loads / stores.
9364 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009365 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9366 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009367
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009368 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009369 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9370 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009371 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009372 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9373 Ld->isVolatile(),
9374 MinAlign(Ld->getAlignment(), 4));
9375
9376 SDValue NewChain = LoLd.getValue(1);
9377 if (TokenFactorIndex != -1) {
9378 Ops.push_back(LoLd);
9379 Ops.push_back(HiLd);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009380 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Chengc944c5d2009-03-12 05:59:15 +00009381 Ops.size());
9382 }
9383
9384 LoAddr = St->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009385 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9386 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009387
9388 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9389 St->getSrcValue(), St->getSrcValueOffset(),
9390 St->isVolatile(), St->getAlignment());
9391 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9392 St->getSrcValue(),
9393 St->getSrcValueOffset() + 4,
9394 St->isVolatile(),
9395 MinAlign(St->getAlignment(), 4));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009396 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00009397 }
Dan Gohman8181bd12008-07-27 21:46:04 +00009398 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00009399}
9400
Chris Lattner470d5dc2008-01-25 06:14:17 +00009401/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9402/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009403static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00009404 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9405 // F[X]OR(0.0, x) -> x
9406 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00009407 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9408 if (C->getValueAPF().isPosZero())
9409 return N->getOperand(1);
9410 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9411 if (C->getValueAPF().isPosZero())
9412 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00009413 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009414}
9415
9416/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009417static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00009418 // FAND(0.0, x) -> 0.0
9419 // FAND(x, 0.0) -> 0.0
9420 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9421 if (C->getValueAPF().isPosZero())
9422 return N->getOperand(0);
9423 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9424 if (C->getValueAPF().isPosZero())
9425 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00009426 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009427}
9428
Dan Gohman22cefb02009-01-29 01:59:02 +00009429static SDValue PerformBTCombine(SDNode *N,
9430 SelectionDAG &DAG,
9431 TargetLowering::DAGCombinerInfo &DCI) {
9432 // BT ignores high bits in the bit index operand.
9433 SDValue Op1 = N->getOperand(1);
9434 if (Op1.hasOneUse()) {
9435 unsigned BitWidth = Op1.getValueSizeInBits();
9436 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9437 APInt KnownZero, KnownOne;
9438 TargetLowering::TargetLoweringOpt TLO(DAG);
9439 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9440 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9441 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9442 DCI.CommitTargetLoweringOpt(TLO);
9443 }
9444 return SDValue();
9445}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009446
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009447static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9448 SDValue Op = N->getOperand(0);
9449 if (Op.getOpcode() == ISD::BIT_CONVERT)
9450 Op = Op.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00009451 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009452 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009453 VT.getVectorElementType().getSizeInBits() ==
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009454 OpVT.getVectorElementType().getSizeInBits()) {
9455 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9456 }
9457 return SDValue();
9458}
9459
Owen Anderson58155b22009-06-29 18:04:45 +00009460// On X86 and X86-64, atomic operations are lowered to locked instructions.
9461// Locked instructions, in turn, have implicit fence semantics (all memory
9462// operations are flushed before issuing the locked instruction, and the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009463// are not buffered), so we can fold away the common pattern of
Owen Anderson58155b22009-06-29 18:04:45 +00009464// fence-atomic-fence.
9465static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9466 SDValue atomic = N->getOperand(0);
9467 switch (atomic.getOpcode()) {
9468 case ISD::ATOMIC_CMP_SWAP:
9469 case ISD::ATOMIC_SWAP:
9470 case ISD::ATOMIC_LOAD_ADD:
9471 case ISD::ATOMIC_LOAD_SUB:
9472 case ISD::ATOMIC_LOAD_AND:
9473 case ISD::ATOMIC_LOAD_OR:
9474 case ISD::ATOMIC_LOAD_XOR:
9475 case ISD::ATOMIC_LOAD_NAND:
9476 case ISD::ATOMIC_LOAD_MIN:
9477 case ISD::ATOMIC_LOAD_MAX:
9478 case ISD::ATOMIC_LOAD_UMIN:
9479 case ISD::ATOMIC_LOAD_UMAX:
9480 break;
9481 default:
9482 return SDValue();
9483 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009484
Owen Anderson58155b22009-06-29 18:04:45 +00009485 SDValue fence = atomic.getOperand(0);
9486 if (fence.getOpcode() != ISD::MEMBARRIER)
9487 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009488
Owen Anderson58155b22009-06-29 18:04:45 +00009489 switch (atomic.getOpcode()) {
9490 case ISD::ATOMIC_CMP_SWAP:
9491 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9492 atomic.getOperand(1), atomic.getOperand(2),
9493 atomic.getOperand(3));
9494 case ISD::ATOMIC_SWAP:
9495 case ISD::ATOMIC_LOAD_ADD:
9496 case ISD::ATOMIC_LOAD_SUB:
9497 case ISD::ATOMIC_LOAD_AND:
9498 case ISD::ATOMIC_LOAD_OR:
9499 case ISD::ATOMIC_LOAD_XOR:
9500 case ISD::ATOMIC_LOAD_NAND:
9501 case ISD::ATOMIC_LOAD_MIN:
9502 case ISD::ATOMIC_LOAD_MAX:
9503 case ISD::ATOMIC_LOAD_UMIN:
9504 case ISD::ATOMIC_LOAD_UMAX:
9505 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9506 atomic.getOperand(1), atomic.getOperand(2));
9507 default:
9508 return SDValue();
9509 }
9510}
9511
Evan Chengedeb1692009-12-16 00:53:11 +00009512static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9513 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9514 // (and (i32 x86isd::setcc_carry), 1)
9515 // This eliminates the zext. This transformation is necessary because
9516 // ISD::SETCC is always legalized to i8.
9517 DebugLoc dl = N->getDebugLoc();
9518 SDValue N0 = N->getOperand(0);
9519 EVT VT = N->getValueType(0);
9520 if (N0.getOpcode() == ISD::AND &&
9521 N0.hasOneUse() &&
9522 N0.getOperand(0).hasOneUse()) {
9523 SDValue N00 = N0.getOperand(0);
9524 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9525 return SDValue();
9526 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9527 if (!C || C->getZExtValue() != 1)
9528 return SDValue();
9529 return DAG.getNode(ISD::AND, dl, VT,
9530 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9531 N00.getOperand(0), N00.getOperand(1)),
9532 DAG.getConstant(1, VT));
9533 }
9534
9535 return SDValue();
9536}
9537
Dan Gohman8181bd12008-07-27 21:46:04 +00009538SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00009539 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009540 SelectionDAG &DAG = DCI.DAG;
9541 switch (N->getOpcode()) {
9542 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00009543 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00009544 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnere4577dc2009-03-12 06:52:53 +00009545 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng04ecee12009-03-28 05:57:29 +00009546 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
sampo025b75c2009-01-26 00:52:55 +00009547 case ISD::SHL:
9548 case ISD::SRA:
9549 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng10957b82010-01-04 21:22:48 +00009550 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00009551 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00009552 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00009553 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9554 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00009555 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009556 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson58155b22009-06-29 18:04:45 +00009557 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Chengedeb1692009-12-16 00:53:11 +00009558 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009559 }
9560
Dan Gohman8181bd12008-07-27 21:46:04 +00009561 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009562}
9563
9564//===----------------------------------------------------------------------===//
9565// X86 Inline Assembly Support
9566//===----------------------------------------------------------------------===//
9567
Chris Lattner7fce21c2009-07-20 17:51:36 +00009568static bool LowerToBSwap(CallInst *CI) {
9569 // FIXME: this should verify that we are targetting a 486 or better. If not,
9570 // we will turn this bswap into something that will be lowered to logical ops
9571 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9572 // so don't worry about this.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009573
Chris Lattner7fce21c2009-07-20 17:51:36 +00009574 // Verify this is a simple bswap.
9575 if (CI->getNumOperands() != 2 ||
9576 CI->getType() != CI->getOperand(1)->getType() ||
9577 !CI->getType()->isInteger())
9578 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009579
Chris Lattner7fce21c2009-07-20 17:51:36 +00009580 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9581 if (!Ty || Ty->getBitWidth() % 16 != 0)
9582 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009583
Chris Lattner7fce21c2009-07-20 17:51:36 +00009584 // Okay, we can do this xform, do so now.
9585 const Type *Tys[] = { Ty };
9586 Module *M = CI->getParent()->getParent()->getParent();
9587 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009588
Chris Lattner7fce21c2009-07-20 17:51:36 +00009589 Value *Op = CI->getOperand(1);
9590 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009591
Chris Lattner7fce21c2009-07-20 17:51:36 +00009592 CI->replaceAllUsesWith(Op);
9593 CI->eraseFromParent();
9594 return true;
9595}
9596
9597bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9598 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9599 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9600
9601 std::string AsmStr = IA->getAsmString();
9602
9603 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009604 SmallVector<StringRef, 4> AsmPieces;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009605 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9606
9607 switch (AsmPieces.size()) {
9608 default: return false;
9609 case 1:
9610 AsmStr = AsmPieces[0];
9611 AsmPieces.clear();
9612 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9613
9614 // bswap $0
9615 if (AsmPieces.size() == 2 &&
9616 (AsmPieces[0] == "bswap" ||
9617 AsmPieces[0] == "bswapq" ||
9618 AsmPieces[0] == "bswapl") &&
9619 (AsmPieces[1] == "$0" ||
9620 AsmPieces[1] == "${0:q}")) {
9621 // No need to check constraints, nothing other than the equivalent of
9622 // "=r,0" would be valid here.
9623 return LowerToBSwap(CI);
9624 }
9625 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer0461f522010-01-05 20:07:06 +00009626 if (CI->getType()->isInteger(16) &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009627 AsmPieces.size() == 3 &&
9628 AsmPieces[0] == "rorw" &&
9629 AsmPieces[1] == "$$8," &&
9630 AsmPieces[2] == "${0:w}" &&
9631 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9632 return LowerToBSwap(CI);
9633 }
9634 break;
9635 case 3:
Benjamin Kramer0461f522010-01-05 20:07:06 +00009636 if (CI->getType()->isInteger(64) &&
Owen Anderson35b47072009-08-13 21:58:54 +00009637 Constraints.size() >= 2 &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009638 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9639 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9640 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009641 SmallVector<StringRef, 4> Words;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009642 SplitString(AsmPieces[0], Words, " \t");
9643 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9644 Words.clear();
9645 SplitString(AsmPieces[1], Words, " \t");
9646 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9647 Words.clear();
9648 SplitString(AsmPieces[2], Words, " \t,");
9649 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9650 Words[2] == "%edx") {
9651 return LowerToBSwap(CI);
9652 }
9653 }
9654 }
9655 }
9656 break;
9657 }
9658 return false;
9659}
9660
9661
9662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009663/// getConstraintType - Given a constraint letter, return the type of
9664/// constraint it is for this target.
9665X86TargetLowering::ConstraintType
9666X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9667 if (Constraint.size() == 1) {
9668 switch (Constraint[0]) {
9669 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00009670 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00009671 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009672 case 'r':
9673 case 'R':
9674 case 'l':
9675 case 'q':
9676 case 'Q':
9677 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00009678 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009679 case 'Y':
9680 return C_RegisterClass;
Dale Johannesenf190a032009-02-12 20:58:09 +00009681 case 'e':
9682 case 'Z':
9683 return C_Other;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009684 default:
9685 break;
9686 }
9687 }
9688 return TargetLowering::getConstraintType(Constraint);
9689}
9690
Dale Johannesene99fc902008-01-29 02:21:21 +00009691/// LowerXConstraint - try to replace an X constraint, which matches anything,
9692/// with another that has more specific requirements based on the type of the
9693/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00009694const char *X86TargetLowering::
Owen Andersonac9de032009-08-10 22:56:29 +00009695LowerXConstraint(EVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00009696 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9697 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00009698 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00009699 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00009700 return "Y";
9701 if (Subtarget->hasSSE1())
9702 return "x";
9703 }
Scott Michel91099d62009-02-17 22:15:04 +00009704
Chris Lattnereca405c2008-04-26 23:02:14 +00009705 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00009706}
9707
Chris Lattnera531abc2007-08-25 00:47:38 +00009708/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9709/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00009710void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00009711 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00009712 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00009713 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00009714 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00009715 SDValue Result(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00009716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009717 switch (Constraint) {
9718 default: break;
9719 case 'I':
9720 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00009721 if (C->getZExtValue() <= 31) {
9722 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00009723 break;
9724 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009725 }
Chris Lattnera531abc2007-08-25 00:47:38 +00009726 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00009727 case 'J':
9728 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +00009729 if (C->getZExtValue() <= 63) {
Chris Lattner6552d0c2009-06-15 04:01:39 +00009730 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9731 break;
9732 }
9733 }
9734 return;
9735 case 'K':
9736 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +00009737 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00009738 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9739 break;
9740 }
9741 }
9742 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009743 case 'N':
9744 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00009745 if (C->getZExtValue() <= 255) {
9746 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00009747 break;
9748 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009749 }
Chris Lattnera531abc2007-08-25 00:47:38 +00009750 return;
Dale Johannesenf190a032009-02-12 20:58:09 +00009751 case 'e': {
9752 // 32-bit signed value
9753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9754 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +00009755 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9756 C->getSExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009757 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009758 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesenf190a032009-02-12 20:58:09 +00009759 break;
9760 }
9761 // FIXME gcc accepts some relocatable values here too, but only in certain
9762 // memory models; it's complicated.
9763 }
9764 return;
9765 }
9766 case 'Z': {
9767 // 32-bit unsigned value
9768 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9769 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +00009770 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9771 C->getZExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009772 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9773 break;
9774 }
9775 }
9776 // FIXME gcc accepts some relocatable values here too, but only in certain
9777 // memory models; it's complicated.
9778 return;
9779 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009780 case 'i': {
9781 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00009782 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009783 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009784 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattnera531abc2007-08-25 00:47:38 +00009785 break;
9786 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009787
9788 // If we are in non-pic codegen mode, we allow the address of a global (with
9789 // an optional displacement) to be used with 'i'.
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009790 GlobalAddressSDNode *GA = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009791 int64_t Offset = 0;
Scott Michel91099d62009-02-17 22:15:04 +00009792
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009793 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9794 while (1) {
9795 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9796 Offset += GA->getOffset();
9797 break;
9798 } else if (Op.getOpcode() == ISD::ADD) {
9799 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9800 Offset += C->getZExtValue();
9801 Op = Op.getOperand(0);
9802 continue;
9803 }
9804 } else if (Op.getOpcode() == ISD::SUB) {
9805 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9806 Offset += -C->getZExtValue();
9807 Op = Op.getOperand(0);
9808 continue;
9809 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009810 }
Dale Johannesen69976cf2009-07-07 00:18:49 +00009811
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009812 // Otherwise, this isn't something we can handle, reject it.
9813 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009814 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009815
Chris Lattner054532c2009-07-10 07:34:39 +00009816 GlobalValue *GV = GA->getGlobal();
Dale Johannesen69976cf2009-07-07 00:18:49 +00009817 // If we require an extra load to get this address, as in PIC mode, we
9818 // can't accept it.
Chris Lattner054532c2009-07-10 07:34:39 +00009819 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9820 getTargetMachine())))
Dale Johannesen69976cf2009-07-07 00:18:49 +00009821 return;
Scott Michel91099d62009-02-17 22:15:04 +00009822
Dale Johannesenf97110c2009-07-21 00:12:29 +00009823 if (hasMemory)
9824 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9825 else
9826 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009827 Result = Op;
9828 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009829 }
9830 }
Scott Michel91099d62009-02-17 22:15:04 +00009831
Gabor Greif1c80d112008-08-28 21:40:38 +00009832 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00009833 Ops.push_back(Result);
9834 return;
9835 }
Evan Cheng7f250d62008-09-24 00:05:32 +00009836 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9837 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009838}
9839
9840std::vector<unsigned> X86TargetLowering::
9841getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +00009842 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009843 if (Constraint.size() == 1) {
9844 // FIXME: not handling fp-stack yet!
9845 switch (Constraint[0]) { // GCC X86 Constraint Letters
9846 default: break; // Unknown constraint letter
Evan Chengf8993d42009-07-17 22:13:25 +00009847 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9848 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009849 if (VT == MVT::i32)
Evan Chengf8993d42009-07-17 22:13:25 +00009850 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9851 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9852 X86::R10D,X86::R11D,X86::R12D,
9853 X86::R13D,X86::R14D,X86::R15D,
9854 X86::EBP, X86::ESP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009855 else if (VT == MVT::i16)
Evan Chengf8993d42009-07-17 22:13:25 +00009856 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9857 X86::SI, X86::DI, X86::R8W,X86::R9W,
9858 X86::R10W,X86::R11W,X86::R12W,
9859 X86::R13W,X86::R14W,X86::R15W,
9860 X86::BP, X86::SP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009861 else if (VT == MVT::i8)
Evan Chengf8993d42009-07-17 22:13:25 +00009862 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9863 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9864 X86::R10B,X86::R11B,X86::R12B,
9865 X86::R13B,X86::R14B,X86::R15B,
9866 X86::BPL, X86::SPL, 0);
9867
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009868 else if (VT == MVT::i64)
Evan Chengf8993d42009-07-17 22:13:25 +00009869 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9870 X86::RSI, X86::RDI, X86::R8, X86::R9,
9871 X86::R10, X86::R11, X86::R12,
9872 X86::R13, X86::R14, X86::R15,
9873 X86::RBP, X86::RSP, 0);
9874
9875 break;
9876 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009877 // 32-bit fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009878 case 'Q': // Q_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009879 if (VT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009880 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009881 else if (VT == MVT::i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009882 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009883 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +00009884 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009885 else if (VT == MVT::i64)
Chris Lattner35032592007-11-04 06:51:12 +00009886 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9887 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009888 }
9889 }
9890
9891 return std::vector<unsigned>();
9892}
9893
9894std::pair<unsigned, const TargetRegisterClass*>
9895X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +00009896 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009897 // First, see if this is a constraint that directly corresponds to an LLVM
9898 // register class.
9899 if (Constraint.size() == 1) {
9900 // GCC Constraint Letters
9901 switch (Constraint[0]) {
9902 default: break;
9903 case 'r': // GENERAL_REGS
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009904 case 'l': // INDEX_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009905 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009906 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009907 if (VT == MVT::i16)
Chris Lattnerbbfea052008-10-17 18:15:05 +00009908 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009909 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michel91099d62009-02-17 22:15:04 +00009910 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +00009911 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen1bf03f72009-10-07 22:47:20 +00009912 case 'R': // LEGACY_REGS
9913 if (VT == MVT::i8)
9914 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9915 if (VT == MVT::i16)
9916 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9917 if (VT == MVT::i32 || !Subtarget->is64Bit())
9918 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9919 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +00009920 case 'f': // FP Stack registers.
9921 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9922 // value to the correct fpstack register class.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009923 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +00009924 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009925 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +00009926 return std::make_pair(0U, X86::RFP64RegisterClass);
9927 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009928 case 'y': // MMX_REGS if MMX allowed.
9929 if (!Subtarget->hasMMX()) break;
9930 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009931 case 'Y': // SSE_REGS if SSE2 allowed
9932 if (!Subtarget->hasSSE2()) break;
9933 // FALL THROUGH.
9934 case 'x': // SSE_REGS if SSE1 allowed
9935 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +00009936
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009937 switch (VT.getSimpleVT().SimpleTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009938 default: break;
9939 // Scalar SSE types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009940 case MVT::f32:
9941 case MVT::i32:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009942 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009943 case MVT::f64:
9944 case MVT::i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009945 return std::make_pair(0U, X86::FR64RegisterClass);
9946 // Vector types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009947 case MVT::v16i8:
9948 case MVT::v8i16:
9949 case MVT::v4i32:
9950 case MVT::v2i64:
9951 case MVT::v4f32:
9952 case MVT::v2f64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009953 return std::make_pair(0U, X86::VR128RegisterClass);
9954 }
9955 break;
9956 }
9957 }
Scott Michel91099d62009-02-17 22:15:04 +00009958
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009959 // Use the default implementation in TargetLowering to convert the register
9960 // constraint into a member of a register class.
9961 std::pair<unsigned, const TargetRegisterClass*> Res;
9962 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
9963
9964 // Not found as a standard register?
9965 if (Res.second == 0) {
Chris Lattner1063d242009-09-13 22:41:48 +00009966 // Map st(0) -> st(7) -> ST0
9967 if (Constraint.size() == 7 && Constraint[0] == '{' &&
9968 tolower(Constraint[1]) == 's' &&
9969 tolower(Constraint[2]) == 't' &&
9970 Constraint[3] == '(' &&
9971 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
9972 Constraint[5] == ')' &&
9973 Constraint[6] == '}') {
Daniel Dunbar3be44e62009-09-20 02:20:51 +00009974
Chris Lattner1063d242009-09-13 22:41:48 +00009975 Res.first = X86::ST0+Constraint[4]-'0';
9976 Res.second = X86::RFP80RegisterClass;
9977 return Res;
9978 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00009979
Chris Lattner1063d242009-09-13 22:41:48 +00009980 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramerea862b02009-11-12 20:36:59 +00009981 if (StringRef("{st}").equals_lower(Constraint)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009982 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +00009983 Res.second = X86::RFP80RegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +00009984 return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009985 }
Chris Lattner1063d242009-09-13 22:41:48 +00009986
9987 // flags -> EFLAGS
Benjamin Kramerea862b02009-11-12 20:36:59 +00009988 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner1063d242009-09-13 22:41:48 +00009989 Res.first = X86::EFLAGS;
9990 Res.second = X86::CCRRegisterClass;
9991 return Res;
9992 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +00009993
Dale Johannesen73920c02008-11-13 21:52:36 +00009994 // 'A' means EAX + EDX.
9995 if (Constraint == "A") {
9996 Res.first = X86::EAX;
Dan Gohmanb4439d02009-07-30 17:02:08 +00009997 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +00009998 return Res;
Dale Johannesen73920c02008-11-13 21:52:36 +00009999 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010000 return Res;
10001 }
10002
10003 // Otherwise, check to see if this is a register class of the wrong value
10004 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10005 // turn into {ax},{dx}.
10006 if (Res.second->hasType(VT))
10007 return Res; // Correct type already, nothing to do.
10008
10009 // All of the single-register GCC register classes map their values onto
10010 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10011 // really want an 8-bit or 32-bit register, map to the appropriate register
10012 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +000010013 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010014 if (VT == MVT::i8) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010015 unsigned DestReg = 0;
10016 switch (Res.first) {
10017 default: break;
10018 case X86::AX: DestReg = X86::AL; break;
10019 case X86::DX: DestReg = X86::DL; break;
10020 case X86::CX: DestReg = X86::CL; break;
10021 case X86::BX: DestReg = X86::BL; break;
10022 }
10023 if (DestReg) {
10024 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010025 Res.second = X86::GR8RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010026 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010027 } else if (VT == MVT::i32) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010028 unsigned DestReg = 0;
10029 switch (Res.first) {
10030 default: break;
10031 case X86::AX: DestReg = X86::EAX; break;
10032 case X86::DX: DestReg = X86::EDX; break;
10033 case X86::CX: DestReg = X86::ECX; break;
10034 case X86::BX: DestReg = X86::EBX; break;
10035 case X86::SI: DestReg = X86::ESI; break;
10036 case X86::DI: DestReg = X86::EDI; break;
10037 case X86::BP: DestReg = X86::EBP; break;
10038 case X86::SP: DestReg = X86::ESP; break;
10039 }
10040 if (DestReg) {
10041 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010042 Res.second = X86::GR32RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010043 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010044 } else if (VT == MVT::i64) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010045 unsigned DestReg = 0;
10046 switch (Res.first) {
10047 default: break;
10048 case X86::AX: DestReg = X86::RAX; break;
10049 case X86::DX: DestReg = X86::RDX; break;
10050 case X86::CX: DestReg = X86::RCX; break;
10051 case X86::BX: DestReg = X86::RBX; break;
10052 case X86::SI: DestReg = X86::RSI; break;
10053 case X86::DI: DestReg = X86::RDI; break;
10054 case X86::BP: DestReg = X86::RBP; break;
10055 case X86::SP: DestReg = X86::RSP; break;
10056 }
10057 if (DestReg) {
10058 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010059 Res.second = X86::GR64RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010060 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010061 }
Chris Lattnere9d7f792008-08-26 06:19:02 +000010062 } else if (Res.second == X86::FR32RegisterClass ||
10063 Res.second == X86::FR64RegisterClass ||
10064 Res.second == X86::VR128RegisterClass) {
10065 // Handle references to XMM physical registers that got mapped into the
10066 // wrong class. This can happen with constraints like {xmm0} where the
10067 // target independent register mapper will just pick the first match it can
10068 // find, ignoring the required type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010069 if (VT == MVT::f32)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010070 Res.second = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010071 else if (VT == MVT::f64)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010072 Res.second = X86::FR64RegisterClass;
10073 else if (X86::VR128RegisterClass->hasType(VT))
10074 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010075 }
10076
10077 return Res;
10078}
Mon P Wang1448aad2008-10-30 08:01:45 +000010079
10080//===----------------------------------------------------------------------===//
10081// X86 Widen vector type
10082//===----------------------------------------------------------------------===//
10083
10084/// getWidenVectorType: given a vector type, returns the type to widen
10085/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010086/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +000010087/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +000010088/// scalarizing vs using the wider vector type.
10089
Owen Andersonac9de032009-08-10 22:56:29 +000010090EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +000010091 assert(VT.isVector());
10092 if (isTypeLegal(VT))
10093 return VT;
Scott Michel91099d62009-02-17 22:15:04 +000010094
Mon P Wang1448aad2008-10-30 08:01:45 +000010095 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10096 // type based on element type. This would speed up our search (though
10097 // it may not be worth it since the size of the list is relatively
10098 // small).
Owen Andersonac9de032009-08-10 22:56:29 +000010099 EVT EltVT = VT.getVectorElementType();
Mon P Wang1448aad2008-10-30 08:01:45 +000010100 unsigned NElts = VT.getVectorNumElements();
Scott Michel91099d62009-02-17 22:15:04 +000010101
Mon P Wang1448aad2008-10-30 08:01:45 +000010102 // On X86, it make sense to widen any vector wider than 1
10103 if (NElts <= 1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010104 return MVT::Other;
Scott Michel91099d62009-02-17 22:15:04 +000010105
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010106 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10107 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10108 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michel91099d62009-02-17 22:15:04 +000010109
10110 if (isTypeLegal(SVT) &&
10111 SVT.getVectorElementType() == EltVT &&
Mon P Wang1448aad2008-10-30 08:01:45 +000010112 SVT.getVectorNumElements() > NElts)
10113 return SVT;
10114 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010115 return MVT::Other;
Mon P Wang1448aad2008-10-30 08:01:45 +000010116}