blob: 228244f06bfd9f5d5bace6ead7d67168da3d15d9 [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
Evan Chengd82fae32010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "X86.h"
17#include "X86InstrBuilder.h"
18#include "X86ISelLowering.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "X86TargetMachine.h"
Chris Lattner8886dc22009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CallingConv.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
Chris Lattnerec7cfd42009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/GlobalVariable.h"
26#include "llvm/Function.h"
Chris Lattner7fce21c2009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Intrinsics.h"
Owen Anderson6361f972009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner25525cd2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Cheng2e28d622008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner541d8902010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattner82411c42010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
39#include "llvm/MC/MCExpr.h"
40#include "llvm/MC/MCSymbol.h"
Chris Lattner82411c42010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng75184a92007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengd82fae32010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattner82411c42010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang1f292322008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattner82411c42010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
48#include "llvm/Support/ErrorHandling.h"
49#include "llvm/Support/MathExtras.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000050#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051using namespace llvm;
52
Evan Chengd82fae32010-01-27 06:25:16 +000053STATISTIC(NumTailCalls, "Number of tail calls");
54
Mon P Wang1f292322008-11-23 04:37:22 +000055static cl::opt<bool>
Mon P Wangba7e48e2008-11-24 02:10:43 +000056DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang1f292322008-11-23 04:37:22 +000057
Dan Gohmane84197b2009-09-03 17:18:51 +000058// Disable16Bit - 16-bit operations typically have a larger encoding than
59// corresponding 32-bit instructions, and 16-bit code is slow on some
60// processors. This is an experimental flag to disable 16-bit operations
61// (which forces them to be Legalized to 32-bit operations).
62static cl::opt<bool>
63Disable16Bit("disable-16bit", cl::Hidden,
64 cl::desc("Disable use of 16-bit instructions"));
65
Evan Cheng2aea0b42008-04-25 19:11:04 +000066// Forward declarations.
Owen Andersonac9de032009-08-10 22:56:29 +000067static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +000068 SDValue V2);
Evan Cheng2aea0b42008-04-25 19:11:04 +000069
Chris Lattnerc4c40a92009-07-28 03:13:23 +000070static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
71 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
72 default: llvm_unreachable("unknown subtarget type");
73 case X86Subtarget::isDarwin:
Chris Lattner8886dc22009-09-16 01:46:41 +000074 if (TM.getSubtarget<X86Subtarget>().is64Bit())
75 return new X8664_MachoTargetObjectFile();
Chris Lattnerf283fb22009-09-18 20:22:52 +000076 return new X8632_MachoTargetObjectFile();
Chris Lattnerc4c40a92009-07-28 03:13:23 +000077 case X86Subtarget::isELF:
78 return new TargetLoweringObjectFileELF();
79 case X86Subtarget::isMingw:
80 case X86Subtarget::isCygwin:
81 case X86Subtarget::isWindows:
82 return new TargetLoweringObjectFileCOFF();
83 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +000084
Chris Lattnerc4c40a92009-07-28 03:13:23 +000085}
86
Dan Gohmanb41dfba2008-05-14 01:58:56 +000087X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerc4c40a92009-07-28 03:13:23 +000088 : TargetLowering(TM, createTLOF(TM)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesene0e0fd02007-09-23 14:52:20 +000090 X86ScalarSSEf64 = Subtarget->hasSSE2();
91 X86ScalarSSEf32 = Subtarget->hasSSE1();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000093
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovd0fef972008-09-09 18:22:57 +000095 TD = getTargetData();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // Set up the TargetLowering object.
98
99 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000100 setShiftAmountType(MVT::i8);
Duncan Sands8cf4a822008-11-23 15:47:28 +0000101 setBooleanContents(ZeroOrOneBooleanContent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 setSchedulingPreference(SchedulingForRegPressure);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 setStackPointerRegisterToSaveRestore(X86StackPtr);
104
105 if (Subtarget->isTargetDarwin()) {
106 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
107 setUseUnderscoreSetJmp(false);
108 setUseUnderscoreLongJmp(false);
109 } else if (Subtarget->isTargetMingw()) {
110 // MS runtime is weird: it exports _setjmp, but longjmp!
111 setUseUnderscoreSetJmp(true);
112 setUseUnderscoreLongJmp(false);
113 } else {
114 setUseUnderscoreSetJmp(true);
115 setUseUnderscoreLongJmp(true);
116 }
Scott Michel91099d62009-02-17 22:15:04 +0000117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // Set up the register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000119 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohmane84197b2009-09-03 17:18:51 +0000120 if (!Disable16Bit)
121 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000122 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000124 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000126 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
Scott Michel91099d62009-02-17 22:15:04 +0000128 // We don't accept any truncstore of integer registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000129 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000130 if (!Disable16Bit)
131 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000132 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000133 if (!Disable16Bit)
134 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000135 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
136 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng71343822008-10-15 02:05:31 +0000137
138 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000139 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
140 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
141 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
142 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
144 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattner3bc08502008-01-17 19:59:44 +0000145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
147 // operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000148 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
149 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
150 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151
152 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
154 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000155 } else if (!UseSoftFloat) {
156 if (X86ScalarSSEf64) {
Dale Johannesena359b8b2008-10-21 20:50:01 +0000157 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000158 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000159 }
Eli Friedman8c3cb582009-05-23 09:59:16 +0000160 // We have an algorithm for SSE2, and we turn this into a 64-bit
161 // FILD for other targets.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000162 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 }
164
165 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
166 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
168 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000169
Devang Patel3c233642009-06-05 18:48:29 +0000170 if (!UseSoftFloat) {
Bill Wendling6b42d012009-03-13 08:41:47 +0000171 // SSE has no i16 to fp conversion, only i32
172 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling6b42d012009-03-13 08:41:47 +0000174 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000175 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000176 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
178 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling6b42d012009-03-13 08:41:47 +0000179 }
Dale Johannesen2fc20782007-09-14 22:26:36 +0000180 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000181 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
182 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 }
184
Dale Johannesen958b08b2007-09-19 23:55:34 +0000185 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
186 // are Legal, f80 is custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
188 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
191 // this operation.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
193 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000195 if (X86ScalarSSEf32) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen2fc20782007-09-14 22:26:36 +0000197 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000198 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000200 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
201 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 }
203
204 // Handle FP_TO_UINT by promoting the destination to a larger signed
205 // conversion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000206 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
207 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
208 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209
210 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman8c3cb582009-05-23 09:59:16 +0000213 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000214 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 // Expand FP_TO_UINT into a select.
216 // FIXME: We would like to use a Custom expander here eventually to do
217 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000218 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 else
Eli Friedman8c3cb582009-05-23 09:59:16 +0000220 // With SSE3 we can use fisttpll to convert to a signed i64; without
221 // SSE, we're stuck with a fistpll.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000222 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 }
224
225 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000226 if (!X86ScalarSSEf64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000227 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
228 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 }
230
Dan Gohman8450d862008-02-18 19:34:53 +0000231 // Scalar integer divide and remainder are lowered to use operations that
232 // produce two results, to match the available instructions. This exposes
233 // the two-result form to trivial CSE, which is able to combine x/y and x%y
234 // into a single instruction.
235 //
236 // Scalar integer multiply-high is also lowered to use two-result
237 // operations, to match the available instructions. However, plain multiply
238 // (low) operations are left as Legal, as there are single-result
239 // instructions for this in x86. Using the two-result multiply instructions
240 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000241 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
242 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
243 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
244 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
245 setOperationAction(ISD::SREM , MVT::i8 , Expand);
246 setOperationAction(ISD::UREM , MVT::i8 , Expand);
247 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
248 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
249 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
250 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
251 setOperationAction(ISD::SREM , MVT::i16 , Expand);
252 setOperationAction(ISD::UREM , MVT::i16 , Expand);
253 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
254 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
255 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
256 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
257 setOperationAction(ISD::SREM , MVT::i32 , Expand);
258 setOperationAction(ISD::UREM , MVT::i32 , Expand);
259 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
260 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
261 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
262 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
263 setOperationAction(ISD::SREM , MVT::i64 , Expand);
264 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohman242a5ba2007-09-25 18:23:27 +0000265
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000266 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
267 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
268 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
269 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
275 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
276 setOperationAction(ISD::FREM , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f64 , Expand);
278 setOperationAction(ISD::FREM , MVT::f80 , Expand);
279 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000280
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000281 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
282 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
284 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohmane84197b2009-09-03 17:18:51 +0000285 if (Disable16Bit) {
286 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
287 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
288 } else {
289 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
291 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000292 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
293 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
294 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000296 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
297 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
298 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 }
300
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000301 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
302 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303
304 // These should be promoted to a larger select which is supported.
Dan Gohman29b998f2009-08-27 00:14:12 +0000305 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // X86 wants to expand cmov itself.
Dan Gohman29b998f2009-08-27 00:14:12 +0000307 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000308 if (Disable16Bit)
309 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
310 else
311 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000312 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
314 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
315 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
316 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohmane84197b2009-09-03 17:18:51 +0000317 if (Disable16Bit)
318 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
319 else
320 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000321 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
322 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
323 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
324 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000326 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
327 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000329 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
331 // Darwin ABI issue.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000332 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
333 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
334 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
335 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +0000336 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000337 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
338 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000339 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000341 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
342 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
343 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
344 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohman064403e2009-10-30 01:28:02 +0000345 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 }
347 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000351 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000352 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
353 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
354 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman092014e2008-03-03 22:22:09 +0000355 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356
Evan Cheng8d51ab32008-03-10 19:38:10 +0000357 if (Subtarget->hasSSE1())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000358 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Chengd1d68072008-03-08 00:58:38 +0000359
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000360 if (!Subtarget->hasSSE2())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000361 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000362
Mon P Wang078a62d2008-05-05 19:05:59 +0000363 // Expand certain atomics
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000364 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
365 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
366 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
367 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendlingdb2280a2008-08-20 00:28:16 +0000368
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000369 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharth0531ec52008-02-16 14:46:26 +0000373
Dale Johannesenf160d802008-10-02 18:53:47 +0000374 if (!Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000375 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
381 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesenf160d802008-10-02 18:53:47 +0000382 }
383
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 // FIXME - use subtarget debug flags
385 if (!Subtarget->isTargetDarwin() &&
386 !Subtarget->isTargetELF() &&
Dan Gohmanfa607c92008-07-01 00:05:16 +0000387 !Subtarget->isTargetCygMing()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000388 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohmanfa607c92008-07-01 00:05:16 +0000389 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000391 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
392 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
393 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
394 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 if (Subtarget->is64Bit()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 setExceptionPointerRegister(X86::RAX);
397 setExceptionSelectorRegister(X86::RDX);
398 } else {
399 setExceptionPointerRegister(X86::EAX);
400 setExceptionSelectorRegister(X86::EDX);
401 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000402 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
403 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov566f9d92008-09-08 21:12:11 +0000404
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000405 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsd8455ca2007-07-27 20:02:49 +0000406
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000407 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov39d40ba2008-01-15 07:02:33 +0000408
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000410 setOperationAction(ISD::VASTART , MVT::Other, Custom);
411 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000412 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000413 setOperationAction(ISD::VAARG , MVT::Other, Custom);
414 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000415 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VAARG , MVT::Other, Expand);
417 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman827cb1f2008-05-10 01:26:14 +0000418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000420 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
421 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 if (Subtarget->is64Bit())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000423 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 if (Subtarget->isTargetCygMing())
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000425 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000427 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428
Evan Cheng0b84fe12009-02-13 22:36:38 +0000429 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000430 // f32 and f64 use SSE.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000432 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
433 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434
435 // Use ANDPD to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000436 setOperationAction(ISD::FABS , MVT::f64, Custom);
437 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438
439 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FNEG , MVT::f64, Custom);
441 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442
443 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000444 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000448 setOperationAction(ISD::FSIN , MVT::f64, Expand);
449 setOperationAction(ISD::FCOS , MVT::f64, Expand);
450 setOperationAction(ISD::FSIN , MVT::f32, Expand);
451 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452
453 // Expand FP immediates into loads from the stack, except for the special
454 // cases we handle.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000455 addLegalFPImmediate(APFloat(+0.0)); // xorpd
456 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Cheng0b84fe12009-02-13 22:36:38 +0000457 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000458 // Use SSE for f32, x87 for f64.
459 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000460 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
461 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000462
463 // Use ANDPS to simulate FABS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000464 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000465
466 // Use XORP to simulate FNEG.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000468
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000469 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000470
471 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000472 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000474
475 // We don't support sin/cos/fmod
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000476 setOperationAction(ISD::FSIN , MVT::f32, Expand);
477 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000478
Nate Begemane2ba64f2008-02-14 08:57:00 +0000479 // Special cases we handle for FP constants.
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000480 addLegalFPImmediate(APFloat(+0.0f)); // xorps
481 addLegalFPImmediate(APFloat(+0.0)); // FLD0
482 addLegalFPImmediate(APFloat(+1.0)); // FLD1
483 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
484 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
485
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000486 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000487 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
488 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000489 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000490 } else if (!UseSoftFloat) {
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000491 // f32 and f64 in x87.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 // Set up the FP register classes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000493 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
494 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000496 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
497 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
499 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen8f83a6b2007-08-09 01:04:01 +0000500
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000502 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
503 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000504 }
Dale Johannesenbbe2b702007-08-30 00:23:21 +0000505 addLegalFPImmediate(APFloat(+0.0)); // FLD0
506 addLegalFPImmediate(APFloat(+1.0)); // FLD1
507 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
508 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesene0e0fd02007-09-23 14:52:20 +0000509 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
510 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
511 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
512 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 }
514
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000515 // Long double always uses X87.
Evan Chenge738dc32009-03-26 23:06:32 +0000516 if (!UseSoftFloat) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000517 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
518 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
519 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000520 {
521 bool ignored;
522 APFloat TmpFlt(+0.0);
523 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
524 &ignored);
525 addLegalFPImmediate(TmpFlt); // FLD0
526 TmpFlt.changeSign();
527 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
528 APFloat TmpFlt2(+1.0);
529 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
530 &ignored);
531 addLegalFPImmediate(TmpFlt2); // FLD1
532 TmpFlt2.changeSign();
533 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
534 }
Scott Michel91099d62009-02-17 22:15:04 +0000535
Evan Cheng0b84fe12009-02-13 22:36:38 +0000536 if (!UnsafeFPMath) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000537 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
538 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000539 }
Dale Johannesen7f1076b2007-09-26 21:10:55 +0000540 }
Dale Johannesen4ab00bd2007-08-05 18:49:15 +0000541
Dan Gohman2f7b1982007-10-11 23:21:31 +0000542 // Always use a library call for pow.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000543 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
544 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
545 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohman2f7b1982007-10-11 23:21:31 +0000546
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000547 setOperationAction(ISD::FLOG, MVT::f80, Expand);
548 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
549 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
550 setOperationAction(ISD::FEXP, MVT::f80, Expand);
551 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen92b33082008-09-04 00:47:13 +0000552
Mon P Wanga5a239f2008-11-06 05:31:54 +0000553 // First set operation action for all vector types to either promote
Mon P Wang1448aad2008-10-30 08:01:45 +0000554 // (for widening) or expand (for scalarization). Then we will selectively
555 // turn on ones that can be effectively codegen'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000556 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
557 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
558 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
573 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
574 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman9d501bd2009-12-11 21:31:27 +0000606 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohmanc6cfdd32009-12-14 23:40:38 +0000607 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
610 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
611 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
612 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
613 setTruncStoreAction((MVT::SimpleValueType)VT,
614 (MVT::SimpleValueType)InnerVT, Expand);
615 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
616 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
617 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 }
619
Evan Cheng0b84fe12009-02-13 22:36:38 +0000620 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
621 // with -msoft-float, disable use of MMX as well.
Evan Chenge738dc32009-03-26 23:06:32 +0000622 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000623 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
624 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
625 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
626 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
627 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000628
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000629 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
630 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
631 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
632 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000634 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
635 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
636 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
637 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000639 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
640 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000642 setOperationAction(ISD::AND, MVT::v8i8, Promote);
643 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
644 setOperationAction(ISD::AND, MVT::v4i16, Promote);
645 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
646 setOperationAction(ISD::AND, MVT::v2i32, Promote);
647 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
648 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000649
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000650 setOperationAction(ISD::OR, MVT::v8i8, Promote);
651 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
652 setOperationAction(ISD::OR, MVT::v4i16, Promote);
653 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
654 setOperationAction(ISD::OR, MVT::v2i32, Promote);
655 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
656 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000658 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
659 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
660 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
661 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
662 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
663 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
664 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000665
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000666 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
667 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
668 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
669 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
670 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
671 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
672 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
673 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
674 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000675
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000676 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
677 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
678 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
679 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
680 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000681
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000682 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
683 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
684 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
685 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
688 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
689 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendlingb9e5f802008-07-20 02:32:23 +0000691
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang83edba52008-12-12 01:25:51 +0000693
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000694 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
695 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
696 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
697 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
698 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
699 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
700 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701 }
702
Evan Chenge738dc32009-03-26 23:06:32 +0000703 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000704 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000705
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000706 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
707 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
708 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
709 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
710 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
711 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
712 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
713 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
714 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
715 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
716 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
717 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 }
719
Evan Chenge738dc32009-03-26 23:06:32 +0000720 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000721 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Cheng0b84fe12009-02-13 22:36:38 +0000722
Bill Wendling042eda32009-03-11 22:30:01 +0000723 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
724 // registers cannot be used even for integer operations.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000725 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
726 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
727 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
728 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000730 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
731 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
732 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
733 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
734 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
735 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
736 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
737 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
738 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
739 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
740 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
741 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
742 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
743 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
744 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
745 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000746
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000747 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
748 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
749 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
750 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begeman061db5f2008-05-12 20:34:32 +0000751
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000752 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
753 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
754 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
755 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
756 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000757
Mon P Wanga8ff0dd2010-01-24 00:05:03 +0000758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
759 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
760 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
761 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
762 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
763
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000764 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000765 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
766 EVT VT = (MVT::SimpleValueType)i;
Nate Begemanc16406d2007-12-11 01:41:33 +0000767 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands92c43912008-06-06 12:08:01 +0000768 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begemanc16406d2007-12-11 01:41:33 +0000769 continue;
David Greenea5acb6e2009-06-29 16:47:10 +0000770 // Do not attempt to custom lower non-128-bit vectors
771 if (!VT.is128BitVector())
772 continue;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000773 setOperationAction(ISD::BUILD_VECTOR,
774 VT.getSimpleVT().SimpleTy, Custom);
775 setOperationAction(ISD::VECTOR_SHUFFLE,
776 VT.getSimpleVT().SimpleTy, Custom);
777 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
778 VT.getSimpleVT().SimpleTy, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 }
Bill Wendling042eda32009-03-11 22:30:01 +0000780
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000781 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
782 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
783 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
784 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
786 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendling042eda32009-03-11 22:30:01 +0000787
Nate Begeman4294c1f2008-02-12 22:51:28 +0000788 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000789 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
790 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begeman4294c1f2008-02-12 22:51:28 +0000791 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000792
793 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000794 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
795 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersonac9de032009-08-10 22:56:29 +0000796 EVT VT = SVT;
David Greenea5acb6e2009-06-29 16:47:10 +0000797
798 // Do not attempt to promote non-128-bit vectors
799 if (!VT.is128BitVector()) {
800 continue;
801 }
Owen Andersona0c69eb2009-08-10 20:46:15 +0000802 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000804 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000805 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000806 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000808 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000809 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersona0c69eb2009-08-10 20:46:15 +0000810 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000811 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 }
813
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000814 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerdec9cb52008-01-24 08:07:48 +0000815
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 // Custom lower v2i64 and v2f64 selects.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000817 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
818 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
819 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
820 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michel91099d62009-02-17 22:15:04 +0000821
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000822 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
823 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000824 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000825 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
826 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedmanc0521fb2009-06-06 03:57:58 +0000827 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 }
Evan Cheng0b84fe12009-02-13 22:36:38 +0000829
Nate Begemand77e59e2008-02-11 04:19:36 +0000830 if (Subtarget->hasSSE41()) {
831 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000832 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000833
834 // i8 and i16 vectors are custom , because the source register and source
835 // source memory operand types are not the same width. f32 vectors are
836 // custom since the immediate controlling the insert encodes additional
837 // information.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000838 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
839 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
840 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000842
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000843 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
844 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
845 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
846 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begemand77e59e2008-02-11 04:19:36 +0000847
848 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000849 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begemand77e59e2008-02-11 04:19:36 +0000851 }
852 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853
Nate Begeman03605a02008-07-17 16:51:19 +0000854 if (Subtarget->hasSSE42()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000855 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman03605a02008-07-17 16:51:19 +0000856 }
Scott Michel91099d62009-02-17 22:15:04 +0000857
David Greenea5acb6e2009-06-29 16:47:10 +0000858 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000859 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
860 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
861 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
862 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greeneed1b3db2009-06-29 22:50:51 +0000863
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000864 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
865 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
866 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
867 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
868 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
869 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
870 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
871 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
872 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
873 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
874 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
875 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
876 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
877 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
878 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000879
880 // Operations to consider commented out -v16i16 v32i8
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000881 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
882 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
883 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
884 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
885 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
886 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
887 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
888 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
889 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
890 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
891 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
892 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
893 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
894 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000895
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000896 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
897 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
898 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
899 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000900
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000901 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
902 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
903 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
905 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000906
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000907 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
908 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
909 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
910 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
911 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
912 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greenea5acb6e2009-06-29 16:47:10 +0000913
914#if 0
915 // Not sure we want to do this since there are no 256-bit integer
916 // operations in AVX
917
918 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
919 // This includes 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000920 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
921 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000922
923 // Do not attempt to custom lower non-power-of-2 vectors
924 if (!isPowerOf2_32(VT.getVectorNumElements()))
925 continue;
926
927 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
928 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
929 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
930 }
931
932 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000933 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
934 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopher3d82bbd2009-08-27 18:07:15 +0000935 }
David Greenea5acb6e2009-06-29 16:47:10 +0000936#endif
937
938#if 0
939 // Not sure we want to do this since there are no 256-bit integer
940 // operations in AVX
941
942 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
943 // Including 256-bit vectors
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000944 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
945 EVT VT = (MVT::SimpleValueType)i;
David Greenea5acb6e2009-06-29 16:47:10 +0000946
947 if (!VT.is256BitVector()) {
948 continue;
949 }
950 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000951 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000952 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000953 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000954 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000955 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000956 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000957 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000958 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000959 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greenea5acb6e2009-06-29 16:47:10 +0000960 }
961
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000962 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greenea5acb6e2009-06-29 16:47:10 +0000963#endif
964 }
965
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 // We want to custom lower some of our intrinsics.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000967 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968
Bill Wendling7e04be62008-12-09 22:08:41 +0000969 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000970 setOperationAction(ISD::SADDO, MVT::i32, Custom);
971 setOperationAction(ISD::SADDO, MVT::i64, Custom);
972 setOperationAction(ISD::UADDO, MVT::i32, Custom);
973 setOperationAction(ISD::UADDO, MVT::i64, Custom);
974 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
975 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
976 setOperationAction(ISD::USUBO, MVT::i32, Custom);
977 setOperationAction(ISD::USUBO, MVT::i64, Custom);
978 setOperationAction(ISD::SMULO, MVT::i32, Custom);
979 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling4c134df2008-11-24 19:21:46 +0000980
Evan Cheng9c215602009-03-31 19:38:51 +0000981 if (!Subtarget->is64Bit()) {
982 // These libcalls are not available in 32-bit.
983 setLibcallName(RTLIB::SHL_I128, 0);
984 setLibcallName(RTLIB::SRL_I128, 0);
985 setLibcallName(RTLIB::SRA_I128, 0);
986 }
987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 // We have target-specific dag combine patterns for the following nodes:
989 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chenge9b9c672008-05-09 21:53:03 +0000990 setTargetDAGCombine(ISD::BUILD_VECTOR);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000991 setTargetDAGCombine(ISD::SELECT);
sampo025b75c2009-01-26 00:52:55 +0000992 setTargetDAGCombine(ISD::SHL);
993 setTargetDAGCombine(ISD::SRA);
994 setTargetDAGCombine(ISD::SRL);
Evan Cheng10957b82010-01-04 21:22:48 +0000995 setTargetDAGCombine(ISD::OR);
Chris Lattnerce84ae42008-02-22 02:09:43 +0000996 setTargetDAGCombine(ISD::STORE);
Owen Anderson58155b22009-06-29 18:04:45 +0000997 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Chengedeb1692009-12-16 00:53:11 +0000998 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng04ecee12009-03-28 05:57:29 +0000999 if (Subtarget->is64Bit())
1000 setTargetDAGCombine(ISD::MUL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001
1002 computeRegisterProperties();
1003
Mon P Wangc707f3f2009-11-30 02:42:02 +00001004 // Divide and reminder operations have no vector equivalent and can
1005 // trap. Do a custom widening for these operations in which we never
1006 // generate more divides/remainder than the original vector width.
1007 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1008 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1009 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1010 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1011 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1012 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1013 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1014 }
1015 }
1016
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001017 // FIXME: These should be based on subtarget info. Plus, the values should
1018 // be smaller when we are in optimizing for size mode.
Dan Gohman97fab242008-06-30 21:00:56 +00001019 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1020 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1021 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Cheng45c1edb2008-02-28 00:43:03 +00001022 setPrefLoopAlignment(16);
Evan Cheng79566822009-05-13 21:42:09 +00001023 benefitFromCodePlacementOpt = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024}
1025
Scott Michel502151f2008-03-10 15:42:14 +00001026
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001027MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1028 return MVT::i8;
Scott Michel502151f2008-03-10 15:42:14 +00001029}
1030
1031
Evan Cheng5a67b812008-01-23 23:17:41 +00001032/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1033/// the desired ByVal argument alignment.
1034static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1035 if (MaxAlign == 16)
1036 return;
1037 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1038 if (VTy->getBitWidth() == 128)
1039 MaxAlign = 16;
Evan Cheng5a67b812008-01-23 23:17:41 +00001040 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1041 unsigned EltAlign = 0;
1042 getMaxByValAlign(ATy->getElementType(), EltAlign);
1043 if (EltAlign > MaxAlign)
1044 MaxAlign = EltAlign;
1045 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1046 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1047 unsigned EltAlign = 0;
1048 getMaxByValAlign(STy->getElementType(i), EltAlign);
1049 if (EltAlign > MaxAlign)
1050 MaxAlign = EltAlign;
1051 if (MaxAlign == 16)
1052 break;
1053 }
1054 }
1055 return;
1056}
1057
1058/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1059/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesena58b8622008-02-08 19:48:20 +00001060/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1061/// are at 4-byte boundaries.
Evan Cheng5a67b812008-01-23 23:17:41 +00001062unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001063 if (Subtarget->is64Bit()) {
1064 // Max of 8 and alignment of type.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00001065 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00001066 if (TyAlign > 8)
1067 return TyAlign;
1068 return 8;
1069 }
1070
Evan Cheng5a67b812008-01-23 23:17:41 +00001071 unsigned Align = 4;
Dale Johannesena58b8622008-02-08 19:48:20 +00001072 if (Subtarget->hasSSE1())
1073 getMaxByValAlign(Ty, Align);
Evan Cheng5a67b812008-01-23 23:17:41 +00001074 return Align;
1075}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001076
Evan Cheng8c590372008-05-15 08:39:06 +00001077/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng2f1033e2008-05-15 22:13:02 +00001078/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001079/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Cheng8c590372008-05-15 08:39:06 +00001080/// determining it.
Owen Andersonac9de032009-08-10 22:56:29 +00001081EVT
Evan Cheng8c590372008-05-15 08:39:06 +00001082X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patelc386c842009-06-05 21:57:13 +00001083 bool isSrcConst, bool isSrcStr,
1084 SelectionDAG &DAG) const {
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001085 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1086 // linux. This is because the stack realignment code can't handle certain
1087 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patelc386c842009-06-05 21:57:13 +00001088 const Function *F = DAG.getMachineFunction().getFunction();
1089 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1090 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001091 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001092 return MVT::v4i32;
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001093 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001094 return MVT::v4f32;
Chris Lattnerf0bf1062008-10-28 05:49:35 +00001095 }
Evan Cheng8c590372008-05-15 08:39:06 +00001096 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001097 return MVT::i64;
1098 return MVT::i32;
Evan Cheng8c590372008-05-15 08:39:06 +00001099}
1100
Chris Lattner25525cd2010-01-25 23:38:14 +00001101/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1102/// current function. The returned value is a member of the
1103/// MachineJumpTableInfo::JTEntryKind enum.
1104unsigned X86TargetLowering::getJumpTableEncoding() const {
1105 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1106 // symbol.
1107 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1108 Subtarget->isPICStyleGOT())
Chris Lattner82411c42010-01-26 05:02:42 +00001109 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner25525cd2010-01-25 23:38:14 +00001110
1111 // Otherwise, use the normal jump table encoding heuristics.
1112 return TargetLowering::getJumpTableEncoding();
1113}
1114
Chris Lattner541d8902010-01-26 06:28:43 +00001115/// getPICBaseSymbol - Return the X86-32 PIC base.
1116MCSymbol *
1117X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1118 MCContext &Ctx) const {
1119 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
1120 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1121 Twine(MF->getFunctionNumber())+"$pb");
1122}
1123
1124
Chris Lattner82411c42010-01-26 05:02:42 +00001125const MCExpr *
1126X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1127 const MachineBasicBlock *MBB,
1128 unsigned uid,MCContext &Ctx) const{
1129 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1130 Subtarget->isPICStyleGOT());
1131 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1132 // entries.
1133
1134 // FIXME: @GOTOFF should be a property of MCSymbolRefExpr not in the MCSymbol.
1135 std::string Name = MBB->getSymbol(Ctx)->getName() + "@GOTOFF";
1136 return MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(StringRef(Name)), Ctx);
1137}
1138
Evan Cheng6fb06762007-11-09 01:32:10 +00001139/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1140/// jumptable.
Dan Gohman8181bd12008-07-27 21:46:04 +00001141SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner541d8902010-01-26 06:28:43 +00001142 SelectionDAG &DAG) const {
Chris Lattneraa7c6d22009-07-09 03:15:51 +00001143 if (!Subtarget->is64Bit())
Dale Johannesen24dd9a52009-02-07 00:55:49 +00001144 // This doesn't have DebugLoc associated with it, but is not really the
1145 // same as a Register.
1146 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1147 getPointerTy());
Evan Cheng6fb06762007-11-09 01:32:10 +00001148 return Table;
1149}
1150
Chris Lattner541d8902010-01-26 06:28:43 +00001151/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1152/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1153/// MCExpr.
1154const MCExpr *X86TargetLowering::
1155getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1156 MCContext &Ctx) const {
1157 // X86-64 uses RIP relative addressing based on the jump table label.
1158 if (Subtarget->isPICStyleRIPRel())
1159 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1160
1161 // Otherwise, the reference is relative to the PIC base.
1162 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1163}
1164
Bill Wendling045f2632009-07-01 18:50:55 +00001165/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling25a8ae32009-06-30 22:38:32 +00001166unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman4f6b95c2009-08-18 00:20:06 +00001167 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling25a8ae32009-06-30 22:38:32 +00001168}
1169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170//===----------------------------------------------------------------------===//
1171// Return Value Calling Convention Implementation
1172//===----------------------------------------------------------------------===//
1173
1174#include "X86GenCallingConv.inc"
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001175
Kenneth Uildriks87d04262009-11-07 02:11:54 +00001176bool
1177X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1178 const SmallVectorImpl<EVT> &OutTys,
1179 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1180 SelectionDAG &DAG) {
1181 SmallVector<CCValAssign, 16> RVLocs;
1182 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1183 RVLocs, *DAG.getContext());
1184 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1185}
1186
Dan Gohman9178de12009-08-05 01:29:28 +00001187SDValue
1188X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001189 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001190 const SmallVectorImpl<ISD::OutputArg> &Outs,
1191 DebugLoc dl, SelectionDAG &DAG) {
Scott Michel91099d62009-02-17 22:15:04 +00001192
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001193 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001194 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1195 RVLocs, *DAG.getContext());
1196 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001197
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001198 // If this is the first return lowered for this function, add the regs to the
1199 // liveout set for the function.
Chris Lattner1b989192007-12-31 04:13:23 +00001200 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001201 for (unsigned i = 0; i != RVLocs.size(); ++i)
1202 if (RVLocs[i].isRegLoc())
Chris Lattner1b989192007-12-31 04:13:23 +00001203 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001204 }
Scott Michel91099d62009-02-17 22:15:04 +00001205
Dan Gohman8181bd12008-07-27 21:46:04 +00001206 SDValue Flag;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001207
Dan Gohman8181bd12008-07-27 21:46:04 +00001208 SmallVector<SDValue, 6> RetOps;
Chris Lattnerb56cc342008-03-11 03:23:40 +00001209 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1210 // Operand #1 = Bytes To Pop
Dan Gohmane84197b2009-09-03 17:18:51 +00001211 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michel91099d62009-02-17 22:15:04 +00001212
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001213 // Copy the result values into the output registers.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001214 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1215 CCValAssign &VA = RVLocs[i];
1216 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman9178de12009-08-05 01:29:28 +00001217 SDValue ValToCopy = Outs[i].Val;
Scott Michel91099d62009-02-17 22:15:04 +00001218
Chris Lattnerb56cc342008-03-11 03:23:40 +00001219 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1220 // the RET instruction and handled by the FP Stackifier.
Dan Gohman6c4be722009-02-04 17:28:58 +00001221 if (VA.getLocReg() == X86::ST0 ||
1222 VA.getLocReg() == X86::ST1) {
Chris Lattnerb56cc342008-03-11 03:23:40 +00001223 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1224 // change the value to the FP stack register class.
Dan Gohman6c4be722009-02-04 17:28:58 +00001225 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001226 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattnerb56cc342008-03-11 03:23:40 +00001227 RetOps.push_back(ValToCopy);
1228 // Don't emit a copytoreg.
1229 continue;
1230 }
Dale Johannesena585daf2008-06-24 22:01:44 +00001231
Evan Chengef356282009-02-23 09:03:22 +00001232 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1233 // which is returned in RAX / RDX.
Evan Chenge8db6e02009-02-22 08:05:12 +00001234 if (Subtarget->is64Bit()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001235 EVT ValVT = ValToCopy.getValueType();
Evan Chengef356282009-02-23 09:03:22 +00001236 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001237 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001238 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001239 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Chengef356282009-02-23 09:03:22 +00001240 }
Evan Chenge8db6e02009-02-22 08:05:12 +00001241 }
1242
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001243 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 Flag = Chain.getValue(1);
1245 }
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001246
1247 // The x86-64 ABI for returning structs by value requires that we copy
1248 // the sret argument into %rax for the return. We saved the argument into
1249 // a virtual register in the entry block, so now we copy the value out
1250 // and into %rax.
1251 if (Subtarget->is64Bit() &&
1252 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1253 MachineFunction &MF = DAG.getMachineFunction();
1254 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1255 unsigned Reg = FuncInfo->getSRetReturnReg();
1256 if (!Reg) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001257 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001258 FuncInfo->setSRetReturnReg(Reg);
1259 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001260 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001261
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001262 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001263 Flag = Chain.getValue(1);
Dan Gohman1c738f52009-10-12 16:36:12 +00001264
1265 // RAX now acts like a return value.
1266 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001267 }
Scott Michel91099d62009-02-17 22:15:04 +00001268
Chris Lattnerb56cc342008-03-11 03:23:40 +00001269 RetOps[0] = Chain; // Update chain.
1270
1271 // Add the flag if we have it.
Gabor Greif1c80d112008-08-28 21:40:38 +00001272 if (Flag.getNode())
Chris Lattnerb56cc342008-03-11 03:23:40 +00001273 RetOps.push_back(Flag);
Scott Michel91099d62009-02-17 22:15:04 +00001274
1275 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001276 MVT::Other, &RetOps[0], RetOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277}
1278
Dan Gohman9178de12009-08-05 01:29:28 +00001279/// LowerCallResult - Lower the result values of a call into the
1280/// appropriate copies out of appropriate physical registers.
1281///
1282SDValue
1283X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001284 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman9178de12009-08-05 01:29:28 +00001285 const SmallVectorImpl<ISD::InputArg> &Ins,
1286 DebugLoc dl, SelectionDAG &DAG,
1287 SmallVectorImpl<SDValue> &InVals) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001289 // Assign locations to each value returned by this call.
1290 SmallVector<CCValAssign, 16> RVLocs;
Edwin Törökaf8e1332009-02-01 18:15:56 +00001291 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman9178de12009-08-05 01:29:28 +00001292 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Anderson175b6542009-07-22 00:24:57 +00001293 RVLocs, *DAG.getContext());
Dan Gohman9178de12009-08-05 01:29:28 +00001294 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michel91099d62009-02-17 22:15:04 +00001295
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 // Copy all of the result registers out of their specified physreg.
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001297 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman6c4be722009-02-04 17:28:58 +00001298 CCValAssign &VA = RVLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001299 EVT CopyVT = VA.getValVT();
Scott Michel91099d62009-02-17 22:15:04 +00001300
Edwin Törökaf8e1332009-02-01 18:15:56 +00001301 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001302 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman9178de12009-08-05 01:29:28 +00001303 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Edwin Török2b331342009-07-08 19:04:27 +00001304 llvm_report_error("SSE register return with SSE disabled");
Edwin Törökaf8e1332009-02-01 18:15:56 +00001305 }
1306
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001307 // If this is a call to a function that returns an fp value on the floating
1308 // point stack, but where we prefer to use the value in xmm registers, copy
1309 // 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 +00001310 if ((VA.getLocReg() == X86::ST0 ||
1311 VA.getLocReg() == X86::ST1) &&
1312 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001313 CopyVT = MVT::f80;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 }
Scott Michel91099d62009-02-17 22:15:04 +00001315
Evan Cheng9cc600e2009-02-20 20:43:02 +00001316 SDValue Val;
1317 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Chengef356282009-02-23 09:03:22 +00001318 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1319 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1320 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001321 MVT::v2i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001322 Val = Chain.getValue(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001323 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1324 Val, DAG.getConstant(0, MVT::i64));
Evan Chengef356282009-02-23 09:03:22 +00001325 } else {
1326 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001327 MVT::i64, InFlag).getValue(1);
Evan Chengef356282009-02-23 09:03:22 +00001328 Val = Chain.getValue(0);
1329 }
Evan Cheng9cc600e2009-02-20 20:43:02 +00001330 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1331 } else {
1332 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1333 CopyVT, InFlag).getValue(1);
1334 Val = Chain.getValue(0);
1335 }
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001336 InFlag = Chain.getValue(2);
Chris Lattner40758732007-12-29 06:41:28 +00001337
Dan Gohman6c4be722009-02-04 17:28:58 +00001338 if (CopyVT != VA.getValVT()) {
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001339 // Round the F80 the right size, which also moves to the appropriate xmm
1340 // register.
Dan Gohman6c4be722009-02-04 17:28:58 +00001341 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattnere22e1fb2008-03-10 21:08:41 +00001342 // This truncation won't change the value.
1343 DAG.getIntPtrConstant(1));
1344 }
Scott Michel91099d62009-02-17 22:15:04 +00001345
Dan Gohman9178de12009-08-05 01:29:28 +00001346 InVals.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001347 }
Duncan Sands698842f2008-07-02 17:40:58 +00001348
Dan Gohman9178de12009-08-05 01:29:28 +00001349 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001350}
1351
1352
1353//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001354// C & StdCall & Fast Calling Convention implementation
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001355//===----------------------------------------------------------------------===//
1356// StdCall calling convention seems to be standard for many Windows' API
1357// routines and around. It differs from C calling convention just a little:
1358// callee should clean up the stack, not caller. Symbols should be also
1359// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001360// For info on fast calling convention see Fast Calling Convention (tail call)
1361// implementation LowerX86_32FastCCCallTo.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001362
Dan Gohman9178de12009-08-05 01:29:28 +00001363/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001364/// semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001365static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1366 if (Outs.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001367 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001368
Dan Gohman9178de12009-08-05 01:29:28 +00001369 return Outs[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001370}
1371
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001372/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001373/// return semantics.
Dan Gohman9178de12009-08-05 01:29:28 +00001374static bool
1375ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1376 if (Ins.empty())
Gordon Henriksen18ace102008-01-05 16:56:59 +00001377 return false;
Duncan Sandsc93fae32008-03-21 09:14:45 +00001378
Dan Gohman9178de12009-08-05 01:29:28 +00001379 return Ins[0].Flags.isSRet();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001380}
1381
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001382/// IsCalleePop - Determines whether the callee is required to pop its
1383/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001384bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen18ace102008-01-05 16:56:59 +00001385 if (IsVarArg)
1386 return false;
1387
Dan Gohman705e3f72008-09-13 01:54:27 +00001388 switch (CallingConv) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001389 default:
1390 return false;
1391 case CallingConv::X86_StdCall:
1392 return !Subtarget->is64Bit();
1393 case CallingConv::X86_FastCall:
1394 return !Subtarget->is64Bit();
1395 case CallingConv::Fast:
1396 return PerformTailCallOpt;
1397 }
1398}
1399
Dan Gohman705e3f72008-09-13 01:54:27 +00001400/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1401/// given CallingConvention value.
Sandeep Patel5838baa2009-09-02 08:44:58 +00001402CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001403 if (Subtarget->is64Bit()) {
Anton Korobeynikov06d49b02008-03-22 20:57:27 +00001404 if (Subtarget->isTargetWin64())
Anton Korobeynikov99bd1882008-03-22 20:37:30 +00001405 return CC_X86_Win64_C;
Evan Chengded8f902008-09-07 09:07:23 +00001406 else
1407 return CC_X86_64_C;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00001408 }
1409
Gordon Henriksen18ace102008-01-05 16:56:59 +00001410 if (CC == CallingConv::X86_FastCall)
1411 return CC_X86_32_FastCall;
Evan Chenga9d15b92008-09-10 18:25:29 +00001412 else if (CC == CallingConv::Fast)
1413 return CC_X86_32_FastCC;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001414 else
1415 return CC_X86_32_C;
1416}
1417
Dan Gohman9178de12009-08-05 01:29:28 +00001418/// NameDecorationForCallConv - Selects the appropriate decoration to
1419/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen18ace102008-01-05 16:56:59 +00001420NameDecorationStyle
Sandeep Patel5838baa2009-09-02 08:44:58 +00001421X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman9178de12009-08-05 01:29:28 +00001422 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001423 return FastCall;
Dan Gohman9178de12009-08-05 01:29:28 +00001424 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001425 return StdCall;
1426 return None;
1427}
1428
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001429
Arnold Schwaighofer56653e32008-02-26 17:50:59 +00001430/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1431/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001432/// the specific parameter attribute. The copy will be passed as a byval
1433/// function parameter.
Scott Michel91099d62009-02-17 22:15:04 +00001434static SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00001435CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001436 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1437 DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001438 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001439 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001440 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001441}
1442
Evan Cheng6b6ed592010-01-27 00:07:07 +00001443/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1444/// a tailcall target by changing its ABI.
1445static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
1446 return PerformTailCallOpt && CC == CallingConv::Fast;
1447}
1448
Dan Gohman9178de12009-08-05 01:29:28 +00001449SDValue
1450X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001451 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001452 const SmallVectorImpl<ISD::InputArg> &Ins,
1453 DebugLoc dl, SelectionDAG &DAG,
1454 const CCValAssign &VA,
1455 MachineFrameInfo *MFI,
1456 unsigned i) {
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001457 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001458 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng6b6ed592010-01-27 00:07:07 +00001459 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001460 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001461 EVT ValVT;
1462
1463 // If value is passed by pointer we have address passed instead of the value
1464 // itself.
1465 if (VA.getLocInfo() == CCValAssign::Indirect)
1466 ValVT = VA.getLocVT();
1467 else
1468 ValVT = VA.getValVT();
Evan Cheng3e42a522008-01-10 02:24:25 +00001469
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001470 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michel91099d62009-02-17 22:15:04 +00001471 // changed with more analysis.
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001472 // In case of tail call optimization mark all arguments mutable. Since they
1473 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001474 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene6424ab92009-11-12 20:49:22 +00001475 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00001476 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sandsc93fae32008-03-21 09:14:45 +00001477 if (Flags.isByVal())
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001478 return FIN;
Anton Korobeynikov5e9f7e82009-08-14 18:19:10 +00001479 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng1f996572009-10-17 07:53:04 +00001480 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola03cbeb72007-09-14 15:48:13 +00001481}
1482
Dan Gohman8181bd12008-07-27 21:46:04 +00001483SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001484X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001485 CallingConv::ID CallConv,
Dan Gohman9178de12009-08-05 01:29:28 +00001486 bool isVarArg,
1487 const SmallVectorImpl<ISD::InputArg> &Ins,
1488 DebugLoc dl,
1489 SelectionDAG &DAG,
1490 SmallVectorImpl<SDValue> &InVals) {
1491
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001493 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michel91099d62009-02-17 22:15:04 +00001494
Gordon Henriksen18ace102008-01-05 16:56:59 +00001495 const Function* Fn = MF.getFunction();
1496 if (Fn->hasExternalLinkage() &&
1497 Subtarget->isTargetCygMing() &&
1498 Fn->getName() == "main")
1499 FuncInfo->setForceFramePointer(true);
1500
1501 // Decorate the function name.
Dan Gohman9178de12009-08-05 01:29:28 +00001502 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001503
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen18ace102008-01-05 16:56:59 +00001505 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001506 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001507
Dan Gohman9178de12009-08-05 01:29:28 +00001508 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001509 "Var args not supported with calling convention fastcc");
1510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 // Assign locations to all of the incoming arguments.
1512 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001513 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1514 ArgLocs, *DAG.getContext());
1515 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001516
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001517 unsigned LastVal = ~0U;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001518 SDValue ArgValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001519 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1520 CCValAssign &VA = ArgLocs[i];
1521 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1522 // places.
1523 assert(VA.getValNo() != LastVal &&
1524 "Don't support value assigned to multiple locs yet");
1525 LastVal = VA.getValNo();
Scott Michel91099d62009-02-17 22:15:04 +00001526
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001527 if (VA.isRegLoc()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001528 EVT RegVT = VA.getLocVT();
Devang Patelf3707e82009-01-05 17:31:22 +00001529 TargetRegisterClass *RC = NULL;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001530 if (RegVT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531 RC = X86::GR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001532 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001533 RC = X86::GR64RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001534 else if (RegVT == MVT::f32)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001535 RC = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001536 else if (RegVT == MVT::f64)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001537 RC = X86::FR64RegisterClass;
Duncan Sands92c43912008-06-06 12:08:01 +00001538 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengf5af6fe2008-04-25 07:56:45 +00001539 RC = X86::VR128RegisterClass;
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001540 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1541 RC = X86::VR64RegisterClass;
1542 else
Edwin Törökbd448e32009-07-14 16:55:14 +00001543 llvm_unreachable("Unknown argument type!");
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001544
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001545 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman9178de12009-08-05 01:29:28 +00001546 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michel91099d62009-02-17 22:15:04 +00001547
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001548 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1549 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1550 // right size.
1551 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001552 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553 DAG.getValueType(VA.getValVT()));
1554 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesence0805b2009-02-03 19:33:06 +00001555 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001557 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikova6ad5be2009-08-03 08:14:14 +00001558 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michel91099d62009-02-17 22:15:04 +00001559
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001560 if (VA.isExtInLoc()) {
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001561 // Handle MMX values passed in XMM regs.
1562 if (RegVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001563 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1564 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001565 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1566 } else
1567 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Chengad6980b2008-04-25 20:13:28 +00001568 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 } else {
1570 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00001571 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001572 }
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001573
1574 // If value is passed via pointer - do a load.
1575 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman9178de12009-08-05 01:29:28 +00001576 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001577
Dan Gohman9178de12009-08-05 01:29:28 +00001578 InVals.push_back(ArgValue);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001579 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001580
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001581 // The x86-64 ABI for returning structs by value requires that we copy
1582 // the sret argument into %rax for the return. Save the argument into
1583 // a virtual register so that we can access it from the return points.
Dan Gohmanc21d06a2009-08-01 19:14:37 +00001584 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001585 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1586 unsigned Reg = FuncInfo->getSRetReturnReg();
1587 if (!Reg) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001588 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001589 FuncInfo->setSRetReturnReg(Reg);
1590 }
Dan Gohman9178de12009-08-05 01:29:28 +00001591 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001592 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohmanb47dabd2008-04-21 23:59:07 +00001593 }
1594
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng6b6ed592010-01-27 00:07:07 +00001596 // Align stack specially for tail calls.
1597 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001598 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001599
1600 // If the function takes variable number of arguments, make a frame index for
1601 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001602 if (isVarArg) {
Dan Gohman9178de12009-08-05 01:29:28 +00001603 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene6424ab92009-11-12 20:49:22 +00001604 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001605 }
1606 if (Is64Bit) {
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001607 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1608
1609 // FIXME: We should really autogenerate these arrays
1610 static const unsigned GPR64ArgRegsWin64[] = {
1611 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen18ace102008-01-05 16:56:59 +00001612 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001613 static const unsigned XMMArgRegsWin64[] = {
1614 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1615 };
1616 static const unsigned GPR64ArgRegs64Bit[] = {
1617 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1618 };
1619 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001620 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1621 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1622 };
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001623 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1624
1625 if (IsWin64) {
1626 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1627 GPR64ArgRegs = GPR64ArgRegsWin64;
1628 XMMArgRegs = XMMArgRegsWin64;
1629 } else {
1630 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1631 GPR64ArgRegs = GPR64ArgRegs64Bit;
1632 XMMArgRegs = XMMArgRegs64Bit;
1633 }
1634 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1635 TotalNumIntRegs);
1636 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1637 TotalNumXMMRegs);
1638
Devang Patelc386c842009-06-05 21:57:13 +00001639 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Cheng0b84fe12009-02-13 22:36:38 +00001640 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Edwin Törökaf8e1332009-02-01 18:15:56 +00001641 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001642 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Cheng0b84fe12009-02-13 22:36:38 +00001643 "SSE register cannot be used when SSE is disabled!");
Devang Patelc386c842009-06-05 21:57:13 +00001644 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Edwin Törökaf8e1332009-02-01 18:15:56 +00001645 // Kernel mode asks for SSE to be disabled, so don't push them
1646 // on the stack.
1647 TotalNumXMMRegs = 0;
Bill Wendling042eda32009-03-11 22:30:01 +00001648
Gordon Henriksen18ace102008-01-05 16:56:59 +00001649 // For X86-64, if there are vararg parameters that are passed via
1650 // registers, then we must store them to their spots on the stack so they
1651 // may be loaded by deferencing the result of va_next.
1652 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001653 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1654 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene6424ab92009-11-12 20:49:22 +00001655 TotalNumXMMRegs * 16, 16,
1656 false);
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001657
Gordon Henriksen18ace102008-01-05 16:56:59 +00001658 // Store the integer parameter registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001659 SmallVector<SDValue, 8> MemOps;
1660 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman34228bf2009-08-15 01:38:56 +00001661 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001662 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohman34228bf2009-08-15 01:38:56 +00001663 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1664 DAG.getIntPtrConstant(Offset));
Bob Wilsonb6737aa2009-04-20 18:36:57 +00001665 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1666 X86::GR64RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001667 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +00001668 SDValue Store =
Dale Johannesence0805b2009-02-03 19:33:06 +00001669 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Cheng174e2cf2009-10-18 18:16:27 +00001670 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohman34228bf2009-08-15 01:38:56 +00001671 Offset);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001672 MemOps.push_back(Store);
Dan Gohman34228bf2009-08-15 01:38:56 +00001673 Offset += 8;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001674 }
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001675
Dan Gohmanb9f06832009-08-16 21:24:25 +00001676 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1677 // Now store the XMM (fp + vector) parameter registers.
1678 SmallVector<SDValue, 11> SaveXMMOps;
1679 SaveXMMOps.push_back(Chain);
Dan Gohman34228bf2009-08-15 01:38:56 +00001680
Dan Gohmanb9f06832009-08-16 21:24:25 +00001681 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1682 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1683 SaveXMMOps.push_back(ALVal);
Dan Gohman34228bf2009-08-15 01:38:56 +00001684
Dan Gohmanb9f06832009-08-16 21:24:25 +00001685 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1686 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohman34228bf2009-08-15 01:38:56 +00001687
Dan Gohmanb9f06832009-08-16 21:24:25 +00001688 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1689 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1690 X86::VR128RegisterClass);
1691 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1692 SaveXMMOps.push_back(Val);
1693 }
1694 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1695 MVT::Other,
1696 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen18ace102008-01-05 16:56:59 +00001697 }
Dan Gohmanb9f06832009-08-16 21:24:25 +00001698
1699 if (!MemOps.empty())
1700 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1701 &MemOps[0], MemOps.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00001702 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001703 }
Scott Michel91099d62009-02-17 22:15:04 +00001704
Gordon Henriksen18ace102008-01-05 16:56:59 +00001705 // Some CCs need callee pop.
Dan Gohman9178de12009-08-05 01:29:28 +00001706 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen18ace102008-01-05 16:56:59 +00001707 BytesToPopOnReturn = StackSize; // Callee pops everything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 } else {
1709 BytesToPopOnReturn = 0; // Callee pops nothing.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001710 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman9178de12009-08-05 01:29:28 +00001711 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michel91099d62009-02-17 22:15:04 +00001712 BytesToPopOnReturn = 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001714
Gordon Henriksen18ace102008-01-05 16:56:59 +00001715 if (!Is64Bit) {
1716 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman9178de12009-08-05 01:29:28 +00001717 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen18ace102008-01-05 16:56:59 +00001718 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1719 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720
Anton Korobeynikove844e472007-08-15 17:12:32 +00001721 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722
Dan Gohman9178de12009-08-05 01:29:28 +00001723 return Chain;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001724}
1725
Dan Gohman8181bd12008-07-27 21:46:04 +00001726SDValue
Dan Gohman9178de12009-08-05 01:29:28 +00001727X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1728 SDValue StackPtr, SDValue Arg,
1729 DebugLoc dl, SelectionDAG &DAG,
Evan Chengbc077bf2008-01-10 00:09:10 +00001730 const CCValAssign &VA,
Dan Gohman9178de12009-08-05 01:29:28 +00001731 ISD::ArgFlagsTy Flags) {
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001732 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikov2cbcdb72009-08-03 08:12:53 +00001733 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman8181bd12008-07-27 21:46:04 +00001734 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesence0805b2009-02-03 19:33:06 +00001735 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sandsc93fae32008-03-21 09:14:45 +00001736 if (Flags.isByVal()) {
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001737 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengbc077bf2008-01-10 00:09:10 +00001738 }
Dale Johannesence0805b2009-02-03 19:33:06 +00001739 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohmanfb020b62008-02-07 18:41:25 +00001740 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengbc077bf2008-01-10 00:09:10 +00001741}
1742
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001743/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001744/// optimization is performed and it is required.
Scott Michel91099d62009-02-17 22:15:04 +00001745SDValue
1746X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Cheng00787d52010-01-26 19:04:47 +00001747 SDValue &OutRetAddr, SDValue Chain,
1748 bool IsTailCall, bool Is64Bit,
1749 int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001750 if (!IsTailCall || FPDiff==0) return Chain;
1751
1752 // Adjust the Return address stack slot.
Owen Andersonac9de032009-08-10 22:56:29 +00001753 EVT VT = getPointerTy();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001754 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling6ddc87b2009-01-16 19:25:27 +00001755
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001756 // Load the "old" Return address.
Dale Johannesence0805b2009-02-03 19:33:06 +00001757 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greif1c80d112008-08-28 21:40:38 +00001758 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001759}
1760
1761/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1762/// optimization is performed and it is required (FPDiff!=0).
Scott Michel91099d62009-02-17 22:15:04 +00001763static SDValue
1764EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman8181bd12008-07-27 21:46:04 +00001765 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesence0805b2009-02-03 19:33:06 +00001766 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001767 // Store the return address to the appropriate stack slot.
1768 if (!FPDiff) return Chain;
1769 // Calculate the new stack slot for the return address.
1770 int SlotSize = Is64Bit ? 8 : 4;
Scott Michel91099d62009-02-17 22:15:04 +00001771 int NewReturnAddrFI =
Evan Cheng00787d52010-01-26 19:04:47 +00001772 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, true,false);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001773 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman8181bd12008-07-27 21:46:04 +00001774 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michel91099d62009-02-17 22:15:04 +00001775 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng1f996572009-10-17 07:53:04 +00001776 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001777 return Chain;
1778}
1779
Dan Gohman9178de12009-08-05 01:29:28 +00001780SDValue
Evan Chengca6c9342010-02-02 21:29:10 +00001781X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee, const Type *RetTy,
Sandeep Patel5838baa2009-09-02 08:44:58 +00001782 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng6b6ed592010-01-27 00:07:07 +00001783 bool &isTailCall,
Dan Gohman9178de12009-08-05 01:29:28 +00001784 const SmallVectorImpl<ISD::OutputArg> &Outs,
1785 const SmallVectorImpl<ISD::InputArg> &Ins,
1786 DebugLoc dl, SelectionDAG &DAG,
1787 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman9178de12009-08-05 01:29:28 +00001788 MachineFunction &MF = DAG.getMachineFunction();
1789 bool Is64Bit = Subtarget->is64Bit();
1790 bool IsStructRet = CallIsStructReturn(Outs);
1791
Evan Cheng6b6ed592010-01-27 00:07:07 +00001792 if (isTailCall)
1793 // Check if it's really possible to do a tail call.
Evan Chengca6c9342010-02-02 21:29:10 +00001794 isTailCall = IsEligibleForTailCallOptimization(Callee, RetTy, CallConv,
1795 isVarArg, Outs, Ins, DAG);
Evan Cheng6b6ed592010-01-27 00:07:07 +00001796
Dan Gohman9178de12009-08-05 01:29:28 +00001797 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001798 "Var args not supported with calling convention fastcc");
1799
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001800 // Analyze operands of the call, assigning locations to each operand.
1801 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman9178de12009-08-05 01:29:28 +00001802 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1803 ArgLocs, *DAG.getContext());
1804 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michel91099d62009-02-17 22:15:04 +00001805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 // Get a count of how many bytes are to be pushed on the stack.
1807 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng6b6ed592010-01-27 00:07:07 +00001808 if (FuncIsMadeTailCallSafe(CallConv))
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00001809 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Evan Chengc38381c2010-02-02 02:22:50 +00001810 else if (isTailCall && !PerformTailCallOpt)
1811 // This is a sibcall. The memory operands are available in caller's
1812 // own caller's stack.
1813 NumBytes = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001814
Gordon Henriksen18ace102008-01-05 16:56:59 +00001815 int FPDiff = 0;
Dan Gohman9178de12009-08-05 01:29:28 +00001816 if (isTailCall) {
Evan Chengd82fae32010-01-27 06:25:16 +00001817 ++NumTailCalls;
1818
Gordon Henriksen18ace102008-01-05 16:56:59 +00001819 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michel91099d62009-02-17 22:15:04 +00001820 unsigned NumBytesCallerPushed =
Gordon Henriksen18ace102008-01-05 16:56:59 +00001821 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1822 FPDiff = NumBytesCallerPushed - NumBytes;
1823
1824 // Set the delta of movement of the returnaddr stackslot.
1825 // But only set if delta is greater than previous delta.
1826 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1827 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1828 }
1829
Chris Lattnerfe5d4022008-10-11 22:08:30 +00001830 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001831
Dan Gohman8181bd12008-07-27 21:46:04 +00001832 SDValue RetAddrFrIdx;
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001833 // Load return adress for tail calls.
Dan Gohman9178de12009-08-05 01:29:28 +00001834 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00001835 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001836
Dan Gohman8181bd12008-07-27 21:46:04 +00001837 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1838 SmallVector<SDValue, 8> MemOpChains;
1839 SDValue StackPtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001841 // Walk the register/memloc assignments, inserting copies/loads. In the case
1842 // of tail call optimization arguments are handle later.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001843 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1844 CCValAssign &VA = ArgLocs[i];
Owen Andersonac9de032009-08-10 22:56:29 +00001845 EVT RegVT = VA.getLocVT();
Dan Gohman9178de12009-08-05 01:29:28 +00001846 SDValue Arg = Outs[i].Val;
1847 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman705e3f72008-09-13 01:54:27 +00001848 bool isByVal = Flags.isByVal();
Scott Michel91099d62009-02-17 22:15:04 +00001849
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001850 // Promote the value if needed.
1851 switch (VA.getLocInfo()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001852 default: llvm_unreachable("Unknown loc info!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001853 case CCValAssign::Full: break;
1854 case CCValAssign::SExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001855 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001856 break;
1857 case CCValAssign::ZExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001858 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001859 break;
1860 case CCValAssign::AExt:
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001861 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1862 // Special case: passing MMX values in XMM registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001863 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1864 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1865 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov8485b632009-08-03 08:13:24 +00001866 } else
1867 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1868 break;
1869 case CCValAssign::BCvt:
1870 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001871 break;
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001872 case CCValAssign::Indirect: {
1873 // Store the argument.
1874 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Cheng174e2cf2009-10-18 18:16:27 +00001875 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001876 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00001877 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov78c31602009-08-03 08:13:56 +00001878 Arg = SpillSlot;
1879 break;
1880 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001881 }
Scott Michel91099d62009-02-17 22:15:04 +00001882
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 if (VA.isRegLoc()) {
1884 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1885 } else {
Dan Gohman9178de12009-08-05 01:29:28 +00001886 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001887 assert(VA.isMemLoc());
Gabor Greif1c80d112008-08-28 21:40:38 +00001888 if (StackPtr.getNode() == 0)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001889 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michel91099d62009-02-17 22:15:04 +00001890
Dan Gohman9178de12009-08-05 01:29:28 +00001891 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1892 dl, DAG, VA, Flags));
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001893 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894 }
1895 }
Scott Michel91099d62009-02-17 22:15:04 +00001896
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001897 if (!MemOpChains.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001898 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001899 &MemOpChains[0], MemOpChains.size());
1900
1901 // Build a sequence of copy-to-reg nodes chained together with token chain
1902 // and flag operands which copy the outgoing args into registers.
Dan Gohman8181bd12008-07-27 21:46:04 +00001903 SDValue InFlag;
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001904 // Tail call byval lowering might overwrite argument registers so in case of
1905 // tail call optimization the copies to registers are lowered later.
Dan Gohman9178de12009-08-05 01:29:28 +00001906 if (!isTailCall)
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001907 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00001908 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001909 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00001910 InFlag = Chain.getValue(1);
1911 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00001912
Eric Christopher3d82bbd2009-08-27 18:07:15 +00001913
Chris Lattnerf165d342009-07-09 04:24:46 +00001914 if (Subtarget->isPICStyleGOT()) {
Chris Lattner679cad52009-07-09 02:55:47 +00001915 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1916 // GOT pointer.
Dan Gohman9178de12009-08-05 01:29:28 +00001917 if (!isTailCall) {
Chris Lattner679cad52009-07-09 02:55:47 +00001918 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1919 DAG.getNode(X86ISD::GlobalBaseReg,
1920 DebugLoc::getUnknownLoc(),
1921 getPointerTy()),
1922 InFlag);
1923 InFlag = Chain.getValue(1);
1924 } else {
1925 // If we are tail calling and generating PIC/GOT style code load the
1926 // address of the callee into ECX. The value in ecx is used as target of
1927 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1928 // for tail calls on PIC/GOT architectures. Normally we would just put the
1929 // address of GOT into ebx and then call target@PLT. But for tail calls
1930 // ebx would be restored (since ebx is callee saved) before jumping to the
1931 // target@PLT.
1932
1933 // Note: The actual moving to ECX is done further down.
1934 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1935 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1936 !G->getGlobal()->hasProtectedVisibility())
1937 Callee = LowerGlobalAddress(Callee, DAG);
1938 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner5d1f2572009-07-09 04:39:06 +00001939 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattner679cad52009-07-09 02:55:47 +00001940 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001941 }
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00001942
Gordon Henriksen18ace102008-01-05 16:56:59 +00001943 if (Is64Bit && isVarArg) {
1944 // From AMD64 ABI document:
1945 // For calls that may call functions that use varargs or stdargs
1946 // (prototype-less calls or calls to functions containing ellipsis (...) in
1947 // the declaration) %al is used as hidden argument to specify the number
1948 // of SSE registers used. The contents of %al do not need to match exactly
1949 // the number of registers, but must be an ubound on the number of SSE
1950 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov1ded0db2008-04-27 23:15:03 +00001951
1952 // FIXME: Verify this on Win64
Gordon Henriksen18ace102008-01-05 16:56:59 +00001953 // Count the number of XMM registers allocated.
1954 static const unsigned XMMArgRegs[] = {
1955 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1956 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1957 };
1958 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michel91099d62009-02-17 22:15:04 +00001959 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Edwin Törökaf8e1332009-02-01 18:15:56 +00001960 && "SSE registers cannot be used when SSE is disabled");
Scott Michel91099d62009-02-17 22:15:04 +00001961
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00001962 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001963 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00001964 InFlag = Chain.getValue(1);
1965 }
1966
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001967
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001968 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman9178de12009-08-05 01:29:28 +00001969 if (isTailCall) {
1970 // Force all the incoming stack arguments to be loaded from the stack
1971 // before any new outgoing arguments are stored to the stack, because the
1972 // outgoing stack slots may alias the incoming argument stack slots, and
1973 // the alias isn't otherwise explicit. This is slightly more conservative
1974 // than necessary, because it means that each store effectively depends
1975 // on every argument instead of just those arguments it would clobber.
1976 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1977
Dan Gohman8181bd12008-07-27 21:46:04 +00001978 SmallVector<SDValue, 8> MemOpChains2;
1979 SDValue FIN;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001980 int FI = 0;
Arnold Schwaighofere2db0f42008-02-26 09:19:59 +00001981 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman8181bd12008-07-27 21:46:04 +00001982 InFlag = SDValue();
Evan Chengc38381c2010-02-02 02:22:50 +00001983 if (PerformTailCallOpt) {
1984 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1985 CCValAssign &VA = ArgLocs[i];
1986 if (VA.isRegLoc())
1987 continue;
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001988 assert(VA.isMemLoc());
Dan Gohman9178de12009-08-05 01:29:28 +00001989 SDValue Arg = Outs[i].Val;
1990 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen18ace102008-01-05 16:56:59 +00001991 // Create frame index.
1992 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands92c43912008-06-06 12:08:01 +00001993 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene6424ab92009-11-12 20:49:22 +00001994 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00001995 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighofer449b01a2008-01-11 16:49:42 +00001996
Duncan Sandsc93fae32008-03-21 09:14:45 +00001997 if (Flags.isByVal()) {
Evan Cheng5817a0e2008-01-12 01:08:07 +00001998 // Copy relative to framepointer.
Dan Gohman8181bd12008-07-27 21:46:04 +00001999 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greif1c80d112008-08-28 21:40:38 +00002000 if (StackPtr.getNode() == 0)
Scott Michel91099d62009-02-17 22:15:04 +00002001 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002002 getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00002003 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002004
Dan Gohman9178de12009-08-05 01:29:28 +00002005 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2006 ArgChain,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002007 Flags, DAG, dl));
Gordon Henriksen18ace102008-01-05 16:56:59 +00002008 } else {
Evan Cheng5817a0e2008-01-12 01:08:07 +00002009 // Store relative to framepointer.
Dan Gohman12a9c082008-02-06 22:27:42 +00002010 MemOpChains2.push_back(
Dan Gohman9178de12009-08-05 01:29:28 +00002011 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng1f996572009-10-17 07:53:04 +00002012 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michel91099d62009-02-17 22:15:04 +00002013 }
Gordon Henriksen18ace102008-01-05 16:56:59 +00002014 }
2015 }
2016
2017 if (!MemOpChains2.empty())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002018 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighoferdfb21302008-01-11 14:34:56 +00002019 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00002020
Arnold Schwaighofera0032722008-04-30 09:16:33 +00002021 // Copy arguments to their registers.
2022 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michel91099d62009-02-17 22:15:04 +00002023 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002024 RegsToPass[i].second, InFlag);
Arnold Schwaighofera0032722008-04-30 09:16:33 +00002025 InFlag = Chain.getValue(1);
2026 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002027 InFlag =SDValue();
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002028
Gordon Henriksen18ace102008-01-05 16:56:59 +00002029 // Store the return address to the appropriate stack slot.
Arnold Schwaighofera38df102008-04-12 18:11:06 +00002030 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesence0805b2009-02-03 19:33:06 +00002031 FPDiff, dl);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002032 }
2033
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002034 bool WasGlobalOrExternal = false;
2035 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2036 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2037 // In the 64-bit large code model, we have to make all calls
2038 // through a register, since the call instruction's 32-bit
2039 // pc-relative offset may not be large enough to hold the whole
2040 // address.
2041 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2042 WasGlobalOrExternal = true;
2043 // If the callee is a GlobalAddress node (quite common, every direct call
2044 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2045 // it.
2046
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002047 // We should use extra load for direct calls to dllimported functions in
2048 // non-JIT mode.
Chris Lattner48837612009-07-09 05:27:35 +00002049 GlobalValue *GV = G->getGlobal();
Chris Lattner180a7ee2009-07-10 05:48:03 +00002050 if (!GV->hasDLLImportLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002051 unsigned char OpFlags = 0;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002052
Chris Lattner8e8afe42009-07-09 05:02:21 +00002053 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2054 // external symbols most go through the PLT in PIC mode. If the symbol
2055 // has hidden or protected visibility, or if it is static or local, then
2056 // we don't need to use the PLT - we can directly call it.
2057 if (Subtarget->isTargetELF() &&
2058 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner48837612009-07-09 05:27:35 +00002059 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002060 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002061 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002062 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2063 Subtarget->getDarwinVers() < 9) {
2064 // PC-relative references to external symbols should go through $stub,
2065 // unless we're building with the leopard linker or later, which
2066 // automatically synthesizes these stubs.
2067 OpFlags = X86II::MO_DARWIN_STUB;
2068 }
Chris Lattner8e8afe42009-07-09 05:02:21 +00002069
Chris Lattner48837612009-07-09 05:27:35 +00002070 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner8e8afe42009-07-09 05:02:21 +00002071 G->getOffset(), OpFlags);
2072 }
Bill Wendlingfef06052008-09-16 21:48:12 +00002073 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002074 WasGlobalOrExternal = true;
Chris Lattner8e8afe42009-07-09 05:02:21 +00002075 unsigned char OpFlags = 0;
2076
2077 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2078 // symbols should go through the PLT.
2079 if (Subtarget->isTargetELF() &&
Chris Lattner48837612009-07-09 05:27:35 +00002080 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner8e8afe42009-07-09 05:02:21 +00002081 OpFlags = X86II::MO_PLT;
Chris Lattner4a948932009-07-10 20:47:30 +00002082 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner48837612009-07-09 05:27:35 +00002083 Subtarget->getDarwinVers() < 9) {
2084 // PC-relative references to external symbols should go through $stub,
2085 // unless we're building with the leopard linker or later, which
2086 // automatically synthesizes these stubs.
2087 OpFlags = X86II::MO_DARWIN_STUB;
2088 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002089
Chris Lattner8e8afe42009-07-09 05:02:21 +00002090 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2091 OpFlags);
Jeffrey Yasskine233d8a2009-11-16 22:41:33 +00002092 }
2093
2094 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighofera8726f02009-06-12 16:26:57 +00002095 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002096
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00002097 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michel91099d62009-02-17 22:15:04 +00002098 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen18ace102008-01-05 16:56:59 +00002099 Callee,InFlag);
2100 Callee = DAG.getRegister(Opc, getPointerTy());
2101 // Add register as live out.
Dan Gohmanc21d06a2009-08-01 19:14:37 +00002102 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002103 }
Scott Michel91099d62009-02-17 22:15:04 +00002104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 // Returns a chain & a flag for retval copy to use.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002106 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00002107 SmallVector<SDValue, 8> Ops;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002108
Dan Gohman9178de12009-08-05 01:29:28 +00002109 if (isTailCall) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002110 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2111 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002112 InFlag = Chain.getValue(1);
Gordon Henriksen18ace102008-01-05 16:56:59 +00002113 }
Scott Michel91099d62009-02-17 22:15:04 +00002114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115 Ops.push_back(Chain);
2116 Ops.push_back(Callee);
2117
Dan Gohman9178de12009-08-05 01:29:28 +00002118 if (isTailCall)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002119 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002120
Gordon Henriksen18ace102008-01-05 16:56:59 +00002121 // Add argument registers to the end of the list so that they are known live
2122 // into the call.
Evan Chenge14fc242008-01-07 23:08:23 +00002123 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2124 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2125 RegsToPass[i].second.getValueType()));
Scott Michel91099d62009-02-17 22:15:04 +00002126
Evan Cheng8ba45e62008-03-18 23:36:35 +00002127 // Add an implicit use GOT pointer in EBX.
Dan Gohman9178de12009-08-05 01:29:28 +00002128 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng8ba45e62008-03-18 23:36:35 +00002129 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2130
2131 // Add an implicit use of AL for x86 vararg functions.
2132 if (Is64Bit && isVarArg)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002133 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng8ba45e62008-03-18 23:36:35 +00002134
Gabor Greif1c80d112008-08-28 21:40:38 +00002135 if (InFlag.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002136 Ops.push_back(InFlag);
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002137
Dan Gohman9178de12009-08-05 01:29:28 +00002138 if (isTailCall) {
2139 // If this is the first return lowered for this function, add the regs
2140 // to the liveout set for the function.
2141 if (MF.getRegInfo().liveout_empty()) {
2142 SmallVector<CCValAssign, 16> RVLocs;
2143 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2144 *DAG.getContext());
2145 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2146 for (unsigned i = 0; i != RVLocs.size(); ++i)
2147 if (RVLocs[i].isRegLoc())
2148 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2149 }
Scott Michel91099d62009-02-17 22:15:04 +00002150
Dan Gohman9178de12009-08-05 01:29:28 +00002151 assert(((Callee.getOpcode() == ISD::Register &&
2152 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskin846123d2010-01-09 18:56:43 +00002153 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman9178de12009-08-05 01:29:28 +00002154 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2155 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskin846123d2010-01-09 18:56:43 +00002156 "Expecting a global address, external symbol, or scratch register");
Dan Gohman9178de12009-08-05 01:29:28 +00002157
2158 return DAG.getNode(X86ISD::TC_RETURN, dl,
2159 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen18ace102008-01-05 16:56:59 +00002160 }
2161
Dale Johannesence0805b2009-02-03 19:33:06 +00002162 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 InFlag = Chain.getValue(1);
2164
2165 // Create the CALLSEQ_END node.
Gordon Henriksen18ace102008-01-05 16:56:59 +00002166 unsigned NumBytesForCalleeToPush;
Dan Gohman9178de12009-08-05 01:29:28 +00002167 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen18ace102008-01-05 16:56:59 +00002168 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman9178de12009-08-05 01:29:28 +00002169 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 // If this is is a call to a struct-return function, the callee
2171 // pops the hidden struct pointer, so we have to push it back.
2172 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002173 NumBytesForCalleeToPush = 4;
Gordon Henriksen18ace102008-01-05 16:56:59 +00002174 else
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002175 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michel91099d62009-02-17 22:15:04 +00002176
Gordon Henriksen6bbcc672008-01-03 16:47:34 +00002177 // Returns a flag for retval copy to use.
Bill Wendling22f8deb2007-11-13 00:44:25 +00002178 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00002179 DAG.getIntPtrConstant(NumBytes, true),
2180 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2181 true),
Bill Wendling22f8deb2007-11-13 00:44:25 +00002182 InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002183 InFlag = Chain.getValue(1);
2184
2185 // Handle result values, copying them out of physregs into vregs that we
2186 // return.
Dan Gohman9178de12009-08-05 01:29:28 +00002187 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2188 Ins, dl, DAG, InVals);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002189}
2190
2191
2192//===----------------------------------------------------------------------===//
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002193// Fast Calling Convention (tail call) implementation
2194//===----------------------------------------------------------------------===//
2195
2196// Like std call, callee cleans arguments, convention except that ECX is
2197// reserved for storing the tail called function address. Only 2 registers are
2198// free for argument passing (inreg). Tail call optimization is performed
2199// provided:
2200// * tailcallopt is enabled
2201// * caller/callee are fastcc
Arnold Schwaighofer480c5672008-02-26 10:21:54 +00002202// On X86_64 architecture with GOT-style position independent code only local
2203// (within module) calls are supported at the moment.
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002204// To keep the stack aligned according to platform abi the function
2205// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2206// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002207// If a tail called function callee has more arguments than the caller the
2208// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer373e8652007-10-12 21:30:57 +00002209// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002210// original REtADDR, but before the saved framepointer or the spilled registers
2211// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2212// stack layout:
2213// arg1
2214// arg2
2215// RETADDR
Scott Michel91099d62009-02-17 22:15:04 +00002216// [ new RETADDR
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002217// move area ]
2218// (possible EBP)
2219// ESI
2220// EDI
2221// local1 ..
2222
2223/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2224/// for a 16 byte align requirement.
Scott Michel91099d62009-02-17 22:15:04 +00002225unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002226 SelectionDAG& DAG) {
Evan Chengded8f902008-09-07 09:07:23 +00002227 MachineFunction &MF = DAG.getMachineFunction();
2228 const TargetMachine &TM = MF.getTarget();
2229 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2230 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michel91099d62009-02-17 22:15:04 +00002231 uint64_t AlignMask = StackAlignment - 1;
Evan Chengded8f902008-09-07 09:07:23 +00002232 int64_t Offset = StackSize;
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00002233 uint64_t SlotSize = TD->getPointerSize();
Evan Chengded8f902008-09-07 09:07:23 +00002234 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2235 // Number smaller than 12 so just add the difference.
2236 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2237 } else {
2238 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michel91099d62009-02-17 22:15:04 +00002239 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chengded8f902008-09-07 09:07:23 +00002240 (StackAlignment-SlotSize);
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002241 }
Evan Chengded8f902008-09-07 09:07:23 +00002242 return Offset;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002243}
2244
Dan Gohman9178de12009-08-05 01:29:28 +00002245/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2246/// for tail call optimization. Targets which want to do tail call
2247/// optimization should implement this function.
2248bool
2249X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Evan Chengca6c9342010-02-02 21:29:10 +00002250 const Type *RetTy,
Sandeep Patel5838baa2009-09-02 08:44:58 +00002251 CallingConv::ID CalleeCC,
Dan Gohman9178de12009-08-05 01:29:28 +00002252 bool isVarArg,
Evan Chengd82fae32010-01-27 06:25:16 +00002253 const SmallVectorImpl<ISD::OutputArg> &Outs,
2254 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman9178de12009-08-05 01:29:28 +00002255 SelectionDAG& DAG) const {
Evan Chengd82fae32010-01-27 06:25:16 +00002256 if (CalleeCC != CallingConv::Fast &&
2257 CalleeCC != CallingConv::C)
2258 return false;
2259
Evan Cheng3d424642010-01-29 06:45:59 +00002260 // If -tailcallopt is specified, make fastcc functions tail-callable.
2261 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Chengca18ef22010-01-31 06:44:49 +00002262 if (PerformTailCallOpt) {
2263 if (CalleeCC == CallingConv::Fast &&
2264 CallerF->getCallingConv() == CalleeCC)
2265 return true;
2266 return false;
2267 }
2268
Evan Chengc38381c2010-02-02 02:22:50 +00002269
2270 // Look for obvious safe cases to perform tail call optimization that does not
2271 // requite ABI changes. This is what gcc calls sibcall.
2272
Evan Chengca18ef22010-01-31 06:44:49 +00002273 // Do not tail call optimize vararg calls for now.
2274 if (isVarArg)
2275 return false;
2276
Evan Cheng73e1dbe2010-01-30 01:22:00 +00002277 // If the callee takes no arguments then go on to check the results of the
2278 // call.
2279 if (!Outs.empty()) {
2280 // Check if stack adjustment is needed. For now, do not do this if any
2281 // argument is passed on the stack.
2282 SmallVector<CCValAssign, 16> ArgLocs;
2283 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2284 ArgLocs, *DAG.getContext());
2285 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengc38381c2010-02-02 02:22:50 +00002286 if (CCInfo.getNextStackOffset()) {
2287 MachineFunction &MF = DAG.getMachineFunction();
2288 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2289 return false;
2290 if (Subtarget->isTargetWin64())
2291 // Win64 ABI has additional complications.
2292 return false;
2293
2294 // Check if the arguments are already laid out in the right way as
2295 // the caller's fixed stack objects.
2296 MachineFrameInfo *MFI = MF.getFrameInfo();
2297 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2298 CCValAssign &VA = ArgLocs[i];
2299 EVT RegVT = VA.getLocVT();
2300 SDValue Arg = Outs[i].Val;
2301 ISD::ArgFlagsTy Flags = Outs[i].Flags;
2302 if (Flags.isByVal())
2303 return false; // TODO
2304 if (VA.getLocInfo() == CCValAssign::Indirect)
2305 return false;
2306 if (!VA.isRegLoc()) {
2307 LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg);
2308 if (!Ld)
2309 return false;
2310 SDValue Ptr = Ld->getBasePtr();
2311 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2312 if (!FINode)
2313 return false;
2314 int FI = FINode->getIndex();
2315 if (!MFI->isFixedObjectIndex(FI))
2316 return false;
2317 if (VA.getLocMemOffset() != MFI->getObjectOffset(FI))
2318 return false;
2319 }
2320 }
2321 }
Evan Cheng73e1dbe2010-01-30 01:22:00 +00002322 }
Evan Chengd82fae32010-01-27 06:25:16 +00002323
Evan Cheng3d424642010-01-29 06:45:59 +00002324 // If the caller does not return a value, then this is obviously safe.
2325 // This is one case where it's safe to perform this optimization even
2326 // if the return types do not match.
2327 const Type *CallerRetTy = CallerF->getReturnType();
2328 if (CallerRetTy->isVoidTy())
2329 return true;
Evan Chengd82fae32010-01-27 06:25:16 +00002330
Evan Cheng3d424642010-01-29 06:45:59 +00002331 // If the return types match, then it's safe.
Evan Chengca6c9342010-02-02 21:29:10 +00002332 return CallerRetTy == RetTy;
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00002333}
2334
Dan Gohmanca4857a2008-09-03 23:12:08 +00002335FastISel *
Evan Cheng00787d52010-01-26 19:04:47 +00002336X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2337 DwarfWriter *dw,
2338 DenseMap<const Value *, unsigned> &vm,
2339 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2340 DenseMap<const AllocaInst *, int> &am
Dan Gohman9dd43582008-10-14 23:54:11 +00002341#ifndef NDEBUG
Evan Cheng00787d52010-01-26 19:04:47 +00002342 , SmallSet<Instruction*, 8> &cil
Dan Gohman9dd43582008-10-14 23:54:11 +00002343#endif
2344 ) {
Devang Patelfcf1c752009-01-13 00:35:13 +00002345 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohman9dd43582008-10-14 23:54:11 +00002346#ifndef NDEBUG
2347 , cil
2348#endif
2349 );
Dan Gohman97805ee2008-08-19 21:32:53 +00002350}
2351
2352
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002353//===----------------------------------------------------------------------===//
2354// Other Lowering Hooks
2355//===----------------------------------------------------------------------===//
2356
2357
Dan Gohman8181bd12008-07-27 21:46:04 +00002358SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikove844e472007-08-15 17:12:32 +00002359 MachineFunction &MF = DAG.getMachineFunction();
2360 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2361 int ReturnAddrIndex = FuncInfo->getRAIndex();
2362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363 if (ReturnAddrIndex == 0) {
2364 // Set up a frame object for the return address.
Bill Wendling6ddc87b2009-01-16 19:25:27 +00002365 uint64_t SlotSize = TD->getPointerSize();
David Greene6424ab92009-11-12 20:49:22 +00002366 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2367 true, false);
Anton Korobeynikove844e472007-08-15 17:12:32 +00002368 FuncInfo->setRAIndex(ReturnAddrIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002369 }
2370
2371 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
2372}
2373
2374
Anton Korobeynikovc283e152009-08-05 23:01:26 +00002375bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2376 bool hasSymbolicDisplacement) {
2377 // Offset should fit into 32 bit immediate field.
2378 if (!isInt32(Offset))
2379 return false;
2380
2381 // If we don't have a symbolic displacement - we don't have any extra
2382 // restrictions.
2383 if (!hasSymbolicDisplacement)
2384 return true;
2385
2386 // FIXME: Some tweaks might be needed for medium code model.
2387 if (M != CodeModel::Small && M != CodeModel::Kernel)
2388 return false;
2389
2390 // For small code model we assume that latest object is 16MB before end of 31
2391 // bits boundary. We may also accept pretty large negative constants knowing
2392 // that all objects are in the positive half of address space.
2393 if (M == CodeModel::Small && Offset < 16*1024*1024)
2394 return true;
2395
2396 // For kernel code model we know that all object resist in the negative half
2397 // of 32bits address space. We may not accept negative offsets, since they may
2398 // be just off and we may accept pretty large positive ones.
2399 if (M == CodeModel::Kernel && Offset > 0)
2400 return true;
2401
2402 return false;
2403}
2404
Chris Lattnerebb91142008-12-24 23:53:05 +00002405/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2406/// specific condition code, returning the condition code and the LHS/RHS of the
2407/// comparison to make.
2408static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2409 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 if (!isFP) {
2411 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2412 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2413 // X > -1 -> X == 0, jump !sign.
2414 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002415 return X86::COND_NS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2417 // X < 0 -> X == 0, jump on sign.
Chris Lattnerebb91142008-12-24 23:53:05 +00002418 return X86::COND_S;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002419 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman37b34262007-09-17 14:49:27 +00002420 // X < 1 -> X <= 0
2421 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattnerebb91142008-12-24 23:53:05 +00002422 return X86::COND_LE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002423 }
2424 }
2425
2426 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002427 default: llvm_unreachable("Invalid integer condition!");
Chris Lattnerebb91142008-12-24 23:53:05 +00002428 case ISD::SETEQ: return X86::COND_E;
2429 case ISD::SETGT: return X86::COND_G;
2430 case ISD::SETGE: return X86::COND_GE;
2431 case ISD::SETLT: return X86::COND_L;
2432 case ISD::SETLE: return X86::COND_LE;
2433 case ISD::SETNE: return X86::COND_NE;
2434 case ISD::SETULT: return X86::COND_B;
2435 case ISD::SETUGT: return X86::COND_A;
2436 case ISD::SETULE: return X86::COND_BE;
2437 case ISD::SETUGE: return X86::COND_AE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002438 }
Chris Lattnerb8397512008-12-23 23:42:27 +00002439 }
Scott Michel91099d62009-02-17 22:15:04 +00002440
Chris Lattnerb8397512008-12-23 23:42:27 +00002441 // First determine if it is required or is profitable to flip the operands.
Duncan Sandsc2a04622008-10-24 13:03:10 +00002442
Chris Lattnerb8397512008-12-23 23:42:27 +00002443 // If LHS is a foldable load, but RHS is not, flip the condition.
2444 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2445 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2446 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2447 std::swap(LHS, RHS);
Evan Chengfc937c92008-08-28 23:48:31 +00002448 }
2449
Chris Lattnerb8397512008-12-23 23:42:27 +00002450 switch (SetCCOpcode) {
2451 default: break;
2452 case ISD::SETOLT:
2453 case ISD::SETOLE:
2454 case ISD::SETUGT:
2455 case ISD::SETUGE:
2456 std::swap(LHS, RHS);
2457 break;
2458 }
2459
2460 // On a floating point condition, the flags are set as follows:
2461 // ZF PF CF op
2462 // 0 | 0 | 0 | X > Y
2463 // 0 | 0 | 1 | X < Y
2464 // 1 | 0 | 0 | X == Y
2465 // 1 | 1 | 1 | unordered
2466 switch (SetCCOpcode) {
Edwin Törökbd448e32009-07-14 16:55:14 +00002467 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattnerb8397512008-12-23 23:42:27 +00002468 case ISD::SETUEQ:
Chris Lattnerebb91142008-12-24 23:53:05 +00002469 case ISD::SETEQ: return X86::COND_E;
Chris Lattnerb8397512008-12-23 23:42:27 +00002470 case ISD::SETOLT: // flipped
2471 case ISD::SETOGT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002472 case ISD::SETGT: return X86::COND_A;
Chris Lattnerb8397512008-12-23 23:42:27 +00002473 case ISD::SETOLE: // flipped
2474 case ISD::SETOGE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002475 case ISD::SETGE: return X86::COND_AE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002476 case ISD::SETUGT: // flipped
2477 case ISD::SETULT:
Chris Lattnerebb91142008-12-24 23:53:05 +00002478 case ISD::SETLT: return X86::COND_B;
Chris Lattnerb8397512008-12-23 23:42:27 +00002479 case ISD::SETUGE: // flipped
2480 case ISD::SETULE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002481 case ISD::SETLE: return X86::COND_BE;
Chris Lattnerb8397512008-12-23 23:42:27 +00002482 case ISD::SETONE:
Chris Lattnerebb91142008-12-24 23:53:05 +00002483 case ISD::SETNE: return X86::COND_NE;
2484 case ISD::SETUO: return X86::COND_P;
2485 case ISD::SETO: return X86::COND_NP;
Dan Gohman8ab7dd02009-10-20 16:22:37 +00002486 case ISD::SETOEQ:
2487 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattnerb8397512008-12-23 23:42:27 +00002488 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002489}
2490
2491/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2492/// code. Current x86 isa includes the following FP cmov instructions:
2493/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2494static bool hasFPCMov(unsigned X86CC) {
2495 switch (X86CC) {
2496 default:
2497 return false;
2498 case X86::COND_B:
2499 case X86::COND_BE:
2500 case X86::COND_E:
2501 case X86::COND_P:
2502 case X86::COND_A:
2503 case X86::COND_AE:
2504 case X86::COND_NE:
2505 case X86::COND_NP:
2506 return true;
2507 }
2508}
2509
Evan Cheng6337b552009-10-27 19:56:55 +00002510/// isFPImmLegal - Returns true if the target can instruction select the
2511/// specified FP immediate natively. If false, the legalizer will
2512/// materialize the FP immediate as a load from a constant pool.
Evan Chenga0e67782009-10-28 01:43:28 +00002513bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Cheng6337b552009-10-27 19:56:55 +00002514 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2515 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2516 return true;
2517 }
2518 return false;
2519}
2520
Nate Begeman543d2142009-04-27 18:41:29 +00002521/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2522/// the specified range (L, H].
2523static bool isUndefOrInRange(int Val, int Low, int Hi) {
2524 return (Val < 0) || (Val >= Low && Val < Hi);
2525}
2526
2527/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2528/// specified value.
2529static bool isUndefOrEqual(int Val, int CmpVal) {
2530 if (Val < 0 || Val == CmpVal)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002531 return true;
Nate Begeman543d2142009-04-27 18:41:29 +00002532 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002533}
2534
Nate Begeman543d2142009-04-27 18:41:29 +00002535/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2536/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2537/// the second operand.
Owen Andersonac9de032009-08-10 22:56:29 +00002538static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002539 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman543d2142009-04-27 18:41:29 +00002540 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002541 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman543d2142009-04-27 18:41:29 +00002542 return (Mask[0] < 2 && Mask[1] < 2);
2543 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544}
2545
Nate Begeman543d2142009-04-27 18:41:29 +00002546bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002547 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002548 N->getMask(M);
2549 return ::isPSHUFDMask(M, N->getValueType(0));
2550}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002551
Nate Begeman543d2142009-04-27 18:41:29 +00002552/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2553/// is suitable for input to PSHUFHW.
Owen Andersonac9de032009-08-10 22:56:29 +00002554static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002555 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002557
Nate Begeman543d2142009-04-27 18:41:29 +00002558 // Lower quadword copied in order or undef.
2559 for (int i = 0; i != 4; ++i)
2560 if (Mask[i] >= 0 && Mask[i] != i)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002561 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002563 // Upper quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002564 for (int i = 4; i != 8; ++i)
2565 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002566 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002567
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002568 return true;
2569}
2570
Nate Begeman543d2142009-04-27 18:41:29 +00002571bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002572 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002573 N->getMask(M);
2574 return ::isPSHUFHWMask(M, N->getValueType(0));
2575}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002576
Nate Begeman543d2142009-04-27 18:41:29 +00002577/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2578/// is suitable for input to PSHUFLW.
Owen Andersonac9de032009-08-10 22:56:29 +00002579static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002580 if (VT != MVT::v8i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002581 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002582
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002583 // Upper quadword copied in order.
Nate Begeman543d2142009-04-27 18:41:29 +00002584 for (int i = 4; i != 8; ++i)
2585 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002586 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002587
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002588 // Lower quadword shuffled.
Nate Begeman543d2142009-04-27 18:41:29 +00002589 for (int i = 0; i != 4; ++i)
2590 if (Mask[i] >= 4)
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002591 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002592
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002593 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002594}
2595
Nate Begeman543d2142009-04-27 18:41:29 +00002596bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002597 SmallVector<int, 8> M;
Nate Begeman543d2142009-04-27 18:41:29 +00002598 N->getMask(M);
2599 return ::isPSHUFLWMask(M, N->getValueType(0));
2600}
2601
Nate Begeman080f8e22009-10-19 02:17:23 +00002602/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2603/// is suitable for input to PALIGNR.
2604static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2605 bool hasSSSE3) {
2606 int i, e = VT.getVectorNumElements();
2607
2608 // Do not handle v2i64 / v2f64 shuffles with palignr.
2609 if (e < 4 || !hasSSSE3)
2610 return false;
2611
2612 for (i = 0; i != e; ++i)
2613 if (Mask[i] >= 0)
2614 break;
2615
2616 // All undef, not a palignr.
2617 if (i == e)
2618 return false;
2619
2620 // Determine if it's ok to perform a palignr with only the LHS, since we
2621 // don't have access to the actual shuffle elements to see if RHS is undef.
2622 bool Unary = Mask[i] < (int)e;
2623 bool NeedsUnary = false;
2624
2625 int s = Mask[i] - i;
2626
2627 // Check the rest of the elements to see if they are consecutive.
2628 for (++i; i != e; ++i) {
2629 int m = Mask[i];
2630 if (m < 0)
2631 continue;
2632
2633 Unary = Unary && (m < (int)e);
2634 NeedsUnary = NeedsUnary || (m < s);
2635
2636 if (NeedsUnary && !Unary)
2637 return false;
2638 if (Unary && m != ((s+i) & (e-1)))
2639 return false;
2640 if (!Unary && m != (s+i))
2641 return false;
2642 }
2643 return true;
2644}
2645
2646bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2647 SmallVector<int, 8> M;
2648 N->getMask(M);
2649 return ::isPALIGNRMask(M, N->getValueType(0), true);
2650}
2651
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002652/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2653/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersonac9de032009-08-10 22:56:29 +00002654static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002655 int NumElems = VT.getVectorNumElements();
2656 if (NumElems != 2 && NumElems != 4)
2657 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002658
Nate Begeman543d2142009-04-27 18:41:29 +00002659 int Half = NumElems / 2;
2660 for (int i = 0; i < Half; ++i)
2661 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002662 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002663 for (int i = Half; i < NumElems; ++i)
2664 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002665 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002666
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002667 return true;
2668}
2669
Nate Begeman543d2142009-04-27 18:41:29 +00002670bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2671 SmallVector<int, 8> M;
2672 N->getMask(M);
2673 return ::isSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002674}
2675
2676/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2677/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2678/// half elements to come from vector 1 (which would equal the dest.) and
2679/// the upper half to come from vector 2.
Owen Andersonac9de032009-08-10 22:56:29 +00002680static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002681 int NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002682
2683 if (NumElems != 2 && NumElems != 4)
Nate Begeman543d2142009-04-27 18:41:29 +00002684 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002685
Nate Begeman543d2142009-04-27 18:41:29 +00002686 int Half = NumElems / 2;
2687 for (int i = 0; i < Half; ++i)
2688 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002689 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002690 for (int i = Half; i < NumElems; ++i)
2691 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 return false;
2693 return true;
2694}
2695
Nate Begeman543d2142009-04-27 18:41:29 +00002696static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2697 SmallVector<int, 8> M;
2698 N->getMask(M);
2699 return isCommutedSHUFPMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002700}
2701
2702/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2703/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman543d2142009-04-27 18:41:29 +00002704bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2705 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 return false;
2707
2708 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman543d2142009-04-27 18:41:29 +00002709 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2710 isUndefOrEqual(N->getMaskElt(1), 7) &&
2711 isUndefOrEqual(N->getMaskElt(2), 2) &&
2712 isUndefOrEqual(N->getMaskElt(3), 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002713}
2714
Nate Begemanb13034d2009-11-07 23:17:15 +00002715/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2716/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2717/// <2, 3, 2, 3>
2718bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2719 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2720
2721 if (NumElems != 4)
2722 return false;
2723
2724 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2725 isUndefOrEqual(N->getMaskElt(1), 3) &&
2726 isUndefOrEqual(N->getMaskElt(2), 2) &&
2727 isUndefOrEqual(N->getMaskElt(3), 3);
2728}
2729
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002730/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2731/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman543d2142009-04-27 18:41:29 +00002732bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2733 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735 if (NumElems != 2 && NumElems != 4)
2736 return false;
2737
2738 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002739 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002740 return false;
2741
2742 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002743 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002744 return false;
2745
2746 return true;
2747}
2748
Nate Begemanb13034d2009-11-07 23:17:15 +00002749/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2750/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2751bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002752 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754 if (NumElems != 2 && NumElems != 4)
2755 return false;
2756
2757 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00002758 if (!isUndefOrEqual(N->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759 return false;
2760
Nate Begeman543d2142009-04-27 18:41:29 +00002761 for (unsigned i = 0; i < NumElems/2; ++i)
2762 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002763 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764
2765 return true;
2766}
2767
2768/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2769/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersonac9de032009-08-10 22:56:29 +00002770static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002771 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002772 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2774 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002775
Nate Begeman543d2142009-04-27 18:41:29 +00002776 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2777 int BitI = Mask[i];
2778 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002779 if (!isUndefOrEqual(BitI, j))
2780 return false;
2781 if (V2IsSplat) {
Mon P Wang56d91642009-02-04 01:16:59 +00002782 if (!isUndefOrEqual(BitI1, NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002783 return false;
2784 } else {
2785 if (!isUndefOrEqual(BitI1, j + NumElts))
2786 return false;
2787 }
2788 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002789 return true;
2790}
2791
Nate Begeman543d2142009-04-27 18:41:29 +00002792bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2793 SmallVector<int, 8> M;
2794 N->getMask(M);
2795 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002796}
2797
2798/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2799/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002800static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002801 bool V2IsSplat = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002802 int NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2804 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002805
Nate Begeman543d2142009-04-27 18:41:29 +00002806 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2807 int BitI = Mask[i];
2808 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002809 if (!isUndefOrEqual(BitI, j + NumElts/2))
2810 return false;
2811 if (V2IsSplat) {
2812 if (isUndefOrEqual(BitI1, NumElts))
2813 return false;
2814 } else {
2815 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2816 return false;
2817 }
2818 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002819 return true;
2820}
2821
Nate Begeman543d2142009-04-27 18:41:29 +00002822bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2823 SmallVector<int, 8> M;
2824 N->getMask(M);
2825 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002826}
2827
2828/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2829/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2830/// <0, 0, 1, 1>
Owen Andersonac9de032009-08-10 22:56:29 +00002831static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002832 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002833 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2834 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002835
Nate Begeman543d2142009-04-27 18:41:29 +00002836 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2837 int BitI = Mask[i];
2838 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002839 if (!isUndefOrEqual(BitI, j))
2840 return false;
2841 if (!isUndefOrEqual(BitI1, j))
2842 return false;
2843 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002844 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002845}
2846
Nate Begeman543d2142009-04-27 18:41:29 +00002847bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2848 SmallVector<int, 8> M;
2849 N->getMask(M);
2850 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2851}
2852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2854/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2855/// <2, 2, 3, 3>
Owen Andersonac9de032009-08-10 22:56:29 +00002856static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman543d2142009-04-27 18:41:29 +00002857 int NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002858 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2859 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002860
Nate Begeman543d2142009-04-27 18:41:29 +00002861 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2862 int BitI = Mask[i];
2863 int BitI1 = Mask[i+1];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002864 if (!isUndefOrEqual(BitI, j))
2865 return false;
2866 if (!isUndefOrEqual(BitI1, j))
2867 return false;
2868 }
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002869 return true;
Nate Begemanda17a812009-04-24 03:42:54 +00002870}
2871
Nate Begeman543d2142009-04-27 18:41:29 +00002872bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2873 SmallVector<int, 8> M;
2874 N->getMask(M);
2875 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2876}
2877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2879/// specifies a shuffle of elements that is suitable for input to MOVSS,
2880/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersonac9de032009-08-10 22:56:29 +00002881static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedmand49401f2009-06-06 06:05:10 +00002882 if (VT.getVectorElementType().getSizeInBits() < 32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002883 return false;
Eli Friedmand49401f2009-06-06 06:05:10 +00002884
2885 int NumElts = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002886
Nate Begeman543d2142009-04-27 18:41:29 +00002887 if (!isUndefOrEqual(Mask[0], NumElts))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002889
Nate Begeman543d2142009-04-27 18:41:29 +00002890 for (int i = 1; i < NumElts; ++i)
2891 if (!isUndefOrEqual(Mask[i], i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002892 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 return true;
2895}
2896
Nate Begeman543d2142009-04-27 18:41:29 +00002897bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2898 SmallVector<int, 8> M;
2899 N->getMask(M);
2900 return ::isMOVLMask(M, N->getValueType(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901}
2902
2903/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2904/// of what x86 movss want. X86 movs requires the lowest element to be lowest
2905/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersonac9de032009-08-10 22:56:29 +00002906static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman543d2142009-04-27 18:41:29 +00002907 bool V2IsSplat = false, bool V2IsUndef = false) {
2908 int NumOps = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2910 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002911
Nate Begeman543d2142009-04-27 18:41:29 +00002912 if (!isUndefOrEqual(Mask[0], 0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002913 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002914
Nate Begeman543d2142009-04-27 18:41:29 +00002915 for (int i = 1; i < NumOps; ++i)
2916 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2917 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2918 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002919 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002920
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002921 return true;
2922}
2923
Nate Begeman543d2142009-04-27 18:41:29 +00002924static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002925 bool V2IsUndef = false) {
Nate Begeman543d2142009-04-27 18:41:29 +00002926 SmallVector<int, 8> M;
2927 N->getMask(M);
2928 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002929}
2930
2931/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2932/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002933bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2934 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002935 return false;
2936
2937 // Expect 1, 1, 3, 3
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002938 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002939 int Elt = N->getMaskElt(i);
2940 if (Elt >= 0 && Elt != 1)
2941 return false;
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00002942 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002943
2944 bool HasHi = false;
2945 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002946 int Elt = N->getMaskElt(i);
2947 if (Elt >= 0 && Elt != 3)
2948 return false;
2949 if (Elt == 3)
2950 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002952 // Don't use movshdup if it can be done with a shufps.
Nate Begeman543d2142009-04-27 18:41:29 +00002953 // FIXME: verify that matching u, u, 3, 3 is what we want.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002954 return HasHi;
2955}
2956
2957/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2958/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002959bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2960 if (N->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 return false;
2962
2963 // Expect 0, 0, 2, 2
Nate Begeman543d2142009-04-27 18:41:29 +00002964 for (unsigned i = 0; i < 2; ++i)
2965 if (N->getMaskElt(i) > 0)
2966 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002967
2968 bool HasHi = false;
2969 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00002970 int Elt = N->getMaskElt(i);
2971 if (Elt >= 0 && Elt != 2)
2972 return false;
2973 if (Elt == 2)
2974 HasHi = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002975 }
Nate Begeman543d2142009-04-27 18:41:29 +00002976 // Don't use movsldup if it can be done with a shufps.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002977 return HasHi;
2978}
2979
Evan Chenga2497eb2008-09-25 20:50:48 +00002980/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2981/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman543d2142009-04-27 18:41:29 +00002982bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2983 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00002984
Nate Begeman543d2142009-04-27 18:41:29 +00002985 for (int i = 0; i < e; ++i)
2986 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002987 return false;
Nate Begeman543d2142009-04-27 18:41:29 +00002988 for (int i = 0; i < e; ++i)
2989 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Chenga2497eb2008-09-25 20:50:48 +00002990 return false;
2991 return true;
2992}
2993
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002994/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00002995/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00002997 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2998 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2999
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3001 unsigned Mask = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00003002 for (int i = 0; i < NumOperands; ++i) {
3003 int Val = SVOp->getMaskElt(NumOperands-i-1);
3004 if (Val < 0) Val = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003005 if (Val >= NumOperands) Val -= NumOperands;
3006 Mask |= Val;
3007 if (i != NumOperands - 1)
3008 Mask <<= Shift;
3009 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 return Mask;
3011}
3012
3013/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00003014/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003015unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00003016 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003017 unsigned Mask = 0;
3018 // 8 nodes, but we only care about the last 4.
3019 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003020 int Val = SVOp->getMaskElt(i);
3021 if (Val >= 0)
Mon P Wang56d91642009-02-04 01:16:59 +00003022 Mask |= (Val - 4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003023 if (i != 4)
3024 Mask <<= 2;
3025 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003026 return Mask;
3027}
3028
3029/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begeman080f8e22009-10-19 02:17:23 +00003030/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003031unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00003032 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003033 unsigned Mask = 0;
3034 // 8 nodes, but we only care about the first 4.
3035 for (int i = 3; i >= 0; --i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003036 int Val = SVOp->getMaskElt(i);
3037 if (Val >= 0)
3038 Mask |= Val;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 if (i != 0)
3040 Mask <<= 2;
3041 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003042 return Mask;
3043}
3044
Nate Begeman080f8e22009-10-19 02:17:23 +00003045/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3046/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3047unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3048 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3049 EVT VVT = N->getValueType(0);
3050 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3051 int Val = 0;
3052
3053 unsigned i, e;
3054 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3055 Val = SVOp->getMaskElt(i);
3056 if (Val >= 0)
3057 break;
3058 }
3059 return (Val - i) * EltSize;
3060}
3061
Evan Chengb723fb52009-07-30 08:33:02 +00003062/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3063/// constant +0.0.
3064bool X86::isZeroNode(SDValue Elt) {
3065 return ((isa<ConstantSDNode>(Elt) &&
3066 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3067 (isa<ConstantFPSDNode>(Elt) &&
3068 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3069}
3070
Nate Begeman543d2142009-04-27 18:41:29 +00003071/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3072/// their permute mask.
3073static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3074 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003075 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003076 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman543d2142009-04-27 18:41:29 +00003077 SmallVector<int, 8> MaskVec;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003078
Nate Begemane8f61cb2009-04-29 05:20:52 +00003079 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003080 int idx = SVOp->getMaskElt(i);
3081 if (idx < 0)
3082 MaskVec.push_back(idx);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003083 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00003084 MaskVec.push_back(idx + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003085 else
Nate Begeman543d2142009-04-27 18:41:29 +00003086 MaskVec.push_back(idx - NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003087 }
Nate Begeman543d2142009-04-27 18:41:29 +00003088 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3089 SVOp->getOperand(0), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090}
3091
Evan Chenga6769df2007-12-07 21:30:01 +00003092/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3093/// the two vector operands have swapped position.
Owen Andersonac9de032009-08-10 22:56:29 +00003094static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00003095 unsigned NumElems = VT.getVectorNumElements();
3096 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003097 int idx = Mask[i];
3098 if (idx < 0)
Evan Chengfca29242007-12-07 08:07:39 +00003099 continue;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003100 else if (idx < (int)NumElems)
Nate Begeman543d2142009-04-27 18:41:29 +00003101 Mask[i] = idx + NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00003102 else
Nate Begeman543d2142009-04-27 18:41:29 +00003103 Mask[i] = idx - NumElems;
Evan Chengfca29242007-12-07 08:07:39 +00003104 }
Evan Chengfca29242007-12-07 08:07:39 +00003105}
3106
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003107/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3108/// match movhlps. The lower half elements should come from upper half of
3109/// V1 (and in order), and the upper half elements should come from the upper
3110/// half of V2 (and in order).
Nate Begeman543d2142009-04-27 18:41:29 +00003111static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3112 if (Op->getValueType(0).getVectorNumElements() != 4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003113 return false;
3114 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003115 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003116 return false;
3117 for (unsigned i = 2; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003118 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003119 return false;
3120 return true;
3121}
3122
3123/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng40ee6e52008-05-08 00:57:18 +00003124/// is promoted to a vector. It also returns the LoadSDNode by reference if
3125/// required.
3126static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Chenga2497eb2008-09-25 20:50:48 +00003127 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3128 return false;
3129 N = N->getOperand(0).getNode();
3130 if (!ISD::isNON_EXTLoad(N))
3131 return false;
3132 if (LD)
3133 *LD = cast<LoadSDNode>(N);
3134 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003135}
3136
3137/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3138/// match movlp{s|d}. The lower half elements should come from lower half of
3139/// V1 (and in order), and the upper half elements should come from the upper
3140/// half of V2 (and in order). And since V1 will become the source of the
3141/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003142static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3143 ShuffleVectorSDNode *Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003144 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
3145 return false;
3146 // Is V2 is a vector load, don't do this transformation. We will try to use
3147 // load folding shufps op.
3148 if (ISD::isNON_EXTLoad(V2))
3149 return false;
3150
Nate Begemane8f61cb2009-04-29 05:20:52 +00003151 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003152
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003153 if (NumElems != 2 && NumElems != 4)
3154 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003155 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003156 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003157 return false;
Nate Begemane8f61cb2009-04-29 05:20:52 +00003158 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003159 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003160 return false;
3161 return true;
3162}
3163
3164/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3165/// all the same.
3166static bool isSplatVector(SDNode *N) {
3167 if (N->getOpcode() != ISD::BUILD_VECTOR)
3168 return false;
3169
Dan Gohman8181bd12008-07-27 21:46:04 +00003170 SDValue SplatValue = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003171 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3172 if (N->getOperand(i) != SplatValue)
3173 return false;
3174 return true;
3175}
3176
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003178/// to an zero vector.
Nate Begemane8f61cb2009-04-29 05:20:52 +00003179/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman543d2142009-04-27 18:41:29 +00003180static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003181 SDValue V1 = N->getOperand(0);
3182 SDValue V2 = N->getOperand(1);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003183 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3184 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003185 int Idx = N->getMaskElt(i);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003186 if (Idx >= (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003187 unsigned Opc = V2.getOpcode();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003188 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3189 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003190 if (Opc != ISD::BUILD_VECTOR ||
3191 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman543d2142009-04-27 18:41:29 +00003192 return false;
3193 } else if (Idx >= 0) {
3194 unsigned Opc = V1.getOpcode();
3195 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3196 continue;
Evan Chengb723fb52009-07-30 08:33:02 +00003197 if (Opc != ISD::BUILD_VECTOR ||
3198 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattnere6aa3862007-11-25 00:24:49 +00003199 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003200 }
3201 }
3202 return true;
3203}
3204
3205/// getZeroVector - Returns a vector of specified type with all zero elements.
3206///
Owen Andersonac9de032009-08-10 22:56:29 +00003207static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesence0805b2009-02-03 19:33:06 +00003208 DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003209 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003210
Chris Lattnere6aa3862007-11-25 00:24:49 +00003211 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3212 // type. This ensures they get CSE'd.
Dan Gohman8181bd12008-07-27 21:46:04 +00003213 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003214 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003215 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3216 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003217 } else if (HasSSE2) { // SSE2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003218 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3219 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003220 } else { // SSE1
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003221 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3222 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Cheng8c590372008-05-15 08:39:06 +00003223 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003224 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003225}
3226
Chris Lattnere6aa3862007-11-25 00:24:49 +00003227/// getOnesVector - Returns a vector of specified type with all bits set.
3228///
Owen Andersonac9de032009-08-10 22:56:29 +00003229static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003230 assert(VT.isVector() && "Expected a vector type");
Scott Michel91099d62009-02-17 22:15:04 +00003231
Chris Lattnere6aa3862007-11-25 00:24:49 +00003232 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3233 // type. This ensures they get CSE'd.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003234 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman8181bd12008-07-27 21:46:04 +00003235 SDValue Vec;
Duncan Sands92c43912008-06-06 12:08:01 +00003236 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003237 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003238 else // SSE
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003239 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesence0805b2009-02-03 19:33:06 +00003240 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003241}
3242
3243
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003244/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3245/// that point to V2 points to its first element.
Nate Begeman543d2142009-04-27 18:41:29 +00003246static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003247 EVT VT = SVOp->getValueType(0);
Nate Begemane8f61cb2009-04-29 05:20:52 +00003248 unsigned NumElems = VT.getVectorNumElements();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003250 bool Changed = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003251 SmallVector<int, 8> MaskVec;
3252 SVOp->getMask(MaskVec);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003253
Nate Begemane8f61cb2009-04-29 05:20:52 +00003254 for (unsigned i = 0; i != NumElems; ++i) {
3255 if (MaskVec[i] > (int)NumElems) {
Nate Begeman543d2142009-04-27 18:41:29 +00003256 MaskVec[i] = NumElems;
3257 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003260 if (Changed)
Nate Begeman543d2142009-04-27 18:41:29 +00003261 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3262 SVOp->getOperand(1), &MaskVec[0]);
3263 return SDValue(SVOp, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264}
3265
3266/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3267/// operation of specified width.
Owen Andersonac9de032009-08-10 22:56:29 +00003268static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003269 SDValue V2) {
3270 unsigned NumElems = VT.getVectorNumElements();
3271 SmallVector<int, 8> Mask;
3272 Mask.push_back(NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003274 Mask.push_back(i);
3275 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276}
3277
Nate Begeman543d2142009-04-27 18:41:29 +00003278/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003279static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003280 SDValue V2) {
3281 unsigned NumElems = VT.getVectorNumElements();
3282 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003283 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003284 Mask.push_back(i);
3285 Mask.push_back(i + NumElems);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 }
Nate Begeman543d2142009-04-27 18:41:29 +00003287 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003288}
3289
Nate Begeman543d2142009-04-27 18:41:29 +00003290/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersonac9de032009-08-10 22:56:29 +00003291static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman543d2142009-04-27 18:41:29 +00003292 SDValue V2) {
3293 unsigned NumElems = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003294 unsigned Half = NumElems/2;
Nate Begeman543d2142009-04-27 18:41:29 +00003295 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003296 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00003297 Mask.push_back(i + Half);
3298 Mask.push_back(i + NumElems + Half);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003299 }
Nate Begeman543d2142009-04-27 18:41:29 +00003300 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003301}
3302
Evan Chengbf8b2c52008-04-05 00:30:36 +00003303/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003304static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00003305 bool HasSSE2) {
3306 if (SV->getValueType(0).getVectorNumElements() <= 4)
3307 return SDValue(SV, 0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003308
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003309 EVT PVT = MVT::v4f32;
Owen Andersonac9de032009-08-10 22:56:29 +00003310 EVT VT = SV->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00003311 DebugLoc dl = SV->getDebugLoc();
3312 SDValue V1 = SV->getOperand(0);
3313 int NumElems = VT.getVectorNumElements();
3314 int EltNo = SV->getSplatIndex();
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00003315
Nate Begeman543d2142009-04-27 18:41:29 +00003316 // unpack elements to the correct location
3317 while (NumElems > 4) {
3318 if (EltNo < NumElems/2) {
3319 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3320 } else {
3321 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3322 EltNo -= NumElems/2;
3323 }
3324 NumElems >>= 1;
3325 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003326
Nate Begeman543d2142009-04-27 18:41:29 +00003327 // Perform the splat.
3328 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesence0805b2009-02-03 19:33:06 +00003329 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman543d2142009-04-27 18:41:29 +00003330 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3331 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003332}
3333
3334/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattnere6aa3862007-11-25 00:24:49 +00003335/// vector of zero or undef vector. This produces a shuffle where the low
3336/// element of V2 is swizzled into the zero/undef vector, landing at element
3337/// 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 +00003338static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Cheng8c590372008-05-15 08:39:06 +00003339 bool isZero, bool HasSSE2,
3340 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00003341 EVT VT = V2.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +00003342 SDValue V1 = isZero
Nate Begeman543d2142009-04-27 18:41:29 +00003343 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3344 unsigned NumElems = VT.getVectorNumElements();
3345 SmallVector<int, 16> MaskVec;
Chris Lattnere6aa3862007-11-25 00:24:49 +00003346 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003347 // If this is the insertion idx, put the low elt of V2 here.
3348 MaskVec.push_back(i == Idx ? NumElems : i);
3349 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003350}
3351
Evan Chengdea99362008-05-29 08:22:04 +00003352/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3353/// a shuffle that is zero.
3354static
Nate Begeman543d2142009-04-27 18:41:29 +00003355unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3356 bool Low, SelectionDAG &DAG) {
Evan Chengdea99362008-05-29 08:22:04 +00003357 unsigned NumZeros = 0;
Nate Begeman543d2142009-04-27 18:41:29 +00003358 for (int i = 0; i < NumElems; ++i) {
Evan Cheng57db53b2008-06-25 20:52:59 +00003359 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman543d2142009-04-27 18:41:29 +00003360 int Idx = SVOp->getMaskElt(Index);
3361 if (Idx < 0) {
Evan Chengdea99362008-05-29 08:22:04 +00003362 ++NumZeros;
3363 continue;
3364 }
Nate Begeman543d2142009-04-27 18:41:29 +00003365 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Chengb723fb52009-07-30 08:33:02 +00003366 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengdea99362008-05-29 08:22:04 +00003367 ++NumZeros;
3368 else
3369 break;
3370 }
3371 return NumZeros;
3372}
3373
3374/// isVectorShift - Returns true if the shuffle can be implemented as a
3375/// logical left or right shift of a vector.
Nate Begeman543d2142009-04-27 18:41:29 +00003376/// FIXME: split into pslldqi, psrldqi, palignr variants.
3377static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman8181bd12008-07-27 21:46:04 +00003378 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman543d2142009-04-27 18:41:29 +00003379 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengdea99362008-05-29 08:22:04 +00003380
3381 isLeft = true;
Nate Begeman543d2142009-04-27 18:41:29 +00003382 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003383 if (!NumZeros) {
3384 isLeft = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003385 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengdea99362008-05-29 08:22:04 +00003386 if (!NumZeros)
3387 return false;
3388 }
Evan Chengdea99362008-05-29 08:22:04 +00003389 bool SeenV1 = false;
3390 bool SeenV2 = false;
Nate Begeman543d2142009-04-27 18:41:29 +00003391 for (int i = NumZeros; i < NumElems; ++i) {
3392 int Val = isLeft ? (i - NumZeros) : i;
3393 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3394 if (Idx < 0)
Evan Chengdea99362008-05-29 08:22:04 +00003395 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00003396 if (Idx < NumElems)
Evan Chengdea99362008-05-29 08:22:04 +00003397 SeenV1 = true;
3398 else {
Nate Begeman543d2142009-04-27 18:41:29 +00003399 Idx -= NumElems;
Evan Chengdea99362008-05-29 08:22:04 +00003400 SeenV2 = true;
3401 }
Nate Begeman543d2142009-04-27 18:41:29 +00003402 if (Idx != Val)
Evan Chengdea99362008-05-29 08:22:04 +00003403 return false;
3404 }
3405 if (SeenV1 && SeenV2)
3406 return false;
3407
Nate Begeman543d2142009-04-27 18:41:29 +00003408 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengdea99362008-05-29 08:22:04 +00003409 ShAmt = NumZeros;
3410 return true;
3411}
3412
3413
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003414/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3415///
Dan Gohman8181bd12008-07-27 21:46:04 +00003416static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003417 unsigned NumNonZero, unsigned NumZero,
3418 SelectionDAG &DAG, TargetLowering &TLI) {
3419 if (NumNonZero > 8)
Dan Gohman8181bd12008-07-27 21:46:04 +00003420 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003422 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003423 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003424 bool First = true;
3425 for (unsigned i = 0; i < 16; ++i) {
3426 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3427 if (ThisIsNonZero && First) {
3428 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003429 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003430 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003431 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003432 First = false;
3433 }
3434
3435 if ((i & 1) != 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003436 SDValue ThisElt(0, 0), LastElt(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3438 if (LastIsNonZero) {
Scott Michel91099d62009-02-17 22:15:04 +00003439 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003440 MVT::i16, Op.getOperand(i-1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003441 }
3442 if (ThisIsNonZero) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003443 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3444 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3445 ThisElt, DAG.getConstant(8, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446 if (LastIsNonZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003447 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 } else
3449 ThisElt = LastElt;
3450
Gabor Greif1c80d112008-08-28 21:40:38 +00003451 if (ThisElt.getNode())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003452 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner5872a362008-01-17 07:00:52 +00003453 DAG.getIntPtrConstant(i/2));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003454 }
3455 }
3456
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003457 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003458}
3459
3460/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
3461///
Dan Gohman8181bd12008-07-27 21:46:04 +00003462static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003463 unsigned NumNonZero, unsigned NumZero,
3464 SelectionDAG &DAG, TargetLowering &TLI) {
3465 if (NumNonZero > 4)
Dan Gohman8181bd12008-07-27 21:46:04 +00003466 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003467
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003468 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00003469 SDValue V(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003470 bool First = true;
3471 for (unsigned i = 0; i < 8; ++i) {
3472 bool isNonZero = (NonZeros & (1 << i)) != 0;
3473 if (isNonZero) {
3474 if (First) {
3475 if (NumZero)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003476 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003477 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003478 V = DAG.getUNDEF(MVT::v8i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003479 First = false;
3480 }
Scott Michel91099d62009-02-17 22:15:04 +00003481 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003482 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner5872a362008-01-17 07:00:52 +00003483 DAG.getIntPtrConstant(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003484 }
3485 }
3486
3487 return V;
3488}
3489
Evan Chengdea99362008-05-29 08:22:04 +00003490/// getVShift - Return a vector logical shift node.
3491///
Owen Andersonac9de032009-08-10 22:56:29 +00003492static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman543d2142009-04-27 18:41:29 +00003493 unsigned NumBits, SelectionDAG &DAG,
3494 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands92c43912008-06-06 12:08:01 +00003495 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003496 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengdea99362008-05-29 08:22:04 +00003497 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesence0805b2009-02-03 19:33:06 +00003498 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3499 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3500 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif825aa892008-08-28 23:19:51 +00003501 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengdea99362008-05-29 08:22:04 +00003502}
3503
Dan Gohman8181bd12008-07-27 21:46:04 +00003504SDValue
Evan Chenge31a26a2009-12-09 21:00:30 +00003505X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3506 SelectionDAG &DAG) {
3507
3508 // Check if the scalar load can be widened into a vector load. And if
3509 // the address is "base + cst" see if the cst can be "absorbed" into
3510 // the shuffle mask.
3511 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3512 SDValue Ptr = LD->getBasePtr();
3513 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3514 return SDValue();
3515 EVT PVT = LD->getValueType(0);
3516 if (PVT != MVT::i32 && PVT != MVT::f32)
3517 return SDValue();
3518
3519 int FI = -1;
3520 int64_t Offset = 0;
3521 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3522 FI = FINode->getIndex();
3523 Offset = 0;
3524 } else if (Ptr.getOpcode() == ISD::ADD &&
3525 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3526 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3527 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3528 Offset = Ptr.getConstantOperandVal(1);
3529 Ptr = Ptr.getOperand(0);
3530 } else {
3531 return SDValue();
3532 }
3533
3534 SDValue Chain = LD->getChain();
3535 // Make sure the stack object alignment is at least 16.
3536 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3537 if (DAG.InferPtrAlignment(Ptr) < 16) {
3538 if (MFI->isFixedObjectIndex(FI)) {
Eric Christopherc21aa852010-01-23 06:02:43 +00003539 // Can't change the alignment. FIXME: It's possible to compute
3540 // the exact stack offset and reference FI + adjust offset instead.
3541 // If someone *really* cares about this. That's the way to implement it.
3542 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003543 } else {
3544 MFI->setObjectAlignment(FI, 16);
3545 }
3546 }
3547
3548 // (Offset % 16) must be multiple of 4. Then address is then
3549 // Ptr + (Offset & ~15).
3550 if (Offset < 0)
3551 return SDValue();
3552 if ((Offset % 16) & 3)
3553 return SDValue();
3554 int64_t StartOffset = Offset & ~15;
3555 if (StartOffset)
3556 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3557 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3558
3559 int EltNo = (Offset - StartOffset) >> 2;
3560 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3561 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3562 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3563 // Canonicalize it to a v4i32 shuffle.
3564 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3565 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3566 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3567 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3568 }
3569
3570 return SDValue();
3571}
3572
3573SDValue
Dan Gohman8181bd12008-07-27 21:46:04 +00003574X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00003575 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere6aa3862007-11-25 00:24:49 +00003576 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif825aa892008-08-28 23:19:51 +00003577 if (ISD::isBuildVectorAllZeros(Op.getNode())
3578 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003579 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3580 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3581 // eliminated on x86-32 hosts.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003582 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattnere6aa3862007-11-25 00:24:49 +00003583 return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003584
Gabor Greif1c80d112008-08-28 21:40:38 +00003585 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00003586 return getOnesVector(Op.getValueType(), DAG, dl);
3587 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattnere6aa3862007-11-25 00:24:49 +00003588 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589
Owen Andersonac9de032009-08-10 22:56:29 +00003590 EVT VT = Op.getValueType();
3591 EVT ExtVT = VT.getVectorElementType();
3592 unsigned EVTBits = ExtVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003593
3594 unsigned NumElems = Op.getNumOperands();
3595 unsigned NumZero = 0;
3596 unsigned NumNonZero = 0;
3597 unsigned NonZeros = 0;
Chris Lattner92bdcb52008-03-08 22:48:29 +00003598 bool IsAllConstants = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00003599 SmallSet<SDValue, 8> Values;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003600 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003601 SDValue Elt = Op.getOperand(i);
Evan Chengc1073492007-12-12 06:45:40 +00003602 if (Elt.getOpcode() == ISD::UNDEF)
3603 continue;
3604 Values.insert(Elt);
3605 if (Elt.getOpcode() != ISD::Constant &&
3606 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattner92bdcb52008-03-08 22:48:29 +00003607 IsAllConstants = false;
Evan Chengb723fb52009-07-30 08:33:02 +00003608 if (X86::isZeroNode(Elt))
Evan Chengc1073492007-12-12 06:45:40 +00003609 NumZero++;
3610 else {
3611 NonZeros |= (1 << i);
3612 NumNonZero++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003613 }
3614 }
3615
3616 if (NumNonZero == 0) {
Chris Lattnere6aa3862007-11-25 00:24:49 +00003617 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003618 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003619 }
3620
Chris Lattner66a4dda2008-03-09 05:42:06 +00003621 // Special case for single non-zero, non-undef, element.
Eli Friedmand49401f2009-06-06 06:05:10 +00003622 if (NumNonZero == 1) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 SDValue Item = Op.getOperand(Idx);
Scott Michel91099d62009-02-17 22:15:04 +00003625
Chris Lattner2d91b962008-03-09 01:05:04 +00003626 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3627 // the value are obviously zero, truncate the value to i32 and do the
3628 // insertion that way. Only do this if the value is non-constant or if the
3629 // value is a constant being inserted into element 0. It is cheaper to do
3630 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003631 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner2d91b962008-03-09 01:05:04 +00003632 (!IsAllConstants || Idx == 0)) {
3633 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3634 // Handle MMX and SSE both.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003635 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3636 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michel91099d62009-02-17 22:15:04 +00003637
Chris Lattner2d91b962008-03-09 01:05:04 +00003638 // Truncate the value (which may itself be a constant) to i32, and
3639 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003640 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesence0805b2009-02-03 19:33:06 +00003641 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Cheng8c590372008-05-15 08:39:06 +00003642 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3643 Subtarget->hasSSE2(), DAG);
Scott Michel91099d62009-02-17 22:15:04 +00003644
Chris Lattner2d91b962008-03-09 01:05:04 +00003645 // Now we have our 32-bit value zero extended in the low element of
3646 // a vector. If Idx != 0, swizzle it into place.
3647 if (Idx != 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003648 SmallVector<int, 4> Mask;
3649 Mask.push_back(Idx);
3650 for (unsigned i = 1; i != VecElts; ++i)
3651 Mask.push_back(i);
3652 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003653 DAG.getUNDEF(Item.getValueType()),
Nate Begeman543d2142009-04-27 18:41:29 +00003654 &Mask[0]);
Chris Lattner2d91b962008-03-09 01:05:04 +00003655 }
Dale Johannesence0805b2009-02-03 19:33:06 +00003656 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner2d91b962008-03-09 01:05:04 +00003657 }
3658 }
Scott Michel91099d62009-02-17 22:15:04 +00003659
Chris Lattnerac914892008-03-08 22:59:52 +00003660 // If we have a constant or non-constant insertion into the low element of
3661 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3662 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedmand49401f2009-06-06 06:05:10 +00003663 // depending on what the source datatype is.
3664 if (Idx == 0) {
3665 if (NumZero == 0) {
3666 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003667 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3668 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedmand49401f2009-06-06 06:05:10 +00003669 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3670 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3671 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3672 DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003673 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3674 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3675 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedmand49401f2009-06-06 06:05:10 +00003676 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3677 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3678 Subtarget->hasSSE2(), DAG);
3679 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3680 }
Chris Lattner92bdcb52008-03-08 22:48:29 +00003681 }
Evan Chengdea99362008-05-29 08:22:04 +00003682
3683 // Is it a vector logical left shift?
3684 if (NumElems == 2 && Idx == 1 &&
Evan Chengb723fb52009-07-30 08:33:02 +00003685 X86::isZeroNode(Op.getOperand(0)) &&
3686 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands92c43912008-06-06 12:08:01 +00003687 unsigned NumBits = VT.getSizeInBits();
Evan Chengdea99362008-05-29 08:22:04 +00003688 return getVShift(true, VT,
Scott Michel91099d62009-02-17 22:15:04 +00003689 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00003690 VT, Op.getOperand(1)),
Dale Johannesence0805b2009-02-03 19:33:06 +00003691 NumBits/2, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00003692 }
Scott Michel91099d62009-02-17 22:15:04 +00003693
Chris Lattner92bdcb52008-03-08 22:48:29 +00003694 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003695 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003696
Chris Lattnerac914892008-03-08 22:59:52 +00003697 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3698 // is a non-constant being inserted into an element other than the low one,
3699 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3700 // movd/movss) to move this into the low element, then shuffle it into
3701 // place.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003702 if (EVTBits == 32) {
Dale Johannesence0805b2009-02-03 19:33:06 +00003703 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michel91099d62009-02-17 22:15:04 +00003704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Cheng8c590372008-05-15 08:39:06 +00003706 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3707 Subtarget->hasSSE2(), DAG);
Nate Begeman543d2142009-04-27 18:41:29 +00003708 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003709 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman543d2142009-04-27 18:41:29 +00003710 MaskVec.push_back(i == Idx ? 0 : 1);
3711 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003712 }
3713 }
3714
Chris Lattner66a4dda2008-03-09 05:42:06 +00003715 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chenge31a26a2009-12-09 21:00:30 +00003716 if (Values.size() == 1) {
3717 if (EVTBits == 32) {
3718 // Instead of a shuffle like this:
3719 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3720 // Check if it's possible to issue this instead.
3721 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3722 unsigned Idx = CountTrailingZeros_32(NonZeros);
3723 SDValue Item = Op.getOperand(Idx);
3724 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3725 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3726 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003727 return SDValue();
Evan Chenge31a26a2009-12-09 21:00:30 +00003728 }
Scott Michel91099d62009-02-17 22:15:04 +00003729
Dan Gohman21463242007-07-24 22:55:08 +00003730 // A vector full of immediates; various special cases are already
3731 // handled, so this is best done with a single constant-pool load.
Chris Lattner92bdcb52008-03-08 22:48:29 +00003732 if (IsAllConstants)
Dan Gohman8181bd12008-07-27 21:46:04 +00003733 return SDValue();
Dan Gohman21463242007-07-24 22:55:08 +00003734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 // Let legalizer expand 2-wide build_vectors.
Evan Cheng40ee6e52008-05-08 00:57:18 +00003736 if (EVTBits == 64) {
3737 if (NumNonZero == 1) {
3738 // One half is zero or undef.
3739 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesence0805b2009-02-03 19:33:06 +00003740 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng40ee6e52008-05-08 00:57:18 +00003741 Op.getOperand(Idx));
Evan Cheng8c590372008-05-15 08:39:06 +00003742 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3743 Subtarget->hasSSE2(), DAG);
Evan Cheng40ee6e52008-05-08 00:57:18 +00003744 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003745 return SDValue();
Evan Cheng40ee6e52008-05-08 00:57:18 +00003746 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747
3748 // If element VT is < 32 bits, convert it to inserts into a zero vector.
3749 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003750 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003751 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003752 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003753 }
3754
3755 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003756 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003757 *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00003758 if (V.getNode()) return V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003759 }
3760
3761 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00003762 SmallVector<SDValue, 8> V;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003763 V.resize(NumElems);
3764 if (NumElems == 4 && NumZero > 0) {
3765 for (unsigned i = 0; i < 4; ++i) {
3766 bool isZero = !(NonZeros & (1 << i));
3767 if (isZero)
Dale Johannesence0805b2009-02-03 19:33:06 +00003768 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003769 else
Dale Johannesence0805b2009-02-03 19:33:06 +00003770 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771 }
3772
3773 for (unsigned i = 0; i < 2; ++i) {
3774 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3775 default: break;
3776 case 0:
3777 V[i] = V[i*2]; // Must be a zero vector.
3778 break;
3779 case 1:
Nate Begeman543d2142009-04-27 18:41:29 +00003780 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 break;
3782 case 2:
Nate Begeman543d2142009-04-27 18:41:29 +00003783 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003784 break;
3785 case 3:
Nate Begeman543d2142009-04-27 18:41:29 +00003786 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003787 break;
3788 }
3789 }
3790
Nate Begeman543d2142009-04-27 18:41:29 +00003791 SmallVector<int, 8> MaskVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003792 bool Reverse = (NonZeros & 0x3) == 2;
3793 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003794 MaskVec.push_back(Reverse ? 1-i : i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003795 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3796 for (unsigned i = 0; i < 2; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003797 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3798 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003799 }
3800
3801 if (Values.size() > 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00003802 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3803 // values to be inserted is equal to the number of elements, in which case
3804 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003805 // load merge pattern for shuffles.
Nate Begeman543d2142009-04-27 18:41:29 +00003806 // FIXME: We could probably just check that here directly.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003807 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman543d2142009-04-27 18:41:29 +00003808 getSubtarget()->hasSSE41()) {
3809 V[0] = DAG.getUNDEF(VT);
3810 for (unsigned i = 0; i < NumElems; ++i)
3811 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3812 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3813 Op.getOperand(i), DAG.getIntPtrConstant(i));
3814 return V[0];
3815 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003816 // Expand into a number of unpckl*.
3817 // e.g. for v4f32
3818 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3819 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3820 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003821 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesence0805b2009-02-03 19:33:06 +00003822 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003823 NumElems >>= 1;
3824 while (NumElems != 0) {
3825 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00003826 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003827 NumElems >>= 1;
3828 }
3829 return V[0];
3830 }
3831
Dan Gohman8181bd12008-07-27 21:46:04 +00003832 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003833}
3834
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00003835SDValue
3836X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3837 // We support concatenate two MMX registers and place them in a MMX
3838 // register. This is better than doing a stack convert.
3839 DebugLoc dl = Op.getDebugLoc();
3840 EVT ResVT = Op.getValueType();
3841 assert(Op.getNumOperands() == 2);
3842 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3843 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3844 int Mask[2];
3845 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3846 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3847 InVec = Op.getOperand(1);
3848 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3849 unsigned NumElts = ResVT.getVectorNumElements();
3850 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3851 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3852 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3853 } else {
3854 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3855 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3856 Mask[0] = 0; Mask[1] = 2;
3857 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3858 }
3859 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3860}
3861
Nate Begeman2c87c422009-02-23 08:49:38 +00003862// v8i16 shuffles - Prefer shuffles in the following order:
3863// 1. [all] pshuflw, pshufhw, optional move
3864// 2. [ssse3] 1 x pshufb
3865// 3. [ssse3] 2 x pshufb + 1 x por
3866// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Chengfca29242007-12-07 08:07:39 +00003867static
Nate Begeman543d2142009-04-27 18:41:29 +00003868SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3869 SelectionDAG &DAG, X86TargetLowering &TLI) {
3870 SDValue V1 = SVOp->getOperand(0);
3871 SDValue V2 = SVOp->getOperand(1);
3872 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00003873 SmallVector<int, 8> MaskVals;
Evan Cheng75184a92007-12-11 01:46:18 +00003874
Nate Begeman2c87c422009-02-23 08:49:38 +00003875 // Determine if more than 1 of the words in each of the low and high quadwords
3876 // of the result come from the same quadword of one of the two inputs. Undef
3877 // mask values count as coming from any quadword, for better codegen.
3878 SmallVector<unsigned, 4> LoQuad(4);
3879 SmallVector<unsigned, 4> HiQuad(4);
3880 BitVector InputQuads(4);
3881 for (unsigned i = 0; i < 8; ++i) {
3882 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman543d2142009-04-27 18:41:29 +00003883 int EltIdx = SVOp->getMaskElt(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00003884 MaskVals.push_back(EltIdx);
3885 if (EltIdx < 0) {
3886 ++Quad[0];
3887 ++Quad[1];
3888 ++Quad[2];
3889 ++Quad[3];
Evan Cheng75184a92007-12-11 01:46:18 +00003890 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003891 }
3892 ++Quad[EltIdx / 4];
3893 InputQuads.set(EltIdx / 4);
Evan Cheng75184a92007-12-11 01:46:18 +00003894 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003895
Nate Begeman2c87c422009-02-23 08:49:38 +00003896 int BestLoQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003897 unsigned MaxQuad = 1;
3898 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003899 if (LoQuad[i] > MaxQuad) {
3900 BestLoQuad = i;
3901 MaxQuad = LoQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003902 }
Evan Chengfca29242007-12-07 08:07:39 +00003903 }
3904
Nate Begeman2c87c422009-02-23 08:49:38 +00003905 int BestHiQuad = -1;
Evan Cheng75184a92007-12-11 01:46:18 +00003906 MaxQuad = 1;
3907 for (unsigned i = 0; i < 4; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003908 if (HiQuad[i] > MaxQuad) {
3909 BestHiQuad = i;
3910 MaxQuad = HiQuad[i];
Evan Cheng75184a92007-12-11 01:46:18 +00003911 }
3912 }
3913
Nate Begeman2c87c422009-02-23 08:49:38 +00003914 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003915 // of the two input vectors, shuffle them into one input vector so only a
Nate Begeman2c87c422009-02-23 08:49:38 +00003916 // single pshufb instruction is necessary. If There are more than 2 input
3917 // quads, disable the next transformation since it does not help SSSE3.
3918 bool V1Used = InputQuads[0] || InputQuads[1];
3919 bool V2Used = InputQuads[2] || InputQuads[3];
3920 if (TLI.getSubtarget()->hasSSSE3()) {
3921 if (InputQuads.count() == 2 && V1Used && V2Used) {
3922 BestLoQuad = InputQuads.find_first();
3923 BestHiQuad = InputQuads.find_next(BestLoQuad);
3924 }
3925 if (InputQuads.count() > 2) {
3926 BestLoQuad = -1;
3927 BestHiQuad = -1;
3928 }
3929 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003930
Nate Begeman2c87c422009-02-23 08:49:38 +00003931 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3932 // the shuffle mask. If a quad is scored as -1, that means that it contains
3933 // words from all 4 input quadwords.
3934 SDValue NewV;
3935 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00003936 SmallVector<int, 8> MaskV;
3937 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3938 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003939 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003940 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3941 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3942 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00003943
Nate Begeman2c87c422009-02-23 08:49:38 +00003944 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3945 // source words for the shuffle, to aid later transformations.
3946 bool AllWordsInNewV = true;
Mon P Wangb1db1202009-03-11 06:35:11 +00003947 bool InOrder[2] = { true, true };
Evan Cheng75184a92007-12-11 01:46:18 +00003948 for (unsigned i = 0; i != 8; ++i) {
Nate Begeman2c87c422009-02-23 08:49:38 +00003949 int idx = MaskVals[i];
Mon P Wangb1db1202009-03-11 06:35:11 +00003950 if (idx != (int)i)
3951 InOrder[i/4] = false;
Nate Begeman2c87c422009-02-23 08:49:38 +00003952 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng75184a92007-12-11 01:46:18 +00003953 continue;
Nate Begeman2c87c422009-02-23 08:49:38 +00003954 AllWordsInNewV = false;
3955 break;
Evan Cheng75184a92007-12-11 01:46:18 +00003956 }
Bill Wendling2c7cd592008-08-21 22:35:37 +00003957
Nate Begeman2c87c422009-02-23 08:49:38 +00003958 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3959 if (AllWordsInNewV) {
3960 for (int i = 0; i != 8; ++i) {
3961 int idx = MaskVals[i];
3962 if (idx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00003963 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003964 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begeman2c87c422009-02-23 08:49:38 +00003965 if ((idx != i) && idx < 4)
3966 pshufhw = false;
3967 if ((idx != i) && idx > 3)
3968 pshuflw = false;
Evan Cheng75184a92007-12-11 01:46:18 +00003969 }
Nate Begeman2c87c422009-02-23 08:49:38 +00003970 V1 = NewV;
3971 V2Used = false;
3972 BestLoQuad = 0;
3973 BestHiQuad = 1;
Evan Chengfca29242007-12-07 08:07:39 +00003974 }
Evan Cheng75184a92007-12-11 01:46:18 +00003975
Nate Begeman2c87c422009-02-23 08:49:38 +00003976 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3977 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wangb1db1202009-03-11 06:35:11 +00003978 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003979 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003980 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng75184a92007-12-11 01:46:18 +00003981 }
Evan Cheng75184a92007-12-11 01:46:18 +00003982 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003983
Nate Begeman2c87c422009-02-23 08:49:38 +00003984 // If we have SSSE3, and all words of the result are from 1 input vector,
3985 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3986 // is present, fall back to case 4.
3987 if (TLI.getSubtarget()->hasSSSE3()) {
3988 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003989
Nate Begeman2c87c422009-02-23 08:49:38 +00003990 // If we have elements from both input vectors, set the high bit of the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00003991 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begeman2c87c422009-02-23 08:49:38 +00003992 // mask, and elements that come from V1 in the V2 mask, so that the two
3993 // results can be OR'd together.
3994 bool TwoInputs = V1Used && V2Used;
3995 for (unsigned i = 0; i != 8; ++i) {
3996 int EltIdx = MaskVals[i] * 2;
3997 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003998 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3999 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004000 continue;
4001 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004002 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4003 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004004 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004005 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004006 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00004007 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004008 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004009 if (!TwoInputs)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004010 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004011
Nate Begeman2c87c422009-02-23 08:49:38 +00004012 // Calculate the shuffle mask for the second input, shuffle it, and
4013 // OR it with the first shuffled input.
4014 pshufbMask.clear();
4015 for (unsigned i = 0; i != 8; ++i) {
4016 int EltIdx = MaskVals[i] * 2;
4017 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004018 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4019 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004020 continue;
4021 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004022 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4023 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004024 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004025 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004026 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00004027 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004028 MVT::v16i8, &pshufbMask[0], 16));
4029 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4030 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004031 }
4032
4033 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4034 // and update MaskVals with new element order.
4035 BitVector InOrder(8);
4036 if (BestLoQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004037 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00004038 for (int i = 0; i != 4; ++i) {
4039 int idx = MaskVals[i];
4040 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004041 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004042 InOrder.set(i);
4043 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00004044 MaskV.push_back(idx & 3);
Nate Begeman2c87c422009-02-23 08:49:38 +00004045 InOrder.set(i);
4046 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004047 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004048 }
4049 }
4050 for (unsigned i = 4; i != 8; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00004051 MaskV.push_back(i);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004052 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00004053 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00004054 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004055
Nate Begeman2c87c422009-02-23 08:49:38 +00004056 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4057 // and update MaskVals with the new element order.
4058 if (BestHiQuad >= 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004059 SmallVector<int, 8> MaskV;
Nate Begeman2c87c422009-02-23 08:49:38 +00004060 for (unsigned i = 0; i != 4; ++i)
Nate Begeman543d2142009-04-27 18:41:29 +00004061 MaskV.push_back(i);
Nate Begeman2c87c422009-02-23 08:49:38 +00004062 for (unsigned i = 4; i != 8; ++i) {
4063 int idx = MaskVals[i];
4064 if (idx < 0) {
Nate Begeman543d2142009-04-27 18:41:29 +00004065 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004066 InOrder.set(i);
4067 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman543d2142009-04-27 18:41:29 +00004068 MaskV.push_back((idx & 3) + 4);
Nate Begeman2c87c422009-02-23 08:49:38 +00004069 InOrder.set(i);
4070 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004071 MaskV.push_back(-1);
Nate Begeman2c87c422009-02-23 08:49:38 +00004072 }
4073 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004074 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman543d2142009-04-27 18:41:29 +00004075 &MaskV[0]);
Nate Begeman2c87c422009-02-23 08:49:38 +00004076 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004077
Nate Begeman2c87c422009-02-23 08:49:38 +00004078 // In case BestHi & BestLo were both -1, which means each quadword has a word
4079 // from each of the four input quadwords, calculate the InOrder bitvector now
4080 // before falling through to the insert/extract cleanup.
4081 if (BestLoQuad == -1 && BestHiQuad == -1) {
4082 NewV = V1;
4083 for (int i = 0; i != 8; ++i)
4084 if (MaskVals[i] < 0 || MaskVals[i] == i)
4085 InOrder.set(i);
4086 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004087
Nate Begeman2c87c422009-02-23 08:49:38 +00004088 // The other elements are put in the right place using pextrw and pinsrw.
4089 for (unsigned i = 0; i != 8; ++i) {
4090 if (InOrder[i])
4091 continue;
4092 int EltIdx = MaskVals[i];
4093 if (EltIdx < 0)
4094 continue;
4095 SDValue ExtOp = (EltIdx < 8)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004096 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begeman2c87c422009-02-23 08:49:38 +00004097 DAG.getIntPtrConstant(EltIdx))
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004098 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begeman2c87c422009-02-23 08:49:38 +00004099 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004100 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begeman2c87c422009-02-23 08:49:38 +00004101 DAG.getIntPtrConstant(i));
4102 }
4103 return NewV;
4104}
4105
4106// v16i8 shuffles - Prefer shuffles in the following order:
4107// 1. [ssse3] 1 x pshufb
4108// 2. [ssse3] 2 x pshufb + 1 x por
4109// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4110static
Nate Begeman543d2142009-04-27 18:41:29 +00004111SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4112 SelectionDAG &DAG, X86TargetLowering &TLI) {
4113 SDValue V1 = SVOp->getOperand(0);
4114 SDValue V2 = SVOp->getOperand(1);
4115 DebugLoc dl = SVOp->getDebugLoc();
Nate Begeman2c87c422009-02-23 08:49:38 +00004116 SmallVector<int, 16> MaskVals;
Nate Begeman543d2142009-04-27 18:41:29 +00004117 SVOp->getMask(MaskVals);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004118
Nate Begeman2c87c422009-02-23 08:49:38 +00004119 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004120 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begeman2c87c422009-02-23 08:49:38 +00004121 // present, fall back to case 3.
4122 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4123 bool V1Only = true;
4124 bool V2Only = true;
4125 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00004126 int EltIdx = MaskVals[i];
Nate Begeman2c87c422009-02-23 08:49:38 +00004127 if (EltIdx < 0)
4128 continue;
4129 if (EltIdx < 16)
4130 V2Only = false;
4131 else
4132 V1Only = false;
4133 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004134
Nate Begeman2c87c422009-02-23 08:49:38 +00004135 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4136 if (TLI.getSubtarget()->hasSSSE3()) {
4137 SmallVector<SDValue,16> pshufbMask;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004138
Nate Begeman2c87c422009-02-23 08:49:38 +00004139 // If all result elements are from one input vector, then only translate
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004140 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begeman2c87c422009-02-23 08:49:38 +00004141 //
4142 // Otherwise, we have elements from both input vectors, and must zero out
4143 // elements that come from V2 in the first mask, and V1 in the second mask
4144 // so that we can OR them together.
4145 bool TwoInputs = !(V1Only || V2Only);
4146 for (unsigned i = 0; i != 16; ++i) {
4147 int EltIdx = MaskVals[i];
4148 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004149 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004150 continue;
4151 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004152 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004153 }
4154 // If all the elements are from V2, assign it to V1 and return after
4155 // building the first pshufb.
4156 if (V2Only)
4157 V1 = V2;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004158 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Cheng907a2d22009-02-25 22:49:59 +00004159 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004160 MVT::v16i8, &pshufbMask[0], 16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004161 if (!TwoInputs)
4162 return V1;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004163
Nate Begeman2c87c422009-02-23 08:49:38 +00004164 // Calculate the shuffle mask for the second input, shuffle it, and
4165 // OR it with the first shuffled input.
4166 pshufbMask.clear();
4167 for (unsigned i = 0; i != 16; ++i) {
4168 int EltIdx = MaskVals[i];
4169 if (EltIdx < 16) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004170 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004171 continue;
4172 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004173 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begeman2c87c422009-02-23 08:49:38 +00004174 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004175 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Cheng907a2d22009-02-25 22:49:59 +00004176 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004177 MVT::v16i8, &pshufbMask[0], 16));
4178 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004179 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004180
Nate Begeman2c87c422009-02-23 08:49:38 +00004181 // No SSSE3 - Calculate in place words and then fix all out of place words
4182 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4183 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004184 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4185 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begeman2c87c422009-02-23 08:49:38 +00004186 SDValue NewV = V2Only ? V2 : V1;
4187 for (int i = 0; i != 8; ++i) {
4188 int Elt0 = MaskVals[i*2];
4189 int Elt1 = MaskVals[i*2+1];
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004190
Nate Begeman2c87c422009-02-23 08:49:38 +00004191 // This word of the result is all undef, skip it.
4192 if (Elt0 < 0 && Elt1 < 0)
4193 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004194
Nate Begeman2c87c422009-02-23 08:49:38 +00004195 // This word of the result is already in the correct place, skip it.
4196 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4197 continue;
4198 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4199 continue;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004200
Nate Begeman2c87c422009-02-23 08:49:38 +00004201 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4202 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4203 SDValue InsElt;
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004204
4205 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4206 // using a single extract together, load it and store it.
4207 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004208 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004209 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004210 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004211 DAG.getIntPtrConstant(i));
4212 continue;
4213 }
4214
Nate Begeman2c87c422009-02-23 08:49:38 +00004215 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004216 // source byte is not also odd, shift the extracted word left 8 bits
4217 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begeman2c87c422009-02-23 08:49:38 +00004218 if (Elt1 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004219 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begeman2c87c422009-02-23 08:49:38 +00004220 DAG.getIntPtrConstant(Elt1 / 2));
4221 if ((Elt1 & 1) == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004222 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004223 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004224 else if (Elt0 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004225 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4226 DAG.getConstant(0xFF00, MVT::i16));
Nate Begeman2c87c422009-02-23 08:49:38 +00004227 }
4228 // If Elt0 is defined, extract it from the appropriate source. If the
4229 // source byte is not also even, shift the extracted word right 8 bits. If
4230 // Elt1 was also defined, OR the extracted values together before
4231 // inserting them in the result.
4232 if (Elt0 >= 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004233 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begeman2c87c422009-02-23 08:49:38 +00004234 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4235 if ((Elt0 & 1) != 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004236 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begeman2c87c422009-02-23 08:49:38 +00004237 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wangd0cec7a2009-03-11 18:47:57 +00004238 else if (Elt1 >= 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004239 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4240 DAG.getConstant(0x00FF, MVT::i16));
4241 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begeman2c87c422009-02-23 08:49:38 +00004242 : InsElt0;
4243 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004244 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begeman2c87c422009-02-23 08:49:38 +00004245 DAG.getIntPtrConstant(i));
4246 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004247 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng75184a92007-12-11 01:46:18 +00004248}
4249
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004250/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4251/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4252/// done when every pair / quad of shuffle mask elements point to elements in
4253/// the right sequence. e.g.
Evan Cheng75184a92007-12-11 01:46:18 +00004254/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4255static
Nate Begeman543d2142009-04-27 18:41:29 +00004256SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4257 SelectionDAG &DAG,
4258 TargetLowering &TLI, DebugLoc dl) {
Owen Andersonac9de032009-08-10 22:56:29 +00004259 EVT VT = SVOp->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00004260 SDValue V1 = SVOp->getOperand(0);
4261 SDValue V2 = SVOp->getOperand(1);
4262 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004263 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004264 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersonac9de032009-08-10 22:56:29 +00004265 EVT MaskEltVT = MaskVT.getVectorElementType();
4266 EVT NewVT = MaskVT;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004267 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00004268 default: assert(false && "Unexpected!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004269 case MVT::v4f32: NewVT = MVT::v2f64; break;
4270 case MVT::v4i32: NewVT = MVT::v2i64; break;
4271 case MVT::v8i16: NewVT = MVT::v4i32; break;
4272 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004273 }
4274
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004275 if (NewWidth == 2) {
Duncan Sands92c43912008-06-06 12:08:01 +00004276 if (VT.isInteger())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004277 NewVT = MVT::v2i64;
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004278 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004279 NewVT = MVT::v2f64;
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +00004280 }
Nate Begeman543d2142009-04-27 18:41:29 +00004281 int Scale = NumElems / NewWidth;
4282 SmallVector<int, 8> MaskVec;
Evan Cheng75184a92007-12-11 01:46:18 +00004283 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman543d2142009-04-27 18:41:29 +00004284 int StartIdx = -1;
4285 for (int j = 0; j < Scale; ++j) {
4286 int EltIdx = SVOp->getMaskElt(i+j);
4287 if (EltIdx < 0)
Evan Cheng75184a92007-12-11 01:46:18 +00004288 continue;
Nate Begeman543d2142009-04-27 18:41:29 +00004289 if (StartIdx == -1)
Evan Cheng75184a92007-12-11 01:46:18 +00004290 StartIdx = EltIdx - (EltIdx % Scale);
4291 if (EltIdx != StartIdx + j)
Dan Gohman8181bd12008-07-27 21:46:04 +00004292 return SDValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004293 }
Nate Begeman543d2142009-04-27 18:41:29 +00004294 if (StartIdx == -1)
4295 MaskVec.push_back(-1);
Evan Cheng75184a92007-12-11 01:46:18 +00004296 else
Nate Begeman543d2142009-04-27 18:41:29 +00004297 MaskVec.push_back(StartIdx / Scale);
Evan Chengfca29242007-12-07 08:07:39 +00004298 }
4299
Dale Johannesence0805b2009-02-03 19:33:06 +00004300 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4301 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman543d2142009-04-27 18:41:29 +00004302 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Chengfca29242007-12-07 08:07:39 +00004303}
4304
Evan Chenge9b9c672008-05-09 21:53:03 +00004305/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng40ee6e52008-05-08 00:57:18 +00004306///
Owen Andersonac9de032009-08-10 22:56:29 +00004307static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman543d2142009-04-27 18:41:29 +00004308 SDValue SrcOp, SelectionDAG &DAG,
4309 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004310 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004311 LoadSDNode *LD = NULL;
Gabor Greif1c80d112008-08-28 21:40:38 +00004312 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng40ee6e52008-05-08 00:57:18 +00004313 LD = dyn_cast<LoadSDNode>(SrcOp);
4314 if (!LD) {
4315 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4316 // instead.
Owen Anderson2dd68a22009-08-11 21:59:30 +00004317 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4318 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng40ee6e52008-05-08 00:57:18 +00004319 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4320 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson2dd68a22009-08-11 21:59:30 +00004321 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004322 // PR2108
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004323 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesence0805b2009-02-03 19:33:06 +00004324 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4325 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4326 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4327 OpVT,
Gabor Greif825aa892008-08-28 23:19:51 +00004328 SrcOp.getOperand(0)
4329 .getOperand(0))));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004330 }
4331 }
4332 }
4333
Dale Johannesence0805b2009-02-03 19:33:06 +00004334 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4335 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michel91099d62009-02-17 22:15:04 +00004336 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesence0805b2009-02-03 19:33:06 +00004337 OpVT, SrcOp)));
Evan Cheng40ee6e52008-05-08 00:57:18 +00004338}
4339
Evan Chengf50554e2008-07-22 21:13:36 +00004340/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4341/// shuffles.
Dan Gohman8181bd12008-07-27 21:46:04 +00004342static SDValue
Nate Begeman543d2142009-04-27 18:41:29 +00004343LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4344 SDValue V1 = SVOp->getOperand(0);
4345 SDValue V2 = SVOp->getOperand(1);
4346 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00004347 EVT VT = SVOp->getValueType(0);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004348
Evan Chengf50554e2008-07-22 21:13:36 +00004349 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola4e3ff5a2008-08-28 18:32:53 +00004350 Locs.resize(4);
Nate Begeman543d2142009-04-27 18:41:29 +00004351 SmallVector<int, 8> Mask1(4U, -1);
4352 SmallVector<int, 8> PermMask;
4353 SVOp->getMask(PermMask);
4354
Evan Chengf50554e2008-07-22 21:13:36 +00004355 unsigned NumHi = 0;
4356 unsigned NumLo = 0;
Evan Chengf50554e2008-07-22 21:13:36 +00004357 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00004358 int Idx = PermMask[i];
4359 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004360 Locs[i] = std::make_pair(-1, -1);
4361 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004362 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4363 if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004364 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman543d2142009-04-27 18:41:29 +00004365 Mask1[NumLo] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004366 NumLo++;
4367 } else {
4368 Locs[i] = std::make_pair(1, NumHi);
4369 if (2+NumHi < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004370 Mask1[2+NumHi] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004371 NumHi++;
4372 }
4373 }
4374 }
Evan Cheng3cae0332008-07-23 00:22:17 +00004375
Evan Chengf50554e2008-07-22 21:13:36 +00004376 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng3cae0332008-07-23 00:22:17 +00004377 // If no more than two elements come from either vector. This can be
4378 // implemented with two shuffles. First shuffle gather the elements.
4379 // The second shuffle, which takes the first shuffle as both of its
4380 // vector operands, put the elements into the right order.
Nate Begeman543d2142009-04-27 18:41:29 +00004381 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004382
Nate Begeman543d2142009-04-27 18:41:29 +00004383 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004384
Evan Chengf50554e2008-07-22 21:13:36 +00004385 for (unsigned i = 0; i != 4; ++i) {
4386 if (Locs[i].first == -1)
4387 continue;
4388 else {
4389 unsigned Idx = (i < 2) ? 0 : 4;
4390 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004391 Mask2[i] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004392 }
4393 }
4394
Nate Begeman543d2142009-04-27 18:41:29 +00004395 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004396 } else if (NumLo == 3 || NumHi == 3) {
4397 // Otherwise, we must have three elements from one vector, call it X, and
4398 // one element from the other, call it Y. First, use a shufps to build an
4399 // intermediate vector with the one element from Y and the element from X
4400 // that will be in the same half in the final destination (the indexes don't
4401 // matter). Then, use a shufps to build the final vector, taking the half
4402 // containing the element from Y from the intermediate, and the other half
4403 // from X.
4404 if (NumHi == 3) {
4405 // Normalize it so the 3 elements come from V1.
Nate Begeman543d2142009-04-27 18:41:29 +00004406 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng3cae0332008-07-23 00:22:17 +00004407 std::swap(V1, V2);
4408 }
4409
4410 // Find the element from V2.
4411 unsigned HiIndex;
4412 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman543d2142009-04-27 18:41:29 +00004413 int Val = PermMask[HiIndex];
4414 if (Val < 0)
Evan Cheng3cae0332008-07-23 00:22:17 +00004415 continue;
Evan Cheng3cae0332008-07-23 00:22:17 +00004416 if (Val >= 4)
4417 break;
4418 }
4419
Nate Begeman543d2142009-04-27 18:41:29 +00004420 Mask1[0] = PermMask[HiIndex];
4421 Mask1[1] = -1;
4422 Mask1[2] = PermMask[HiIndex^1];
4423 Mask1[3] = -1;
4424 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004425
4426 if (HiIndex >= 2) {
Nate Begeman543d2142009-04-27 18:41:29 +00004427 Mask1[0] = PermMask[0];
4428 Mask1[1] = PermMask[1];
4429 Mask1[2] = HiIndex & 1 ? 6 : 4;
4430 Mask1[3] = HiIndex & 1 ? 4 : 6;
4431 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004432 } else {
Nate Begeman543d2142009-04-27 18:41:29 +00004433 Mask1[0] = HiIndex & 1 ? 2 : 0;
4434 Mask1[1] = HiIndex & 1 ? 0 : 2;
4435 Mask1[2] = PermMask[2];
4436 Mask1[3] = PermMask[3];
4437 if (Mask1[2] >= 0)
4438 Mask1[2] += 4;
4439 if (Mask1[3] >= 0)
4440 Mask1[3] += 4;
4441 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng3cae0332008-07-23 00:22:17 +00004442 }
Evan Chengf50554e2008-07-22 21:13:36 +00004443 }
4444
4445 // Break it into (shuffle shuffle_hi, shuffle_lo).
4446 Locs.clear();
Nate Begeman543d2142009-04-27 18:41:29 +00004447 SmallVector<int,8> LoMask(4U, -1);
4448 SmallVector<int,8> HiMask(4U, -1);
4449
4450 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengf50554e2008-07-22 21:13:36 +00004451 unsigned MaskIdx = 0;
4452 unsigned LoIdx = 0;
4453 unsigned HiIdx = 2;
4454 for (unsigned i = 0; i != 4; ++i) {
4455 if (i == 2) {
4456 MaskPtr = &HiMask;
4457 MaskIdx = 1;
4458 LoIdx = 0;
4459 HiIdx = 2;
4460 }
Nate Begeman543d2142009-04-27 18:41:29 +00004461 int Idx = PermMask[i];
4462 if (Idx < 0) {
Evan Chengf50554e2008-07-22 21:13:36 +00004463 Locs[i] = std::make_pair(-1, -1);
Nate Begeman543d2142009-04-27 18:41:29 +00004464 } else if (Idx < 4) {
Evan Chengf50554e2008-07-22 21:13:36 +00004465 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004466 (*MaskPtr)[LoIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004467 LoIdx++;
4468 } else {
4469 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman543d2142009-04-27 18:41:29 +00004470 (*MaskPtr)[HiIdx] = Idx;
Evan Chengf50554e2008-07-22 21:13:36 +00004471 HiIdx++;
4472 }
4473 }
4474
Nate Begeman543d2142009-04-27 18:41:29 +00004475 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4476 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4477 SmallVector<int, 8> MaskOps;
Evan Chengf50554e2008-07-22 21:13:36 +00004478 for (unsigned i = 0; i != 4; ++i) {
4479 if (Locs[i].first == -1) {
Nate Begeman543d2142009-04-27 18:41:29 +00004480 MaskOps.push_back(-1);
Evan Chengf50554e2008-07-22 21:13:36 +00004481 } else {
4482 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman543d2142009-04-27 18:41:29 +00004483 MaskOps.push_back(Idx);
Evan Chengf50554e2008-07-22 21:13:36 +00004484 }
4485 }
Nate Begeman543d2142009-04-27 18:41:29 +00004486 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengf50554e2008-07-22 21:13:36 +00004487}
4488
Dan Gohman8181bd12008-07-27 21:46:04 +00004489SDValue
4490X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman543d2142009-04-27 18:41:29 +00004491 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman8181bd12008-07-27 21:46:04 +00004492 SDValue V1 = Op.getOperand(0);
4493 SDValue V2 = Op.getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00004494 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004495 DebugLoc dl = Op.getDebugLoc();
Nate Begeman543d2142009-04-27 18:41:29 +00004496 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands92c43912008-06-06 12:08:01 +00004497 bool isMMX = VT.getSizeInBits() == 64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004498 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4499 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
4500 bool V1IsSplat = false;
4501 bool V2IsSplat = false;
4502
Nate Begeman543d2142009-04-27 18:41:29 +00004503 if (isZeroShuffle(SVOp))
Dale Johannesence0805b2009-02-03 19:33:06 +00004504 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505
Nate Begeman543d2142009-04-27 18:41:29 +00004506 // Promote splats to v4f32.
4507 if (SVOp->isSplat()) {
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004508 if (isMMX || NumElems < 4)
Nate Begeman543d2142009-04-27 18:41:29 +00004509 return Op;
4510 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004511 }
4512
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004513 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4514 // do it!
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004515 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004516 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004517 if (NewOp.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00004518 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesence0805b2009-02-03 19:33:06 +00004519 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004520 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004521 // FIXME: Figure out a cleaner way to do this.
4522 // Try to make use of movq to zero out the top part.
Gabor Greif1c80d112008-08-28 21:40:38 +00004523 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004524 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greif1c80d112008-08-28 21:40:38 +00004525 if (NewOp.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00004526 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4527 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4528 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004529 }
Gabor Greif1c80d112008-08-28 21:40:38 +00004530 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman543d2142009-04-27 18:41:29 +00004531 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4532 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chenge9b9c672008-05-09 21:53:03 +00004533 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman543d2142009-04-27 18:41:29 +00004534 DAG, Subtarget, dl);
Evan Cheng15e8f5a2007-12-15 03:00:47 +00004535 }
4536 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004537
Nate Begeman543d2142009-04-27 18:41:29 +00004538 if (X86::isPSHUFDMask(SVOp))
4539 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004540
Evan Chengdea99362008-05-29 08:22:04 +00004541 // Check if this can be converted into a logical shift.
4542 bool isLeft = false;
4543 unsigned ShAmt = 0;
Dan Gohman8181bd12008-07-27 21:46:04 +00004544 SDValue ShVal;
Nate Begeman543d2142009-04-27 18:41:29 +00004545 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chenge31a26a2009-12-09 21:00:30 +00004546 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengdea99362008-05-29 08:22:04 +00004547 if (isShift && ShVal.hasOneUse()) {
Scott Michel91099d62009-02-17 22:15:04 +00004548 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengdea99362008-05-29 08:22:04 +00004549 // v_set0 + movlhps or movhlps, etc.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004550 EVT EltVT = VT.getVectorElementType();
4551 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004552 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004553 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004554
Nate Begeman543d2142009-04-27 18:41:29 +00004555 if (X86::isMOVLMask(SVOp)) {
Evan Cheng40ee6e52008-05-08 00:57:18 +00004556 if (V1IsUndef)
4557 return V2;
Gabor Greif1c80d112008-08-28 21:40:38 +00004558 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesence0805b2009-02-03 19:33:06 +00004559 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begeman6357f9d2008-07-25 19:05:58 +00004560 if (!isMMX)
4561 return Op;
Evan Cheng40ee6e52008-05-08 00:57:18 +00004562 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004563
Nate Begeman543d2142009-04-27 18:41:29 +00004564 // FIXME: fold these into legal mask.
4565 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4566 X86::isMOVSLDUPMask(SVOp) ||
4567 X86::isMOVHLPSMask(SVOp) ||
Nate Begemanb13034d2009-11-07 23:17:15 +00004568 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman543d2142009-04-27 18:41:29 +00004569 X86::isMOVLPMask(SVOp)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004570 return Op;
4571
Nate Begeman543d2142009-04-27 18:41:29 +00004572 if (ShouldXformToMOVHLPS(SVOp) ||
4573 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4574 return CommuteVectorShuffle(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004575
Evan Chengdea99362008-05-29 08:22:04 +00004576 if (isShift) {
4577 // No better options. Use a vshl / vsrl.
Dan Gohman3bab1f72009-09-23 21:02:20 +00004578 EVT EltVT = VT.getVectorElementType();
4579 ShAmt *= EltVT.getSizeInBits();
Dale Johannesence0805b2009-02-03 19:33:06 +00004580 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengdea99362008-05-29 08:22:04 +00004581 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004582
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004583 bool Commuted = false;
Chris Lattnere6aa3862007-11-25 00:24:49 +00004584 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4585 // 1,1,1,1 -> v8i16 though.
Gabor Greif1c80d112008-08-28 21:40:38 +00004586 V1IsSplat = isSplatVector(V1.getNode());
4587 V2IsSplat = isSplatVector(V2.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00004588
Chris Lattnere6aa3862007-11-25 00:24:49 +00004589 // Canonicalize the splat or undef, if present, to be on the RHS.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004590 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman543d2142009-04-27 18:41:29 +00004591 Op = CommuteVectorShuffle(SVOp, DAG);
4592 SVOp = cast<ShuffleVectorSDNode>(Op);
4593 V1 = SVOp->getOperand(0);
4594 V2 = SVOp->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004595 std::swap(V1IsSplat, V2IsSplat);
4596 std::swap(V1IsUndef, V2IsUndef);
4597 Commuted = true;
4598 }
4599
Nate Begeman543d2142009-04-27 18:41:29 +00004600 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4601 // Shuffling low element of v1 into undef, just return v1.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004602 if (V2IsUndef)
Nate Begeman543d2142009-04-27 18:41:29 +00004603 return V1;
4604 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4605 // the instruction selector will not match, so get a canonical MOVL with
4606 // swapped operands to undo the commute.
4607 return getMOVL(DAG, dl, VT, V2, V1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004608 }
4609
Nate Begeman543d2142009-04-27 18:41:29 +00004610 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4611 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4612 X86::isUNPCKLMask(SVOp) ||
4613 X86::isUNPCKHMask(SVOp))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004614 return Op;
4615
4616 if (V2IsSplat) {
4617 // Normalize mask so all entries that point to V2 points to its first
4618 // element then try to match unpck{h|l} again. If match, return a
4619 // new vector_shuffle with the corrected mask.
Nate Begeman543d2142009-04-27 18:41:29 +00004620 SDValue NewMask = NormalizeMask(SVOp, DAG);
4621 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4622 if (NSVOp != SVOp) {
4623 if (X86::isUNPCKLMask(NSVOp, true)) {
4624 return NewMask;
4625 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4626 return NewMask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 }
4628 }
4629 }
4630
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004631 if (Commuted) {
4632 // Commute is back and try unpck* again.
Nate Begeman543d2142009-04-27 18:41:29 +00004633 // FIXME: this seems wrong.
4634 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4635 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4636 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4637 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4638 X86::isUNPCKLMask(NewSVOp) ||
4639 X86::isUNPCKHMask(NewSVOp))
4640 return NewOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004641 }
4642
Nate Begeman2c87c422009-02-23 08:49:38 +00004643 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman543d2142009-04-27 18:41:29 +00004644
4645 // Normalize the node to match x86 shuffle ops if needed
4646 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4647 return CommuteVectorShuffle(SVOp, DAG);
4648
4649 // Check for legal shuffle and return?
4650 SmallVector<int, 16> PermMask;
4651 SVOp->getMask(PermMask);
4652 if (isShuffleMaskLegal(PermMask, VT))
Evan Chengbf8b2c52008-04-05 00:30:36 +00004653 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004654
Evan Cheng75184a92007-12-11 01:46:18 +00004655 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004656 if (VT == MVT::v8i16) {
Nate Begeman543d2142009-04-27 18:41:29 +00004657 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greif1c80d112008-08-28 21:40:38 +00004658 if (NewOp.getNode())
Evan Cheng75184a92007-12-11 01:46:18 +00004659 return NewOp;
4660 }
4661
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004662 if (VT == MVT::v16i8) {
Nate Begeman543d2142009-04-27 18:41:29 +00004663 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begeman2c87c422009-02-23 08:49:38 +00004664 if (NewOp.getNode())
4665 return NewOp;
4666 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004667
Evan Chengf50554e2008-07-22 21:13:36 +00004668 // Handle all 4 wide cases with a number of shuffles except for MMX.
4669 if (NumElems == 4 && !isMMX)
Nate Begeman543d2142009-04-27 18:41:29 +00004670 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004671
Dan Gohman8181bd12008-07-27 21:46:04 +00004672 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004673}
4674
Dan Gohman8181bd12008-07-27 21:46:04 +00004675SDValue
4676X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begemand77e59e2008-02-11 04:19:36 +00004677 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004678 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004679 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00004680 if (VT.getSizeInBits() == 8) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004681 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004682 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004683 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004684 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004685 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004686 } else if (VT.getSizeInBits() == 16) {
Evan Chengf9393b32009-01-02 05:29:08 +00004687 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4688 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4689 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004690 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4691 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004692 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004693 MVT::v4i32,
Evan Chengf9393b32009-01-02 05:29:08 +00004694 Op.getOperand(0)),
4695 Op.getOperand(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004696 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begemand77e59e2008-02-11 04:19:36 +00004697 Op.getOperand(0), Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004698 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begemand77e59e2008-02-11 04:19:36 +00004699 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004700 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004701 } else if (VT == MVT::f32) {
Evan Cheng6c249332008-03-24 21:52:23 +00004702 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4703 // the result back to FR32 register. It's only worth matching if the
Dan Gohman9fdd0142008-10-31 00:57:24 +00004704 // result has a single use which is a store or a bitcast to i32. And in
4705 // the case of a store, it's not worth it if the index is a constant 0,
4706 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng6c249332008-03-24 21:52:23 +00004707 if (!Op.hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00004708 return SDValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004709 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman9fdd0142008-10-31 00:57:24 +00004710 if ((User->getOpcode() != ISD::STORE ||
4711 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4712 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman788db592008-04-16 02:32:24 +00004713 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004714 User->getValueType(0) != MVT::i32))
Dan Gohman8181bd12008-07-27 21:46:04 +00004715 return SDValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004716 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4717 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesence0805b2009-02-03 19:33:06 +00004718 Op.getOperand(0)),
4719 Op.getOperand(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004720 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4721 } else if (VT == MVT::i32) {
Mon P Wangac2a3c52009-01-15 21:10:20 +00004722 // ExtractPS works with constant index.
4723 if (isa<ConstantSDNode>(Op.getOperand(1)))
4724 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004725 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004726 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004727}
4728
4729
Dan Gohman8181bd12008-07-27 21:46:04 +00004730SDValue
4731X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004732 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00004733 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004734
Evan Cheng6c249332008-03-24 21:52:23 +00004735 if (Subtarget->hasSSE41()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004736 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00004737 if (Res.getNode())
Evan Cheng6c249332008-03-24 21:52:23 +00004738 return Res;
4739 }
Nate Begemand77e59e2008-02-11 04:19:36 +00004740
Owen Andersonac9de032009-08-10 22:56:29 +00004741 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004742 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004743 // TODO: handle v16i8.
Duncan Sands92c43912008-06-06 12:08:01 +00004744 if (VT.getSizeInBits() == 16) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004745 SDValue Vec = Op.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004746 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng75184a92007-12-11 01:46:18 +00004747 if (Idx == 0)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004748 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4749 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michel91099d62009-02-17 22:15:04 +00004750 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004751 MVT::v4i32, Vec),
Evan Cheng75184a92007-12-11 01:46:18 +00004752 Op.getOperand(1)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004753 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck5d3fa642009-12-17 15:31:52 +00004754 EVT EltVT = MVT::i32;
Dan Gohman3bab1f72009-09-23 21:02:20 +00004755 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 Op.getOperand(0), Op.getOperand(1));
Dan Gohman3bab1f72009-09-23 21:02:20 +00004757 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004758 DAG.getValueType(VT));
Dale Johannesence0805b2009-02-03 19:33:06 +00004759 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands92c43912008-06-06 12:08:01 +00004760 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004761 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004762 if (Idx == 0)
4763 return Op;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004764
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004765 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman543d2142009-04-27 18:41:29 +00004766 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004767 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004768 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004769 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004770 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004771 DAG.getIntPtrConstant(0));
Duncan Sands92c43912008-06-06 12:08:01 +00004772 } else if (VT.getSizeInBits() == 64) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004773 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4774 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4775 // to match extract_elt for f64.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004776 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004777 if (Idx == 0)
4778 return Op;
4779
4780 // UNPCKHPD the element to the lowest double word, then movsd.
4781 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4782 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman543d2142009-04-27 18:41:29 +00004783 int Mask[2] = { 1, -1 };
Owen Andersonac9de032009-08-10 22:56:29 +00004784 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004785 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman543d2142009-04-27 18:41:29 +00004786 DAG.getUNDEF(VVT), Mask);
Dale Johannesence0805b2009-02-03 19:33:06 +00004787 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner5872a362008-01-17 07:00:52 +00004788 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004789 }
4790
Dan Gohman8181bd12008-07-27 21:46:04 +00004791 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004792}
4793
Dan Gohman8181bd12008-07-27 21:46:04 +00004794SDValue
4795X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersonac9de032009-08-10 22:56:29 +00004796 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004797 EVT EltVT = VT.getVectorElementType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004798 DebugLoc dl = Op.getDebugLoc();
Nate Begemand77e59e2008-02-11 04:19:36 +00004799
Dan Gohman8181bd12008-07-27 21:46:04 +00004800 SDValue N0 = Op.getOperand(0);
4801 SDValue N1 = Op.getOperand(1);
4802 SDValue N2 = Op.getOperand(2);
Nate Begemand77e59e2008-02-11 04:19:36 +00004803
Dan Gohman3bab1f72009-09-23 21:02:20 +00004804 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohman5a7af042008-08-14 22:53:18 +00004805 isa<ConstantSDNode>(N2)) {
Dan Gohman3bab1f72009-09-23 21:02:20 +00004806 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4807 : X86ISD::PINSRW;
Nate Begemand77e59e2008-02-11 04:19:36 +00004808 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4809 // argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004810 if (N1.getValueType() != MVT::i32)
4811 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4812 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004813 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004814 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004815 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begemand77e59e2008-02-11 04:19:36 +00004816 // Bits [7:6] of the constant are the source select. This will always be
4817 // zero here. The DAG Combiner may combine an extract_elt index into these
4818 // bits. For example (insert (extract, 3), 2) could be matched by putting
4819 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michel91099d62009-02-17 22:15:04 +00004820 // Bits [5:4] of the constant are the destination select. This is the
Nate Begemand77e59e2008-02-11 04:19:36 +00004821 // value of the incoming immediate.
Scott Michel91099d62009-02-17 22:15:04 +00004822 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begemand77e59e2008-02-11 04:19:36 +00004823 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004824 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherefb657e2009-07-24 00:33:09 +00004825 // Create this as a scalar to vector..
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004826 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesence0805b2009-02-03 19:33:06 +00004827 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman3bab1f72009-09-23 21:02:20 +00004828 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherefb657e2009-07-24 00:33:09 +00004829 // PINSR* works with constant index.
4830 return Op;
Nate Begemand77e59e2008-02-11 04:19:36 +00004831 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004832 return SDValue();
Nate Begemand77e59e2008-02-11 04:19:36 +00004833}
4834
Dan Gohman8181bd12008-07-27 21:46:04 +00004835SDValue
4836X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00004837 EVT VT = Op.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00004838 EVT EltVT = VT.getVectorElementType();
Nate Begemand77e59e2008-02-11 04:19:36 +00004839
4840 if (Subtarget->hasSSE41())
4841 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4842
Dan Gohman3bab1f72009-09-23 21:02:20 +00004843 if (EltVT == MVT::i8)
Dan Gohman8181bd12008-07-27 21:46:04 +00004844 return SDValue();
Evan Chenge12a7eb2007-12-12 07:55:34 +00004845
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004846 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00004847 SDValue N0 = Op.getOperand(0);
4848 SDValue N1 = Op.getOperand(1);
4849 SDValue N2 = Op.getOperand(2);
Evan Chenge12a7eb2007-12-12 07:55:34 +00004850
Dan Gohman3bab1f72009-09-23 21:02:20 +00004851 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Chenge12a7eb2007-12-12 07:55:34 +00004852 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4853 // as its second argument.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004854 if (N1.getValueType() != MVT::i32)
4855 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4856 if (N2.getValueType() != MVT::i32)
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00004857 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesence0805b2009-02-03 19:33:06 +00004858 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004859 }
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004861}
4862
Dan Gohman8181bd12008-07-27 21:46:04 +00004863SDValue
4864X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00004865 DebugLoc dl = Op.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004866 if (Op.getValueType() == MVT::v2f32)
4867 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4868 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4869 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng759fe022008-07-22 18:39:19 +00004870 Op.getOperand(0))));
4871
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004872 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4873 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindolafe2a3972009-08-03 02:45:34 +00004874
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004875 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4876 EVT VT = MVT::v2i32;
4877 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengd1045a62008-02-18 23:04:32 +00004878 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004879 case MVT::v16i8:
4880 case MVT::v8i16:
4881 VT = MVT::v4i32;
Evan Chengd1045a62008-02-18 23:04:32 +00004882 break;
4883 }
Dale Johannesence0805b2009-02-03 19:33:06 +00004884 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4885 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886}
4887
Bill Wendlingfef06052008-09-16 21:48:12 +00004888// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4889// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4890// one of the above mentioned nodes. It has to be wrapped because otherwise
4891// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4892// be used to form addressing mode. These wrapped nodes will be selected
4893// into MOV32ri.
Dan Gohman8181bd12008-07-27 21:46:04 +00004894SDValue
4895X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004896 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004897
Chris Lattner5062b3b2009-06-26 19:22:52 +00004898 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4899 // global base reg.
4900 unsigned char OpFlag = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004901 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004902 CodeModel::Model M = getTargetMachine().getCodeModel();
4903
Chris Lattner28d40c62009-07-11 20:29:19 +00004904 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004905 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004906 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004907 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004908 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004909 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004910 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004911
Evan Cheng68c18682009-03-13 07:51:59 +00004912 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner5062b3b2009-06-26 19:22:52 +00004913 CP->getAlignment(),
4914 CP->getOffset(), OpFlag);
4915 DebugLoc DL = CP->getDebugLoc();
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004916 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004917 // With PIC, the address is actually $g + Offset.
Chris Lattner5062b3b2009-06-26 19:22:52 +00004918 if (OpFlag) {
4919 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesen24dd9a52009-02-07 00:55:49 +00004920 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner5062b3b2009-06-26 19:22:52 +00004921 DebugLoc::getUnknownLoc(), getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 Result);
4923 }
4924
4925 return Result;
4926}
4927
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004928SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4929 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004930
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004931 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4932 // global base reg.
4933 unsigned char OpFlag = 0;
4934 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004935 CodeModel::Model M = getTargetMachine().getCodeModel();
4936
Chris Lattner28d40c62009-07-11 20:29:19 +00004937 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004938 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004939 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004940 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004941 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004942 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004943 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004944
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004945 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4946 OpFlag);
4947 DebugLoc DL = JT->getDebugLoc();
4948 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004949
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004950 // With PIC, the address is actually $g + Offset.
4951 if (OpFlag) {
4952 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4953 DAG.getNode(X86ISD::GlobalBaseReg,
4954 DebugLoc::getUnknownLoc(), getPointerTy()),
4955 Result);
4956 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004957
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004958 return Result;
4959}
4960
4961SDValue
4962X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4963 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004964
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004965 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4966 // global base reg.
4967 unsigned char OpFlag = 0;
4968 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004969 CodeModel::Model M = getTargetMachine().getCodeModel();
4970
Chris Lattner28d40c62009-07-11 20:29:19 +00004971 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00004972 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004973 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner4a948932009-07-10 20:47:30 +00004974 else if (Subtarget->isPICStyleGOT())
Chris Lattnerf165d342009-07-09 04:24:46 +00004975 OpFlag = X86II::MO_GOTOFF;
Chris Lattner2e9393c2009-07-10 21:00:45 +00004976 else if (Subtarget->isPICStyleStubPIC())
Chris Lattnerf165d342009-07-09 04:24:46 +00004977 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004978
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004979 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004980
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004981 DebugLoc DL = Op.getDebugLoc();
4982 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004983
4984
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004985 // With PIC, the address is actually $g + Offset.
4986 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattneraa7c6d22009-07-09 03:15:51 +00004987 !Subtarget->is64Bit()) {
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004988 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4989 DAG.getNode(X86ISD::GlobalBaseReg,
4990 DebugLoc::getUnknownLoc(),
4991 getPointerTy()),
4992 Result);
4993 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00004994
Chris Lattnerdc6fc472009-06-27 04:16:01 +00004995 return Result;
4996}
4997
Dan Gohman8181bd12008-07-27 21:46:04 +00004998SDValue
Dan Gohman064403e2009-10-30 01:28:02 +00004999X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman885793b2009-11-20 23:18:13 +00005000 // Create the TargetBlockAddressAddress node.
5001 unsigned char OpFlags =
5002 Subtarget->ClassifyBlockAddressReference();
Dan Gohman064403e2009-10-30 01:28:02 +00005003 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman885793b2009-11-20 23:18:13 +00005004 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5005 DebugLoc dl = Op.getDebugLoc();
5006 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5007 /*isTarget=*/true, OpFlags);
5008
Dan Gohman064403e2009-10-30 01:28:02 +00005009 if (Subtarget->isPICStyleRIPRel() &&
5010 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman885793b2009-11-20 23:18:13 +00005011 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5012 else
5013 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman064403e2009-10-30 01:28:02 +00005014
Dan Gohman885793b2009-11-20 23:18:13 +00005015 // With PIC, the address is actually $g + Offset.
5016 if (isGlobalRelativeToPICBase(OpFlags)) {
5017 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5018 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5019 Result);
5020 }
Dan Gohman064403e2009-10-30 01:28:02 +00005021
5022 return Result;
5023}
5024
5025SDValue
Dale Johannesenea996922009-02-04 20:06:27 +00005026X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman36322c72008-10-18 02:06:02 +00005027 int64_t Offset,
Evan Cheng7f250d62008-09-24 00:05:32 +00005028 SelectionDAG &DAG) const {
Dan Gohman36322c72008-10-18 02:06:02 +00005029 // Create the TargetGlobalAddress node, folding in the constant
5030 // offset if it is legal.
Chris Lattner505aa6c2009-07-10 07:20:05 +00005031 unsigned char OpFlags =
5032 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005033 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman36322c72008-10-18 02:06:02 +00005034 SDValue Result;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005035 if (OpFlags == X86II::MO_NO_FLAG &&
5036 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner9ab4e662009-07-09 00:58:53 +00005037 // A direct static reference to a global.
Dale Johannesenf97110c2009-07-21 00:12:29 +00005038 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman36322c72008-10-18 02:06:02 +00005039 Offset = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005040 } else {
Chris Lattner5bdaa522009-06-27 05:39:56 +00005041 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005042 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005043
Chris Lattner28d40c62009-07-11 20:29:19 +00005044 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovc283e152009-08-05 23:01:26 +00005045 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005046 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5047 else
5048 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman36322c72008-10-18 02:06:02 +00005049
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005050 // With PIC, the address is actually $g + Offset.
Chris Lattner054532c2009-07-10 07:34:39 +00005051 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesenea996922009-02-04 20:06:27 +00005052 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5053 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005054 Result);
5055 }
Scott Michel91099d62009-02-17 22:15:04 +00005056
Chris Lattner054532c2009-07-10 07:34:39 +00005057 // For globals that require a load from a stub to get the address, emit the
5058 // load.
5059 if (isGlobalStubReference(OpFlags))
Dale Johannesenea996922009-02-04 20:06:27 +00005060 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005061 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005062
Dan Gohman36322c72008-10-18 02:06:02 +00005063 // If there was a non-zero offset that we didn't fold, create an explicit
5064 // addition for it.
5065 if (Offset != 0)
Dale Johannesenea996922009-02-04 20:06:27 +00005066 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman36322c72008-10-18 02:06:02 +00005067 DAG.getConstant(Offset, getPointerTy()));
5068
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005069 return Result;
5070}
5071
Evan Cheng7f250d62008-09-24 00:05:32 +00005072SDValue
5073X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5074 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00005075 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005076 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Cheng7f250d62008-09-24 00:05:32 +00005077}
5078
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005079static SDValue
5080GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersonac9de032009-08-10 22:56:29 +00005081 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005082 unsigned char OperandFlags) {
Anton Korobeynikov7767af52009-12-11 19:39:55 +00005083 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005084 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005085 DebugLoc dl = GA->getDebugLoc();
5086 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5087 GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005088 GA->getOffset(),
5089 OperandFlags);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005090 if (InFlag) {
5091 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005092 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005093 } else {
5094 SDValue Ops[] = { Chain, TGA };
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005095 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005096 }
Anton Korobeynikov7767af52009-12-11 19:39:55 +00005097
5098 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5099 MFI->setHasCalls(true);
5100
Rafael Espindola7fc4b8d2009-04-24 12:59:40 +00005101 SDValue Flag = Chain.getValue(1);
5102 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindolaaf759ab2009-04-17 14:35:58 +00005103}
5104
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005105// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00005106static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005107LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005108 const EVT PtrVT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005109 SDValue InFlag;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00005110 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5111 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005112 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesen24dd9a52009-02-07 00:55:49 +00005113 DebugLoc::getUnknownLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005114 PtrVT), InFlag);
5115 InFlag = Chain.getValue(1);
5116
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005117 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005118}
5119
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005120// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman8181bd12008-07-27 21:46:04 +00005121static SDValue
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005122LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005123 const EVT PtrVT) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005124 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5125 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005126}
5127
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005128// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5129// "local exec" model.
Dan Gohman8181bd12008-07-27 21:46:04 +00005130static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersonac9de032009-08-10 22:56:29 +00005131 const EVT PtrVT, TLSModel::Model model,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005132 bool is64Bit) {
Dale Johannesenea996922009-02-04 20:06:27 +00005133 DebugLoc dl = GA->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005134 // Get the Thread Pointer
Rafael Espindolabca99f72009-04-08 21:14:34 +00005135 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5136 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindolab93a5122009-04-13 13:02:49 +00005137 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005138 MVT::i32));
Rafael Espindolabca99f72009-04-08 21:14:34 +00005139
5140 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5141 NULL, 0);
5142
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005143 unsigned char OperandFlags = 0;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005144 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5145 // initialexec.
5146 unsigned WrapperKind = X86ISD::Wrapper;
5147 if (model == TLSModel::LocalExec) {
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005148 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005149 } else if (is64Bit) {
5150 assert(model == TLSModel::InitialExec);
5151 OperandFlags = X86II::MO_GOTTPOFF;
5152 WrapperKind = X86ISD::WrapperRIP;
5153 } else {
5154 assert(model == TLSModel::InitialExec);
5155 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005156 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005158 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5159 // exec)
Chris Lattner3207f8b2009-06-21 02:22:34 +00005160 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005161 GA->getOffset(), OperandFlags);
Chris Lattnerdc6fc472009-06-27 04:16:01 +00005162 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005163
Rafael Espindola7b620af2009-02-27 13:37:18 +00005164 if (model == TLSModel::InitialExec)
Dale Johannesenea996922009-02-04 20:06:27 +00005165 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005166 PseudoSourceValue::getGOT(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005167
5168 // The address of the thread local variable is the add of the thread
5169 // pointer with the offset of the variable.
Dale Johannesenea996922009-02-04 20:06:27 +00005170 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005171}
5172
Dan Gohman8181bd12008-07-27 21:46:04 +00005173SDValue
5174X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005175 // TODO: implement the "local dynamic" model
5176 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005177 assert(Subtarget->isTargetELF() &&
5178 "TLS not implemented for non-ELF targets");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005179 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005180 const GlobalValue *GV = GA->getGlobal();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005181
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005182 // If GV is an alias then use the aliasee for determining
5183 // thread-localness.
5184 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5185 GV = GA->resolveAliasedGlobal(false);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005186
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005187 TLSModel::Model model = getTLSModel(GV,
5188 getTargetMachine().getRelocationModel());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005189
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005190 switch (model) {
5191 case TLSModel::GeneralDynamic:
5192 case TLSModel::LocalDynamic: // not implemented
5193 if (Subtarget->is64Bit())
Rafael Espindola7b620af2009-02-27 13:37:18 +00005194 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005195 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005196
Chris Lattnerec7cfd42009-06-26 21:20:29 +00005197 case TLSModel::InitialExec:
5198 case TLSModel::LocalExec:
5199 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5200 Subtarget->is64Bit());
Anton Korobeynikov4fbf00b2008-05-04 21:36:32 +00005201 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005202
Edwin Törökbd448e32009-07-14 16:55:14 +00005203 llvm_unreachable("Unreachable");
Chris Lattnerda028df2009-04-01 22:14:45 +00005204 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005205}
5206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005207
Chris Lattner62814a32007-10-17 06:02:13 +00005208/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michel91099d62009-02-17 22:15:04 +00005209/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman8181bd12008-07-27 21:46:04 +00005210SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman092014e2008-03-03 22:22:09 +00005211 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersonac9de032009-08-10 22:56:29 +00005212 EVT VT = Op.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00005213 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005214 DebugLoc dl = Op.getDebugLoc();
Chris Lattner62814a32007-10-17 06:02:13 +00005215 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman8181bd12008-07-27 21:46:04 +00005216 SDValue ShOpLo = Op.getOperand(0);
5217 SDValue ShOpHi = Op.getOperand(1);
5218 SDValue ShAmt = Op.getOperand(2);
Chris Lattner996d9e52009-07-29 05:48:09 +00005219 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005220 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner996d9e52009-07-29 05:48:09 +00005221 : DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005222
Dan Gohman8181bd12008-07-27 21:46:04 +00005223 SDValue Tmp2, Tmp3;
Chris Lattner62814a32007-10-17 06:02:13 +00005224 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005225 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5226 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005227 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005228 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5229 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner62814a32007-10-17 06:02:13 +00005230 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005231
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005232 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5233 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00005234 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005235 AndNode, DAG.getConstant(0, MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005236
Dan Gohman8181bd12008-07-27 21:46:04 +00005237 SDValue Hi, Lo;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005238 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman8181bd12008-07-27 21:46:04 +00005239 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5240 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf19591c2008-06-30 10:19:09 +00005241
Chris Lattner62814a32007-10-17 06:02:13 +00005242 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005243 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5244 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005245 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005246 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5247 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner62814a32007-10-17 06:02:13 +00005248 }
5249
Dan Gohman8181bd12008-07-27 21:46:04 +00005250 SDValue Ops[2] = { Lo, Hi };
Dale Johannesence0805b2009-02-03 19:33:06 +00005251 return DAG.getMergeValues(Ops, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005252}
5253
Dan Gohman8181bd12008-07-27 21:46:04 +00005254SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00005255 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005256
5257 if (SrcVT.isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005258 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005259 return Op;
5260 }
5261 return SDValue();
5262 }
5263
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005264 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerdd3e1422008-02-27 05:57:41 +00005265 "Unknown SINT_TO_FP to lower!");
Scott Michel91099d62009-02-17 22:15:04 +00005266
Eli Friedman9d77ac32009-05-27 00:47:34 +00005267 // These are really Legal; return the operand so the caller accepts it as
5268 // Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005269 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman9d77ac32009-05-27 00:47:34 +00005270 return Op;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005271 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005272 Subtarget->is64Bit()) {
5273 return Op;
5274 }
Scott Michel91099d62009-02-17 22:15:04 +00005275
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005276 DebugLoc dl = Op.getDebugLoc();
Duncan Sands92c43912008-06-06 12:08:01 +00005277 unsigned Size = SrcVT.getSizeInBits()/8;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005278 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005279 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005280 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesence0805b2009-02-03 19:33:06 +00005281 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling6b42d012009-03-13 08:41:47 +00005282 StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005283 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005284 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5285}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286
Owen Andersonac9de032009-08-10 22:56:29 +00005287SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman8c3cb582009-05-23 09:59:16 +00005288 SDValue StackSlot,
5289 SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 // Build the FILD
Eli Friedman8c3cb582009-05-23 09:59:16 +00005291 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005292 SDVTList Tys;
Chris Lattnercf515b52008-01-16 06:24:21 +00005293 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005294 if (useSSE)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005295 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005296 else
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005297 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005298 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesence0805b2009-02-03 19:33:06 +00005299 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005300 Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301
Dale Johannesen2fc20782007-09-14 22:26:36 +00005302 if (useSSE) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 Chain = Result.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00005304 SDValue InFlag = Result.getValue(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005305
5306 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5307 // shouldn't be necessary except that RFP cannot be live across
5308 // multiple blocks. When stackifier is fixed, they can be uncoupled.
5309 MachineFunction &MF = DAG.getMachineFunction();
David Greene6424ab92009-11-12 20:49:22 +00005310 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005311 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005312 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00005313 SDValue Ops[] = {
5314 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5315 };
5316 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesence0805b2009-02-03 19:33:06 +00005317 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005318 PseudoSourceValue::getFixedStack(SSFI), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005319 }
5320
5321 return Result;
5322}
5323
Bill Wendling14a30ef2009-01-17 03:56:04 +00005324// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5325SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5326 // This algorithm is not obvious. Here it is in C code, more or less:
5327 /*
5328 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5329 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5330 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesenfb019af2008-10-21 23:07:49 +00005331
Bill Wendling14a30ef2009-01-17 03:56:04 +00005332 // Copy ints to xmm registers.
5333 __m128i xh = _mm_cvtsi32_si128( hi );
5334 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005335
Bill Wendling14a30ef2009-01-17 03:56:04 +00005336 // Combine into low half of a single xmm register.
5337 __m128i x = _mm_unpacklo_epi32( xh, xl );
5338 __m128d d;
5339 double sd;
Dale Johannesenfb019af2008-10-21 23:07:49 +00005340
Bill Wendling14a30ef2009-01-17 03:56:04 +00005341 // Merge in appropriate exponents to give the integer bits the right
5342 // magnitude.
5343 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005344
Bill Wendling14a30ef2009-01-17 03:56:04 +00005345 // Subtract away the biases to deal with the IEEE-754 double precision
5346 // implicit 1.
5347 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesenfb019af2008-10-21 23:07:49 +00005348
Bill Wendling14a30ef2009-01-17 03:56:04 +00005349 // All conversions up to here are exact. The correctly rounded result is
5350 // calculated using the current rounding mode using the following
5351 // horizontal add.
5352 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5353 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5354 // store doesn't really need to be here (except
5355 // maybe to zero the other double)
5356 return sd;
5357 }
5358 */
Dale Johannesenfb019af2008-10-21 23:07:49 +00005359
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005360 DebugLoc dl = Op.getDebugLoc();
Owen Anderson6361f972009-07-15 21:51:10 +00005361 LLVMContext *Context = DAG.getContext();
Dale Johannesence0805b2009-02-03 19:33:06 +00005362
Dale Johannesena359b8b2008-10-21 20:50:01 +00005363 // Build some magic constants.
Bill Wendling14a30ef2009-01-17 03:56:04 +00005364 std::vector<Constant*> CV0;
Owen Andersoneacb44d2009-07-24 23:12:02 +00005365 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5366 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5367 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5368 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Anderson2f422e02009-07-28 21:19:26 +00005369 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng68c18682009-03-13 07:51:59 +00005370 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005371
Bill Wendling14a30ef2009-01-17 03:56:04 +00005372 std::vector<Constant*> CV1;
Owen Anderson6361f972009-07-15 21:51:10 +00005373 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005374 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Anderson6361f972009-07-15 21:51:10 +00005375 CV1.push_back(
Owen Andersond363a0e2009-07-27 20:59:43 +00005376 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Anderson2f422e02009-07-28 21:19:26 +00005377 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng68c18682009-03-13 07:51:59 +00005378 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesena359b8b2008-10-21 20:50:01 +00005379
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005380 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5381 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005382 Op.getOperand(0),
5383 DAG.getIntPtrConstant(1)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005384 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5385 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sandsca872ca2008-10-22 11:24:12 +00005386 Op.getOperand(0),
5387 DAG.getIntPtrConstant(0)));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005388 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5389 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005390 PseudoSourceValue::getConstantPool(), 0,
5391 false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005392 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5393 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5394 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005395 PseudoSourceValue::getConstantPool(), 0,
5396 false, 16);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005397 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005398
Dale Johannesena359b8b2008-10-21 20:50:01 +00005399 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman543d2142009-04-27 18:41:29 +00005400 int ShufMask[2] = { 1, -1 };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005401 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5402 DAG.getUNDEF(MVT::v2f64), ShufMask);
5403 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5404 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesena359b8b2008-10-21 20:50:01 +00005405 DAG.getIntPtrConstant(0));
5406}
5407
Bill Wendling14a30ef2009-01-17 03:56:04 +00005408// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5409SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005410 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005411 // FP constant to bias correct the final result.
5412 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005413 MVT::f64);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005414
5415 // Load the 32-bit value into an XMM register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005416 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5417 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling14a30ef2009-01-17 03:56:04 +00005418 Op.getOperand(0),
5419 DAG.getIntPtrConstant(0)));
5420
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005421 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5422 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005423 DAG.getIntPtrConstant(0));
5424
5425 // Or the load with the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005426 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5427 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005428 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005429 MVT::v2f64, Load)),
5430 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005431 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005432 MVT::v2f64, Bias)));
5433 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5434 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling14a30ef2009-01-17 03:56:04 +00005435 DAG.getIntPtrConstant(0));
5436
5437 // Subtract the bias.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005438 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005439
5440 // Handle final rounding.
Owen Andersonac9de032009-08-10 22:56:29 +00005441 EVT DestVT = Op.getValueType();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005442
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005443 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005444 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendlingdb547de2009-01-17 07:40:19 +00005445 DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005446 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005447 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendlingdb547de2009-01-17 07:40:19 +00005448 }
5449
5450 // Handle final rounding.
5451 return Sub;
Bill Wendling14a30ef2009-01-17 03:56:04 +00005452}
5453
5454SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Cheng44fd2392009-01-19 08:08:22 +00005455 SDValue N0 = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005456 DebugLoc dl = Op.getDebugLoc();
Bill Wendling14a30ef2009-01-17 03:56:04 +00005457
Evan Cheng44fd2392009-01-19 08:08:22 +00005458 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5459 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5460 // the optimization here.
5461 if (DAG.SignBitIsZero(N0))
Dale Johannesence0805b2009-02-03 19:33:06 +00005462 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Cheng44fd2392009-01-19 08:08:22 +00005463
Owen Andersonac9de032009-08-10 22:56:29 +00005464 EVT SrcVT = N0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005465 if (SrcVT == MVT::i64) {
Eli Friedman9d77ac32009-05-27 00:47:34 +00005466 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005467 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar00261df2009-05-26 21:27:02 +00005468 return SDValue();
Bill Wendlingdb547de2009-01-17 07:40:19 +00005469
Bill Wendling14a30ef2009-01-17 03:56:04 +00005470 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005471 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling14a30ef2009-01-17 03:56:04 +00005472 return LowerUINT_TO_FP_i32(Op, DAG);
5473 }
5474
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005475 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman8c3cb582009-05-23 09:59:16 +00005476
5477 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005478 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman8c3cb582009-05-23 09:59:16 +00005479 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5480 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5481 getPointerTy(), StackSlot, WordOff);
5482 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5483 StackSlot, NULL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005484 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman8c3cb582009-05-23 09:59:16 +00005485 OffsetSlot, NULL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005486 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling14a30ef2009-01-17 03:56:04 +00005487}
5488
Dan Gohman8181bd12008-07-27 21:46:04 +00005489std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman8c3cb582009-05-23 09:59:16 +00005490FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005491 DebugLoc dl = Op.getDebugLoc();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005492
Owen Andersonac9de032009-08-10 22:56:29 +00005493 EVT DstTy = Op.getValueType();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005494
5495 if (!IsSigned) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005496 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5497 DstTy = MVT::i64;
Eli Friedman8c3cb582009-05-23 09:59:16 +00005498 }
5499
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005500 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5501 DstTy.getSimpleVT() >= MVT::i16 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005502 "Unknown FP_TO_SINT to lower!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005503
Dale Johannesen2fc20782007-09-14 22:26:36 +00005504 // These are really Legal.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005505 if (DstTy == MVT::i32 &&
Chris Lattnercf515b52008-01-16 06:24:21 +00005506 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005507 return std::make_pair(SDValue(), SDValue());
Dale Johannesen958b08b2007-09-19 23:55:34 +00005508 if (Subtarget->is64Bit() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005509 DstTy == MVT::i64 &&
Eli Friedman9d77ac32009-05-27 00:47:34 +00005510 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005511 return std::make_pair(SDValue(), SDValue());
Dale Johannesen2fc20782007-09-14 22:26:36 +00005512
Evan Cheng05441e62007-10-15 20:11:21 +00005513 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5514 // stack slot.
5515 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman8c3cb582009-05-23 09:59:16 +00005516 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene6424ab92009-11-12 20:49:22 +00005517 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00005518 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopher3d82bbd2009-08-27 18:07:15 +00005519
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005520 unsigned Opc;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005521 switch (DstTy.getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005522 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005523 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5524 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5525 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005526 }
5527
Dan Gohman8181bd12008-07-27 21:46:04 +00005528 SDValue Chain = DAG.getEntryNode();
5529 SDValue Value = Op.getOperand(0);
Chris Lattnercf515b52008-01-16 06:24:21 +00005530 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005531 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesence0805b2009-02-03 19:33:06 +00005532 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Cheng174e2cf2009-10-18 18:16:27 +00005533 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005534 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman8181bd12008-07-27 21:46:04 +00005535 SDValue Ops[] = {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005536 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5537 };
Dale Johannesence0805b2009-02-03 19:33:06 +00005538 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 Chain = Value.getValue(1);
David Greene6424ab92009-11-12 20:49:22 +00005540 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005541 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5542 }
5543
5544 // Build the FP_TO_INT*_IN_MEM
Dan Gohman8181bd12008-07-27 21:46:04 +00005545 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005546 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005547
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005548 return std::make_pair(FIST, StackSlot);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005549}
5550
Dan Gohman8181bd12008-07-27 21:46:04 +00005551SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005552 if (Op.getValueType().isVector()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005553 if (Op.getValueType() == MVT::v2i32 &&
5554 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedmanc0521fb2009-06-06 03:57:58 +00005555 return Op;
5556 }
5557 return SDValue();
5558 }
5559
Eli Friedman8c3cb582009-05-23 09:59:16 +00005560 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman8181bd12008-07-27 21:46:04 +00005561 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman9d77ac32009-05-27 00:47:34 +00005562 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5563 if (FIST.getNode() == 0) return Op;
Scott Michel91099d62009-02-17 22:15:04 +00005564
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005565 // Load the result.
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005566 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesence0805b2009-02-03 19:33:06 +00005567 FIST, StackSlot, NULL, 0);
Chris Lattnerdfb947d2007-11-24 07:07:01 +00005568}
5569
Eli Friedman8c3cb582009-05-23 09:59:16 +00005570SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5571 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5572 SDValue FIST = Vals.first, StackSlot = Vals.second;
5573 assert(FIST.getNode() && "Unexpected failure");
5574
5575 // Load the result.
5576 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5577 FIST, StackSlot, NULL, 0);
5578}
5579
Dan Gohman8181bd12008-07-27 21:46:04 +00005580SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005581 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005582 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005583 EVT VT = Op.getValueType();
5584 EVT EltVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00005585 if (VT.isVector())
5586 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005587 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005588 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005589 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 CV.push_back(C);
5591 CV.push_back(C);
5592 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005593 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 CV.push_back(C);
5595 CV.push_back(C);
5596 CV.push_back(C);
5597 CV.push_back(C);
5598 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005599 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005600 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005601 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005602 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005603 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005604 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005605}
5606
Dan Gohman8181bd12008-07-27 21:46:04 +00005607SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005608 LLVMContext *Context = DAG.getContext();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005609 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005610 EVT VT = Op.getValueType();
5611 EVT EltVT = VT;
Duncan Sands831102e2009-09-06 19:29:07 +00005612 if (VT.isVector())
Duncan Sands92c43912008-06-06 12:08:01 +00005613 EltVT = VT.getVectorElementType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005614 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005615 if (EltVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005616 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005617 CV.push_back(C);
5618 CV.push_back(C);
5619 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005620 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005621 CV.push_back(C);
5622 CV.push_back(C);
5623 CV.push_back(C);
5624 CV.push_back(C);
5625 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005626 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005627 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005628 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005629 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005630 false, 16);
Duncan Sands92c43912008-06-06 12:08:01 +00005631 if (VT.isVector()) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005632 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005633 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5634 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesence0805b2009-02-03 19:33:06 +00005635 Op.getOperand(0)),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005636 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Cheng92b8f782007-07-19 23:36:01 +00005637 } else {
Dale Johannesence0805b2009-02-03 19:33:06 +00005638 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Cheng92b8f782007-07-19 23:36:01 +00005639 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005640}
5641
Dan Gohman8181bd12008-07-27 21:46:04 +00005642SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Anderson6361f972009-07-15 21:51:10 +00005643 LLVMContext *Context = DAG.getContext();
Dan Gohman8181bd12008-07-27 21:46:04 +00005644 SDValue Op0 = Op.getOperand(0);
5645 SDValue Op1 = Op.getOperand(1);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005646 DebugLoc dl = Op.getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00005647 EVT VT = Op.getValueType();
5648 EVT SrcVT = Op1.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005649
5650 // If second operand is smaller, extend it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005651 if (SrcVT.bitsLT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005652 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005653 SrcVT = VT;
5654 }
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005655 // And if it is bigger, shrink it first.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005656 if (SrcVT.bitsGT(VT)) {
Dale Johannesence0805b2009-02-03 19:33:06 +00005657 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005658 SrcVT = VT;
Dale Johannesenfb0fa912007-10-21 01:07:44 +00005659 }
5660
5661 // At this point the operands and the result should have the same
5662 // type, and that won't be f80 since that is not custom lowered.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005663
5664 // First get the sign bit of second operand.
5665 std::vector<Constant*> CV;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005666 if (SrcVT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005667 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5668 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005669 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005670 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5671 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5672 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5673 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005674 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005675 Constant *C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005676 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005677 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005678 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005679 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005680 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681
5682 // Shift sign bit right or left if the two operands have different types.
Duncan Sandsec142ee2008-06-08 20:54:56 +00005683 if (SrcVT.bitsGT(VT)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005684 // Op0 is MVT::f32, Op1 is MVT::f64.
5685 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5686 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5687 DAG.getConstant(32, MVT::i32));
5688 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5689 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner5872a362008-01-17 07:00:52 +00005690 DAG.getIntPtrConstant(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 }
5692
5693 // Clear first operand sign bit.
5694 CV.clear();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005695 if (VT == MVT::f64) {
Owen Andersond363a0e2009-07-27 20:59:43 +00005696 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5697 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698 } else {
Owen Andersond363a0e2009-07-27 20:59:43 +00005699 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5700 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5701 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5702 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 }
Owen Anderson2f422e02009-07-28 21:19:26 +00005704 C = ConstantVector::get(CV);
Evan Cheng68c18682009-03-13 07:51:59 +00005705 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005706 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohmanfb020b62008-02-07 18:41:25 +00005707 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman11821702007-07-27 17:16:43 +00005708 false, 16);
Dale Johannesence0805b2009-02-03 19:33:06 +00005709 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005710
5711 // Or the value with the sign bit.
Dale Johannesence0805b2009-02-03 19:33:06 +00005712 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005713}
5714
Dan Gohman99a12192009-03-04 19:44:21 +00005715/// Emit nodes that will be selected as "test Op0,Op0", or something
5716/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005717SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5718 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005719 DebugLoc dl = Op.getDebugLoc();
5720
Dan Gohmanc8b47852009-03-07 01:58:32 +00005721 // CF and OF aren't always set the way we want. Determine which
5722 // of these we need.
5723 bool NeedCF = false;
5724 bool NeedOF = false;
5725 switch (X86CC) {
5726 case X86::COND_A: case X86::COND_AE:
5727 case X86::COND_B: case X86::COND_BE:
5728 NeedCF = true;
5729 break;
5730 case X86::COND_G: case X86::COND_GE:
5731 case X86::COND_L: case X86::COND_LE:
5732 case X86::COND_O: case X86::COND_NO:
5733 NeedOF = true;
5734 break;
5735 default: break;
5736 }
5737
Dan Gohman99a12192009-03-04 19:44:21 +00005738 // See if we can use the EFLAGS value from the operand instead of
Dan Gohmanc8b47852009-03-07 01:58:32 +00005739 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5740 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5741 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman99a12192009-03-04 19:44:21 +00005742 unsigned Opcode = 0;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005743 unsigned NumOperands = 0;
Dan Gohman99a12192009-03-04 19:44:21 +00005744 switch (Op.getNode()->getOpcode()) {
5745 case ISD::ADD:
5746 // Due to an isel shortcoming, be conservative if this add is likely to
5747 // be selected as part of a load-modify-store instruction. When the root
5748 // node in a match is a store, isel doesn't know how to remap non-chain
5749 // non-flag uses of other nodes in the match, such as the ADD in this
5750 // case. This leads to the ADD being left around and reselected, with
5751 // the result being two adds in the output.
5752 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5753 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5754 if (UI->getOpcode() == ISD::STORE)
5755 goto default_case;
Dan Gohman99a12192009-03-04 19:44:21 +00005756 if (ConstantSDNode *C =
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005757 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5758 // An add of one will be selected as an INC.
Dan Gohman99a12192009-03-04 19:44:21 +00005759 if (C->getAPIntValue() == 1) {
5760 Opcode = X86ISD::INC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005761 NumOperands = 1;
Dan Gohman99a12192009-03-04 19:44:21 +00005762 break;
5763 }
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005764 // An add of negative one (subtract of one) will be selected as a DEC.
5765 if (C->getAPIntValue().isAllOnesValue()) {
5766 Opcode = X86ISD::DEC;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005767 NumOperands = 1;
Dan Gohmand90a8fd2009-03-05 19:32:48 +00005768 break;
5769 }
5770 }
Dan Gohman99a12192009-03-04 19:44:21 +00005771 // Otherwise use a regular EFLAGS-setting add.
5772 Opcode = X86ISD::ADD;
Dan Gohman8c8a8022009-03-05 21:29:28 +00005773 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005774 break;
Dan Gohman12e03292009-09-18 19:59:53 +00005775 case ISD::AND: {
5776 // If the primary and result isn't used, don't bother using X86ISD::AND,
5777 // because a TEST instruction will be better.
5778 bool NonFlagUse = false;
5779 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Chengc429ff52010-01-07 00:54:06 +00005780 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5781 SDNode *User = *UI;
5782 unsigned UOpNo = UI.getOperandNo();
5783 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5784 // Look pass truncate.
5785 UOpNo = User->use_begin().getOperandNo();
5786 User = *User->use_begin();
5787 }
5788 if (User->getOpcode() != ISD::BRCOND &&
5789 User->getOpcode() != ISD::SETCC &&
5790 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohman12e03292009-09-18 19:59:53 +00005791 NonFlagUse = true;
5792 break;
5793 }
Evan Chengc429ff52010-01-07 00:54:06 +00005794 }
Dan Gohman12e03292009-09-18 19:59:53 +00005795 if (!NonFlagUse)
5796 break;
5797 }
5798 // FALL THROUGH
Dan Gohman99a12192009-03-04 19:44:21 +00005799 case ISD::SUB:
Dan Gohman12e03292009-09-18 19:59:53 +00005800 case ISD::OR:
5801 case ISD::XOR:
5802 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman99a12192009-03-04 19:44:21 +00005803 // likely to be selected as part of a load-modify-store instruction.
5804 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5805 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5806 if (UI->getOpcode() == ISD::STORE)
5807 goto default_case;
Dan Gohman12e03292009-09-18 19:59:53 +00005808 // Otherwise use a regular EFLAGS-setting instruction.
5809 switch (Op.getNode()->getOpcode()) {
5810 case ISD::SUB: Opcode = X86ISD::SUB; break;
5811 case ISD::OR: Opcode = X86ISD::OR; break;
5812 case ISD::XOR: Opcode = X86ISD::XOR; break;
5813 case ISD::AND: Opcode = X86ISD::AND; break;
5814 default: llvm_unreachable("unexpected operator!");
5815 }
Dan Gohman8c8a8022009-03-05 21:29:28 +00005816 NumOperands = 2;
Dan Gohman99a12192009-03-04 19:44:21 +00005817 break;
5818 case X86ISD::ADD:
5819 case X86ISD::SUB:
5820 case X86ISD::INC:
5821 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00005822 case X86ISD::OR:
5823 case X86ISD::XOR:
5824 case X86ISD::AND:
Dan Gohman99a12192009-03-04 19:44:21 +00005825 return SDValue(Op.getNode(), 1);
5826 default:
5827 default_case:
5828 break;
5829 }
5830 if (Opcode != 0) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005831 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman99a12192009-03-04 19:44:21 +00005832 SmallVector<SDValue, 4> Ops;
Dan Gohmanc8b47852009-03-07 01:58:32 +00005833 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman99a12192009-03-04 19:44:21 +00005834 Ops.push_back(Op.getOperand(i));
Dan Gohmanee036282009-04-09 23:54:40 +00005835 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman99a12192009-03-04 19:44:21 +00005836 DAG.ReplaceAllUsesWith(Op, New);
5837 return SDValue(New.getNode(), 1);
5838 }
5839 }
5840
5841 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005842 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman99a12192009-03-04 19:44:21 +00005843 DAG.getConstant(0, Op.getValueType()));
5844}
5845
5846/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5847/// equivalent.
Dan Gohmanc8b47852009-03-07 01:58:32 +00005848SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5849 SelectionDAG &DAG) {
Dan Gohman99a12192009-03-04 19:44:21 +00005850 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5851 if (C->getAPIntValue() == 0)
Dan Gohmanc8b47852009-03-07 01:58:32 +00005852 return EmitTest(Op0, X86CC, DAG);
Dan Gohman99a12192009-03-04 19:44:21 +00005853
5854 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005855 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman99a12192009-03-04 19:44:21 +00005856}
5857
Evan Cheng095dac22010-01-06 19:38:29 +00005858/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5859/// if it's possible.
5860static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Chengc621d452010-01-05 06:52:31 +00005861 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng095dac22010-01-06 19:38:29 +00005862 SDValue LHS, RHS;
5863 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5864 if (ConstantSDNode *Op010C =
5865 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5866 if (Op010C->getZExtValue() == 1) {
5867 LHS = Op0.getOperand(0);
5868 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00005869 }
Evan Cheng095dac22010-01-06 19:38:29 +00005870 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5871 if (ConstantSDNode *Op000C =
5872 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5873 if (Op000C->getZExtValue() == 1) {
5874 LHS = Op0.getOperand(1);
5875 RHS = Op0.getOperand(0).getOperand(1);
5876 }
5877 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5878 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5879 SDValue AndLHS = Op0.getOperand(0);
5880 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5881 LHS = AndLHS.getOperand(0);
5882 RHS = AndLHS.getOperand(1);
Dan Gohman22cefb02009-01-29 01:59:02 +00005883 }
Evan Cheng095dac22010-01-06 19:38:29 +00005884 }
Evan Cheng950aac02007-09-25 01:57:46 +00005885
Evan Cheng095dac22010-01-06 19:38:29 +00005886 if (LHS.getNode()) {
5887 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5888 // instruction. Since the shift amount is in-range-or-undefined, we know
5889 // that doing a bittest on the i16 value is ok. We extend to i32 because
5890 // the encoding for the i16 version is larger than the i32 version.
5891 if (LHS.getValueType() == MVT::i8)
5892 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattner77a62312008-12-25 05:34:37 +00005893
Evan Cheng095dac22010-01-06 19:38:29 +00005894 // If the operand types disagree, extend the shift amount to match. Since
5895 // BT ignores high bits (like shifts) we can use anyextend.
5896 if (LHS.getValueType() != RHS.getValueType())
5897 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohman22cefb02009-01-29 01:59:02 +00005898
Evan Cheng095dac22010-01-06 19:38:29 +00005899 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5900 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5901 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5902 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattner77a62312008-12-25 05:34:37 +00005903 }
5904
Evan Chengc621d452010-01-05 06:52:31 +00005905 return SDValue();
5906}
5907
5908SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5909 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5910 SDValue Op0 = Op.getOperand(0);
5911 SDValue Op1 = Op.getOperand(1);
5912 DebugLoc dl = Op.getDebugLoc();
5913 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5914
5915 // Optimize to BT if possible.
Evan Cheng095dac22010-01-06 19:38:29 +00005916 // Lower (X & (1 << N)) == 0 to BT(X, N).
5917 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5918 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5919 if (Op0.getOpcode() == ISD::AND &&
5920 Op0.hasOneUse() &&
5921 Op1.getOpcode() == ISD::Constant &&
5922 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5923 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5924 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5925 if (NewSetCC.getNode())
5926 return NewSetCC;
5927 }
Evan Chengc621d452010-01-05 06:52:31 +00005928
Chris Lattner77a62312008-12-25 05:34:37 +00005929 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5930 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00005931 if (X86CC == X86::COND_INVALID)
5932 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00005933
Dan Gohmanc8b47852009-03-07 01:58:32 +00005934 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Cheng834ae6b2009-12-15 00:53:42 +00005935
5936 // Use sbb x, x to materialize carry bit into a GPR.
Evan Chengedeb1692009-12-16 00:53:11 +00005937 if (X86CC == X86::COND_B)
Evan Cheng834ae6b2009-12-15 00:53:42 +00005938 return DAG.getNode(ISD::AND, dl, MVT::i8,
5939 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5940 DAG.getConstant(X86CC, MVT::i8), Cond),
5941 DAG.getConstant(1, MVT::i8));
Evan Cheng834ae6b2009-12-15 00:53:42 +00005942
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005943 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5944 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00005945}
5946
Dan Gohman8181bd12008-07-27 21:46:04 +00005947SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5948 SDValue Cond;
5949 SDValue Op0 = Op.getOperand(0);
5950 SDValue Op1 = Op.getOperand(1);
5951 SDValue CC = Op.getOperand(2);
Owen Andersonac9de032009-08-10 22:56:29 +00005952 EVT VT = Op.getValueType();
Nate Begeman03605a02008-07-17 16:51:19 +00005953 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5954 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00005955 DebugLoc dl = Op.getDebugLoc();
Nate Begeman03605a02008-07-17 16:51:19 +00005956
5957 if (isFP) {
5958 unsigned SSECC = 8;
Owen Andersonac9de032009-08-10 22:56:29 +00005959 EVT VT0 = Op0.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005960 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5961 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman03605a02008-07-17 16:51:19 +00005962 bool Swap = false;
5963
5964 switch (SetCCOpcode) {
5965 default: break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005966 case ISD::SETOEQ:
Nate Begeman03605a02008-07-17 16:51:19 +00005967 case ISD::SETEQ: SSECC = 0; break;
Scott Michel91099d62009-02-17 22:15:04 +00005968 case ISD::SETOGT:
Nate Begeman03605a02008-07-17 16:51:19 +00005969 case ISD::SETGT: Swap = true; // Fallthrough
5970 case ISD::SETLT:
5971 case ISD::SETOLT: SSECC = 1; break;
5972 case ISD::SETOGE:
5973 case ISD::SETGE: Swap = true; // Fallthrough
5974 case ISD::SETLE:
5975 case ISD::SETOLE: SSECC = 2; break;
5976 case ISD::SETUO: SSECC = 3; break;
Nate Begeman6357f9d2008-07-25 19:05:58 +00005977 case ISD::SETUNE:
Nate Begeman03605a02008-07-17 16:51:19 +00005978 case ISD::SETNE: SSECC = 4; break;
5979 case ISD::SETULE: Swap = true;
5980 case ISD::SETUGE: SSECC = 5; break;
5981 case ISD::SETULT: Swap = true;
5982 case ISD::SETUGT: SSECC = 6; break;
5983 case ISD::SETO: SSECC = 7; break;
5984 }
5985 if (Swap)
5986 std::swap(Op0, Op1);
5987
Nate Begeman6357f9d2008-07-25 19:05:58 +00005988 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman03605a02008-07-17 16:51:19 +00005989 if (SSECC == 8) {
Nate Begeman6357f9d2008-07-25 19:05:58 +00005990 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005991 SDValue UNORD, EQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005992 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5993 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00005994 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00005995 }
5996 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005997 SDValue ORD, NEQ;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005998 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5999 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesence0805b2009-02-03 19:33:06 +00006000 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begeman6357f9d2008-07-25 19:05:58 +00006001 }
Edwin Törökbd448e32009-07-14 16:55:14 +00006002 llvm_unreachable("Illegal FP comparison");
Nate Begeman03605a02008-07-17 16:51:19 +00006003 }
6004 // Handle all other FP comparisons here.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006005 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman03605a02008-07-17 16:51:19 +00006006 }
Scott Michel91099d62009-02-17 22:15:04 +00006007
Nate Begeman03605a02008-07-17 16:51:19 +00006008 // We are handling one of the integer comparisons here. Since SSE only has
6009 // GT and EQ comparisons for integer, swapping operands and multiple
6010 // operations may be required for some comparisons.
6011 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6012 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michel91099d62009-02-17 22:15:04 +00006013
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006014 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman03605a02008-07-17 16:51:19 +00006015 default: break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006016 case MVT::v8i8:
6017 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6018 case MVT::v4i16:
6019 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6020 case MVT::v2i32:
6021 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6022 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman03605a02008-07-17 16:51:19 +00006023 }
Scott Michel91099d62009-02-17 22:15:04 +00006024
Nate Begeman03605a02008-07-17 16:51:19 +00006025 switch (SetCCOpcode) {
6026 default: break;
6027 case ISD::SETNE: Invert = true;
6028 case ISD::SETEQ: Opc = EQOpc; break;
6029 case ISD::SETLT: Swap = true;
6030 case ISD::SETGT: Opc = GTOpc; break;
6031 case ISD::SETGE: Swap = true;
6032 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6033 case ISD::SETULT: Swap = true;
6034 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6035 case ISD::SETUGE: Swap = true;
6036 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6037 }
6038 if (Swap)
6039 std::swap(Op0, Op1);
Scott Michel91099d62009-02-17 22:15:04 +00006040
Nate Begeman03605a02008-07-17 16:51:19 +00006041 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6042 // bits of the inputs before performing those operations.
6043 if (FlipSigns) {
Owen Andersonac9de032009-08-10 22:56:29 +00006044 EVT EltVT = VT.getVectorElementType();
Duncan Sands505ba942009-02-01 18:06:53 +00006045 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6046 EltVT);
Dan Gohman8181bd12008-07-27 21:46:04 +00006047 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Cheng907a2d22009-02-25 22:49:59 +00006048 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6049 SignBits.size());
Dale Johannesence0805b2009-02-03 19:33:06 +00006050 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6051 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman03605a02008-07-17 16:51:19 +00006052 }
Scott Michel91099d62009-02-17 22:15:04 +00006053
Dale Johannesence0805b2009-02-03 19:33:06 +00006054 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman03605a02008-07-17 16:51:19 +00006055
6056 // If the logical-not of the result is required, perform that now.
Bob Wilson81a42cf2009-01-22 17:39:32 +00006057 if (Invert)
Dale Johannesence0805b2009-02-03 19:33:06 +00006058 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00006059
Nate Begeman03605a02008-07-17 16:51:19 +00006060 return Result;
6061}
Evan Cheng950aac02007-09-25 01:57:46 +00006062
Evan Chengd580f022008-12-03 08:38:43 +00006063// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman99a12192009-03-04 19:44:21 +00006064static bool isX86LogicalCmp(SDValue Op) {
6065 unsigned Opc = Op.getNode()->getOpcode();
6066 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6067 return true;
6068 if (Op.getResNo() == 1 &&
6069 (Opc == X86ISD::ADD ||
6070 Opc == X86ISD::SUB ||
6071 Opc == X86ISD::SMUL ||
6072 Opc == X86ISD::UMUL ||
6073 Opc == X86ISD::INC ||
Dan Gohman12e03292009-09-18 19:59:53 +00006074 Opc == X86ISD::DEC ||
6075 Opc == X86ISD::OR ||
6076 Opc == X86ISD::XOR ||
6077 Opc == X86ISD::AND))
Dan Gohman99a12192009-03-04 19:44:21 +00006078 return true;
6079
6080 return false;
Evan Chengd580f022008-12-03 08:38:43 +00006081}
6082
Dan Gohman8181bd12008-07-27 21:46:04 +00006083SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006084 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00006085 SDValue Cond = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006086 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00006087 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006088
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006089 if (Cond.getOpcode() == ISD::SETCC) {
6090 SDValue NewCond = LowerSETCC(Cond, DAG);
6091 if (NewCond.getNode())
6092 Cond = NewCond;
6093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006094
Evan Cheng506f6f02010-01-26 02:00:44 +00006095 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6096 SDValue Op1 = Op.getOperand(1);
6097 SDValue Op2 = Op.getOperand(2);
6098 if (Cond.getOpcode() == X86ISD::SETCC &&
6099 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6100 SDValue Cmp = Cond.getOperand(1);
6101 if (Cmp.getOpcode() == X86ISD::CMP) {
6102 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6103 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6104 ConstantSDNode *RHSC =
6105 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6106 if (N1C && N1C->isAllOnesValue() &&
6107 N2C && N2C->isNullValue() &&
6108 RHSC && RHSC->isNullValue()) {
6109 SDValue CmpOp0 = Cmp.getOperand(0);
Evan Cheng1badb8d2010-01-28 01:57:22 +00006110 Cmp = DAG.getNode(X86ISD::CMP, dl, CmpOp0.getValueType(),
Evan Cheng506f6f02010-01-26 02:00:44 +00006111 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6112 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6113 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6114 }
6115 }
6116 }
6117
Evan Cheng834ae6b2009-12-15 00:53:42 +00006118 // Look pass (and (setcc_carry (cmp ...)), 1).
6119 if (Cond.getOpcode() == ISD::AND &&
6120 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6121 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6122 if (C && C->getAPIntValue() == 1)
6123 Cond = Cond.getOperand(0);
6124 }
6125
Evan Cheng50d37ab2007-10-08 22:16:29 +00006126 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6127 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006128 if (Cond.getOpcode() == X86ISD::SETCC ||
6129 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006130 CC = Cond.getOperand(0);
6131
Dan Gohman8181bd12008-07-27 21:46:04 +00006132 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006133 unsigned Opc = Cmp.getOpcode();
Owen Andersonac9de032009-08-10 22:56:29 +00006134 EVT VT = Op.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006135
Evan Cheng50d37ab2007-10-08 22:16:29 +00006136 bool IllegalFPCMov = false;
Duncan Sands92c43912008-06-06 12:08:01 +00006137 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattnercf515b52008-01-16 06:24:21 +00006138 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman40686732008-09-26 21:54:37 +00006139 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michel91099d62009-02-17 22:15:04 +00006140
Chris Lattnere4577dc2009-03-12 06:52:53 +00006141 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6142 Opc == X86ISD::BT) { // FIXME
Evan Cheng50d37ab2007-10-08 22:16:29 +00006143 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006144 addTest = false;
6145 }
6146 }
6147
6148 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006149 // Look pass the truncate.
6150 if (Cond.getOpcode() == ISD::TRUNCATE)
6151 Cond = Cond.getOperand(0);
6152
6153 // We know the result of AND is compared against zero. Try to match
6154 // it to BT.
6155 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6156 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6157 if (NewSetCC.getNode()) {
6158 CC = NewSetCC.getOperand(0);
6159 Cond = NewSetCC.getOperand(1);
6160 addTest = false;
6161 }
6162 }
6163 }
6164
6165 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006166 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006167 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006168 }
6169
Evan Cheng950aac02007-09-25 01:57:46 +00006170 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6171 // condition is true.
Evan Cheng506f6f02010-01-26 02:00:44 +00006172 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6173 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006174 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng950aac02007-09-25 01:57:46 +00006175}
6176
Evan Chengd580f022008-12-03 08:38:43 +00006177// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6178// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6179// from the AND / OR.
6180static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6181 Opc = Op.getOpcode();
6182 if (Opc != ISD::OR && Opc != ISD::AND)
6183 return false;
6184 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6185 Op.getOperand(0).hasOneUse() &&
6186 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6187 Op.getOperand(1).hasOneUse());
6188}
6189
Evan Cheng67f98b12009-02-02 08:19:07 +00006190// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6191// 1 and that the SETCC node has a single use.
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006192static bool isXor1OfSetCC(SDValue Op) {
6193 if (Op.getOpcode() != ISD::XOR)
6194 return false;
6195 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6196 if (N1C && N1C->getAPIntValue() == 1) {
6197 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6198 Op.getOperand(0).hasOneUse();
6199 }
6200 return false;
6201}
6202
Dan Gohman8181bd12008-07-27 21:46:04 +00006203SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006204 bool addTest = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00006205 SDValue Chain = Op.getOperand(0);
6206 SDValue Cond = Op.getOperand(1);
6207 SDValue Dest = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006208 DebugLoc dl = Op.getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00006209 SDValue CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006211 if (Cond.getOpcode() == ISD::SETCC) {
6212 SDValue NewCond = LowerSETCC(Cond, DAG);
6213 if (NewCond.getNode())
6214 Cond = NewCond;
6215 }
Chris Lattner77a62312008-12-25 05:34:37 +00006216#if 0
6217 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingf5399032008-12-12 21:15:41 +00006218 else if (Cond.getOpcode() == X86ISD::ADD ||
6219 Cond.getOpcode() == X86ISD::SUB ||
6220 Cond.getOpcode() == X86ISD::SMUL ||
6221 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling7e04be62008-12-09 22:08:41 +00006222 Cond = LowerXALUO(Cond, DAG);
Chris Lattner77a62312008-12-25 05:34:37 +00006223#endif
Scott Michel91099d62009-02-17 22:15:04 +00006224
Evan Cheng834ae6b2009-12-15 00:53:42 +00006225 // Look pass (and (setcc_carry (cmp ...)), 1).
6226 if (Cond.getOpcode() == ISD::AND &&
6227 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6228 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6229 if (C && C->getAPIntValue() == 1)
6230 Cond = Cond.getOperand(0);
6231 }
6232
Evan Cheng50d37ab2007-10-08 22:16:29 +00006233 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6234 // setting operand in place of the X86ISD::SETCC.
Evan Cheng834ae6b2009-12-15 00:53:42 +00006235 if (Cond.getOpcode() == X86ISD::SETCC ||
6236 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006237 CC = Cond.getOperand(0);
6238
Dan Gohman8181bd12008-07-27 21:46:04 +00006239 SDValue Cmp = Cond.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006240 unsigned Opc = Cmp.getOpcode();
Chris Lattner77a62312008-12-25 05:34:37 +00006241 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman99a12192009-03-04 19:44:21 +00006242 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng50d37ab2007-10-08 22:16:29 +00006243 Cond = Cmp;
Evan Cheng950aac02007-09-25 01:57:46 +00006244 addTest = false;
Bill Wendlingd3511522008-12-02 01:06:39 +00006245 } else {
Evan Chengd580f022008-12-03 08:38:43 +00006246 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling809e7bd2008-12-03 08:32:02 +00006247 default: break;
6248 case X86::COND_O:
Dan Gohman0fc9ed62009-01-07 00:15:08 +00006249 case X86::COND_B:
Chris Lattner77a62312008-12-25 05:34:37 +00006250 // These can only come from an arithmetic instruction with overflow,
6251 // e.g. SADDO, UADDO.
Bill Wendling809e7bd2008-12-03 08:32:02 +00006252 Cond = Cond.getNode()->getOperand(1);
6253 addTest = false;
6254 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00006255 }
Evan Cheng950aac02007-09-25 01:57:46 +00006256 }
Evan Chengd580f022008-12-03 08:38:43 +00006257 } else {
6258 unsigned CondOpc;
6259 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6260 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Chengd580f022008-12-03 08:38:43 +00006261 if (CondOpc == ISD::OR) {
6262 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6263 // two branches instead of an explicit OR instruction with a
6264 // separate test.
6265 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006266 isX86LogicalCmp(Cmp)) {
Evan Chengd580f022008-12-03 08:38:43 +00006267 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006268 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006269 Chain, Dest, CC, Cmp);
6270 CC = Cond.getOperand(1).getOperand(0);
6271 Cond = Cmp;
6272 addTest = false;
6273 }
6274 } else { // ISD::AND
6275 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6276 // two branches instead of an explicit AND instruction with a
6277 // separate test. However, we only do this if this block doesn't
6278 // have a fall-through edge, because this requires an explicit
6279 // jmp when the condition is false.
6280 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman99a12192009-03-04 19:44:21 +00006281 isX86LogicalCmp(Cmp) &&
Evan Chengd580f022008-12-03 08:38:43 +00006282 Op.getNode()->hasOneUse()) {
6283 X86::CondCode CCode =
6284 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6285 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006286 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006287 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6288 // Look for an unconditional branch following this conditional branch.
6289 // We need this because we need to reverse the successors in order
6290 // to implement FCMP_OEQ.
6291 if (User.getOpcode() == ISD::BR) {
6292 SDValue FalseBB = User.getOperand(1);
6293 SDValue NewBR =
6294 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6295 assert(NewBR == User);
6296 Dest = FalseBB;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006297
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006298 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Chengd580f022008-12-03 08:38:43 +00006299 Chain, Dest, CC, Cmp);
6300 X86::CondCode CCode =
6301 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6302 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006303 CC = DAG.getConstant(CCode, MVT::i8);
Evan Chengd580f022008-12-03 08:38:43 +00006304 Cond = Cmp;
6305 addTest = false;
6306 }
6307 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006308 }
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006309 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6310 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6311 // It should be transformed during dag combiner except when the condition
6312 // is set by a arithmetics with overflow node.
6313 X86::CondCode CCode =
6314 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6315 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006316 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng8c3af2c2009-02-02 08:07:36 +00006317 Cond = Cond.getOperand(0).getOperand(1);
6318 addTest = false;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006319 }
Evan Cheng950aac02007-09-25 01:57:46 +00006320 }
6321
6322 if (addTest) {
Evan Cheng095dac22010-01-06 19:38:29 +00006323 // Look pass the truncate.
6324 if (Cond.getOpcode() == ISD::TRUNCATE)
6325 Cond = Cond.getOperand(0);
6326
6327 // We know the result of AND is compared against zero. Try to match
6328 // it to BT.
6329 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6330 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6331 if (NewSetCC.getNode()) {
6332 CC = NewSetCC.getOperand(0);
6333 Cond = NewSetCC.getOperand(1);
6334 addTest = false;
6335 }
6336 }
6337 }
6338
6339 if (addTest) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006340 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohmanc8b47852009-03-07 01:58:32 +00006341 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng950aac02007-09-25 01:57:46 +00006342 }
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006343 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman6a00fcb2008-10-21 03:29:32 +00006344 Chain, Dest, CC, Cond);
Evan Cheng950aac02007-09-25 01:57:46 +00006345}
6346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006347
6348// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6349// Calls to _alloca is needed to probe the stack when allocating more than 4k
6350// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6351// that the guard pages used by the OS virtual memory manager are allocated in
6352// correct sequence.
Dan Gohman8181bd12008-07-27 21:46:04 +00006353SDValue
6354X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006355 SelectionDAG &DAG) {
6356 assert(Subtarget->isTargetCygMing() &&
6357 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006358 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006359
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006360 // Get the inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00006361 SDValue Chain = Op.getOperand(0);
6362 SDValue Size = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006363 // FIXME: Ensure alignment here
6364
Dan Gohman8181bd12008-07-27 21:46:04 +00006365 SDValue Flag;
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006366
Owen Andersonac9de032009-08-10 22:56:29 +00006367 EVT IntPtr = getPointerTy();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006368 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006369
Chris Lattnerfe5d4022008-10-11 22:08:30 +00006370 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006371
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006372 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006373 Flag = Chain.getValue(1);
6374
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006375 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman8181bd12008-07-27 21:46:04 +00006376 SDValue Ops[] = { Chain,
Bill Wendlingfef06052008-09-16 21:48:12 +00006377 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006378 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006379 DAG.getRegister(X86StackPtr, SPTy),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006380 Flag };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006381 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006382 Flag = Chain.getValue(1);
6383
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006384 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnerfe5d4022008-10-11 22:08:30 +00006385 DAG.getIntPtrConstant(0, true),
6386 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006387 Flag);
6388
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006389 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov487aefd2008-06-11 20:16:42 +00006390
Dan Gohman8181bd12008-07-27 21:46:04 +00006391 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006392 return DAG.getMergeValues(Ops1, 2, dl);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006393}
6394
Dan Gohman8181bd12008-07-27 21:46:04 +00006395SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006396X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006397 SDValue Chain,
6398 SDValue Dst, SDValue Src,
6399 SDValue Size, unsigned Align,
6400 const Value *DstSV,
Bill Wendling4b2e3782008-10-01 00:59:58 +00006401 uint64_t DstSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006402 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006404 // If not DWORD aligned or size is more than the threshold, call the library.
6405 // The libc version is likely to be faster for these cases. It can use the
6406 // address value and run time information about the CPU.
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006407 if ((Align & 3) != 0 ||
Dan Gohmane8b391e2008-04-12 04:36:06 +00006408 !ConstantSize ||
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006409 ConstantSize->getZExtValue() >
6410 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006411 SDValue InFlag(0, 0);
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006412
6413 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohmane8b391e2008-04-12 04:36:06 +00006414 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling5db7ffb2008-09-30 21:22:07 +00006415
Bill Wendling4b2e3782008-10-01 00:59:58 +00006416 if (const char *bzeroEntry = V &&
6417 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00006418 EVT IntPtr = getPointerTy();
Owen Anderson35b47072009-08-13 21:58:54 +00006419 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michel91099d62009-02-17 22:15:04 +00006420 TargetLowering::ArgListTy Args;
Bill Wendling4b2e3782008-10-01 00:59:58 +00006421 TargetLowering::ArgListEntry Entry;
6422 Entry.Node = Dst;
6423 Entry.Ty = IntPtrTy;
6424 Args.push_back(Entry);
6425 Entry.Node = Size;
6426 Args.push_back(Entry);
6427 std::pair<SDValue,SDValue> CallResult =
Owen Anderson35b47072009-08-13 21:58:54 +00006428 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6429 false, false, false, false,
Dan Gohman9178de12009-08-05 01:29:28 +00006430 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendlingaa181762009-12-22 02:10:19 +00006431 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6432 DAG.GetOrdering(Chain.getNode()));
Bill Wendling4b2e3782008-10-01 00:59:58 +00006433 return CallResult.second;
Dan Gohmanf95c2bf2008-04-01 20:38:36 +00006434 }
6435
Dan Gohmane8b391e2008-04-12 04:36:06 +00006436 // Otherwise have the target-independent code call memset.
Dan Gohman8181bd12008-07-27 21:46:04 +00006437 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006438 }
6439
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006440 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman8181bd12008-07-27 21:46:04 +00006441 SDValue InFlag(0, 0);
Owen Andersonac9de032009-08-10 22:56:29 +00006442 EVT AVT;
Dan Gohman8181bd12008-07-27 21:46:04 +00006443 SDValue Count;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006444 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006445 unsigned BytesLeft = 0;
6446 bool TwoRepStos = false;
6447 if (ValC) {
6448 unsigned ValReg;
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006449 uint64_t Val = ValC->getZExtValue() & 255;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006450
6451 // If the value is a constant, then we can potentially use larger sets.
6452 switch (Align & 3) {
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006453 case 2: // WORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006454 AVT = MVT::i16;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006455 ValReg = X86::AX;
6456 Val = (Val << 8) | Val;
6457 break;
6458 case 0: // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006459 AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006460 ValReg = X86::EAX;
6461 Val = (Val << 8) | Val;
6462 Val = (Val << 16) | Val;
6463 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006464 AVT = MVT::i64;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006465 ValReg = X86::RAX;
6466 Val = (Val << 32) | Val;
6467 }
6468 break;
6469 default: // Byte aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006470 AVT = MVT::i8;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006471 ValReg = X86::AL;
6472 Count = DAG.getIntPtrConstant(SizeVal);
6473 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006474 }
6475
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006476 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands92c43912008-06-06 12:08:01 +00006477 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006478 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6479 BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006480 }
6481
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006482 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006483 InFlag);
6484 InFlag = Chain.getValue(1);
6485 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006486 AVT = MVT::i8;
Dan Gohman271d1c22008-04-16 01:32:32 +00006487 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006488 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006489 InFlag = Chain.getValue(1);
6490 }
6491
Scott Michel91099d62009-02-17 22:15:04 +00006492 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006493 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006494 Count, InFlag);
6495 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006496 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006497 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006498 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006499 InFlag = Chain.getValue(1);
6500
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006501 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006502 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6503 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006504
6505 if (TwoRepStos) {
6506 InFlag = Chain.getValue(1);
Dan Gohmane8b391e2008-04-12 04:36:06 +00006507 Count = Size;
Owen Andersonac9de032009-08-10 22:56:29 +00006508 EVT CVT = Count.getValueType();
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006509 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006510 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6511 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006512 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006513 Left, InFlag);
6514 InFlag = Chain.getValue(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006515 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006516 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6517 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006518 } else if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006519 // Handle the last 1 - 7 bytes.
6520 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006521 EVT AddrVT = Dst.getValueType();
6522 EVT SizeVT = Size.getValueType();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006523
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006524 Chain = DAG.getMemset(Chain, dl,
6525 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006526 DAG.getConstant(Offset, AddrVT)),
6527 Src,
6528 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman65118f42008-04-28 17:15:20 +00006529 Align, DstSV, DstSVOff + Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006530 }
6531
Dan Gohmane8b391e2008-04-12 04:36:06 +00006532 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006533 return Chain;
6534}
6535
Dan Gohman8181bd12008-07-27 21:46:04 +00006536SDValue
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006537X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006538 SDValue Chain, SDValue Dst, SDValue Src,
6539 SDValue Size, unsigned Align,
6540 bool AlwaysInline,
6541 const Value *DstSV, uint64_t DstSVOff,
Scott Michel91099d62009-02-17 22:15:04 +00006542 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006543 // This requires the copy size to be a constant, preferrably
6544 // within a subtarget-specific limit.
6545 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6546 if (!ConstantSize)
Dan Gohman8181bd12008-07-27 21:46:04 +00006547 return SDValue();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006548 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006549 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman8181bd12008-07-27 21:46:04 +00006550 return SDValue();
Dan Gohmane8b391e2008-04-12 04:36:06 +00006551
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006552 /// If not DWORD aligned, call the library.
6553 if ((Align & 3) != 0)
6554 return SDValue();
6555
6556 // DWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006557 EVT AVT = MVT::i32;
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006558 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006559 AVT = MVT::i64;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006560
Duncan Sands92c43912008-06-06 12:08:01 +00006561 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohmane8b391e2008-04-12 04:36:06 +00006562 unsigned CountVal = SizeVal / UBytes;
Dan Gohman8181bd12008-07-27 21:46:04 +00006563 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng9a6e0fa2008-08-21 21:00:15 +00006564 unsigned BytesLeft = SizeVal % UBytes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006565
Dan Gohman8181bd12008-07-27 21:46:04 +00006566 SDValue InFlag(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00006567 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006568 X86::ECX,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006569 Count, InFlag);
6570 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006571 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006572 X86::EDI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006573 Dst, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006574 InFlag = Chain.getValue(1);
Scott Michel91099d62009-02-17 22:15:04 +00006575 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006576 X86::ESI,
Dan Gohmane8b391e2008-04-12 04:36:06 +00006577 Src, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006578 InFlag = Chain.getValue(1);
6579
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006580 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer65f60c92009-12-29 16:57:26 +00006581 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6582 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6583 array_lengthof(Ops));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006584
Dan Gohman8181bd12008-07-27 21:46:04 +00006585 SmallVector<SDValue, 4> Results;
Evan Cheng38d3c522008-04-25 00:26:43 +00006586 Results.push_back(RepMovs);
Rafael Espindolaf12f3a92007-09-28 12:53:01 +00006587 if (BytesLeft) {
Dan Gohmane8b391e2008-04-12 04:36:06 +00006588 // Handle the last 1 - 7 bytes.
6589 unsigned Offset = SizeVal - BytesLeft;
Owen Andersonac9de032009-08-10 22:56:29 +00006590 EVT DstVT = Dst.getValueType();
6591 EVT SrcVT = Src.getValueType();
6592 EVT SizeVT = Size.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00006593 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006594 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng38d3c522008-04-25 00:26:43 +00006595 DAG.getConstant(Offset, DstVT)),
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006596 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng38d3c522008-04-25 00:26:43 +00006597 DAG.getConstant(Offset, SrcVT)),
Dan Gohmane8b391e2008-04-12 04:36:06 +00006598 DAG.getConstant(BytesLeft, SizeVT),
6599 Align, AlwaysInline,
Dan Gohman65118f42008-04-28 17:15:20 +00006600 DstSV, DstSVOff + Offset,
6601 SrcSV, SrcSVOff + Offset));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006602 }
6603
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006604 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen7f2abf42009-02-03 22:26:09 +00006605 &Results[0], Results.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006606}
6607
Dan Gohman8181bd12008-07-27 21:46:04 +00006608SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman12a9c082008-02-06 22:27:42 +00006609 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006610 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006611
6612 if (!Subtarget->is64Bit()) {
6613 // vastart just stores the address of the VarArgsFrameIndex slot into the
6614 // memory location argument.
Dan Gohman8181bd12008-07-27 21:46:04 +00006615 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006616 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006617 }
6618
6619 // __va_list_tag:
6620 // gp_offset (0 - 6 * 8)
6621 // fp_offset (48 - 48 + 8 * 16)
6622 // overflow_arg_area (point to parameters coming in memory).
6623 // reg_save_area
Dan Gohman8181bd12008-07-27 21:46:04 +00006624 SmallVector<SDValue, 8> MemOps;
6625 SDValue FIN = Op.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006626 // Store gp_offset
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006627 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006628 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00006629 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006630 MemOps.push_back(Store);
6631
6632 // Store fp_offset
Scott Michel91099d62009-02-17 22:15:04 +00006633 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006634 FIN, DAG.getIntPtrConstant(4));
6635 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006636 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman12a9c082008-02-06 22:27:42 +00006637 FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006638 MemOps.push_back(Store);
6639
6640 // Store ptr to overflow_arg_area
Scott Michel91099d62009-02-17 22:15:04 +00006641 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006642 FIN, DAG.getIntPtrConstant(4));
Dan Gohman8181bd12008-07-27 21:46:04 +00006643 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006644 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006645 MemOps.push_back(Store);
6646
6647 // Store ptr to reg_save_area.
Scott Michel91099d62009-02-17 22:15:04 +00006648 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006649 FIN, DAG.getIntPtrConstant(8));
Dan Gohman8181bd12008-07-27 21:46:04 +00006650 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006651 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006652 MemOps.push_back(Store);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006653 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006654 &MemOps[0], MemOps.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006655}
6656
Dan Gohman8181bd12008-07-27 21:46:04 +00006657SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman827cb1f2008-05-10 01:26:14 +00006658 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6659 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006660 SDValue Chain = Op.getOperand(0);
6661 SDValue SrcPtr = Op.getOperand(1);
6662 SDValue SrcSV = Op.getOperand(2);
Dan Gohman827cb1f2008-05-10 01:26:14 +00006663
Edwin Török4d9756a2009-07-08 20:53:28 +00006664 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006665 return SDValue();
Dan Gohman827cb1f2008-05-10 01:26:14 +00006666}
6667
Dan Gohman8181bd12008-07-27 21:46:04 +00006668SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006669 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman840ff5c2008-04-18 20:55:41 +00006670 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman8181bd12008-07-27 21:46:04 +00006671 SDValue Chain = Op.getOperand(0);
6672 SDValue DstPtr = Op.getOperand(1);
6673 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman12a9c082008-02-06 22:27:42 +00006674 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6675 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006676 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006677
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006678 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman840ff5c2008-04-18 20:55:41 +00006679 DAG.getIntPtrConstant(24), 8, false,
6680 DstSV, 0, SrcSV, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006681}
6682
Dan Gohman8181bd12008-07-27 21:46:04 +00006683SDValue
6684X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006685 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006686 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006687 switch (IntNo) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006688 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006689 // Comparison intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006690 case Intrinsic::x86_sse_comieq_ss:
6691 case Intrinsic::x86_sse_comilt_ss:
6692 case Intrinsic::x86_sse_comile_ss:
6693 case Intrinsic::x86_sse_comigt_ss:
6694 case Intrinsic::x86_sse_comige_ss:
6695 case Intrinsic::x86_sse_comineq_ss:
6696 case Intrinsic::x86_sse_ucomieq_ss:
6697 case Intrinsic::x86_sse_ucomilt_ss:
6698 case Intrinsic::x86_sse_ucomile_ss:
6699 case Intrinsic::x86_sse_ucomigt_ss:
6700 case Intrinsic::x86_sse_ucomige_ss:
6701 case Intrinsic::x86_sse_ucomineq_ss:
6702 case Intrinsic::x86_sse2_comieq_sd:
6703 case Intrinsic::x86_sse2_comilt_sd:
6704 case Intrinsic::x86_sse2_comile_sd:
6705 case Intrinsic::x86_sse2_comigt_sd:
6706 case Intrinsic::x86_sse2_comige_sd:
6707 case Intrinsic::x86_sse2_comineq_sd:
6708 case Intrinsic::x86_sse2_ucomieq_sd:
6709 case Intrinsic::x86_sse2_ucomilt_sd:
6710 case Intrinsic::x86_sse2_ucomile_sd:
6711 case Intrinsic::x86_sse2_ucomigt_sd:
6712 case Intrinsic::x86_sse2_ucomige_sd:
6713 case Intrinsic::x86_sse2_ucomineq_sd: {
6714 unsigned Opc = 0;
6715 ISD::CondCode CC = ISD::SETCC_INVALID;
6716 switch (IntNo) {
6717 default: break;
6718 case Intrinsic::x86_sse_comieq_ss:
6719 case Intrinsic::x86_sse2_comieq_sd:
6720 Opc = X86ISD::COMI;
6721 CC = ISD::SETEQ;
6722 break;
6723 case Intrinsic::x86_sse_comilt_ss:
6724 case Intrinsic::x86_sse2_comilt_sd:
6725 Opc = X86ISD::COMI;
6726 CC = ISD::SETLT;
6727 break;
6728 case Intrinsic::x86_sse_comile_ss:
6729 case Intrinsic::x86_sse2_comile_sd:
6730 Opc = X86ISD::COMI;
6731 CC = ISD::SETLE;
6732 break;
6733 case Intrinsic::x86_sse_comigt_ss:
6734 case Intrinsic::x86_sse2_comigt_sd:
6735 Opc = X86ISD::COMI;
6736 CC = ISD::SETGT;
6737 break;
6738 case Intrinsic::x86_sse_comige_ss:
6739 case Intrinsic::x86_sse2_comige_sd:
6740 Opc = X86ISD::COMI;
6741 CC = ISD::SETGE;
6742 break;
6743 case Intrinsic::x86_sse_comineq_ss:
6744 case Intrinsic::x86_sse2_comineq_sd:
6745 Opc = X86ISD::COMI;
6746 CC = ISD::SETNE;
6747 break;
6748 case Intrinsic::x86_sse_ucomieq_ss:
6749 case Intrinsic::x86_sse2_ucomieq_sd:
6750 Opc = X86ISD::UCOMI;
6751 CC = ISD::SETEQ;
6752 break;
6753 case Intrinsic::x86_sse_ucomilt_ss:
6754 case Intrinsic::x86_sse2_ucomilt_sd:
6755 Opc = X86ISD::UCOMI;
6756 CC = ISD::SETLT;
6757 break;
6758 case Intrinsic::x86_sse_ucomile_ss:
6759 case Intrinsic::x86_sse2_ucomile_sd:
6760 Opc = X86ISD::UCOMI;
6761 CC = ISD::SETLE;
6762 break;
6763 case Intrinsic::x86_sse_ucomigt_ss:
6764 case Intrinsic::x86_sse2_ucomigt_sd:
6765 Opc = X86ISD::UCOMI;
6766 CC = ISD::SETGT;
6767 break;
6768 case Intrinsic::x86_sse_ucomige_ss:
6769 case Intrinsic::x86_sse2_ucomige_sd:
6770 Opc = X86ISD::UCOMI;
6771 CC = ISD::SETGE;
6772 break;
6773 case Intrinsic::x86_sse_ucomineq_ss:
6774 case Intrinsic::x86_sse2_ucomineq_sd:
6775 Opc = X86ISD::UCOMI;
6776 CC = ISD::SETNE;
6777 break;
6778 }
6779
Dan Gohman8181bd12008-07-27 21:46:04 +00006780 SDValue LHS = Op.getOperand(1);
6781 SDValue RHS = Op.getOperand(2);
Chris Lattnerebb91142008-12-24 23:53:05 +00006782 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman8ab7dd02009-10-20 16:22:37 +00006783 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006784 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6785 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6786 DAG.getConstant(X86CC, MVT::i8), Cond);
6787 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006788 }
Eric Christopher95d79262009-07-29 00:28:05 +00006789 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher79e0e8b2009-07-29 01:01:19 +00006790 // an integer value, not just an instruction so lower it to the ptest
6791 // pattern and a setcc for the result.
Eric Christopher95d79262009-07-29 00:28:05 +00006792 case Intrinsic::x86_sse41_ptestz:
6793 case Intrinsic::x86_sse41_ptestc:
6794 case Intrinsic::x86_sse41_ptestnzc:{
6795 unsigned X86CC = 0;
6796 switch (IntNo) {
Eric Christopher6612b082009-07-29 18:14:04 +00006797 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher95d79262009-07-29 00:28:05 +00006798 case Intrinsic::x86_sse41_ptestz:
6799 // ZF = 1
6800 X86CC = X86::COND_E;
6801 break;
6802 case Intrinsic::x86_sse41_ptestc:
6803 // CF = 1
6804 X86CC = X86::COND_B;
6805 break;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006806 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher95d79262009-07-29 00:28:05 +00006807 // ZF and CF = 0
6808 X86CC = X86::COND_A;
6809 break;
6810 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00006811
Eric Christopher95d79262009-07-29 00:28:05 +00006812 SDValue LHS = Op.getOperand(1);
6813 SDValue RHS = Op.getOperand(2);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006814 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6815 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6816 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6817 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher95d79262009-07-29 00:28:05 +00006818 }
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006819
6820 // Fix vector shift instructions where the last operand is a non-immediate
6821 // i32 value.
6822 case Intrinsic::x86_sse2_pslli_w:
6823 case Intrinsic::x86_sse2_pslli_d:
6824 case Intrinsic::x86_sse2_pslli_q:
6825 case Intrinsic::x86_sse2_psrli_w:
6826 case Intrinsic::x86_sse2_psrli_d:
6827 case Intrinsic::x86_sse2_psrli_q:
6828 case Intrinsic::x86_sse2_psrai_w:
6829 case Intrinsic::x86_sse2_psrai_d:
6830 case Intrinsic::x86_mmx_pslli_w:
6831 case Intrinsic::x86_mmx_pslli_d:
6832 case Intrinsic::x86_mmx_pslli_q:
6833 case Intrinsic::x86_mmx_psrli_w:
6834 case Intrinsic::x86_mmx_psrli_d:
6835 case Intrinsic::x86_mmx_psrli_q:
6836 case Intrinsic::x86_mmx_psrai_w:
6837 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman8181bd12008-07-27 21:46:04 +00006838 SDValue ShAmt = Op.getOperand(2);
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006839 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman8181bd12008-07-27 21:46:04 +00006840 return SDValue();
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006841
6842 unsigned NewIntNo = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006843 EVT ShAmtVT = MVT::v4i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006844 switch (IntNo) {
6845 case Intrinsic::x86_sse2_pslli_w:
6846 NewIntNo = Intrinsic::x86_sse2_psll_w;
6847 break;
6848 case Intrinsic::x86_sse2_pslli_d:
6849 NewIntNo = Intrinsic::x86_sse2_psll_d;
6850 break;
6851 case Intrinsic::x86_sse2_pslli_q:
6852 NewIntNo = Intrinsic::x86_sse2_psll_q;
6853 break;
6854 case Intrinsic::x86_sse2_psrli_w:
6855 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6856 break;
6857 case Intrinsic::x86_sse2_psrli_d:
6858 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6859 break;
6860 case Intrinsic::x86_sse2_psrli_q:
6861 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6862 break;
6863 case Intrinsic::x86_sse2_psrai_w:
6864 NewIntNo = Intrinsic::x86_sse2_psra_w;
6865 break;
6866 case Intrinsic::x86_sse2_psrai_d:
6867 NewIntNo = Intrinsic::x86_sse2_psra_d;
6868 break;
6869 default: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006870 ShAmtVT = MVT::v2i32;
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006871 switch (IntNo) {
6872 case Intrinsic::x86_mmx_pslli_w:
6873 NewIntNo = Intrinsic::x86_mmx_psll_w;
6874 break;
6875 case Intrinsic::x86_mmx_pslli_d:
6876 NewIntNo = Intrinsic::x86_mmx_psll_d;
6877 break;
6878 case Intrinsic::x86_mmx_pslli_q:
6879 NewIntNo = Intrinsic::x86_mmx_psll_q;
6880 break;
6881 case Intrinsic::x86_mmx_psrli_w:
6882 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6883 break;
6884 case Intrinsic::x86_mmx_psrli_d:
6885 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6886 break;
6887 case Intrinsic::x86_mmx_psrli_q:
6888 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6889 break;
6890 case Intrinsic::x86_mmx_psrai_w:
6891 NewIntNo = Intrinsic::x86_mmx_psra_w;
6892 break;
6893 case Intrinsic::x86_mmx_psrai_d:
6894 NewIntNo = Intrinsic::x86_mmx_psra_d;
6895 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00006896 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006897 }
6898 break;
6899 }
6900 }
Mon P Wang04c767e2009-09-03 19:56:25 +00006901
6902 // The vector shift intrinsics with scalars uses 32b shift amounts but
6903 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6904 // to be zero.
6905 SDValue ShOps[4];
6906 ShOps[0] = ShAmt;
6907 ShOps[1] = DAG.getConstant(0, MVT::i32);
6908 if (ShAmtVT == MVT::v4i32) {
6909 ShOps[2] = DAG.getUNDEF(MVT::i32);
6910 ShOps[3] = DAG.getUNDEF(MVT::i32);
6911 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6912 } else {
6913 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6914 }
6915
Owen Andersonac9de032009-08-10 22:56:29 +00006916 EVT VT = Op.getValueType();
Mon P Wang04c767e2009-09-03 19:56:25 +00006917 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006918 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006919 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng9f69f9d2008-05-04 09:15:50 +00006920 Op.getOperand(1), ShAmt);
6921 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006922 }
6923}
6924
Dan Gohman8181bd12008-07-27 21:46:04 +00006925SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006926 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006927 DebugLoc dl = Op.getDebugLoc();
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006928
6929 if (Depth > 0) {
6930 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6931 SDValue Offset =
6932 DAG.getConstant(TD->getPointerSize(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006933 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006934 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michel91099d62009-02-17 22:15:04 +00006935 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006936 FrameAddr, Offset),
Bill Wendling6ddc87b2009-01-16 19:25:27 +00006937 NULL, 0);
6938 }
6939
6940 // Just load the return address.
Dan Gohman8181bd12008-07-27 21:46:04 +00006941 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michel91099d62009-02-17 22:15:04 +00006942 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006943 RetAddrFI, NULL, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006944}
6945
Dan Gohman8181bd12008-07-27 21:46:04 +00006946SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng33633672008-09-27 01:56:22 +00006947 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6948 MFI->setFrameAddressIsTaken(true);
Owen Andersonac9de032009-08-10 22:56:29 +00006949 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006950 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng33633672008-09-27 01:56:22 +00006951 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6952 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006953 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng33633672008-09-27 01:56:22 +00006954 while (Depth--)
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006955 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng33633672008-09-27 01:56:22 +00006956 return FrameAddr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006957}
6958
Dan Gohman8181bd12008-07-27 21:46:04 +00006959SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov566f9d92008-09-08 21:12:11 +00006960 SelectionDAG &DAG) {
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006961 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006962}
6963
Dan Gohman8181bd12008-07-27 21:46:04 +00006964SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006965{
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006966 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman8181bd12008-07-27 21:46:04 +00006967 SDValue Chain = Op.getOperand(0);
6968 SDValue Offset = Op.getOperand(1);
6969 SDValue Handler = Op.getOperand(2);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006970 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006971
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006972 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6973 getPointerTy());
6974 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006975
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006976 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00006977 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006978 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6979 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00006980 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006981 MF.getRegInfo().addLiveOut(StoreAddrReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006982
Dale Johannesen0db52dd2009-02-03 20:21:25 +00006983 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006984 MVT::Other,
Anton Korobeynikov1ec04ee2008-09-08 21:12:47 +00006985 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006986}
6987
Dan Gohman8181bd12008-07-27 21:46:04 +00006988SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006989 SelectionDAG &DAG) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006990 SDValue Root = Op.getOperand(0);
6991 SDValue Trmp = Op.getOperand(1); // trampoline
6992 SDValue FPtr = Op.getOperand(2); // nested function
6993 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00006994 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006995
Dan Gohman12a9c082008-02-06 22:27:42 +00006996 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00006997
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00006998 const X86InstrInfo *TII =
6999 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
7000
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007001 if (Subtarget->is64Bit()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00007002 SDValue OutChains[6];
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007003
7004 // Large code-model.
7005
7006 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
7007 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
7008
Dan Gohmanb41dfba2008-05-14 01:58:56 +00007009 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7010 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007011
7012 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7013
7014 // Load the pointer to the nested function into R11.
7015 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman8181bd12008-07-27 21:46:04 +00007016 SDValue Addr = Trmp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007017 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007018 Addr, TrmpAddr, 0);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007019
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007020 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7021 DAG.getConstant(2, MVT::i64));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007022 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007023
7024 // Load the 'nest' parameter value into R10.
7025 // R10 is specified in X86CallingConv.td
7026 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007027 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7028 DAG.getConstant(10, MVT::i64));
7029 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007030 Addr, TrmpAddr, 10);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007031
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007032 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7033 DAG.getConstant(12, MVT::i64));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007034 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007035
7036 // Jump to the nested function.
7037 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007038 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7039 DAG.getConstant(20, MVT::i64));
7040 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007041 Addr, TrmpAddr, 20);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007042
7043 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007044 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7045 DAG.getConstant(22, MVT::i64));
7046 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00007047 TrmpAddr, 22);
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007048
Dan Gohman8181bd12008-07-27 21:46:04 +00007049 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007050 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007051 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007052 } else {
Dan Gohman0bd70702008-01-31 01:01:48 +00007053 const Function *Func =
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007054 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel5838baa2009-09-02 08:44:58 +00007055 CallingConv::ID CC = Func->getCallingConv();
Duncan Sands466eadd2007-08-29 19:01:20 +00007056 unsigned NestReg;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007057
7058 switch (CC) {
7059 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00007060 llvm_unreachable("Unsupported calling convention");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007061 case CallingConv::C:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007062 case CallingConv::X86_StdCall: {
7063 // Pass 'nest' parameter in ECX.
7064 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00007065 NestReg = X86::ECX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007066
7067 // Check that ECX wasn't needed by an 'inreg' parameter.
7068 const FunctionType *FTy = Func->getFunctionType();
Devang Pateld222f862008-09-25 21:00:45 +00007069 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007070
Chris Lattner1c8733e2008-03-12 17:45:29 +00007071 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007072 unsigned InRegCount = 0;
7073 unsigned Idx = 1;
7074
7075 for (FunctionType::param_iterator I = FTy->param_begin(),
7076 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Pateld222f862008-09-25 21:00:45 +00007077 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007078 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovd0fef972008-09-09 18:22:57 +00007079 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007080
7081 if (InRegCount > 2) {
Edwin Török3cb88482009-07-08 18:01:40 +00007082 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007083 }
7084 }
7085 break;
7086 }
7087 case CallingConv::X86_FastCall:
Duncan Sands162c1d52008-09-10 13:22:10 +00007088 case CallingConv::Fast:
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007089 // Pass 'nest' parameter in EAX.
7090 // Must be kept in sync with X86CallingConv.td
Duncan Sands466eadd2007-08-29 19:01:20 +00007091 NestReg = X86::EAX;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007092 break;
7093 }
7094
Dan Gohman8181bd12008-07-27 21:46:04 +00007095 SDValue OutChains[4];
7096 SDValue Addr, Disp;
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007097
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007098 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7099 DAG.getConstant(10, MVT::i32));
7100 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007101
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007102 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanb41dfba2008-05-14 01:58:56 +00007103 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michel91099d62009-02-17 22:15:04 +00007104 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007105 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman12a9c082008-02-06 22:27:42 +00007106 Trmp, TrmpAddr, 0);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007107
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007108 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7109 DAG.getConstant(1, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007110 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007111
Duncan Sands3e8ff6f2008-01-16 22:55:25 +00007112 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007113 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7114 DAG.getConstant(5, MVT::i32));
7115 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman12a9c082008-02-06 22:27:42 +00007116 TrmpAddr, 5, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007117
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007118 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7119 DAG.getConstant(6, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007120 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007121
Dan Gohman8181bd12008-07-27 21:46:04 +00007122 SDValue Ops[] =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007123 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007124 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007125 }
7126}
7127
Dan Gohman8181bd12008-07-27 21:46:04 +00007128SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007129 /*
7130 The rounding mode is in bits 11:10 of FPSR, and has the following
7131 settings:
7132 00 Round to nearest
7133 01 Round to -inf
7134 10 Round to +inf
7135 11 Round to 0
7136
7137 FLT_ROUNDS, on the other hand, expects the following:
7138 -1 Undefined
7139 0 Round to 0
7140 1 Round to nearest
7141 2 Round to +inf
7142 3 Round to -inf
7143
7144 To perform the conversion, we do:
7145 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7146 */
7147
7148 MachineFunction &MF = DAG.getMachineFunction();
7149 const TargetMachine &TM = MF.getTarget();
7150 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7151 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersonac9de032009-08-10 22:56:29 +00007152 EVT VT = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007153 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007154
7155 // Save FP Control Word to stack slot
David Greene6424ab92009-11-12 20:49:22 +00007156 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman8181bd12008-07-27 21:46:04 +00007157 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007158
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007159 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng6617eed2008-09-24 23:26:36 +00007160 DAG.getEntryNode(), StackSlot);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007161
7162 // Load FP Control Word from stack slot
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007163 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007164
7165 // Transform as necessary
Dan Gohman8181bd12008-07-27 21:46:04 +00007166 SDValue CWD1 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007167 DAG.getNode(ISD::SRL, dl, MVT::i16,
7168 DAG.getNode(ISD::AND, dl, MVT::i16,
7169 CWD, DAG.getConstant(0x800, MVT::i16)),
7170 DAG.getConstant(11, MVT::i8));
Dan Gohman8181bd12008-07-27 21:46:04 +00007171 SDValue CWD2 =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007172 DAG.getNode(ISD::SRL, dl, MVT::i16,
7173 DAG.getNode(ISD::AND, dl, MVT::i16,
7174 CWD, DAG.getConstant(0x400, MVT::i16)),
7175 DAG.getConstant(9, MVT::i8));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007176
Dan Gohman8181bd12008-07-27 21:46:04 +00007177 SDValue RetVal =
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007178 DAG.getNode(ISD::AND, dl, MVT::i16,
7179 DAG.getNode(ISD::ADD, dl, MVT::i16,
7180 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7181 DAG.getConstant(1, MVT::i16)),
7182 DAG.getConstant(3, MVT::i16));
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007183
7184
Duncan Sands92c43912008-06-06 12:08:01 +00007185 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesen24dd9a52009-02-07 00:55:49 +00007186 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007187}
7188
Dan Gohman8181bd12008-07-27 21:46:04 +00007189SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007190 EVT VT = Op.getValueType();
7191 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007192 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007193 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007194
7195 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007196 if (VT == MVT::i8) {
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007197 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007198 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007199 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007200 }
Evan Cheng48679f42007-12-14 02:13:44 +00007201
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007202 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007203 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007204 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007205
7206 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007207 SDValue Ops[] = {
7208 Op,
7209 DAG.getConstant(NumBits+NumBits-1, OpVT),
7210 DAG.getConstant(X86::COND_E, MVT::i8),
7211 Op.getValue(1)
7212 };
7213 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007214
7215 // Finally xor with NumBits-1.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007216 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007217
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007218 if (VT == MVT::i8)
7219 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007220 return Op;
7221}
7222
Dan Gohman8181bd12008-07-27 21:46:04 +00007223SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007224 EVT VT = Op.getValueType();
7225 EVT OpVT = VT;
Duncan Sands92c43912008-06-06 12:08:01 +00007226 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007227 DebugLoc dl = Op.getDebugLoc();
Evan Cheng48679f42007-12-14 02:13:44 +00007228
7229 Op = Op.getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007230 if (VT == MVT::i8) {
7231 OpVT = MVT::i32;
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007232 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007233 }
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007234
7235 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007236 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007237 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007238
7239 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer65f60c92009-12-29 16:57:26 +00007240 SDValue Ops[] = {
7241 Op,
7242 DAG.getConstant(NumBits, OpVT),
7243 DAG.getConstant(X86::COND_E, MVT::i8),
7244 Op.getValue(1)
7245 };
7246 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng7cfbfe32007-12-14 08:30:15 +00007247
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007248 if (VT == MVT::i8)
7249 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng48679f42007-12-14 02:13:44 +00007250 return Op;
7251}
7252
Mon P Wang14edb092008-12-18 21:42:19 +00007253SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007254 EVT VT = Op.getValueType();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007255 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007256 DebugLoc dl = Op.getDebugLoc();
Scott Michel91099d62009-02-17 22:15:04 +00007257
Mon P Wang14edb092008-12-18 21:42:19 +00007258 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7259 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7260 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7261 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7262 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7263 //
7264 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7265 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7266 // return AloBlo + AloBhi + AhiBlo;
7267
7268 SDValue A = Op.getOperand(0);
7269 SDValue B = Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +00007270
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007271 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007272 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7273 A, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007274 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007275 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7276 B, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007277 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007278 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007279 A, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007280 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007281 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007282 A, Bhi);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007283 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007284 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wang14edb092008-12-18 21:42:19 +00007285 Ahi, B);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007286 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007287 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7288 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007289 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007290 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7291 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007292 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7293 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wang14edb092008-12-18 21:42:19 +00007294 return Res;
7295}
7296
7297
Bill Wendling7e04be62008-12-09 22:08:41 +00007298SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7299 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7300 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendlingd3511522008-12-02 01:06:39 +00007301 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7302 // has only one use.
Bill Wendlingd06b4202008-11-26 22:37:40 +00007303 SDNode *N = Op.getNode();
Bill Wendlingd3511522008-12-02 01:06:39 +00007304 SDValue LHS = N->getOperand(0);
7305 SDValue RHS = N->getOperand(1);
Bill Wendling7e04be62008-12-09 22:08:41 +00007306 unsigned BaseOp = 0;
7307 unsigned Cond = 0;
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007308 DebugLoc dl = Op.getDebugLoc();
Bill Wendling7e04be62008-12-09 22:08:41 +00007309
7310 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007311 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling7e04be62008-12-09 22:08:41 +00007312 case ISD::SADDO:
Dan Gohman99a12192009-03-04 19:44:21 +00007313 // A subtract of one will be selected as a INC. Note that INC doesn't
7314 // set CF, so we can't do this for UADDO.
7315 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7316 if (C->getAPIntValue() == 1) {
7317 BaseOp = X86ISD::INC;
7318 Cond = X86::COND_O;
7319 break;
7320 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007321 BaseOp = X86ISD::ADD;
Bill Wendling7e04be62008-12-09 22:08:41 +00007322 Cond = X86::COND_O;
7323 break;
7324 case ISD::UADDO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007325 BaseOp = X86ISD::ADD;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007326 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007327 break;
7328 case ISD::SSUBO:
Dan Gohman99a12192009-03-04 19:44:21 +00007329 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7330 // set CF, so we can't do this for USUBO.
7331 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7332 if (C->getAPIntValue() == 1) {
7333 BaseOp = X86ISD::DEC;
7334 Cond = X86::COND_O;
7335 break;
7336 }
Bill Wendlingae034ed2008-12-12 00:56:36 +00007337 BaseOp = X86ISD::SUB;
Bill Wendling7e04be62008-12-09 22:08:41 +00007338 Cond = X86::COND_O;
7339 break;
7340 case ISD::USUBO:
Bill Wendlingae034ed2008-12-12 00:56:36 +00007341 BaseOp = X86ISD::SUB;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007342 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007343 break;
7344 case ISD::SMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007345 BaseOp = X86ISD::SMUL;
Bill Wendling7e04be62008-12-09 22:08:41 +00007346 Cond = X86::COND_O;
7347 break;
7348 case ISD::UMULO:
Bill Wendlingf5399032008-12-12 21:15:41 +00007349 BaseOp = X86ISD::UMUL;
Dan Gohman0fc9ed62009-01-07 00:15:08 +00007350 Cond = X86::COND_B;
Bill Wendling7e04be62008-12-09 22:08:41 +00007351 break;
7352 }
Bill Wendlingd06b4202008-11-26 22:37:40 +00007353
Bill Wendlingd3511522008-12-02 01:06:39 +00007354 // Also sets EFLAGS.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007355 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007356 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendlingd06b4202008-11-26 22:37:40 +00007357
Bill Wendlingd3511522008-12-02 01:06:39 +00007358 SDValue SetCC =
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007359 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007360 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendlingd06b4202008-11-26 22:37:40 +00007361
Bill Wendlingd3511522008-12-02 01:06:39 +00007362 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7363 return Sum;
Bill Wendling4c134df2008-11-24 19:21:46 +00007364}
7365
Dan Gohman8181bd12008-07-27 21:46:04 +00007366SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +00007367 EVT T = Op.getValueType();
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007368 DebugLoc dl = Op.getDebugLoc();
Andrew Lenharthbd7d3262008-03-04 21:13:33 +00007369 unsigned Reg = 0;
7370 unsigned size = 0;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007371 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands92c43912008-06-06 12:08:01 +00007372 default:
7373 assert(false && "Invalid value type!");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007374 case MVT::i8: Reg = X86::AL; size = 1; break;
7375 case MVT::i16: Reg = X86::AX; size = 2; break;
7376 case MVT::i32: Reg = X86::EAX; size = 4; break;
7377 case MVT::i64:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007378 assert(Subtarget->is64Bit() && "Node not type legal!");
7379 Reg = X86::RAX; size = 8;
Andrew Lenharth81580822008-03-05 01:15:49 +00007380 break;
Bill Wendlingd3511522008-12-02 01:06:39 +00007381 }
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007382 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesenddb761b2008-09-11 03:12:59 +00007383 Op.getOperand(2), SDValue());
Dan Gohman8181bd12008-07-27 21:46:04 +00007384 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng6617eed2008-09-24 23:26:36 +00007385 Op.getOperand(1),
7386 Op.getOperand(3),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007387 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng6617eed2008-09-24 23:26:36 +00007388 cpIn.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007389 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007390 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michel91099d62009-02-17 22:15:04 +00007391 SDValue cpOut =
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007392 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth7dfe23f2008-03-01 21:52:34 +00007393 return cpOut;
7394}
7395
Duncan Sands7d9834b2008-12-01 11:39:25 +00007396SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif825aa892008-08-28 23:19:51 +00007397 SelectionDAG &DAG) {
Duncan Sands7d9834b2008-12-01 11:39:25 +00007398 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007399 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007400 SDValue TheChain = Op.getOperand(0);
Dale Johannesen2dbdb0e2009-02-07 19:59:05 +00007401 DebugLoc dl = Op.getDebugLoc();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007402 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007403 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7404 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007405 rax.getValue(2));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007406 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7407 DAG.getConstant(32, MVT::i8));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007408 SDValue Ops[] = {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007409 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands7d9834b2008-12-01 11:39:25 +00007410 rdx.getValue(1)
7411 };
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007412 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesenf160d802008-10-02 18:53:47 +00007413}
7414
Dale Johannesen9011d872008-09-29 22:25:26 +00007415SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7416 SDNode *Node = Op.getNode();
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007417 DebugLoc dl = Node->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00007418 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007419 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Chengef356282009-02-23 09:03:22 +00007420 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007421 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007422 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen9011d872008-09-29 22:25:26 +00007423 Node->getOperand(0),
7424 Node->getOperand(1), negOp,
7425 cast<AtomicSDNode>(Node)->getSrcValue(),
7426 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang078a62d2008-05-05 19:05:59 +00007427}
7428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007429/// LowerOperation - Provide custom lowering hooks for some operations.
7430///
Dan Gohman8181bd12008-07-27 21:46:04 +00007431SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007432 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00007433 default: llvm_unreachable("Should not custom lower this!");
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007434 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7435 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007436 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wanga8ff0dd2010-01-24 00:05:03 +00007437 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007438 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7439 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7440 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7441 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7442 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7443 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
7444 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendlingfef06052008-09-16 21:48:12 +00007445 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohman064403e2009-10-30 01:28:02 +00007446 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007447 case ISD::SHL_PARTS:
7448 case ISD::SRA_PARTS:
7449 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7450 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesena359b8b2008-10-21 20:50:01 +00007451 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007452 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman8c3cb582009-05-23 09:59:16 +00007453 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007454 case ISD::FABS: return LowerFABS(Op, DAG);
7455 case ISD::FNEG: return LowerFNEG(Op, DAG);
7456 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007457 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman03605a02008-07-17 16:51:19 +00007458 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Cheng621216e2007-09-29 00:00:36 +00007459 case ISD::SELECT: return LowerSELECT(Op, DAG);
7460 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007461 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007462 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman827cb1f2008-05-10 01:26:14 +00007463 case ISD::VAARG: return LowerVAARG(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007464 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
7465 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
7466 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7467 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
7468 case ISD::FRAME_TO_ARGS_OFFSET:
7469 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
7470 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
7471 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsd8455ca2007-07-27 20:02:49 +00007472 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman819574c2008-01-31 00:41:03 +00007473 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng48679f42007-12-14 02:13:44 +00007474 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7475 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wang14edb092008-12-18 21:42:19 +00007476 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling7e04be62008-12-09 22:08:41 +00007477 case ISD::SADDO:
7478 case ISD::UADDO:
7479 case ISD::SSUBO:
7480 case ISD::USUBO:
7481 case ISD::SMULO:
7482 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007483 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007484 }
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007485}
7486
Duncan Sands7d9834b2008-12-01 11:39:25 +00007487void X86TargetLowering::
7488ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7489 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersonac9de032009-08-10 22:56:29 +00007490 EVT T = Node->getValueType(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007491 DebugLoc dl = Node->getDebugLoc();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007492 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007493
7494 SDValue Chain = Node->getOperand(0);
7495 SDValue In1 = Node->getOperand(1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007496 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007497 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007498 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007499 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007500 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007501 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007502 SDValue Result =
7503 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7504 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands7d9834b2008-12-01 11:39:25 +00007505 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007506 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007507 Results.push_back(Result.getValue(2));
7508}
7509
Duncan Sandsac496a12008-07-04 11:47:58 +00007510/// ReplaceNodeResults - Replace a node with an illegal result type
7511/// with a new node built out of custom code.
Duncan Sands7d9834b2008-12-01 11:39:25 +00007512void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7513 SmallVectorImpl<SDValue>&Results,
7514 SelectionDAG &DAG) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007515 DebugLoc dl = N->getDebugLoc();
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007516 switch (N->getOpcode()) {
Duncan Sands8ec7aa72008-10-20 15:56:33 +00007517 default:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007518 assert(false && "Do not know how to custom type legalize this operation!");
7519 return;
7520 case ISD::FP_TO_SINT: {
Eli Friedman8c3cb582009-05-23 09:59:16 +00007521 std::pair<SDValue,SDValue> Vals =
7522 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007523 SDValue FIST = Vals.first, StackSlot = Vals.second;
7524 if (FIST.getNode() != 0) {
Owen Andersonac9de032009-08-10 22:56:29 +00007525 EVT VT = N->getValueType(0);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007526 // Return a load from the stack slot.
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007527 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007528 }
7529 return;
7530 }
7531 case ISD::READCYCLECOUNTER: {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007532 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands7d9834b2008-12-01 11:39:25 +00007533 SDValue TheChain = N->getOperand(0);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007534 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007535 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007536 rd.getValue(1));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007537 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007538 eax.getValue(2));
7539 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7540 SDValue Ops[] = { eax, edx };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007541 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007542 Results.push_back(edx.getValue(1));
7543 return;
7544 }
Mon P Wangc707f3f2009-11-30 02:42:02 +00007545 case ISD::SDIV:
7546 case ISD::UDIV:
7547 case ISD::SREM:
7548 case ISD::UREM: {
7549 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7550 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7551 return;
7552 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007553 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersonac9de032009-08-10 22:56:29 +00007554 EVT T = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007555 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands7d9834b2008-12-01 11:39:25 +00007556 SDValue cpInL, cpInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007557 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7558 DAG.getConstant(0, MVT::i32));
7559 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7560 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007561 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7562 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007563 cpInL.getValue(1));
7564 SDValue swapInL, swapInH;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007565 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7566 DAG.getConstant(0, MVT::i32));
7567 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7568 DAG.getConstant(1, MVT::i32));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007569 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007570 cpInH.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007571 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands7d9834b2008-12-01 11:39:25 +00007572 swapInL.getValue(1));
7573 SDValue Ops[] = { swapInH.getValue(0),
7574 N->getOperand(1),
7575 swapInH.getValue(1) };
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007576 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007577 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007578 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007579 MVT::i32, Result.getValue(1));
Dale Johannesenbbd9ceb2009-02-04 00:33:20 +00007580 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007581 MVT::i32, cpOutL.getValue(2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007582 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007583 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands7d9834b2008-12-01 11:39:25 +00007584 Results.push_back(cpOutH.getValue(1));
7585 return;
7586 }
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007587 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007588 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7589 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007590 case ISD::ATOMIC_LOAD_AND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007591 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7592 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007593 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007594 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7595 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007596 case ISD::ATOMIC_LOAD_OR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007597 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7598 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007599 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007600 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7601 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007602 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007603 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7604 return;
Dan Gohmanbebba8d2008-12-23 21:37:04 +00007605 case ISD::ATOMIC_SWAP:
Duncan Sands7d9834b2008-12-01 11:39:25 +00007606 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7607 return;
Chris Lattnerdfb947d2007-11-24 07:07:01 +00007608 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007609}
7610
7611const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7612 switch (Opcode) {
7613 default: return NULL;
Evan Cheng48679f42007-12-14 02:13:44 +00007614 case X86ISD::BSF: return "X86ISD::BSF";
7615 case X86ISD::BSR: return "X86ISD::BSR";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007616 case X86ISD::SHLD: return "X86ISD::SHLD";
7617 case X86ISD::SHRD: return "X86ISD::SHRD";
7618 case X86ISD::FAND: return "X86ISD::FAND";
7619 case X86ISD::FOR: return "X86ISD::FOR";
7620 case X86ISD::FXOR: return "X86ISD::FXOR";
7621 case X86ISD::FSRL: return "X86ISD::FSRL";
7622 case X86ISD::FILD: return "X86ISD::FILD";
7623 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
7624 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7625 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7626 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
7627 case X86ISD::FLD: return "X86ISD::FLD";
7628 case X86ISD::FST: return "X86ISD::FST";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007629 case X86ISD::CALL: return "X86ISD::CALL";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007630 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohman7fe9b7f2008-12-23 22:45:23 +00007631 case X86ISD::BT: return "X86ISD::BT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007632 case X86ISD::CMP: return "X86ISD::CMP";
7633 case X86ISD::COMI: return "X86ISD::COMI";
7634 case X86ISD::UCOMI: return "X86ISD::UCOMI";
7635 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng834ae6b2009-12-15 00:53:42 +00007636 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007637 case X86ISD::CMOV: return "X86ISD::CMOV";
7638 case X86ISD::BRCOND: return "X86ISD::BRCOND";
7639 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
7640 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7641 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007642 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
7643 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattnerdc6fc472009-06-27 04:16:01 +00007644 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begemand77e59e2008-02-11 04:19:36 +00007645 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007646 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begemand77e59e2008-02-11 04:19:36 +00007647 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7648 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007649 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begeman2c87c422009-02-23 08:49:38 +00007650 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007651 case X86ISD::FMAX: return "X86ISD::FMAX";
7652 case X86ISD::FMIN: return "X86ISD::FMIN";
7653 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7654 case X86ISD::FRCP: return "X86ISD::FRCP";
7655 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindolabca99f72009-04-08 21:14:34 +00007656 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007657 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighofere2d6bbb2007-10-11 19:40:01 +00007658 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikovfbe230e2007-11-16 01:31:51 +00007659 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng40ee6e52008-05-08 00:57:18 +00007660 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7661 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesenf160d802008-10-02 18:53:47 +00007662 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7663 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7664 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7665 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7666 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7667 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chenge9b9c672008-05-09 21:53:03 +00007668 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7669 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengdea99362008-05-29 08:22:04 +00007670 case X86ISD::VSHL: return "X86ISD::VSHL";
7671 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman03605a02008-07-17 16:51:19 +00007672 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7673 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7674 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7675 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7676 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7677 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7678 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7679 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7680 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7681 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingae034ed2008-12-12 00:56:36 +00007682 case X86ISD::ADD: return "X86ISD::ADD";
7683 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingf5399032008-12-12 21:15:41 +00007684 case X86ISD::SMUL: return "X86ISD::SMUL";
7685 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman99a12192009-03-04 19:44:21 +00007686 case X86ISD::INC: return "X86ISD::INC";
7687 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohman12e03292009-09-18 19:59:53 +00007688 case X86ISD::OR: return "X86ISD::OR";
7689 case X86ISD::XOR: return "X86ISD::XOR";
7690 case X86ISD::AND: return "X86ISD::AND";
Evan Chengc3495762009-03-30 21:36:47 +00007691 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher95d79262009-07-29 00:28:05 +00007692 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohman34228bf2009-08-15 01:38:56 +00007693 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007694 }
7695}
7696
7697// isLegalAddressingMode - Return true if the addressing mode represented
7698// by AM is legal for this target, for a load/store of the specified type.
Scott Michel91099d62009-02-17 22:15:04 +00007699bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007700 const Type *Ty) const {
7701 // X86 supports extremely general addressing modes.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007702 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michel91099d62009-02-17 22:15:04 +00007703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007704 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007705 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007706 return false;
Scott Michel91099d62009-02-17 22:15:04 +00007707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007708 if (AM.BaseGV) {
Chris Lattner01e39942009-07-10 07:38:24 +00007709 unsigned GVFlags =
7710 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007711
Chris Lattner01e39942009-07-10 07:38:24 +00007712 // If a reference to this global requires an extra load, we can't fold it.
7713 if (isGlobalStubReference(GVFlags))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007714 return false;
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007715
Chris Lattner01e39942009-07-10 07:38:24 +00007716 // If BaseGV requires a register for the PIC base, we cannot also have a
7717 // BaseReg specified.
7718 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen64660e92008-12-05 21:47:27 +00007719 return false;
Evan Cheng6a1f3f12007-08-01 23:46:47 +00007720
Anton Korobeynikovc283e152009-08-05 23:01:26 +00007721 // If lower 4G is not available, then we must use rip-relative addressing.
7722 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7723 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007724 }
Scott Michel91099d62009-02-17 22:15:04 +00007725
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007726 switch (AM.Scale) {
7727 case 0:
7728 case 1:
7729 case 2:
7730 case 4:
7731 case 8:
7732 // These scales always work.
7733 break;
7734 case 3:
7735 case 5:
7736 case 9:
7737 // These scales are formed with basereg+scalereg. Only accept if there is
7738 // no basereg yet.
7739 if (AM.HasBaseReg)
7740 return false;
7741 break;
7742 default: // Other stuff never works.
7743 return false;
7744 }
Scott Michel91099d62009-02-17 22:15:04 +00007745
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007746 return true;
7747}
7748
7749
Evan Cheng27a820a2007-10-26 01:56:11 +00007750bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7751 if (!Ty1->isInteger() || !Ty2->isInteger())
7752 return false;
Evan Cheng7f152602007-10-29 07:57:50 +00007753 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7754 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007755 if (NumBits1 <= NumBits2)
Evan Cheng7f152602007-10-29 07:57:50 +00007756 return false;
7757 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng27a820a2007-10-26 01:56:11 +00007758}
7759
Owen Andersonac9de032009-08-10 22:56:29 +00007760bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands92c43912008-06-06 12:08:01 +00007761 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng9decb332007-10-29 19:58:20 +00007762 return false;
Duncan Sands92c43912008-06-06 12:08:01 +00007763 unsigned NumBits1 = VT1.getSizeInBits();
7764 unsigned NumBits2 = VT2.getSizeInBits();
Evan Chengca0e80f2008-03-20 02:18:41 +00007765 if (NumBits1 <= NumBits2)
Evan Cheng9decb332007-10-29 19:58:20 +00007766 return false;
7767 return Subtarget->is64Bit() || NumBits1 < 64;
7768}
Evan Cheng27a820a2007-10-26 01:56:11 +00007769
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007770bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007771 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman65054cc2010-01-15 22:18:15 +00007772 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007773}
7774
Owen Andersonac9de032009-08-10 22:56:29 +00007775bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohmanb044da32009-04-09 02:06:09 +00007776 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007777 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman4cedb1c2009-04-08 00:15:30 +00007778}
7779
Owen Andersonac9de032009-08-10 22:56:29 +00007780bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007781 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00007782 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00007783}
7784
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007785/// isShuffleMaskLegal - Targets can use this to indicate that they only
7786/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7787/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7788/// are assumed to be legal.
7789bool
Eric Christopher3d82bbd2009-08-27 18:07:15 +00007790X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersonac9de032009-08-10 22:56:29 +00007791 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007792 // Only do shuffles on 128-bit vector types for now.
Nate Begeman543d2142009-04-27 18:41:29 +00007793 if (VT.getSizeInBits() == 64)
7794 return false;
7795
Nate Begeman080f8e22009-10-19 02:17:23 +00007796 // FIXME: pshufb, blends, shifts.
Nate Begeman543d2142009-04-27 18:41:29 +00007797 return (VT.getVectorNumElements() == 2 ||
7798 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7799 isMOVLMask(M, VT) ||
7800 isSHUFPMask(M, VT) ||
7801 isPSHUFDMask(M, VT) ||
7802 isPSHUFHWMask(M, VT) ||
7803 isPSHUFLWMask(M, VT) ||
Nate Begeman080f8e22009-10-19 02:17:23 +00007804 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman543d2142009-04-27 18:41:29 +00007805 isUNPCKLMask(M, VT) ||
7806 isUNPCKHMask(M, VT) ||
7807 isUNPCKL_v_undef_Mask(M, VT) ||
7808 isUNPCKH_v_undef_Mask(M, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007809}
7810
Dan Gohman48d5f062008-04-09 20:09:42 +00007811bool
Nate Begemane8f61cb2009-04-29 05:20:52 +00007812X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersonac9de032009-08-10 22:56:29 +00007813 EVT VT) const {
Nate Begeman543d2142009-04-27 18:41:29 +00007814 unsigned NumElts = VT.getVectorNumElements();
7815 // FIXME: This collection of masks seems suspect.
7816 if (NumElts == 2)
7817 return true;
7818 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7819 return (isMOVLMask(Mask, VT) ||
7820 isCommutedMOVLMask(Mask, VT, true) ||
7821 isSHUFPMask(Mask, VT) ||
7822 isCommutedSHUFPMask(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007823 }
7824 return false;
7825}
7826
7827//===----------------------------------------------------------------------===//
7828// X86 Scheduler Hooks
7829//===----------------------------------------------------------------------===//
7830
Mon P Wang078a62d2008-05-05 19:05:59 +00007831// private utility function
7832MachineBasicBlock *
7833X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7834 MachineBasicBlock *MBB,
7835 unsigned regOpc,
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007836 unsigned immOpc,
Dale Johannesend20e4452008-08-19 18:47:28 +00007837 unsigned LoadOpc,
7838 unsigned CXchgOpc,
7839 unsigned copyOpc,
7840 unsigned notOpc,
7841 unsigned EAXreg,
7842 TargetRegisterClass *RC,
Dan Gohman96d60922009-02-07 16:15:20 +00007843 bool invSrc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00007844 // For the atomic bitwise operator, we generate
7845 // thisMBB:
7846 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00007847 // ld t1 = [bitinstr.addr]
7848 // op t2 = t1, [bitinstr.val]
7849 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00007850 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7851 // bz newMBB
7852 // fallthrough -->nextMBB
7853 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7854 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00007855 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00007856 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007857
Mon P Wang078a62d2008-05-05 19:05:59 +00007858 /// First build the CFG
7859 MachineFunction *F = MBB->getParent();
7860 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00007861 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7862 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7863 F->insert(MBBIter, newMBB);
7864 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007865
Mon P Wang078a62d2008-05-05 19:05:59 +00007866 // Move all successors to thisMBB to nextMBB
7867 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007868
Mon P Wang078a62d2008-05-05 19:05:59 +00007869 // Update thisMBB to fall through to newMBB
7870 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007871
Mon P Wang078a62d2008-05-05 19:05:59 +00007872 // newMBB jumps to itself and fall through to nextMBB
7873 newMBB->addSuccessor(nextMBB);
7874 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007875
Mon P Wang078a62d2008-05-05 19:05:59 +00007876 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007877 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00007878 "unexpected number of operands");
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007879 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00007880 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007881 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00007882 int numArgs = bInstr->getNumOperands() - 1;
7883 for (int i=0; i < numArgs; ++i)
7884 argOpers[i] = &bInstr->getOperand(i+1);
7885
7886 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007887 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7888 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00007889
Dale Johannesend20e4452008-08-19 18:47:28 +00007890 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007891 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00007892 for (int i=0; i <= lastAddrIndx; ++i)
7893 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007894
Dale Johannesend20e4452008-08-19 18:47:28 +00007895 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007896 if (invSrc) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007897 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007898 }
Scott Michel91099d62009-02-17 22:15:04 +00007899 else
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007900 tt = t1;
7901
Dale Johannesend20e4452008-08-19 18:47:28 +00007902 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007903 assert((argOpers[valArgIndx]->isReg() ||
7904 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00007905 "invalid operand");
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00007906 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007907 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00007908 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007909 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007910 MIB.addReg(tt);
Mon P Wang078a62d2008-05-05 19:05:59 +00007911 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00007912
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007913 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wang318b0372008-05-05 22:56:23 +00007914 MIB.addReg(t1);
Scott Michel91099d62009-02-17 22:15:04 +00007915
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007916 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang078a62d2008-05-05 19:05:59 +00007917 for (int i=0; i <= lastAddrIndx; ++i)
7918 (*MIB).addOperand(*argOpers[i]);
7919 MIB.addReg(t2);
Mon P Wang50584a62008-07-17 04:54:06 +00007920 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00007921 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7922 bInstr->memoperands_end());
Mon P Wang50584a62008-07-17 04:54:06 +00007923
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007924 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesend20e4452008-08-19 18:47:28 +00007925 MIB.addReg(EAXreg);
Scott Michel91099d62009-02-17 22:15:04 +00007926
Mon P Wang078a62d2008-05-05 19:05:59 +00007927 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007928 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00007929
Dan Gohman221a4372008-07-07 23:14:23 +00007930 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00007931 return nextMBB;
7932}
7933
Dale Johannesen44eb5372008-10-03 19:41:08 +00007934// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang078a62d2008-05-05 19:05:59 +00007935MachineBasicBlock *
Dale Johannesenf160d802008-10-02 18:53:47 +00007936X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7937 MachineBasicBlock *MBB,
7938 unsigned regOpcL,
7939 unsigned regOpcH,
7940 unsigned immOpcL,
7941 unsigned immOpcH,
Dan Gohman96d60922009-02-07 16:15:20 +00007942 bool invSrc) const {
Dale Johannesenf160d802008-10-02 18:53:47 +00007943 // For the atomic bitwise operator, we generate
7944 // thisMBB (instructions are in pairs, except cmpxchg8b)
7945 // ld t1,t2 = [bitinstr.addr]
7946 // newMBB:
7947 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7948 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen51c58ee2008-10-03 22:25:52 +00007949 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesenf160d802008-10-02 18:53:47 +00007950 // mov ECX, EBX <- t5, t6
7951 // mov EAX, EDX <- t1, t2
7952 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7953 // mov t3, t4 <- EAX, EDX
7954 // bz newMBB
7955 // result in out1, out2
7956 // fallthrough -->nextMBB
7957
7958 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7959 const unsigned LoadOpc = X86::MOV32rm;
7960 const unsigned copyOpc = X86::MOV32rr;
7961 const unsigned NotOpc = X86::NOT32r;
7962 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7963 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7964 MachineFunction::iterator MBBIter = MBB;
7965 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00007966
Dale Johannesenf160d802008-10-02 18:53:47 +00007967 /// First build the CFG
7968 MachineFunction *F = MBB->getParent();
7969 MachineBasicBlock *thisMBB = MBB;
7970 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7971 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7972 F->insert(MBBIter, newMBB);
7973 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007974
Dale Johannesenf160d802008-10-02 18:53:47 +00007975 // Move all successors to thisMBB to nextMBB
7976 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007977
Dale Johannesenf160d802008-10-02 18:53:47 +00007978 // Update thisMBB to fall through to newMBB
7979 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007980
Dale Johannesenf160d802008-10-02 18:53:47 +00007981 // newMBB jumps to itself and fall through to nextMBB
7982 newMBB->addSuccessor(nextMBB);
7983 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00007984
Dale Johannesen0db52dd2009-02-03 20:21:25 +00007985 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesenf160d802008-10-02 18:53:47 +00007986 // Insert instructions into newMBB based on incoming instruction
7987 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007988 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00007989 "unexpected number of operands");
Dale Johannesenf160d802008-10-02 18:53:47 +00007990 MachineOperand& dest1Oper = bInstr->getOperand(0);
7991 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007992 MachineOperand* argOpers[2 + X86AddrNumOperands];
7993 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00007994 argOpers[i] = &bInstr->getOperand(i+2);
7995
Evan Cheng4460e1b2010-01-08 19:14:57 +00007996 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolacfc409e2009-03-27 15:26:30 +00007997 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michel91099d62009-02-17 22:15:04 +00007998
Dale Johannesenf160d802008-10-02 18:53:47 +00007999 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008000 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesenf160d802008-10-02 18:53:47 +00008001 for (int i=0; i <= lastAddrIndx; ++i)
8002 (*MIB).addOperand(*argOpers[i]);
8003 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008004 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008005 // add 4 to displacement.
Rafael Espindolabca99f72009-04-08 21:14:34 +00008006 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesenf160d802008-10-02 18:53:47 +00008007 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008008 MachineOperand newOp3 = *(argOpers[3]);
8009 if (newOp3.isImm())
8010 newOp3.setImm(newOp3.getImm()+4);
8011 else
8012 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesenf160d802008-10-02 18:53:47 +00008013 (*MIB).addOperand(newOp3);
Rafael Espindolabca99f72009-04-08 21:14:34 +00008014 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesenf160d802008-10-02 18:53:47 +00008015
8016 // t3/4 are defined later, at the bottom of the loop
8017 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8018 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008019 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00008020 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008021 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesenf160d802008-10-02 18:53:47 +00008022 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8023
Evan Chengcdd58c32010-01-08 23:41:50 +00008024 // The subsequent operations should be using the destination registers of
8025 //the PHI instructions.
Scott Michel91099d62009-02-17 22:15:04 +00008026 if (invSrc) {
Evan Chengcdd58c32010-01-08 23:41:50 +00008027 t1 = F->getRegInfo().createVirtualRegister(RC);
8028 t2 = F->getRegInfo().createVirtualRegister(RC);
8029 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8030 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesenf160d802008-10-02 18:53:47 +00008031 } else {
Evan Chengcdd58c32010-01-08 23:41:50 +00008032 t1 = dest1Oper.getReg();
8033 t2 = dest2Oper.getReg();
Dale Johannesenf160d802008-10-02 18:53:47 +00008034 }
8035
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008036 int valArgIndx = lastAddrIndx + 1;
8037 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendlingc1946742009-05-30 01:09:53 +00008038 argOpers[valArgIndx]->isImm()) &&
Dale Johannesenf160d802008-10-02 18:53:47 +00008039 "invalid operand");
8040 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8041 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008042 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008043 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesenf160d802008-10-02 18:53:47 +00008044 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008045 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008046 if (regOpcL != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00008047 MIB.addReg(t1);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008048 (*MIB).addOperand(*argOpers[valArgIndx]);
8049 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00008050 argOpers[valArgIndx]->isReg());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008051 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendlingc1946742009-05-30 01:09:53 +00008052 argOpers[valArgIndx]->isImm());
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008053 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008054 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesenf160d802008-10-02 18:53:47 +00008055 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008056 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008057 if (regOpcH != X86::MOV32rr)
Evan Chengcdd58c32010-01-08 23:41:50 +00008058 MIB.addReg(t2);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008059 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesenf160d802008-10-02 18:53:47 +00008060
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008061 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008062 MIB.addReg(t1);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008063 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008064 MIB.addReg(t2);
8065
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008066 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008067 MIB.addReg(t5);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008068 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesenf160d802008-10-02 18:53:47 +00008069 MIB.addReg(t6);
Scott Michel91099d62009-02-17 22:15:04 +00008070
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008071 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesenf160d802008-10-02 18:53:47 +00008072 for (int i=0; i <= lastAddrIndx; ++i)
8073 (*MIB).addOperand(*argOpers[i]);
8074
8075 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008076 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8077 bInstr->memoperands_end());
Dale Johannesenf160d802008-10-02 18:53:47 +00008078
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008079 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesenf160d802008-10-02 18:53:47 +00008080 MIB.addReg(X86::EAX);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008081 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesenf160d802008-10-02 18:53:47 +00008082 MIB.addReg(X86::EDX);
Scott Michel91099d62009-02-17 22:15:04 +00008083
Dale Johannesenf160d802008-10-02 18:53:47 +00008084 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008085 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesenf160d802008-10-02 18:53:47 +00008086
8087 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8088 return nextMBB;
8089}
8090
8091// private utility function
8092MachineBasicBlock *
Mon P Wang078a62d2008-05-05 19:05:59 +00008093X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8094 MachineBasicBlock *MBB,
Dan Gohman96d60922009-02-07 16:15:20 +00008095 unsigned cmovOpc) const {
Mon P Wang078a62d2008-05-05 19:05:59 +00008096 // For the atomic min/max operator, we generate
8097 // thisMBB:
8098 // newMBB:
Mon P Wang318b0372008-05-05 22:56:23 +00008099 // ld t1 = [min/max.addr]
Scott Michel91099d62009-02-17 22:15:04 +00008100 // mov t2 = [min/max.val]
Mon P Wang078a62d2008-05-05 19:05:59 +00008101 // cmp t1, t2
8102 // cmov[cond] t2 = t1
Mon P Wang318b0372008-05-05 22:56:23 +00008103 // mov EAX = t1
Mon P Wang078a62d2008-05-05 19:05:59 +00008104 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8105 // bz newMBB
8106 // fallthrough -->nextMBB
8107 //
8108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8109 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman221a4372008-07-07 23:14:23 +00008110 MachineFunction::iterator MBBIter = MBB;
Mon P Wang078a62d2008-05-05 19:05:59 +00008111 ++MBBIter;
Scott Michel91099d62009-02-17 22:15:04 +00008112
Mon P Wang078a62d2008-05-05 19:05:59 +00008113 /// First build the CFG
8114 MachineFunction *F = MBB->getParent();
8115 MachineBasicBlock *thisMBB = MBB;
Dan Gohman221a4372008-07-07 23:14:23 +00008116 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8117 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8118 F->insert(MBBIter, newMBB);
8119 F->insert(MBBIter, nextMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008120
Dan Gohman34228bf2009-08-15 01:38:56 +00008121 // Move all successors of thisMBB to nextMBB
Mon P Wang078a62d2008-05-05 19:05:59 +00008122 nextMBB->transferSuccessors(thisMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008123
Mon P Wang078a62d2008-05-05 19:05:59 +00008124 // Update thisMBB to fall through to newMBB
8125 thisMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008126
Mon P Wang078a62d2008-05-05 19:05:59 +00008127 // newMBB jumps to newMBB and fall through to nextMBB
8128 newMBB->addSuccessor(nextMBB);
8129 newMBB->addSuccessor(newMBB);
Scott Michel91099d62009-02-17 22:15:04 +00008130
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008131 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang078a62d2008-05-05 19:05:59 +00008132 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008133 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendlingc1946742009-05-30 01:09:53 +00008134 "unexpected number of operands");
Mon P Wang078a62d2008-05-05 19:05:59 +00008135 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008136 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang078a62d2008-05-05 19:05:59 +00008137 int numArgs = mInstr->getNumOperands() - 1;
8138 for (int i=0; i < numArgs; ++i)
8139 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michel91099d62009-02-17 22:15:04 +00008140
Mon P Wang078a62d2008-05-05 19:05:59 +00008141 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolacfc409e2009-03-27 15:26:30 +00008142 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8143 int valArgIndx = lastAddrIndx + 1;
Scott Michel91099d62009-02-17 22:15:04 +00008144
Mon P Wang318b0372008-05-05 22:56:23 +00008145 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008146 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang078a62d2008-05-05 19:05:59 +00008147 for (int i=0; i <= lastAddrIndx; ++i)
8148 (*MIB).addOperand(*argOpers[i]);
Mon P Wang318b0372008-05-05 22:56:23 +00008149
Mon P Wang078a62d2008-05-05 19:05:59 +00008150 // We only support register and immediate values
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008151 assert((argOpers[valArgIndx]->isReg() ||
8152 argOpers[valArgIndx]->isImm()) &&
Dan Gohman7f7f3652008-09-13 17:58:21 +00008153 "invalid operand");
Scott Michel91099d62009-02-17 22:15:04 +00008154
8155 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008156 if (argOpers[valArgIndx]->isReg())
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008157 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michel91099d62009-02-17 22:15:04 +00008158 else
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008159 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang078a62d2008-05-05 19:05:59 +00008160 (*MIB).addOperand(*argOpers[valArgIndx]);
8161
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008162 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wang318b0372008-05-05 22:56:23 +00008163 MIB.addReg(t1);
8164
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008165 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang078a62d2008-05-05 19:05:59 +00008166 MIB.addReg(t1);
8167 MIB.addReg(t2);
8168
8169 // Generate movc
8170 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008171 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang078a62d2008-05-05 19:05:59 +00008172 MIB.addReg(t2);
8173 MIB.addReg(t1);
8174
8175 // Cmp and exchange if none has modified the memory location
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008176 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang078a62d2008-05-05 19:05:59 +00008177 for (int i=0; i <= lastAddrIndx; ++i)
8178 (*MIB).addOperand(*argOpers[i]);
8179 MIB.addReg(t3);
Mon P Wang50584a62008-07-17 04:54:06 +00008180 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008181 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8182 mInstr->memoperands_end());
Scott Michel91099d62009-02-17 22:15:04 +00008183
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008184 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang078a62d2008-05-05 19:05:59 +00008185 MIB.addReg(X86::EAX);
Scott Michel91099d62009-02-17 22:15:04 +00008186
Mon P Wang078a62d2008-05-05 19:05:59 +00008187 // insert branch
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008188 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang078a62d2008-05-05 19:05:59 +00008189
Dan Gohman221a4372008-07-07 23:14:23 +00008190 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang078a62d2008-05-05 19:05:59 +00008191 return nextMBB;
8192}
8193
Eric Christopher20391ca62009-08-27 18:08:16 +00008194// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8195// all of this code can be replaced with that in the .td file.
Dan Gohman34228bf2009-08-15 01:38:56 +00008196MachineBasicBlock *
Eric Christopher22a39402009-08-18 22:50:32 +00008197X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008198 unsigned numArgs, bool memArg) const {
Eric Christopher22a39402009-08-18 22:50:32 +00008199
8200 MachineFunction *F = BB->getParent();
8201 DebugLoc dl = MI->getDebugLoc();
8202 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8203
8204 unsigned Opc;
Evan Cheng5f3a5402009-09-19 09:51:03 +00008205 if (memArg)
8206 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8207 else
8208 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopher22a39402009-08-18 22:50:32 +00008209
8210 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8211
8212 for (unsigned i = 0; i < numArgs; ++i) {
8213 MachineOperand &Op = MI->getOperand(i+1);
8214
8215 if (!(Op.isReg() && Op.isImplicit()))
8216 MIB.addOperand(Op);
8217 }
8218
8219 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8220 .addReg(X86::XMM0);
8221
8222 F->DeleteMachineInstr(MI);
8223
8224 return BB;
8225}
8226
8227MachineBasicBlock *
Dan Gohman34228bf2009-08-15 01:38:56 +00008228X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8229 MachineInstr *MI,
8230 MachineBasicBlock *MBB) const {
8231 // Emit code to save XMM registers to the stack. The ABI says that the
8232 // number of registers to save is given in %al, so it's theoretically
8233 // possible to do an indirect jump trick to avoid saving all of them,
8234 // however this code takes a simpler approach and just executes all
8235 // of the stores if %al is non-zero. It's less code, and it's probably
8236 // easier on the hardware branch predictor, and stores aren't all that
8237 // expensive anyway.
8238
8239 // Create the new basic blocks. One block contains all the XMM stores,
8240 // and one block is the final destination regardless of whether any
8241 // stores were performed.
8242 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8243 MachineFunction *F = MBB->getParent();
8244 MachineFunction::iterator MBBIter = MBB;
8245 ++MBBIter;
8246 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8247 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8248 F->insert(MBBIter, XMMSaveMBB);
8249 F->insert(MBBIter, EndMBB);
8250
8251 // Set up the CFG.
8252 // Move any original successors of MBB to the end block.
8253 EndMBB->transferSuccessors(MBB);
8254 // The original block will now fall through to the XMM save block.
8255 MBB->addSuccessor(XMMSaveMBB);
8256 // The XMMSaveMBB will fall through to the end block.
8257 XMMSaveMBB->addSuccessor(EndMBB);
8258
8259 // Now add the instructions.
8260 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8261 DebugLoc DL = MI->getDebugLoc();
8262
8263 unsigned CountReg = MI->getOperand(0).getReg();
8264 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8265 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8266
8267 if (!Subtarget->isTargetWin64()) {
8268 // If %al is 0, branch around the XMM save block.
8269 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8270 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8271 MBB->addSuccessor(EndMBB);
8272 }
8273
8274 // In the XMM save block, save all the XMM argument registers.
8275 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8276 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008277 MachineMemOperand *MMO =
Evan Cheng174e2cf2009-10-18 18:16:27 +00008278 F->getMachineMemOperand(
8279 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8280 MachineMemOperand::MOStore, Offset,
8281 /*Size=*/16, /*Align=*/16);
Dan Gohman34228bf2009-08-15 01:38:56 +00008282 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8283 .addFrameIndex(RegSaveFrameIndex)
8284 .addImm(/*Scale=*/1)
8285 .addReg(/*IndexReg=*/0)
8286 .addImm(/*Disp=*/Offset)
8287 .addReg(/*Segment=*/0)
8288 .addReg(MI->getOperand(i).getReg())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00008289 .addMemOperand(MMO);
Dan Gohman34228bf2009-08-15 01:38:56 +00008290 }
8291
8292 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8293
8294 return EndMBB;
8295}
Mon P Wang078a62d2008-05-05 19:05:59 +00008296
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008297MachineBasicBlock *
Chris Lattner84a67202009-09-02 05:57:00 +00008298X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Cheng5f3a5402009-09-19 09:51:03 +00008299 MachineBasicBlock *BB,
8300 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner84a67202009-09-02 05:57:00 +00008301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8302 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008303
Chris Lattner84a67202009-09-02 05:57:00 +00008304 // To "insert" a SELECT_CC instruction, we actually have to insert the
8305 // diamond control-flow pattern. The incoming instruction knows the
8306 // destination vreg to set, the condition code register to branch on, the
8307 // true/false values to select between, and a branch opcode to use.
8308 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8309 MachineFunction::iterator It = BB;
8310 ++It;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008311
Chris Lattner84a67202009-09-02 05:57:00 +00008312 // thisMBB:
8313 // ...
8314 // TrueVal = ...
8315 // cmpTY ccX, r1, r2
8316 // bCC copy1MBB
8317 // fallthrough --> copy0MBB
8318 MachineBasicBlock *thisMBB = BB;
8319 MachineFunction *F = BB->getParent();
8320 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8321 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8322 unsigned Opc =
8323 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8324 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8325 F->insert(It, copy0MBB);
8326 F->insert(It, sinkMBB);
Evan Cheng5f3a5402009-09-19 09:51:03 +00008327 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner84a67202009-09-02 05:57:00 +00008328 // block to the new block which will contain the Phi node for the select.
Evan Cheng5f3a5402009-09-19 09:51:03 +00008329 // Also inform sdisel of the edge changes.
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008330 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Cheng5f3a5402009-09-19 09:51:03 +00008331 E = BB->succ_end(); I != E; ++I) {
8332 EM->insert(std::make_pair(*I, sinkMBB));
8333 sinkMBB->addSuccessor(*I);
8334 }
8335 // Next, remove all successors of the current block, and add the true
8336 // and fallthrough blocks as its successors.
8337 while (!BB->succ_empty())
8338 BB->removeSuccessor(BB->succ_begin());
Chris Lattner84a67202009-09-02 05:57:00 +00008339 // Add the true and fallthrough blocks as its successors.
8340 BB->addSuccessor(copy0MBB);
8341 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008342
Chris Lattner84a67202009-09-02 05:57:00 +00008343 // copy0MBB:
8344 // %FalseValue = ...
8345 // # fallthrough to sinkMBB
8346 BB = copy0MBB;
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008347
Chris Lattner84a67202009-09-02 05:57:00 +00008348 // Update machine-CFG edges
8349 BB->addSuccessor(sinkMBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +00008350
Chris Lattner84a67202009-09-02 05:57:00 +00008351 // sinkMBB:
8352 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8353 // ...
8354 BB = sinkMBB;
8355 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8356 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8357 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8358
8359 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8360 return BB;
8361}
8362
8363
8364MachineBasicBlock *
Evan Chenge637db12008-01-30 18:18:23 +00008365X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengd7dc9832009-09-18 21:02:19 +00008366 MachineBasicBlock *BB,
8367 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008368 switch (MI->getOpcode()) {
8369 default: assert(false && "Unexpected instr type to insert");
Dan Gohman29b998f2009-08-27 00:14:12 +00008370 case X86::CMOV_GR8:
Mon P Wang83edba52008-12-12 01:25:51 +00008371 case X86::CMOV_V1I64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008372 case X86::CMOV_FR32:
8373 case X86::CMOV_FR64:
8374 case X86::CMOV_V4F32:
8375 case X86::CMOV_V2F64:
Chris Lattner84a67202009-09-02 05:57:00 +00008376 case X86::CMOV_V2I64:
Evan Cheng5f3a5402009-09-19 09:51:03 +00008377 return EmitLoweredSelect(MI, BB, EM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008378
8379 case X86::FP32_TO_INT16_IN_MEM:
8380 case X86::FP32_TO_INT32_IN_MEM:
8381 case X86::FP32_TO_INT64_IN_MEM:
8382 case X86::FP64_TO_INT16_IN_MEM:
8383 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008384 case X86::FP64_TO_INT64_IN_MEM:
8385 case X86::FP80_TO_INT16_IN_MEM:
8386 case X86::FP80_TO_INT32_IN_MEM:
8387 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner84a67202009-09-02 05:57:00 +00008388 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8389 DebugLoc DL = MI->getDebugLoc();
8390
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008391 // Change the floating point control register to use "round towards zero"
8392 // mode when truncating to an integer value.
8393 MachineFunction *F = BB->getParent();
David Greene6424ab92009-11-12 20:49:22 +00008394 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner84a67202009-09-02 05:57:00 +00008395 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008396
8397 // Load the old value of the high byte of the control word...
8398 unsigned OldCW =
Chris Lattner1b989192007-12-31 04:13:23 +00008399 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner84a67202009-09-02 05:57:00 +00008400 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008401 CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008402
8403 // Set the high part to be round to zero...
Chris Lattner84a67202009-09-02 05:57:00 +00008404 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008405 .addImm(0xC7F);
8406
8407 // Reload the modified control word now...
Chris Lattner84a67202009-09-02 05:57:00 +00008408 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008409
8410 // Restore the memory image of control word to original value
Chris Lattner84a67202009-09-02 05:57:00 +00008411 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008412 .addReg(OldCW);
8413
8414 // Get the X86 opcode to use.
8415 unsigned Opc;
8416 switch (MI->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00008417 default: llvm_unreachable("illegal opcode!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008418 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8419 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8420 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8421 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8422 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8423 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesen6d0e36a2007-08-07 01:17:37 +00008424 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8425 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8426 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008427 }
8428
8429 X86AddressMode AM;
8430 MachineOperand &Op = MI->getOperand(0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008431 if (Op.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008432 AM.BaseType = X86AddressMode::RegBase;
8433 AM.Base.Reg = Op.getReg();
8434 } else {
8435 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner6017d482007-12-30 23:10:15 +00008436 AM.Base.FrameIndex = Op.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008437 }
8438 Op = MI->getOperand(1);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008439 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008440 AM.Scale = Op.getImm();
8441 Op = MI->getOperand(2);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008442 if (Op.isImm())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008443 AM.IndexReg = Op.getImm();
8444 Op = MI->getOperand(3);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00008445 if (Op.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008446 AM.GV = Op.getGlobal();
8447 } else {
8448 AM.Disp = Op.getImm();
8449 }
Chris Lattner84a67202009-09-02 05:57:00 +00008450 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindolafee9c0f2009-04-08 08:09:33 +00008451 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008452
8453 // Reload the original control word now.
Chris Lattner84a67202009-09-02 05:57:00 +00008454 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008455
Dan Gohman221a4372008-07-07 23:14:23 +00008456 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008457 return BB;
8458 }
Eric Christopher22a39402009-08-18 22:50:32 +00008459 // String/text processing lowering.
8460 case X86::PCMPISTRM128REG:
8461 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8462 case X86::PCMPISTRM128MEM:
8463 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8464 case X86::PCMPESTRM128REG:
8465 return EmitPCMP(MI, BB, 5, false /* in mem */);
8466 case X86::PCMPESTRM128MEM:
8467 return EmitPCMP(MI, BB, 5, true /* in mem */);
8468
8469 // Atomic Lowering.
Mon P Wang078a62d2008-05-05 19:05:59 +00008470 case X86::ATOMAND32:
8471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008472 X86::AND32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008473 X86::LCMPXCHG32, X86::MOV32rr,
8474 X86::NOT32r, X86::EAX,
8475 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008476 case X86::ATOMOR32:
Scott Michel91099d62009-02-17 22:15:04 +00008477 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8478 X86::OR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008479 X86::LCMPXCHG32, X86::MOV32rr,
8480 X86::NOT32r, X86::EAX,
8481 X86::GR32RegisterClass);
Mon P Wang078a62d2008-05-05 19:05:59 +00008482 case X86::ATOMXOR32:
8483 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michel91099d62009-02-17 22:15:04 +00008484 X86::XOR32ri, X86::MOV32rm,
Dale Johannesend20e4452008-08-19 18:47:28 +00008485 X86::LCMPXCHG32, X86::MOV32rr,
8486 X86::NOT32r, X86::EAX,
8487 X86::GR32RegisterClass);
Andrew Lenharthaf02d592008-06-14 05:48:15 +00008488 case X86::ATOMNAND32:
8489 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008490 X86::AND32ri, X86::MOV32rm,
8491 X86::LCMPXCHG32, X86::MOV32rr,
8492 X86::NOT32r, X86::EAX,
8493 X86::GR32RegisterClass, true);
Mon P Wang078a62d2008-05-05 19:05:59 +00008494 case X86::ATOMMIN32:
8495 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8496 case X86::ATOMMAX32:
8497 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8498 case X86::ATOMUMIN32:
8499 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8500 case X86::ATOMUMAX32:
8501 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesend20e4452008-08-19 18:47:28 +00008502
8503 case X86::ATOMAND16:
8504 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8505 X86::AND16ri, X86::MOV16rm,
8506 X86::LCMPXCHG16, X86::MOV16rr,
8507 X86::NOT16r, X86::AX,
8508 X86::GR16RegisterClass);
8509 case X86::ATOMOR16:
Scott Michel91099d62009-02-17 22:15:04 +00008510 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008511 X86::OR16ri, X86::MOV16rm,
8512 X86::LCMPXCHG16, X86::MOV16rr,
8513 X86::NOT16r, X86::AX,
8514 X86::GR16RegisterClass);
8515 case X86::ATOMXOR16:
8516 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8517 X86::XOR16ri, X86::MOV16rm,
8518 X86::LCMPXCHG16, X86::MOV16rr,
8519 X86::NOT16r, X86::AX,
8520 X86::GR16RegisterClass);
8521 case X86::ATOMNAND16:
8522 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8523 X86::AND16ri, X86::MOV16rm,
8524 X86::LCMPXCHG16, X86::MOV16rr,
8525 X86::NOT16r, X86::AX,
8526 X86::GR16RegisterClass, true);
8527 case X86::ATOMMIN16:
8528 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8529 case X86::ATOMMAX16:
8530 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8531 case X86::ATOMUMIN16:
8532 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8533 case X86::ATOMUMAX16:
8534 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8535
8536 case X86::ATOMAND8:
8537 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8538 X86::AND8ri, X86::MOV8rm,
8539 X86::LCMPXCHG8, X86::MOV8rr,
8540 X86::NOT8r, X86::AL,
8541 X86::GR8RegisterClass);
8542 case X86::ATOMOR8:
Scott Michel91099d62009-02-17 22:15:04 +00008543 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesend20e4452008-08-19 18:47:28 +00008544 X86::OR8ri, X86::MOV8rm,
8545 X86::LCMPXCHG8, X86::MOV8rr,
8546 X86::NOT8r, X86::AL,
8547 X86::GR8RegisterClass);
8548 case X86::ATOMXOR8:
8549 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8550 X86::XOR8ri, X86::MOV8rm,
8551 X86::LCMPXCHG8, X86::MOV8rr,
8552 X86::NOT8r, X86::AL,
8553 X86::GR8RegisterClass);
8554 case X86::ATOMNAND8:
8555 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8556 X86::AND8ri, X86::MOV8rm,
8557 X86::LCMPXCHG8, X86::MOV8rr,
8558 X86::NOT8r, X86::AL,
8559 X86::GR8RegisterClass, true);
8560 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesenf160d802008-10-02 18:53:47 +00008561 // This group is for 64-bit host.
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008562 case X86::ATOMAND64:
8563 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008564 X86::AND64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008565 X86::LCMPXCHG64, X86::MOV64rr,
8566 X86::NOT64r, X86::RAX,
8567 X86::GR64RegisterClass);
8568 case X86::ATOMOR64:
Scott Michel91099d62009-02-17 22:15:04 +00008569 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8570 X86::OR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008571 X86::LCMPXCHG64, X86::MOV64rr,
8572 X86::NOT64r, X86::RAX,
8573 X86::GR64RegisterClass);
8574 case X86::ATOMXOR64:
8575 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michel91099d62009-02-17 22:15:04 +00008576 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesen6b60eca2008-08-20 00:48:50 +00008577 X86::LCMPXCHG64, X86::MOV64rr,
8578 X86::NOT64r, X86::RAX,
8579 X86::GR64RegisterClass);
8580 case X86::ATOMNAND64:
8581 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8582 X86::AND64ri32, X86::MOV64rm,
8583 X86::LCMPXCHG64, X86::MOV64rr,
8584 X86::NOT64r, X86::RAX,
8585 X86::GR64RegisterClass, true);
8586 case X86::ATOMMIN64:
8587 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8588 case X86::ATOMMAX64:
8589 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8590 case X86::ATOMUMIN64:
8591 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8592 case X86::ATOMUMAX64:
8593 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesenf160d802008-10-02 18:53:47 +00008594
8595 // This group does 64-bit operations on a 32-bit host.
8596 case X86::ATOMAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008597 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008598 X86::AND32rr, X86::AND32rr,
8599 X86::AND32ri, X86::AND32ri,
8600 false);
8601 case X86::ATOMOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008602 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008603 X86::OR32rr, X86::OR32rr,
8604 X86::OR32ri, X86::OR32ri,
8605 false);
8606 case X86::ATOMXOR6432:
Scott Michel91099d62009-02-17 22:15:04 +00008607 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008608 X86::XOR32rr, X86::XOR32rr,
8609 X86::XOR32ri, X86::XOR32ri,
8610 false);
8611 case X86::ATOMNAND6432:
Scott Michel91099d62009-02-17 22:15:04 +00008612 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008613 X86::AND32rr, X86::AND32rr,
8614 X86::AND32ri, X86::AND32ri,
8615 true);
Dale Johannesenf160d802008-10-02 18:53:47 +00008616 case X86::ATOMADD6432:
Scott Michel91099d62009-02-17 22:15:04 +00008617 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008618 X86::ADD32rr, X86::ADC32rr,
8619 X86::ADD32ri, X86::ADC32ri,
8620 false);
Dale Johannesenf160d802008-10-02 18:53:47 +00008621 case X86::ATOMSUB6432:
Scott Michel91099d62009-02-17 22:15:04 +00008622 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesenf160d802008-10-02 18:53:47 +00008623 X86::SUB32rr, X86::SBB32rr,
8624 X86::SUB32ri, X86::SBB32ri,
8625 false);
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008626 case X86::ATOMSWAP6432:
Scott Michel91099d62009-02-17 22:15:04 +00008627 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen51c58ee2008-10-03 22:25:52 +00008628 X86::MOV32rr, X86::MOV32rr,
8629 X86::MOV32ri, X86::MOV32ri,
8630 false);
Dan Gohman34228bf2009-08-15 01:38:56 +00008631 case X86::VASTART_SAVE_XMM_REGS:
8632 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008633 }
8634}
8635
8636//===----------------------------------------------------------------------===//
8637// X86 Optimization Hooks
8638//===----------------------------------------------------------------------===//
8639
Dan Gohman8181bd12008-07-27 21:46:04 +00008640void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohmand0dfc772008-02-13 22:28:48 +00008641 const APInt &Mask,
Dan Gohman229fa052008-02-13 00:35:47 +00008642 APInt &KnownZero,
8643 APInt &KnownOne,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008644 const SelectionDAG &DAG,
8645 unsigned Depth) const {
8646 unsigned Opc = Op.getOpcode();
8647 assert((Opc >= ISD::BUILTIN_OP_END ||
8648 Opc == ISD::INTRINSIC_WO_CHAIN ||
8649 Opc == ISD::INTRINSIC_W_CHAIN ||
8650 Opc == ISD::INTRINSIC_VOID) &&
8651 "Should use MaskedValueIsZero if you don't know whether Op"
8652 " is a target node!");
8653
Dan Gohman1d79e432008-02-13 23:07:24 +00008654 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008655 switch (Opc) {
8656 default: break;
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008657 case X86ISD::ADD:
8658 case X86ISD::SUB:
8659 case X86ISD::SMUL:
8660 case X86ISD::UMUL:
Dan Gohman99a12192009-03-04 19:44:21 +00008661 case X86ISD::INC:
8662 case X86ISD::DEC:
Dan Gohman12e03292009-09-18 19:59:53 +00008663 case X86ISD::OR:
8664 case X86ISD::XOR:
8665 case X86ISD::AND:
Evan Cheng8e9b21c2009-02-02 09:15:04 +00008666 // These nodes' second result is a boolean.
8667 if (Op.getResNo() == 0)
8668 break;
8669 // Fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008670 case X86ISD::SETCC:
Dan Gohman229fa052008-02-13 00:35:47 +00008671 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8672 Mask.getBitWidth() - 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008673 break;
8674 }
8675}
8676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008677/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengef7be082008-05-12 19:56:52 +00008678/// node is a GlobalAddress + offset.
8679bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8680 GlobalValue* &GA, int64_t &Offset) const{
8681 if (N->getOpcode() == X86ISD::Wrapper) {
8682 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008683 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman36322c72008-10-18 02:06:02 +00008684 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008685 return true;
8686 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008687 }
Evan Chengef7be082008-05-12 19:56:52 +00008688 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008689}
8690
Nate Begeman543d2142009-04-27 18:41:29 +00008691static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman3bab1f72009-09-23 21:02:20 +00008692 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008693 unsigned &LastLoadedElt,
Evan Chengef7be082008-05-12 19:56:52 +00008694 SelectionDAG &DAG, MachineFrameInfo *MFI,
8695 const TargetLowering &TLI) {
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008696 LDBase = NULL;
Anton Korobeynikova99a2862009-06-09 23:00:39 +00008697 LastLoadedElt = -1U;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008698 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman543d2142009-04-27 18:41:29 +00008699 if (N->getMaskElt(i) < 0) {
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008700 if (!LDBase)
Evan Cheng40ee6e52008-05-08 00:57:18 +00008701 return false;
8702 continue;
8703 }
8704
Dan Gohman8181bd12008-07-27 21:46:04 +00008705 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greif1c80d112008-08-28 21:40:38 +00008706 if (!Elt.getNode() ||
8707 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng40ee6e52008-05-08 00:57:18 +00008708 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008709 if (!LDBase) {
8710 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng92ee6822008-05-10 06:46:49 +00008711 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008712 LDBase = cast<LoadSDNode>(Elt.getNode());
8713 LastLoadedElt = i;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008714 continue;
8715 }
8716 if (Elt.getOpcode() == ISD::UNDEF)
8717 continue;
8718
Nate Begeman65e80032009-06-05 21:37:30 +00008719 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng1a029cb2009-12-09 01:36:00 +00008720 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng40ee6e52008-05-08 00:57:18 +00008721 return false;
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008722 LastLoadedElt = i;
Evan Cheng40ee6e52008-05-08 00:57:18 +00008723 }
8724 return true;
8725}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008726
8727/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8728/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8729/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang6e30ad02009-04-03 02:43:30 +00008730/// order. In the case of v2i64, it will see if it can rewrite the
8731/// shuffle to be an appropriate build vector so it can take advantage of
8732// performBuildVectorCombine.
Dan Gohman8181bd12008-07-27 21:46:04 +00008733static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman543d2142009-04-27 18:41:29 +00008734 const TargetLowering &TLI) {
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008735 DebugLoc dl = N->getDebugLoc();
Owen Andersonac9de032009-08-10 22:56:29 +00008736 EVT VT = N->getValueType(0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00008737 EVT EltVT = VT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00008738 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8739 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang6e30ad02009-04-03 02:43:30 +00008740
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008741 if (VT.getSizeInBits() != 128)
8742 return SDValue();
8743
Mon P Wang6e30ad02009-04-03 02:43:30 +00008744 // Try to combine a vector_shuffle into a 128-bit load.
8745 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008746 LoadSDNode *LD = NULL;
8747 unsigned LastLoadedElt;
Dan Gohman3bab1f72009-09-23 21:02:20 +00008748 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008749 MFI, TLI))
Dan Gohman8181bd12008-07-27 21:46:04 +00008750 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008751
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008752 if (LastLoadedElt == NumElems - 1) {
Evan Cheng76ebe862009-12-09 01:53:58 +00008753 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008754 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8755 LD->getSrcValue(), LD->getSrcValueOffset(),
8756 LD->isVolatile());
Dale Johannesen0db52dd2009-02-03 20:21:25 +00008757 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michel91099d62009-02-17 22:15:04 +00008758 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedmane6bb1e52009-06-07 06:52:44 +00008759 LD->isVolatile(), LD->getAlignment());
8760 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008761 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begeman65e80032009-06-05 21:37:30 +00008762 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8763 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begeman65e80032009-06-05 21:37:30 +00008764 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8765 }
8766 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00008767}
Evan Chenge9b9c672008-05-09 21:53:03 +00008768
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008769/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00008770static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner472f1d52009-03-11 05:48:52 +00008771 const X86Subtarget *Subtarget) {
8772 DebugLoc DL = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00008773 SDValue Cond = N->getOperand(0);
Chris Lattner472f1d52009-03-11 05:48:52 +00008774 // Get the LHS/RHS of the select.
8775 SDValue LHS = N->getOperand(1);
8776 SDValue RHS = N->getOperand(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008777
Dan Gohman19488552009-09-21 18:03:22 +00008778 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8779 // instructions have the peculiarity that if either operand is a NaN,
8780 // they chose what we call the RHS operand (and as such are not symmetric).
8781 // It happens that this matches the semantics of the common C idiom
8782 // x<y?x:y and related forms, so we can recognize these cases.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008783 if (Subtarget->hasSSE2() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008784 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner472f1d52009-03-11 05:48:52 +00008785 Cond.getOpcode() == ISD::SETCC) {
8786 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008787
Chris Lattner472f1d52009-03-11 05:48:52 +00008788 unsigned Opcode = 0;
Dan Gohman19488552009-09-21 18:03:22 +00008789 // Check for x CC y ? x : y.
Chris Lattner472f1d52009-03-11 05:48:52 +00008790 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8791 switch (CC) {
8792 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00008793 case ISD::SETULT:
8794 // This can be a min if we can prove that at least one of the operands
8795 // is not a nan.
8796 if (!FiniteOnlyFPMath()) {
8797 if (DAG.isKnownNeverNaN(RHS)) {
8798 // Put the potential NaN in the RHS so that SSE will preserve it.
8799 std::swap(LHS, RHS);
8800 } else if (!DAG.isKnownNeverNaN(LHS))
8801 break;
8802 }
8803 Opcode = X86ISD::FMIN;
8804 break;
8805 case ISD::SETOLE:
8806 // This can be a min if we can prove that at least one of the operands
8807 // is not a nan.
8808 if (!FiniteOnlyFPMath()) {
8809 if (DAG.isKnownNeverNaN(LHS)) {
8810 // Put the potential NaN in the RHS so that SSE will preserve it.
8811 std::swap(LHS, RHS);
8812 } else if (!DAG.isKnownNeverNaN(RHS))
8813 break;
8814 }
8815 Opcode = X86ISD::FMIN;
8816 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00008817 case ISD::SETULE:
Dan Gohman19488552009-09-21 18:03:22 +00008818 // This can be a min, but if either operand is a NaN we need it to
8819 // preserve the original LHS.
8820 std::swap(LHS, RHS);
8821 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008822 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00008823 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00008824 Opcode = X86ISD::FMIN;
8825 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008826
Dan Gohman19488552009-09-21 18:03:22 +00008827 case ISD::SETOGE:
8828 // This can be a max if we can prove that at least one of the operands
8829 // is not a nan.
8830 if (!FiniteOnlyFPMath()) {
8831 if (DAG.isKnownNeverNaN(LHS)) {
8832 // Put the potential NaN in the RHS so that SSE will preserve it.
8833 std::swap(LHS, RHS);
8834 } else if (!DAG.isKnownNeverNaN(RHS))
8835 break;
8836 }
8837 Opcode = X86ISD::FMAX;
8838 break;
Chris Lattner472f1d52009-03-11 05:48:52 +00008839 case ISD::SETUGT:
Dan Gohman19488552009-09-21 18:03:22 +00008840 // This can be a max if we can prove that at least one of the operands
8841 // is not a nan.
8842 if (!FiniteOnlyFPMath()) {
8843 if (DAG.isKnownNeverNaN(RHS)) {
8844 // Put the potential NaN in the RHS so that SSE will preserve it.
8845 std::swap(LHS, RHS);
8846 } else if (!DAG.isKnownNeverNaN(LHS))
8847 break;
8848 }
8849 Opcode = X86ISD::FMAX;
8850 break;
8851 case ISD::SETUGE:
8852 // This can be a max, but if either operand is a NaN we need it to
8853 // preserve the original LHS.
8854 std::swap(LHS, RHS);
8855 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008856 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008857 case ISD::SETGE:
8858 Opcode = X86ISD::FMAX;
8859 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008860 }
Dan Gohman19488552009-09-21 18:03:22 +00008861 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner472f1d52009-03-11 05:48:52 +00008862 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8863 switch (CC) {
8864 default: break;
Dan Gohman19488552009-09-21 18:03:22 +00008865 case ISD::SETOGE:
8866 // This can be a min if we can prove that at least one of the operands
8867 // is not a nan.
8868 if (!FiniteOnlyFPMath()) {
8869 if (DAG.isKnownNeverNaN(RHS)) {
8870 // Put the potential NaN in the RHS so that SSE will preserve it.
8871 std::swap(LHS, RHS);
8872 } else if (!DAG.isKnownNeverNaN(LHS))
8873 break;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008874 }
Dan Gohman19488552009-09-21 18:03:22 +00008875 Opcode = X86ISD::FMIN;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008876 break;
Dan Gohman19488552009-09-21 18:03:22 +00008877 case ISD::SETUGT:
8878 // This can be a min if we can prove that at least one of the operands
8879 // is not a nan.
8880 if (!FiniteOnlyFPMath()) {
8881 if (DAG.isKnownNeverNaN(LHS)) {
8882 // Put the potential NaN in the RHS so that SSE will preserve it.
8883 std::swap(LHS, RHS);
8884 } else if (!DAG.isKnownNeverNaN(RHS))
8885 break;
8886 }
8887 Opcode = X86ISD::FMIN;
8888 break;
8889 case ISD::SETUGE:
8890 // This can be a min, but if either operand is a NaN we need it to
8891 // preserve the original LHS.
8892 std::swap(LHS, RHS);
8893 case ISD::SETOGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008894 case ISD::SETGT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008895 case ISD::SETGE:
8896 Opcode = X86ISD::FMIN;
8897 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008898
Dan Gohman19488552009-09-21 18:03:22 +00008899 case ISD::SETULT:
8900 // This can be a max if we can prove that at least one of the operands
8901 // is not a nan.
8902 if (!FiniteOnlyFPMath()) {
8903 if (DAG.isKnownNeverNaN(LHS)) {
8904 // Put the potential NaN in the RHS so that SSE will preserve it.
8905 std::swap(LHS, RHS);
8906 } else if (!DAG.isKnownNeverNaN(RHS))
8907 break;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008908 }
Dan Gohman19488552009-09-21 18:03:22 +00008909 Opcode = X86ISD::FMAX;
Dan Gohman41b3f4a2009-09-03 20:34:31 +00008910 break;
Dan Gohman19488552009-09-21 18:03:22 +00008911 case ISD::SETOLE:
8912 // This can be a max if we can prove that at least one of the operands
8913 // is not a nan.
8914 if (!FiniteOnlyFPMath()) {
8915 if (DAG.isKnownNeverNaN(RHS)) {
8916 // Put the potential NaN in the RHS so that SSE will preserve it.
8917 std::swap(LHS, RHS);
8918 } else if (!DAG.isKnownNeverNaN(LHS))
8919 break;
8920 }
8921 Opcode = X86ISD::FMAX;
8922 break;
8923 case ISD::SETULE:
8924 // This can be a max, but if either operand is a NaN we need it to
8925 // preserve the original LHS.
8926 std::swap(LHS, RHS);
8927 case ISD::SETOLT:
Chris Lattner472f1d52009-03-11 05:48:52 +00008928 case ISD::SETLT:
Dan Gohman19488552009-09-21 18:03:22 +00008929 case ISD::SETLE:
Chris Lattner472f1d52009-03-11 05:48:52 +00008930 Opcode = X86ISD::FMAX;
8931 break;
8932 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008933 }
8934
Chris Lattner472f1d52009-03-11 05:48:52 +00008935 if (Opcode)
8936 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00008937 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008938
Chris Lattnere4577dc2009-03-12 06:52:53 +00008939 // If this is a select between two integer constants, try to do some
8940 // optimizations.
Chris Lattnera054e842009-03-13 05:53:31 +00008941 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8942 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnere4577dc2009-03-12 06:52:53 +00008943 // Don't do this for crazy integer types.
8944 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8945 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnera054e842009-03-13 05:53:31 +00008946 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnere4577dc2009-03-12 06:52:53 +00008947 bool NeedsCondInvert = false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008948
Chris Lattnera054e842009-03-13 05:53:31 +00008949 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnere4577dc2009-03-12 06:52:53 +00008950 // Efficiently invertible.
8951 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8952 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8953 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8954 NeedsCondInvert = true;
Chris Lattnera054e842009-03-13 05:53:31 +00008955 std::swap(TrueC, FalseC);
Chris Lattnere4577dc2009-03-12 06:52:53 +00008956 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008957
Chris Lattnere4577dc2009-03-12 06:52:53 +00008958 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00008959 if (FalseC->getAPIntValue() == 0 &&
8960 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnere4577dc2009-03-12 06:52:53 +00008961 if (NeedsCondInvert) // Invert the condition if needed.
8962 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8963 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008964
Chris Lattnere4577dc2009-03-12 06:52:53 +00008965 // Zero extend the condition if needed.
8966 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008967
Chris Lattnera054e842009-03-13 05:53:31 +00008968 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnere4577dc2009-03-12 06:52:53 +00008969 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008970 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00008971 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008972
Chris Lattner938d6652009-03-13 05:22:11 +00008973 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnera054e842009-03-13 05:53:31 +00008974 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner938d6652009-03-13 05:22:11 +00008975 if (NeedsCondInvert) // Invert the condition if needed.
8976 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8977 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008978
Chris Lattner938d6652009-03-13 05:22:11 +00008979 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00008980 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8981 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00008982 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnera054e842009-03-13 05:53:31 +00008983 SDValue(FalseC, 0));
Chris Lattner938d6652009-03-13 05:22:11 +00008984 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008985
Chris Lattnera054e842009-03-13 05:53:31 +00008986 // Optimize cases that will turn into an LEA instruction. This requires
8987 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008988 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00008989 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00008990 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00008991
Chris Lattnera054e842009-03-13 05:53:31 +00008992 bool isFastMultiplier = false;
8993 if (Diff < 10) {
8994 switch ((unsigned char)Diff) {
8995 default: break;
8996 case 1: // result = add base, cond
8997 case 2: // result = lea base( , cond*2)
8998 case 3: // result = lea base(cond, cond*2)
8999 case 4: // result = lea base( , cond*4)
9000 case 5: // result = lea base(cond, cond*4)
9001 case 8: // result = lea base( , cond*8)
9002 case 9: // result = lea base(cond, cond*8)
9003 isFastMultiplier = true;
9004 break;
9005 }
9006 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009007
Chris Lattnera054e842009-03-13 05:53:31 +00009008 if (isFastMultiplier) {
9009 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9010 if (NeedsCondInvert) // Invert the condition if needed.
9011 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9012 DAG.getConstant(1, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009013
Chris Lattnera054e842009-03-13 05:53:31 +00009014 // Zero extend the condition if needed.
9015 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9016 Cond);
9017 // Scale the condition by the difference.
9018 if (Diff != 1)
9019 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9020 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009021
Chris Lattnera054e842009-03-13 05:53:31 +00009022 // Add the base if non-zero.
9023 if (FalseC->getAPIntValue() != 0)
9024 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9025 SDValue(FalseC, 0));
9026 return Cond;
9027 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009028 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00009029 }
9030 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009031
Dan Gohman8181bd12008-07-27 21:46:04 +00009032 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009033}
9034
Chris Lattnere4577dc2009-03-12 06:52:53 +00009035/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9036static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9037 TargetLowering::DAGCombinerInfo &DCI) {
9038 DebugLoc DL = N->getDebugLoc();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009039
Chris Lattnere4577dc2009-03-12 06:52:53 +00009040 // If the flag operand isn't dead, don't touch this CMOV.
9041 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9042 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009043
Chris Lattnere4577dc2009-03-12 06:52:53 +00009044 // If this is a select between two integer constants, try to do some
9045 // optimizations. Note that the operands are ordered the opposite of SELECT
9046 // operands.
9047 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9048 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9049 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9050 // larger than FalseC (the false value).
9051 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009052
Chris Lattnere4577dc2009-03-12 06:52:53 +00009053 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9054 CC = X86::GetOppositeBranchCondition(CC);
9055 std::swap(TrueC, FalseC);
9056 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009057
Chris Lattnere4577dc2009-03-12 06:52:53 +00009058 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnera054e842009-03-13 05:53:31 +00009059 // This is efficient for any integer data type (including i8/i16) and
9060 // shift amount.
Chris Lattnere4577dc2009-03-12 06:52:53 +00009061 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9062 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009063 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9064 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009065
Chris Lattnere4577dc2009-03-12 06:52:53 +00009066 // Zero extend the condition if needed.
9067 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009068
Chris Lattnere4577dc2009-03-12 06:52:53 +00009069 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9070 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009071 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnere4577dc2009-03-12 06:52:53 +00009072 if (N->getNumValues() == 2) // Dead flag value?
9073 return DCI.CombineTo(N, Cond, SDValue());
9074 return Cond;
9075 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009076
Chris Lattnera054e842009-03-13 05:53:31 +00009077 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9078 // for any integer data type, including i8/i16.
Chris Lattner938d6652009-03-13 05:22:11 +00009079 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9080 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009081 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9082 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009083
Chris Lattner938d6652009-03-13 05:22:11 +00009084 // Zero extend the condition if needed.
Chris Lattnera054e842009-03-13 05:53:31 +00009085 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9086 FalseC->getValueType(0), Cond);
Chris Lattner938d6652009-03-13 05:22:11 +00009087 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9088 SDValue(FalseC, 0));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009089
Chris Lattner938d6652009-03-13 05:22:11 +00009090 if (N->getNumValues() == 2) // Dead flag value?
9091 return DCI.CombineTo(N, Cond, SDValue());
9092 return Cond;
9093 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009094
Chris Lattnera054e842009-03-13 05:53:31 +00009095 // Optimize cases that will turn into an LEA instruction. This requires
9096 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009097 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnera054e842009-03-13 05:53:31 +00009098 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009099 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009100
Chris Lattnera054e842009-03-13 05:53:31 +00009101 bool isFastMultiplier = false;
9102 if (Diff < 10) {
9103 switch ((unsigned char)Diff) {
9104 default: break;
9105 case 1: // result = add base, cond
9106 case 2: // result = lea base( , cond*2)
9107 case 3: // result = lea base(cond, cond*2)
9108 case 4: // result = lea base( , cond*4)
9109 case 5: // result = lea base(cond, cond*4)
9110 case 8: // result = lea base( , cond*8)
9111 case 9: // result = lea base(cond, cond*8)
9112 isFastMultiplier = true;
9113 break;
9114 }
9115 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009116
Chris Lattnera054e842009-03-13 05:53:31 +00009117 if (isFastMultiplier) {
9118 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9119 SDValue Cond = N->getOperand(3);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009120 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9121 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnera054e842009-03-13 05:53:31 +00009122 // Zero extend the condition if needed.
9123 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9124 Cond);
9125 // Scale the condition by the difference.
9126 if (Diff != 1)
9127 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9128 DAG.getConstant(Diff, Cond.getValueType()));
9129
9130 // Add the base if non-zero.
9131 if (FalseC->getAPIntValue() != 0)
9132 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9133 SDValue(FalseC, 0));
9134 if (N->getNumValues() == 2) // Dead flag value?
9135 return DCI.CombineTo(N, Cond, SDValue());
9136 return Cond;
9137 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009138 }
Chris Lattnere4577dc2009-03-12 06:52:53 +00009139 }
9140 }
9141 return SDValue();
9142}
9143
9144
Evan Cheng04ecee12009-03-28 05:57:29 +00009145/// PerformMulCombine - Optimize a single multiply with constant into two
9146/// in order to implement it with two cheaper instructions, e.g.
9147/// LEA + SHL, LEA + LEA.
9148static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9149 TargetLowering::DAGCombinerInfo &DCI) {
9150 if (DAG.getMachineFunction().
9151 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9152 return SDValue();
9153
9154 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9155 return SDValue();
9156
Owen Andersonac9de032009-08-10 22:56:29 +00009157 EVT VT = N->getValueType(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009158 if (VT != MVT::i64)
Evan Cheng04ecee12009-03-28 05:57:29 +00009159 return SDValue();
9160
9161 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9162 if (!C)
9163 return SDValue();
9164 uint64_t MulAmt = C->getZExtValue();
9165 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9166 return SDValue();
9167
9168 uint64_t MulAmt1 = 0;
9169 uint64_t MulAmt2 = 0;
9170 if ((MulAmt % 9) == 0) {
9171 MulAmt1 = 9;
9172 MulAmt2 = MulAmt / 9;
9173 } else if ((MulAmt % 5) == 0) {
9174 MulAmt1 = 5;
9175 MulAmt2 = MulAmt / 5;
9176 } else if ((MulAmt % 3) == 0) {
9177 MulAmt1 = 3;
9178 MulAmt2 = MulAmt / 3;
9179 }
9180 if (MulAmt2 &&
9181 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9182 DebugLoc DL = N->getDebugLoc();
9183
9184 if (isPowerOf2_64(MulAmt2) &&
9185 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9186 // If second multiplifer is pow2, issue it first. We want the multiply by
9187 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9188 // is an add.
9189 std::swap(MulAmt1, MulAmt2);
9190
9191 SDValue NewMul;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009192 if (isPowerOf2_64(MulAmt1))
Evan Cheng04ecee12009-03-28 05:57:29 +00009193 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009194 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng04ecee12009-03-28 05:57:29 +00009195 else
Evan Chengc3495762009-03-30 21:36:47 +00009196 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng04ecee12009-03-28 05:57:29 +00009197 DAG.getConstant(MulAmt1, VT));
9198
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009199 if (isPowerOf2_64(MulAmt2))
Evan Cheng04ecee12009-03-28 05:57:29 +00009200 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009201 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009202 else
Evan Chengc3495762009-03-30 21:36:47 +00009203 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng04ecee12009-03-28 05:57:29 +00009204 DAG.getConstant(MulAmt2, VT));
9205
9206 // Do not add new nodes to DAG combiner worklist.
9207 DCI.CombineTo(N, NewMul, false);
9208 }
9209 return SDValue();
9210}
9211
Evan Cheng834ae6b2009-12-15 00:53:42 +00009212static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9213 SDValue N0 = N->getOperand(0);
9214 SDValue N1 = N->getOperand(1);
9215 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9216 EVT VT = N0.getValueType();
9217
9218 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9219 // since the result of setcc_c is all zero's or all ones.
9220 if (N1C && N0.getOpcode() == ISD::AND &&
9221 N0.getOperand(1).getOpcode() == ISD::Constant) {
9222 SDValue N00 = N0.getOperand(0);
9223 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9224 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9225 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9226 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9227 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9228 APInt ShAmt = N1C->getAPIntValue();
9229 Mask = Mask.shl(ShAmt);
9230 if (Mask != 0)
9231 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9232 N00, DAG.getConstant(Mask, VT));
9233 }
9234 }
9235
9236 return SDValue();
9237}
Evan Cheng04ecee12009-03-28 05:57:29 +00009238
sampo025b75c2009-01-26 00:52:55 +00009239/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9240/// when possible.
9241static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9242 const X86Subtarget *Subtarget) {
Evan Cheng834ae6b2009-12-15 00:53:42 +00009243 EVT VT = N->getValueType(0);
9244 if (!VT.isVector() && VT.isInteger() &&
9245 N->getOpcode() == ISD::SHL)
9246 return PerformSHLCombine(N, DAG);
9247
sampo025b75c2009-01-26 00:52:55 +00009248 // On X86 with SSE2 support, we can transform this to a vector shift if
9249 // all elements are shifted by the same amount. We can't do this in legalize
9250 // because the a constant vector is typically transformed to a constant pool
9251 // so we have no knowledge of the shift amount.
sampo087d53c2009-01-26 03:15:31 +00009252 if (!Subtarget->hasSSE2())
9253 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009254
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009255 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
sampo087d53c2009-01-26 03:15:31 +00009256 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00009257
Mon P Wanga91e9642009-01-28 08:12:05 +00009258 SDValue ShAmtOp = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00009259 EVT EltVT = VT.getVectorElementType();
Chris Lattner472f1d52009-03-11 05:48:52 +00009260 DebugLoc DL = N->getDebugLoc();
Mon P Wang04c767e2009-09-03 19:56:25 +00009261 SDValue BaseShAmt = SDValue();
Mon P Wanga91e9642009-01-28 08:12:05 +00009262 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9263 unsigned NumElts = VT.getVectorNumElements();
9264 unsigned i = 0;
9265 for (; i != NumElts; ++i) {
9266 SDValue Arg = ShAmtOp.getOperand(i);
9267 if (Arg.getOpcode() == ISD::UNDEF) continue;
9268 BaseShAmt = Arg;
9269 break;
9270 }
9271 for (; i != NumElts; ++i) {
9272 SDValue Arg = ShAmtOp.getOperand(i);
9273 if (Arg.getOpcode() == ISD::UNDEF) continue;
9274 if (Arg != BaseShAmt) {
9275 return SDValue();
9276 }
9277 }
9278 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman543d2142009-04-27 18:41:29 +00009279 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wang04c767e2009-09-03 19:56:25 +00009280 SDValue InVec = ShAmtOp.getOperand(0);
9281 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9282 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9283 unsigned i = 0;
9284 for (; i != NumElts; ++i) {
9285 SDValue Arg = InVec.getOperand(i);
9286 if (Arg.getOpcode() == ISD::UNDEF) continue;
9287 BaseShAmt = Arg;
9288 break;
9289 }
9290 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9291 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9292 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9293 if (C->getZExtValue() == SplatIdx)
9294 BaseShAmt = InVec.getOperand(1);
9295 }
9296 }
9297 if (BaseShAmt.getNode() == 0)
9298 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9299 DAG.getIntPtrConstant(0));
Mon P Wanga91e9642009-01-28 08:12:05 +00009300 } else
sampo087d53c2009-01-26 03:15:31 +00009301 return SDValue();
sampo025b75c2009-01-26 00:52:55 +00009302
Mon P Wang04c767e2009-09-03 19:56:25 +00009303 // The shift amount is an i32.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009304 if (EltVT.bitsGT(MVT::i32))
9305 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9306 else if (EltVT.bitsLT(MVT::i32))
Mon P Wang04c767e2009-09-03 19:56:25 +00009307 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
sampo025b75c2009-01-26 00:52:55 +00009308
sampo087d53c2009-01-26 03:15:31 +00009309 // The shift amount is identical so we can do a vector shift.
9310 SDValue ValOp = N->getOperand(0);
9311 switch (N->getOpcode()) {
9312 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00009313 llvm_unreachable("Unknown shift opcode!");
sampo087d53c2009-01-26 03:15:31 +00009314 break;
9315 case ISD::SHL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009316 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009317 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009318 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009319 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009320 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009321 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009322 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009323 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009324 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009325 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009326 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009327 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009328 break;
9329 case ISD::SRA:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009330 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009331 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009332 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009333 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009334 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009335 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009336 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009337 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009338 break;
9339 case ISD::SRL:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009340 if (VT == MVT::v2i64)
Chris Lattner472f1d52009-03-11 05:48:52 +00009341 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009342 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009343 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009344 if (VT == MVT::v4i32)
Chris Lattner472f1d52009-03-11 05:48:52 +00009345 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009346 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009347 ValOp, BaseShAmt);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009348 if (VT == MVT::v8i16)
Chris Lattner472f1d52009-03-11 05:48:52 +00009349 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009350 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
sampo025b75c2009-01-26 00:52:55 +00009351 ValOp, BaseShAmt);
sampo087d53c2009-01-26 03:15:31 +00009352 break;
sampo025b75c2009-01-26 00:52:55 +00009353 }
9354 return SDValue();
9355}
9356
Evan Cheng10957b82010-01-04 21:22:48 +00009357static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9358 const X86Subtarget *Subtarget) {
9359 EVT VT = N->getValueType(0);
9360 if (VT != MVT::i64 || !Subtarget->is64Bit())
9361 return SDValue();
9362
9363 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9364 SDValue N0 = N->getOperand(0);
9365 SDValue N1 = N->getOperand(1);
9366 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9367 std::swap(N0, N1);
9368 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9369 return SDValue();
9370
9371 SDValue ShAmt0 = N0.getOperand(1);
9372 if (ShAmt0.getValueType() != MVT::i8)
9373 return SDValue();
9374 SDValue ShAmt1 = N1.getOperand(1);
9375 if (ShAmt1.getValueType() != MVT::i8)
9376 return SDValue();
9377 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9378 ShAmt0 = ShAmt0.getOperand(0);
9379 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9380 ShAmt1 = ShAmt1.getOperand(0);
9381
9382 DebugLoc DL = N->getDebugLoc();
9383 unsigned Opc = X86ISD::SHLD;
9384 SDValue Op0 = N0.getOperand(0);
9385 SDValue Op1 = N1.getOperand(0);
9386 if (ShAmt0.getOpcode() == ISD::SUB) {
9387 Opc = X86ISD::SHRD;
9388 std::swap(Op0, Op1);
9389 std::swap(ShAmt0, ShAmt1);
9390 }
9391
9392 if (ShAmt1.getOpcode() == ISD::SUB) {
9393 SDValue Sum = ShAmt1.getOperand(0);
9394 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9395 if (SumC->getSExtValue() == 64 &&
9396 ShAmt1.getOperand(1) == ShAmt0)
9397 return DAG.getNode(Opc, DL, VT,
9398 Op0, Op1,
9399 DAG.getNode(ISD::TRUNCATE, DL,
9400 MVT::i8, ShAmt0));
9401 }
9402 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9403 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9404 if (ShAmt0C &&
9405 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9406 return DAG.getNode(Opc, DL, VT,
9407 N0.getOperand(0), N1.getOperand(0),
9408 DAG.getNode(ISD::TRUNCATE, DL,
9409 MVT::i8, ShAmt0));
9410 }
9411
9412 return SDValue();
9413}
9414
Chris Lattnerce84ae42008-02-22 02:09:43 +00009415/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009416static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Chengc944c5d2009-03-12 05:59:15 +00009417 const X86Subtarget *Subtarget) {
Chris Lattnerce84ae42008-02-22 02:09:43 +00009418 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9419 // the FP state in cases where an emms may be missing.
Dale Johannesend112b802008-02-25 19:20:14 +00009420 // A preferable solution to the general problem is to figure out the right
9421 // places to insert EMMS. This qualifies as a quick hack.
Evan Chengc944c5d2009-03-12 05:59:15 +00009422
9423 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng40ee6e52008-05-08 00:57:18 +00009424 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersonac9de032009-08-10 22:56:29 +00009425 EVT VT = St->getValue().getValueType();
Evan Chengc944c5d2009-03-12 05:59:15 +00009426 if (VT.getSizeInBits() != 64)
9427 return SDValue();
9428
Devang Patelc386c842009-06-05 21:57:13 +00009429 const Function *F = DAG.getMachineFunction().getFunction();
9430 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009431 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patelc386c842009-06-05 21:57:13 +00009432 && Subtarget->hasSSE2();
Evan Chengc944c5d2009-03-12 05:59:15 +00009433 if ((VT.isVector() ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009434 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesend112b802008-02-25 19:20:14 +00009435 isa<LoadSDNode>(St->getValue()) &&
9436 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9437 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009438 SDNode* LdVal = St->getValue().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009439 LoadSDNode *Ld = 0;
9440 int TokenFactorIndex = -1;
Dan Gohman8181bd12008-07-27 21:46:04 +00009441 SmallVector<SDValue, 8> Ops;
Gabor Greif1c80d112008-08-28 21:40:38 +00009442 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesend112b802008-02-25 19:20:14 +00009443 // Must be a store of a load. We currently handle two cases: the load
9444 // is a direct child, and it's under an intervening TokenFactor. It is
9445 // possible to dig deeper under nested TokenFactors.
Dale Johannesen49151bc2008-02-25 22:29:22 +00009446 if (ChainVal == LdVal)
Dale Johannesend112b802008-02-25 19:20:14 +00009447 Ld = cast<LoadSDNode>(St->getChain());
9448 else if (St->getValue().hasOneUse() &&
9449 ChainVal->getOpcode() == ISD::TokenFactor) {
9450 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greif1c80d112008-08-28 21:40:38 +00009451 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesend112b802008-02-25 19:20:14 +00009452 TokenFactorIndex = i;
9453 Ld = cast<LoadSDNode>(St->getValue());
9454 } else
9455 Ops.push_back(ChainVal->getOperand(i));
9456 }
9457 }
Dale Johannesend112b802008-02-25 19:20:14 +00009458
Evan Chengc944c5d2009-03-12 05:59:15 +00009459 if (!Ld || !ISD::isNormalLoad(Ld))
9460 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009461
Evan Chengc944c5d2009-03-12 05:59:15 +00009462 // If this is not the MMX case, i.e. we are just turning i64 load/store
9463 // into f64 load/store, avoid the transformation if there are multiple
9464 // uses of the loaded value.
9465 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9466 return SDValue();
Dale Johannesend112b802008-02-25 19:20:14 +00009467
Evan Chengc944c5d2009-03-12 05:59:15 +00009468 DebugLoc LdDL = Ld->getDebugLoc();
9469 DebugLoc StDL = N->getDebugLoc();
9470 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9471 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9472 // pair instead.
9473 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009474 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Chengc944c5d2009-03-12 05:59:15 +00009475 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9476 Ld->getBasePtr(), Ld->getSrcValue(),
9477 Ld->getSrcValueOffset(), Ld->isVolatile(),
9478 Ld->getAlignment());
9479 SDValue NewChain = NewLd.getValue(1);
Dale Johannesend112b802008-02-25 19:20:14 +00009480 if (TokenFactorIndex != -1) {
Evan Chengc944c5d2009-03-12 05:59:15 +00009481 Ops.push_back(NewChain);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009482 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesend112b802008-02-25 19:20:14 +00009483 Ops.size());
9484 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009485 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattnerce84ae42008-02-22 02:09:43 +00009486 St->getSrcValue(), St->getSrcValueOffset(),
9487 St->isVolatile(), St->getAlignment());
9488 }
Evan Chengc944c5d2009-03-12 05:59:15 +00009489
9490 // Otherwise, lower to two pairs of 32-bit loads / stores.
9491 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009492 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9493 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009494
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009495 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009496 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9497 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009498 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Chengc944c5d2009-03-12 05:59:15 +00009499 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9500 Ld->isVolatile(),
9501 MinAlign(Ld->getAlignment(), 4));
9502
9503 SDValue NewChain = LoLd.getValue(1);
9504 if (TokenFactorIndex != -1) {
9505 Ops.push_back(LoLd);
9506 Ops.push_back(HiLd);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009507 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Chengc944c5d2009-03-12 05:59:15 +00009508 Ops.size());
9509 }
9510
9511 LoAddr = St->getBasePtr();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009512 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9513 DAG.getConstant(4, MVT::i32));
Evan Chengc944c5d2009-03-12 05:59:15 +00009514
9515 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9516 St->getSrcValue(), St->getSrcValueOffset(),
9517 St->isVolatile(), St->getAlignment());
9518 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9519 St->getSrcValue(),
9520 St->getSrcValueOffset() + 4,
9521 St->isVolatile(),
9522 MinAlign(St->getAlignment(), 4));
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009523 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattnerce84ae42008-02-22 02:09:43 +00009524 }
Dan Gohman8181bd12008-07-27 21:46:04 +00009525 return SDValue();
Chris Lattnerce84ae42008-02-22 02:09:43 +00009526}
9527
Chris Lattner470d5dc2008-01-25 06:14:17 +00009528/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9529/// X86ISD::FXOR nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009530static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner470d5dc2008-01-25 06:14:17 +00009531 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9532 // F[X]OR(0.0, x) -> x
9533 // F[X]OR(x, 0.0) -> x
Chris Lattnerf82998f2008-01-25 05:46:26 +00009534 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9535 if (C->getValueAPF().isPosZero())
9536 return N->getOperand(1);
9537 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9538 if (C->getValueAPF().isPosZero())
9539 return N->getOperand(0);
Dan Gohman8181bd12008-07-27 21:46:04 +00009540 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009541}
9542
9543/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00009544static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattnerf82998f2008-01-25 05:46:26 +00009545 // FAND(0.0, x) -> 0.0
9546 // FAND(x, 0.0) -> 0.0
9547 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9548 if (C->getValueAPF().isPosZero())
9549 return N->getOperand(0);
9550 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9551 if (C->getValueAPF().isPosZero())
9552 return N->getOperand(1);
Dan Gohman8181bd12008-07-27 21:46:04 +00009553 return SDValue();
Chris Lattnerf82998f2008-01-25 05:46:26 +00009554}
9555
Dan Gohman22cefb02009-01-29 01:59:02 +00009556static SDValue PerformBTCombine(SDNode *N,
9557 SelectionDAG &DAG,
9558 TargetLowering::DAGCombinerInfo &DCI) {
9559 // BT ignores high bits in the bit index operand.
9560 SDValue Op1 = N->getOperand(1);
9561 if (Op1.hasOneUse()) {
9562 unsigned BitWidth = Op1.getValueSizeInBits();
9563 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9564 APInt KnownZero, KnownOne;
9565 TargetLowering::TargetLoweringOpt TLO(DAG);
9566 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9567 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9568 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9569 DCI.CommitTargetLoweringOpt(TLO);
9570 }
9571 return SDValue();
9572}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009573
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009574static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9575 SDValue Op = N->getOperand(0);
9576 if (Op.getOpcode() == ISD::BIT_CONVERT)
9577 Op = Op.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00009578 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009579 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009580 VT.getVectorElementType().getSizeInBits() ==
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009581 OpVT.getVectorElementType().getSizeInBits()) {
9582 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9583 }
9584 return SDValue();
9585}
9586
Owen Anderson58155b22009-06-29 18:04:45 +00009587// On X86 and X86-64, atomic operations are lowered to locked instructions.
9588// Locked instructions, in turn, have implicit fence semantics (all memory
9589// operations are flushed before issuing the locked instruction, and the
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009590// are not buffered), so we can fold away the common pattern of
Owen Anderson58155b22009-06-29 18:04:45 +00009591// fence-atomic-fence.
9592static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9593 SDValue atomic = N->getOperand(0);
9594 switch (atomic.getOpcode()) {
9595 case ISD::ATOMIC_CMP_SWAP:
9596 case ISD::ATOMIC_SWAP:
9597 case ISD::ATOMIC_LOAD_ADD:
9598 case ISD::ATOMIC_LOAD_SUB:
9599 case ISD::ATOMIC_LOAD_AND:
9600 case ISD::ATOMIC_LOAD_OR:
9601 case ISD::ATOMIC_LOAD_XOR:
9602 case ISD::ATOMIC_LOAD_NAND:
9603 case ISD::ATOMIC_LOAD_MIN:
9604 case ISD::ATOMIC_LOAD_MAX:
9605 case ISD::ATOMIC_LOAD_UMIN:
9606 case ISD::ATOMIC_LOAD_UMAX:
9607 break;
9608 default:
9609 return SDValue();
9610 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009611
Owen Anderson58155b22009-06-29 18:04:45 +00009612 SDValue fence = atomic.getOperand(0);
9613 if (fence.getOpcode() != ISD::MEMBARRIER)
9614 return SDValue();
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009615
Owen Anderson58155b22009-06-29 18:04:45 +00009616 switch (atomic.getOpcode()) {
9617 case ISD::ATOMIC_CMP_SWAP:
9618 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9619 atomic.getOperand(1), atomic.getOperand(2),
9620 atomic.getOperand(3));
9621 case ISD::ATOMIC_SWAP:
9622 case ISD::ATOMIC_LOAD_ADD:
9623 case ISD::ATOMIC_LOAD_SUB:
9624 case ISD::ATOMIC_LOAD_AND:
9625 case ISD::ATOMIC_LOAD_OR:
9626 case ISD::ATOMIC_LOAD_XOR:
9627 case ISD::ATOMIC_LOAD_NAND:
9628 case ISD::ATOMIC_LOAD_MIN:
9629 case ISD::ATOMIC_LOAD_MAX:
9630 case ISD::ATOMIC_LOAD_UMIN:
9631 case ISD::ATOMIC_LOAD_UMAX:
9632 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9633 atomic.getOperand(1), atomic.getOperand(2));
9634 default:
9635 return SDValue();
9636 }
9637}
9638
Evan Chengedeb1692009-12-16 00:53:11 +00009639static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9640 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9641 // (and (i32 x86isd::setcc_carry), 1)
9642 // This eliminates the zext. This transformation is necessary because
9643 // ISD::SETCC is always legalized to i8.
9644 DebugLoc dl = N->getDebugLoc();
9645 SDValue N0 = N->getOperand(0);
9646 EVT VT = N->getValueType(0);
9647 if (N0.getOpcode() == ISD::AND &&
9648 N0.hasOneUse() &&
9649 N0.getOperand(0).hasOneUse()) {
9650 SDValue N00 = N0.getOperand(0);
9651 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9652 return SDValue();
9653 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9654 if (!C || C->getZExtValue() != 1)
9655 return SDValue();
9656 return DAG.getNode(ISD::AND, dl, VT,
9657 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9658 N00.getOperand(0), N00.getOperand(1)),
9659 DAG.getConstant(1, VT));
9660 }
9661
9662 return SDValue();
9663}
9664
Dan Gohman8181bd12008-07-27 21:46:04 +00009665SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng62370f32008-11-05 06:03:38 +00009666 DAGCombinerInfo &DCI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009667 SelectionDAG &DAG = DCI.DAG;
9668 switch (N->getOpcode()) {
9669 default: break;
Evan Chengef7be082008-05-12 19:56:52 +00009670 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattnerf82998f2008-01-25 05:46:26 +00009671 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnere4577dc2009-03-12 06:52:53 +00009672 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng04ecee12009-03-28 05:57:29 +00009673 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
sampo025b75c2009-01-26 00:52:55 +00009674 case ISD::SHL:
9675 case ISD::SRA:
9676 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng10957b82010-01-04 21:22:48 +00009677 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng40ee6e52008-05-08 00:57:18 +00009678 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner470d5dc2008-01-25 06:14:17 +00009679 case X86ISD::FXOR:
Chris Lattnerf82998f2008-01-25 05:46:26 +00009680 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9681 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohman22cefb02009-01-29 01:59:02 +00009682 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedmane6bb1e52009-06-07 06:52:44 +00009683 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson58155b22009-06-29 18:04:45 +00009684 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Chengedeb1692009-12-16 00:53:11 +00009685 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009686 }
9687
Dan Gohman8181bd12008-07-27 21:46:04 +00009688 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009689}
9690
9691//===----------------------------------------------------------------------===//
9692// X86 Inline Assembly Support
9693//===----------------------------------------------------------------------===//
9694
Chris Lattner7fce21c2009-07-20 17:51:36 +00009695static bool LowerToBSwap(CallInst *CI) {
9696 // FIXME: this should verify that we are targetting a 486 or better. If not,
9697 // we will turn this bswap into something that will be lowered to logical ops
9698 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9699 // so don't worry about this.
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009700
Chris Lattner7fce21c2009-07-20 17:51:36 +00009701 // Verify this is a simple bswap.
9702 if (CI->getNumOperands() != 2 ||
9703 CI->getType() != CI->getOperand(1)->getType() ||
9704 !CI->getType()->isInteger())
9705 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009706
Chris Lattner7fce21c2009-07-20 17:51:36 +00009707 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9708 if (!Ty || Ty->getBitWidth() % 16 != 0)
9709 return false;
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009710
Chris Lattner7fce21c2009-07-20 17:51:36 +00009711 // Okay, we can do this xform, do so now.
9712 const Type *Tys[] = { Ty };
9713 Module *M = CI->getParent()->getParent()->getParent();
9714 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009715
Chris Lattner7fce21c2009-07-20 17:51:36 +00009716 Value *Op = CI->getOperand(1);
9717 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009718
Chris Lattner7fce21c2009-07-20 17:51:36 +00009719 CI->replaceAllUsesWith(Op);
9720 CI->eraseFromParent();
9721 return true;
9722}
9723
9724bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9725 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9726 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9727
9728 std::string AsmStr = IA->getAsmString();
9729
9730 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009731 SmallVector<StringRef, 4> AsmPieces;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009732 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9733
9734 switch (AsmPieces.size()) {
9735 default: return false;
9736 case 1:
9737 AsmStr = AsmPieces[0];
9738 AsmPieces.clear();
9739 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9740
9741 // bswap $0
9742 if (AsmPieces.size() == 2 &&
9743 (AsmPieces[0] == "bswap" ||
9744 AsmPieces[0] == "bswapq" ||
9745 AsmPieces[0] == "bswapl") &&
9746 (AsmPieces[1] == "$0" ||
9747 AsmPieces[1] == "${0:q}")) {
9748 // No need to check constraints, nothing other than the equivalent of
9749 // "=r,0" would be valid here.
9750 return LowerToBSwap(CI);
9751 }
9752 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer0461f522010-01-05 20:07:06 +00009753 if (CI->getType()->isInteger(16) &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009754 AsmPieces.size() == 3 &&
9755 AsmPieces[0] == "rorw" &&
9756 AsmPieces[1] == "$$8," &&
9757 AsmPieces[2] == "${0:w}" &&
9758 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9759 return LowerToBSwap(CI);
9760 }
9761 break;
9762 case 3:
Benjamin Kramer0461f522010-01-05 20:07:06 +00009763 if (CI->getType()->isInteger(64) &&
Owen Anderson35b47072009-08-13 21:58:54 +00009764 Constraints.size() >= 2 &&
Chris Lattner7fce21c2009-07-20 17:51:36 +00009765 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9766 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9767 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramer3601d1b2010-01-11 18:03:24 +00009768 SmallVector<StringRef, 4> Words;
Chris Lattner7fce21c2009-07-20 17:51:36 +00009769 SplitString(AsmPieces[0], Words, " \t");
9770 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9771 Words.clear();
9772 SplitString(AsmPieces[1], Words, " \t");
9773 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9774 Words.clear();
9775 SplitString(AsmPieces[2], Words, " \t,");
9776 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9777 Words[2] == "%edx") {
9778 return LowerToBSwap(CI);
9779 }
9780 }
9781 }
9782 }
9783 break;
9784 }
9785 return false;
9786}
9787
9788
9789
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009790/// getConstraintType - Given a constraint letter, return the type of
9791/// constraint it is for this target.
9792X86TargetLowering::ConstraintType
9793X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9794 if (Constraint.size() == 1) {
9795 switch (Constraint[0]) {
9796 case 'A':
Dale Johannesen73920c02008-11-13 21:52:36 +00009797 return C_Register;
Chris Lattner267805f2008-03-11 19:06:29 +00009798 case 'f':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009799 case 'r':
9800 case 'R':
9801 case 'l':
9802 case 'q':
9803 case 'Q':
9804 case 'x':
Dale Johannesen9ab553f2008-04-01 00:57:48 +00009805 case 'y':
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009806 case 'Y':
9807 return C_RegisterClass;
Dale Johannesenf190a032009-02-12 20:58:09 +00009808 case 'e':
9809 case 'Z':
9810 return C_Other;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009811 default:
9812 break;
9813 }
9814 }
9815 return TargetLowering::getConstraintType(Constraint);
9816}
9817
Dale Johannesene99fc902008-01-29 02:21:21 +00009818/// LowerXConstraint - try to replace an X constraint, which matches anything,
9819/// with another that has more specific requirements based on the type of the
9820/// corresponding operand.
Chris Lattnereca405c2008-04-26 23:02:14 +00009821const char *X86TargetLowering::
Owen Andersonac9de032009-08-10 22:56:29 +00009822LowerXConstraint(EVT ConstraintVT) const {
Chris Lattnereca405c2008-04-26 23:02:14 +00009823 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9824 // 'f' like normal targets.
Duncan Sands92c43912008-06-06 12:08:01 +00009825 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesene99fc902008-01-29 02:21:21 +00009826 if (Subtarget->hasSSE2())
Chris Lattnereca405c2008-04-26 23:02:14 +00009827 return "Y";
9828 if (Subtarget->hasSSE1())
9829 return "x";
9830 }
Scott Michel91099d62009-02-17 22:15:04 +00009831
Chris Lattnereca405c2008-04-26 23:02:14 +00009832 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesene99fc902008-01-29 02:21:21 +00009833}
9834
Chris Lattnera531abc2007-08-25 00:47:38 +00009835/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9836/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman8181bd12008-07-27 21:46:04 +00009837void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattnera531abc2007-08-25 00:47:38 +00009838 char Constraint,
Evan Cheng7f250d62008-09-24 00:05:32 +00009839 bool hasMemory,
Dan Gohman8181bd12008-07-27 21:46:04 +00009840 std::vector<SDValue>&Ops,
Chris Lattnereca405c2008-04-26 23:02:14 +00009841 SelectionDAG &DAG) const {
Dan Gohman8181bd12008-07-27 21:46:04 +00009842 SDValue Result(0, 0);
Scott Michel91099d62009-02-17 22:15:04 +00009843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009844 switch (Constraint) {
9845 default: break;
9846 case 'I':
9847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00009848 if (C->getZExtValue() <= 31) {
9849 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00009850 break;
9851 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009852 }
Chris Lattnera531abc2007-08-25 00:47:38 +00009853 return;
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00009854 case 'J':
9855 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +00009856 if (C->getZExtValue() <= 63) {
Chris Lattner6552d0c2009-06-15 04:01:39 +00009857 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9858 break;
9859 }
9860 }
9861 return;
9862 case 'K':
9863 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattnerb84a1ac2009-06-15 04:39:05 +00009864 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng4fb2c0f2008-09-22 23:57:37 +00009865 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9866 break;
9867 }
9868 }
9869 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009870 case 'N':
9871 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00009872 if (C->getZExtValue() <= 255) {
9873 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattnera531abc2007-08-25 00:47:38 +00009874 break;
9875 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009876 }
Chris Lattnera531abc2007-08-25 00:47:38 +00009877 return;
Dale Johannesenf190a032009-02-12 20:58:09 +00009878 case 'e': {
9879 // 32-bit signed value
9880 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9881 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +00009882 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9883 C->getSExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009884 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009885 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesenf190a032009-02-12 20:58:09 +00009886 break;
9887 }
9888 // FIXME gcc accepts some relocatable values here too, but only in certain
9889 // memory models; it's complicated.
9890 }
9891 return;
9892 }
9893 case 'Z': {
9894 // 32-bit unsigned value
9895 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9896 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson35b47072009-08-13 21:58:54 +00009897 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9898 C->getZExtValue())) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009899 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9900 break;
9901 }
9902 }
9903 // FIXME gcc accepts some relocatable values here too, but only in certain
9904 // memory models; it's complicated.
9905 return;
9906 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009907 case 'i': {
9908 // Literal immediates are always ok.
Chris Lattnera531abc2007-08-25 00:47:38 +00009909 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesenf190a032009-02-12 20:58:09 +00009910 // Widen to 64 bits here to get it sign extended.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009911 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattnera531abc2007-08-25 00:47:38 +00009912 break;
9913 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009914
9915 // If we are in non-pic codegen mode, we allow the address of a global (with
9916 // an optional displacement) to be used with 'i'.
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009917 GlobalAddressSDNode *GA = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009918 int64_t Offset = 0;
Scott Michel91099d62009-02-17 22:15:04 +00009919
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009920 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9921 while (1) {
9922 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9923 Offset += GA->getOffset();
9924 break;
9925 } else if (Op.getOpcode() == ISD::ADD) {
9926 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9927 Offset += C->getZExtValue();
9928 Op = Op.getOperand(0);
9929 continue;
9930 }
9931 } else if (Op.getOpcode() == ISD::SUB) {
9932 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9933 Offset += -C->getZExtValue();
9934 Op = Op.getOperand(0);
9935 continue;
9936 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009937 }
Dale Johannesen69976cf2009-07-07 00:18:49 +00009938
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009939 // Otherwise, this isn't something we can handle, reject it.
9940 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009941 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +00009942
Chris Lattner054532c2009-07-10 07:34:39 +00009943 GlobalValue *GV = GA->getGlobal();
Dale Johannesen69976cf2009-07-07 00:18:49 +00009944 // If we require an extra load to get this address, as in PIC mode, we
9945 // can't accept it.
Chris Lattner054532c2009-07-10 07:34:39 +00009946 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9947 getTargetMachine())))
Dale Johannesen69976cf2009-07-07 00:18:49 +00009948 return;
Scott Michel91099d62009-02-17 22:15:04 +00009949
Dale Johannesenf97110c2009-07-21 00:12:29 +00009950 if (hasMemory)
9951 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9952 else
9953 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattnerd73ba7f2009-05-08 18:23:14 +00009954 Result = Op;
9955 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009956 }
9957 }
Scott Michel91099d62009-02-17 22:15:04 +00009958
Gabor Greif1c80d112008-08-28 21:40:38 +00009959 if (Result.getNode()) {
Chris Lattnera531abc2007-08-25 00:47:38 +00009960 Ops.push_back(Result);
9961 return;
9962 }
Evan Cheng7f250d62008-09-24 00:05:32 +00009963 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9964 Ops, DAG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009965}
9966
9967std::vector<unsigned> X86TargetLowering::
9968getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +00009969 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00009970 if (Constraint.size() == 1) {
9971 // FIXME: not handling fp-stack yet!
9972 switch (Constraint[0]) { // GCC X86 Constraint Letters
9973 default: break; // Unknown constraint letter
Evan Chengf8993d42009-07-17 22:13:25 +00009974 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9975 if (Subtarget->is64Bit()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009976 if (VT == MVT::i32)
Evan Chengf8993d42009-07-17 22:13:25 +00009977 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9978 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9979 X86::R10D,X86::R11D,X86::R12D,
9980 X86::R13D,X86::R14D,X86::R15D,
9981 X86::EBP, X86::ESP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009982 else if (VT == MVT::i16)
Evan Chengf8993d42009-07-17 22:13:25 +00009983 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9984 X86::SI, X86::DI, X86::R8W,X86::R9W,
9985 X86::R10W,X86::R11W,X86::R12W,
9986 X86::R13W,X86::R14W,X86::R15W,
9987 X86::BP, X86::SP, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009988 else if (VT == MVT::i8)
Evan Chengf8993d42009-07-17 22:13:25 +00009989 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9990 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9991 X86::R10B,X86::R11B,X86::R12B,
9992 X86::R13B,X86::R14B,X86::R15B,
9993 X86::BPL, X86::SPL, 0);
9994
Owen Anderson36e3a6e2009-08-11 20:47:22 +00009995 else if (VT == MVT::i64)
Evan Chengf8993d42009-07-17 22:13:25 +00009996 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9997 X86::RSI, X86::RDI, X86::R8, X86::R9,
9998 X86::R10, X86::R11, X86::R12,
9999 X86::R13, X86::R14, X86::R15,
10000 X86::RBP, X86::RSP, 0);
10001
10002 break;
10003 }
Eric Christopher3d82bbd2009-08-27 18:07:15 +000010004 // 32-bit fallthrough
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010005 case 'Q': // Q_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010006 if (VT == MVT::i32)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010007 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010008 else if (VT == MVT::i16)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010009 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010010 else if (VT == MVT::i8)
Evan Chengf85c10f2007-08-13 23:27:11 +000010011 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010012 else if (VT == MVT::i64)
Chris Lattner35032592007-11-04 06:51:12 +000010013 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10014 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010015 }
10016 }
10017
10018 return std::vector<unsigned>();
10019}
10020
10021std::pair<unsigned, const TargetRegisterClass*>
10022X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersonac9de032009-08-10 22:56:29 +000010023 EVT VT) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010024 // First, see if this is a constraint that directly corresponds to an LLVM
10025 // register class.
10026 if (Constraint.size() == 1) {
10027 // GCC Constraint Letters
10028 switch (Constraint[0]) {
10029 default: break;
10030 case 'r': // GENERAL_REGS
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010031 case 'l': // INDEX_REGS
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010032 if (VT == MVT::i8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010033 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010034 if (VT == MVT::i16)
Chris Lattnerbbfea052008-10-17 18:15:05 +000010035 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010036 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michel91099d62009-02-17 22:15:04 +000010037 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattnerbbfea052008-10-17 18:15:05 +000010038 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen1bf03f72009-10-07 22:47:20 +000010039 case 'R': // LEGACY_REGS
10040 if (VT == MVT::i8)
10041 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10042 if (VT == MVT::i16)
10043 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10044 if (VT == MVT::i32 || !Subtarget->is64Bit())
10045 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10046 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattner267805f2008-03-11 19:06:29 +000010047 case 'f': // FP Stack registers.
10048 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10049 // value to the correct fpstack register class.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010050 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +000010051 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010052 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattner267805f2008-03-11 19:06:29 +000010053 return std::make_pair(0U, X86::RFP64RegisterClass);
10054 return std::make_pair(0U, X86::RFP80RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010055 case 'y': // MMX_REGS if MMX allowed.
10056 if (!Subtarget->hasMMX()) break;
10057 return std::make_pair(0U, X86::VR64RegisterClass);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010058 case 'Y': // SSE_REGS if SSE2 allowed
10059 if (!Subtarget->hasSSE2()) break;
10060 // FALL THROUGH.
10061 case 'x': // SSE_REGS if SSE1 allowed
10062 if (!Subtarget->hasSSE1()) break;
Duncan Sands92c43912008-06-06 12:08:01 +000010063
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010064 switch (VT.getSimpleVT().SimpleTy) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010065 default: break;
10066 // Scalar SSE types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010067 case MVT::f32:
10068 case MVT::i32:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010069 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010070 case MVT::f64:
10071 case MVT::i64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010072 return std::make_pair(0U, X86::FR64RegisterClass);
10073 // Vector types.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010074 case MVT::v16i8:
10075 case MVT::v8i16:
10076 case MVT::v4i32:
10077 case MVT::v2i64:
10078 case MVT::v4f32:
10079 case MVT::v2f64:
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010080 return std::make_pair(0U, X86::VR128RegisterClass);
10081 }
10082 break;
10083 }
10084 }
Scott Michel91099d62009-02-17 22:15:04 +000010085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010086 // Use the default implementation in TargetLowering to convert the register
10087 // constraint into a member of a register class.
10088 std::pair<unsigned, const TargetRegisterClass*> Res;
10089 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10090
10091 // Not found as a standard register?
10092 if (Res.second == 0) {
Chris Lattner1063d242009-09-13 22:41:48 +000010093 // Map st(0) -> st(7) -> ST0
10094 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10095 tolower(Constraint[1]) == 's' &&
10096 tolower(Constraint[2]) == 't' &&
10097 Constraint[3] == '(' &&
10098 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10099 Constraint[5] == ')' &&
10100 Constraint[6] == '}') {
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010101
Chris Lattner1063d242009-09-13 22:41:48 +000010102 Res.first = X86::ST0+Constraint[4]-'0';
10103 Res.second = X86::RFP80RegisterClass;
10104 return Res;
10105 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010106
Chris Lattner1063d242009-09-13 22:41:48 +000010107 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramerea862b02009-11-12 20:36:59 +000010108 if (StringRef("{st}").equals_lower(Constraint)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010109 Res.first = X86::ST0;
Chris Lattner3cfe51b2007-09-24 05:27:37 +000010110 Res.second = X86::RFP80RegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +000010111 return Res;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010112 }
Chris Lattner1063d242009-09-13 22:41:48 +000010113
10114 // flags -> EFLAGS
Benjamin Kramerea862b02009-11-12 20:36:59 +000010115 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner1063d242009-09-13 22:41:48 +000010116 Res.first = X86::EFLAGS;
10117 Res.second = X86::CCRRegisterClass;
10118 return Res;
10119 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +000010120
Dale Johannesen73920c02008-11-13 21:52:36 +000010121 // 'A' means EAX + EDX.
10122 if (Constraint == "A") {
10123 Res.first = X86::EAX;
Dan Gohmanb4439d02009-07-30 17:02:08 +000010124 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner1063d242009-09-13 22:41:48 +000010125 return Res;
Dale Johannesen73920c02008-11-13 21:52:36 +000010126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010127 return Res;
10128 }
10129
10130 // Otherwise, check to see if this is a register class of the wrong value
10131 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10132 // turn into {ax},{dx}.
10133 if (Res.second->hasType(VT))
10134 return Res; // Correct type already, nothing to do.
10135
10136 // All of the single-register GCC register classes map their values onto
10137 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10138 // really want an 8-bit or 32-bit register, map to the appropriate register
10139 // class and return the appropriate register.
Chris Lattnere9d7f792008-08-26 06:19:02 +000010140 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010141 if (VT == MVT::i8) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010142 unsigned DestReg = 0;
10143 switch (Res.first) {
10144 default: break;
10145 case X86::AX: DestReg = X86::AL; break;
10146 case X86::DX: DestReg = X86::DL; break;
10147 case X86::CX: DestReg = X86::CL; break;
10148 case X86::BX: DestReg = X86::BL; break;
10149 }
10150 if (DestReg) {
10151 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010152 Res.second = X86::GR8RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010153 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010154 } else if (VT == MVT::i32) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010155 unsigned DestReg = 0;
10156 switch (Res.first) {
10157 default: break;
10158 case X86::AX: DestReg = X86::EAX; break;
10159 case X86::DX: DestReg = X86::EDX; break;
10160 case X86::CX: DestReg = X86::ECX; break;
10161 case X86::BX: DestReg = X86::EBX; break;
10162 case X86::SI: DestReg = X86::ESI; break;
10163 case X86::DI: DestReg = X86::EDI; break;
10164 case X86::BP: DestReg = X86::EBP; break;
10165 case X86::SP: DestReg = X86::ESP; break;
10166 }
10167 if (DestReg) {
10168 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010169 Res.second = X86::GR32RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010170 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010171 } else if (VT == MVT::i64) {
Chris Lattnere9d7f792008-08-26 06:19:02 +000010172 unsigned DestReg = 0;
10173 switch (Res.first) {
10174 default: break;
10175 case X86::AX: DestReg = X86::RAX; break;
10176 case X86::DX: DestReg = X86::RDX; break;
10177 case X86::CX: DestReg = X86::RCX; break;
10178 case X86::BX: DestReg = X86::RBX; break;
10179 case X86::SI: DestReg = X86::RSI; break;
10180 case X86::DI: DestReg = X86::RDI; break;
10181 case X86::BP: DestReg = X86::RBP; break;
10182 case X86::SP: DestReg = X86::RSP; break;
10183 }
10184 if (DestReg) {
10185 Res.first = DestReg;
Duncan Sands553fb412009-04-21 09:44:39 +000010186 Res.second = X86::GR64RegisterClass;
Chris Lattnere9d7f792008-08-26 06:19:02 +000010187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010188 }
Chris Lattnere9d7f792008-08-26 06:19:02 +000010189 } else if (Res.second == X86::FR32RegisterClass ||
10190 Res.second == X86::FR64RegisterClass ||
10191 Res.second == X86::VR128RegisterClass) {
10192 // Handle references to XMM physical registers that got mapped into the
10193 // wrong class. This can happen with constraints like {xmm0} where the
10194 // target independent register mapper will just pick the first match it can
10195 // find, ignoring the required type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010196 if (VT == MVT::f32)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010197 Res.second = X86::FR32RegisterClass;
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010198 else if (VT == MVT::f64)
Chris Lattnere9d7f792008-08-26 06:19:02 +000010199 Res.second = X86::FR64RegisterClass;
10200 else if (X86::VR128RegisterClass->hasType(VT))
10201 Res.second = X86::VR128RegisterClass;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010202 }
10203
10204 return Res;
10205}
Mon P Wang1448aad2008-10-30 08:01:45 +000010206
10207//===----------------------------------------------------------------------===//
10208// X86 Widen vector type
10209//===----------------------------------------------------------------------===//
10210
10211/// getWidenVectorType: given a vector type, returns the type to widen
10212/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010213/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wanga5a239f2008-11-06 05:31:54 +000010214/// When and where to widen is target dependent based on the cost of
Mon P Wang1448aad2008-10-30 08:01:45 +000010215/// scalarizing vs using the wider vector type.
10216
Owen Andersonac9de032009-08-10 22:56:29 +000010217EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang1448aad2008-10-30 08:01:45 +000010218 assert(VT.isVector());
10219 if (isTypeLegal(VT))
10220 return VT;
Scott Michel91099d62009-02-17 22:15:04 +000010221
Mon P Wang1448aad2008-10-30 08:01:45 +000010222 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10223 // type based on element type. This would speed up our search (though
10224 // it may not be worth it since the size of the list is relatively
10225 // small).
Owen Andersonac9de032009-08-10 22:56:29 +000010226 EVT EltVT = VT.getVectorElementType();
Mon P Wang1448aad2008-10-30 08:01:45 +000010227 unsigned NElts = VT.getVectorNumElements();
Scott Michel91099d62009-02-17 22:15:04 +000010228
Mon P Wang1448aad2008-10-30 08:01:45 +000010229 // On X86, it make sense to widen any vector wider than 1
10230 if (NElts <= 1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010231 return MVT::Other;
Scott Michel91099d62009-02-17 22:15:04 +000010232
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010233 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10234 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10235 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michel91099d62009-02-17 22:15:04 +000010236
10237 if (isTypeLegal(SVT) &&
10238 SVT.getVectorElementType() == EltVT &&
Mon P Wang1448aad2008-10-30 08:01:45 +000010239 SVT.getVectorNumElements() > NElts)
10240 return SVT;
10241 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000010242 return MVT::Other;
Mon P Wang1448aad2008-10-30 08:01:45 +000010243}