blob: aae17a8cf1bb77536b5a3750510ff76d699d15e2 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000019#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000023#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000024#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000026#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000027#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000028#include "llvm/LLVMContext.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000029#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000030#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000038#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000039#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000040#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000043#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000044#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000045#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000046using namespace llvm;
47
Mon P Wang3c81d352008-11-23 04:37:22 +000048static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000049DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000050
Dan Gohman2f67df72009-09-03 17:18:51 +000051// Disable16Bit - 16-bit operations typically have a larger encoding than
52// corresponding 32-bit instructions, and 16-bit code is slow on some
53// processors. This is an experimental flag to disable 16-bit operations
54// (which forces them to be Legalized to 32-bit operations).
55static cl::opt<bool>
56Disable16Bit("disable-16bit", cl::Hidden,
57 cl::desc("Disable use of 16-bit instructions"));
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
Chris Lattnerf0144122009-07-28 03:13:23 +000063static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
64 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
65 default: llvm_unreachable("unknown subtarget type");
66 case X86Subtarget::isDarwin:
Chris Lattner8c6ed052009-09-16 01:46:41 +000067 if (TM.getSubtarget<X86Subtarget>().is64Bit())
68 return new X8664_MachoTargetObjectFile();
Chris Lattner228252f2009-09-18 20:22:52 +000069 return new X8632_MachoTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +000070 case X86Subtarget::isELF:
71 return new TargetLoweringObjectFileELF();
72 case X86Subtarget::isMingw:
73 case X86Subtarget::isCygwin:
74 case X86Subtarget::isWindows:
75 return new TargetLoweringObjectFileCOFF();
76 }
Eric Christopherfd179292009-08-27 18:07:15 +000077
Chris Lattnerf0144122009-07-28 03:13:23 +000078}
79
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000080X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000081 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000082 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000083 X86ScalarSSEf64 = Subtarget->hasSSE2();
84 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000085 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000086
Anton Korobeynikov2365f512007-07-14 14:06:15 +000087 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000090 // Set up the TargetLowering object.
91
92 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000093 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000094 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000095 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000096 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000097
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000098 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000099 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000100 setUseUnderscoreSetJmp(false);
101 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000102 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000103 // MS runtime is weird: it exports _setjmp, but longjmp!
104 setUseUnderscoreSetJmp(true);
105 setUseUnderscoreLongJmp(false);
106 } else {
107 setUseUnderscoreSetJmp(true);
108 setUseUnderscoreLongJmp(true);
109 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000110
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000112 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000113 if (!Disable16Bit)
114 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000116 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000118
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000120
Scott Michelfdc40a02009-02-17 22:15:04 +0000121 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000123 if (!Disable16Bit)
124 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000126 if (!Disable16Bit)
127 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
129 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000130
131 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
134 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
137 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000138
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000139 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
140 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
143 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000144
Evan Cheng25ab6902006-09-08 06:48:29 +0000145 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000146 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
147 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000148 } else if (!UseSoftFloat) {
149 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000150 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000152 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000153 // We have an algorithm for SSE2, and we turn this into a 64-bit
154 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000156 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000157
158 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
159 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
161 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000162
Devang Patel6a784892009-06-05 18:48:29 +0000163 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000164 // SSE has no i16 to fp conversion, only i32
165 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000167 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000169 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000172 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000173 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
175 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000176 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000177
Dale Johannesen73328d12007-09-19 23:55:34 +0000178 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
179 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
181 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000182
Evan Cheng02568ff2006-01-30 22:13:22 +0000183 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
184 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
186 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000187
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000188 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000190 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000192 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
194 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000195 }
196
197 // Handle FP_TO_UINT by promoting the destination to a larger signed
198 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
200 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
201 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
205 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000206 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000207 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000208 // Expand FP_TO_UINT into a select.
209 // FIXME: We would like to use a Custom expander here eventually to do
210 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000213 // With SSE3 we can use fisttpll to convert to a signed i64; without
214 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000216 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000217
Chris Lattner399610a2006-12-05 18:22:22 +0000218 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000219 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
221 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000222 }
Chris Lattner21f66852005-12-23 05:15:23 +0000223
Dan Gohmanb00ee212008-02-18 19:34:53 +0000224 // Scalar integer divide and remainder are lowered to use operations that
225 // produce two results, to match the available instructions. This exposes
226 // the two-result form to trivial CSE, which is able to combine x/y and x%y
227 // into a single instruction.
228 //
229 // Scalar integer multiply-high is also lowered to use two-result
230 // operations, to match the available instructions. However, plain multiply
231 // (low) operations are left as Legal, as there are single-result
232 // instructions for this in x86. Using the two-result multiply instructions
233 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000234 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
235 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
236 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
237 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
238 setOperationAction(ISD::SREM , MVT::i8 , Expand);
239 setOperationAction(ISD::UREM , MVT::i8 , Expand);
240 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
241 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
242 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
243 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
244 setOperationAction(ISD::SREM , MVT::i16 , Expand);
245 setOperationAction(ISD::UREM , MVT::i16 , Expand);
246 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
247 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
248 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
249 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
250 setOperationAction(ISD::SREM , MVT::i32 , Expand);
251 setOperationAction(ISD::UREM , MVT::i32 , Expand);
252 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
253 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
254 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
255 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
256 setOperationAction(ISD::SREM , MVT::i64 , Expand);
257 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000258
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
260 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
261 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
262 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
268 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
269 setOperationAction(ISD::FREM , MVT::f32 , Expand);
270 setOperationAction(ISD::FREM , MVT::f64 , Expand);
271 setOperationAction(ISD::FREM , MVT::f80 , Expand);
272 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000273
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
275 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
276 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
277 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000278 if (Disable16Bit) {
279 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
280 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
281 } else {
282 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
284 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
290 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
291 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 }
293
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
295 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000296
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000297 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000298 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000299 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000300 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000301 if (Disable16Bit)
302 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
303 else
304 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000305 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
307 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
308 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
309 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000310 if (Disable16Bit)
311 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
312 else
313 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
315 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
316 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
317 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000318 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000323
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000324 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
326 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
327 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
328 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000329 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
331 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000332 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000333 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
335 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
336 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
337 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000338 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000339 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000340 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
342 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
343 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000344 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
346 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
347 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000348 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000349
Evan Chengd2cde682008-03-10 19:38:10 +0000350 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000352
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000353 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000355
Mon P Wang63307c32008-05-05 19:05:59 +0000356 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000361
Owen Anderson825b72b2009-08-11 20:47:22 +0000362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000366
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000367 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000368 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000375 }
376
Evan Cheng3c992d22006-03-07 02:02:57 +0000377 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000378 if (!Subtarget->isTargetDarwin() &&
379 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000380 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000382 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000383
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
385 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
386 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
387 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000388 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000389 setExceptionPointerRegister(X86::RAX);
390 setExceptionSelectorRegister(X86::RDX);
391 } else {
392 setExceptionPointerRegister(X86::EAX);
393 setExceptionSelectorRegister(X86::EDX);
394 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
396 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000397
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000399
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000401
Nate Begemanacc398c2006-01-25 18:21:52 +0000402 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::VASTART , MVT::Other, Custom);
404 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000405 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::VAARG , MVT::Other, Custom);
407 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000408 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VAARG , MVT::Other, Expand);
410 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 }
Evan Chengae642192007-03-02 23:16:35 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
414 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000415 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000417 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000418 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000419 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000421
Evan Chengc7ce29b2009-02-13 22:36:38 +0000422 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000423 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000424 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000425 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
426 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000427
Evan Cheng223547a2006-01-31 22:28:30 +0000428 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::FABS , MVT::f64, Custom);
430 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000431
432 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FNEG , MVT::f64, Custom);
434 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000435
Evan Cheng68c47cb2007-01-05 07:55:56 +0000436 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
438 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000439
Evan Chengd25e9e82006-02-02 00:28:23 +0000440 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::FSIN , MVT::f64, Expand);
442 setOperationAction(ISD::FCOS , MVT::f64, Expand);
443 setOperationAction(ISD::FSIN , MVT::f32, Expand);
444 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000445
Chris Lattnera54aa942006-01-29 06:26:08 +0000446 // Expand FP immediates into loads from the stack, except for the special
447 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000448 addLegalFPImmediate(APFloat(+0.0)); // xorpd
449 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000450 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451 // Use SSE for f32, x87 for f64.
452 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
454 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455
456 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
459 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
Owen Anderson825b72b2009-08-11 20:47:22 +0000462 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463
464 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
466 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
468 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::FSIN , MVT::f32, Expand);
470 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
Nate Begemane1795842008-02-14 08:57:00 +0000472 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473 addLegalFPImmediate(APFloat(+0.0f)); // xorps
474 addLegalFPImmediate(APFloat(+0.0)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
478
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
481 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000483 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000485 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
487 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000488
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
490 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
492 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000493
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000494 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
496 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000497 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000498 addLegalFPImmediate(APFloat(+0.0)); // FLD0
499 addLegalFPImmediate(APFloat(+1.0)); // FLD1
500 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
501 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000502 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
503 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
504 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
505 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000506 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000507
Dale Johannesen59a58732007-08-05 18:49:15 +0000508 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000509 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000510 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
511 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
512 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000513 {
514 bool ignored;
515 APFloat TmpFlt(+0.0);
516 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
517 &ignored);
518 addLegalFPImmediate(TmpFlt); // FLD0
519 TmpFlt.changeSign();
520 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
521 APFloat TmpFlt2(+1.0);
522 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
523 &ignored);
524 addLegalFPImmediate(TmpFlt2); // FLD1
525 TmpFlt2.changeSign();
526 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
527 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000528
Evan Chengc7ce29b2009-02-13 22:36:38 +0000529 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000530 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
531 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000532 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000533 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000534
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000535 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000536 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
537 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
538 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000539
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FLOG, MVT::f80, Expand);
541 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
542 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
543 setOperationAction(ISD::FEXP, MVT::f80, Expand);
544 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000545
Mon P Wangf007a8b2008-11-06 05:31:54 +0000546 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000547 // (for widening) or expand (for scalarization). Then we will selectively
548 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
550 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
551 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
566 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
567 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000599 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000600 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
604 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
605 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
606 setTruncStoreAction((MVT::SimpleValueType)VT,
607 (MVT::SimpleValueType)InnerVT, Expand);
608 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
609 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
610 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000611 }
612
Evan Chengc7ce29b2009-02-13 22:36:38 +0000613 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
614 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000615 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
617 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
618 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
619 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
620 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
623 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
624 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
625 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000626
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
628 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
629 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
630 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
633 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::AND, MVT::v8i8, Promote);
636 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
637 setOperationAction(ISD::AND, MVT::v4i16, Promote);
638 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
639 setOperationAction(ISD::AND, MVT::v2i32, Promote);
640 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
641 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000642
Owen Anderson825b72b2009-08-11 20:47:22 +0000643 setOperationAction(ISD::OR, MVT::v8i8, Promote);
644 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
645 setOperationAction(ISD::OR, MVT::v4i16, Promote);
646 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
647 setOperationAction(ISD::OR, MVT::v2i32, Promote);
648 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
649 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000650
Owen Anderson825b72b2009-08-11 20:47:22 +0000651 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
652 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
653 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
654 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
655 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
656 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
657 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000658
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
660 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
661 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
662 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
663 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
664 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
665 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
666 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
667 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
670 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
671 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
681 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
688 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
689 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
690 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
691 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
692 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
693 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000694 }
695
Evan Cheng92722532009-03-26 23:06:32 +0000696 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
700 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
701 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
702 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
704 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
705 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
706 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
707 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
708 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
709 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
710 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000711 }
712
Evan Cheng92722532009-03-26 23:06:32 +0000713 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000715
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000716 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
717 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
719 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
720 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
721 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000722
Owen Anderson825b72b2009-08-11 20:47:22 +0000723 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
724 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
725 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
726 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
727 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
728 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
729 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
730 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
731 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
732 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
733 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
734 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
735 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
736 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
738 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000739
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
741 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
742 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
743 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000744
Owen Anderson825b72b2009-08-11 20:47:22 +0000745 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
746 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
749 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000750
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000751 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
752 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
753 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
756
Evan Cheng2c3ae372006-04-12 21:21:57 +0000757 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
759 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000760 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000761 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000762 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000763 // Do not attempt to custom lower non-128-bit vectors
764 if (!VT.is128BitVector())
765 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::BUILD_VECTOR,
767 VT.getSimpleVT().SimpleTy, Custom);
768 setOperationAction(ISD::VECTOR_SHUFFLE,
769 VT.getSimpleVT().SimpleTy, Custom);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
771 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000772 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000773
Owen Anderson825b72b2009-08-11 20:47:22 +0000774 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
775 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
776 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
777 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
778 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
779 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000780
Nate Begemancdd1eec2008-02-12 22:51:28 +0000781 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000784 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000785
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000786 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
788 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000789 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000790
791 // Do not attempt to promote non-128-bit vectors
792 if (!VT.is128BitVector()) {
793 continue;
794 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000795 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000797 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000798 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000799 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000801 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000803 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000805 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000806
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000808
Evan Cheng2c3ae372006-04-12 21:21:57 +0000809 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
811 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
812 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
813 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000814
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
816 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000817 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
819 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000820 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000821 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000822
Nate Begeman14d12ca2008-02-11 04:19:36 +0000823 if (Subtarget->hasSSE41()) {
824 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000826
827 // i8 and i16 vectors are custom , because the source register and source
828 // source memory operand types are not the same width. f32 vectors are
829 // custom since the immediate controlling the insert encodes additional
830 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000835
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
837 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
839 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000840
841 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
843 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000844 }
845 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000846
Nate Begeman30a0de92008-07-17 16:51:19 +0000847 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000849 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000850
David Greene9b9838d2009-06-29 16:47:10 +0000851 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
853 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
854 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
855 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000856
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
858 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
859 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
860 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
861 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
862 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
863 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
864 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
865 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
866 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
867 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
868 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
869 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
870 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
871 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000872
873 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
875 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
876 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
877 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
878 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
879 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
880 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
881 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
882 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
883 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
884 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
885 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
886 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
887 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000888
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
890 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
891 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
892 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000893
Owen Anderson825b72b2009-08-11 20:47:22 +0000894 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
895 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
896 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
898 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
901 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
902 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
903 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
904 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
905 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000906
907#if 0
908 // Not sure we want to do this since there are no 256-bit integer
909 // operations in AVX
910
911 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
912 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
914 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000915
916 // Do not attempt to custom lower non-power-of-2 vectors
917 if (!isPowerOf2_32(VT.getVectorNumElements()))
918 continue;
919
920 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
921 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
922 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
923 }
924
925 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000926 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
927 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000928 }
David Greene9b9838d2009-06-29 16:47:10 +0000929#endif
930
931#if 0
932 // Not sure we want to do this since there are no 256-bit integer
933 // operations in AVX
934
935 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
936 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
938 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000939
940 if (!VT.is256BitVector()) {
941 continue;
942 }
943 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000945 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000947 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000949 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000951 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000953 }
954
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000956#endif
957 }
958
Evan Cheng6be2c582006-04-05 23:38:46 +0000959 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000961
Bill Wendling74c37652008-12-09 22:08:41 +0000962 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::SADDO, MVT::i32, Custom);
964 setOperationAction(ISD::SADDO, MVT::i64, Custom);
965 setOperationAction(ISD::UADDO, MVT::i32, Custom);
966 setOperationAction(ISD::UADDO, MVT::i64, Custom);
967 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
968 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
969 setOperationAction(ISD::USUBO, MVT::i32, Custom);
970 setOperationAction(ISD::USUBO, MVT::i64, Custom);
971 setOperationAction(ISD::SMULO, MVT::i32, Custom);
972 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000973
Evan Chengd54f2d52009-03-31 19:38:51 +0000974 if (!Subtarget->is64Bit()) {
975 // These libcalls are not available in 32-bit.
976 setLibcallName(RTLIB::SHL_I128, 0);
977 setLibcallName(RTLIB::SRL_I128, 0);
978 setLibcallName(RTLIB::SRA_I128, 0);
979 }
980
Evan Cheng206ee9d2006-07-07 08:33:52 +0000981 // We have target-specific dag combine patterns for the following nodes:
982 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000983 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000984 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000985 setTargetDAGCombine(ISD::SHL);
986 setTargetDAGCombine(ISD::SRA);
987 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000988 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000989 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000990 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000991 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000992 if (Subtarget->is64Bit())
993 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000994
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000995 computeRegisterProperties();
996
Mon P Wangcd6e7252009-11-30 02:42:02 +0000997 // Divide and reminder operations have no vector equivalent and can
998 // trap. Do a custom widening for these operations in which we never
999 // generate more divides/remainder than the original vector width.
1000 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1001 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1002 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1003 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1004 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1005 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1006 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1007 }
1008 }
1009
Evan Cheng87ed7162006-02-14 08:25:08 +00001010 // FIXME: These should be based on subtarget info. Plus, the values should
1011 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001012 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1013 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1014 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001015 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001016 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001017}
1018
Scott Michel5b8f82e2008-03-10 15:42:14 +00001019
Owen Anderson825b72b2009-08-11 20:47:22 +00001020MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1021 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001022}
1023
1024
Evan Cheng29286502008-01-23 23:17:41 +00001025/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1026/// the desired ByVal argument alignment.
1027static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1028 if (MaxAlign == 16)
1029 return;
1030 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1031 if (VTy->getBitWidth() == 128)
1032 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001033 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1034 unsigned EltAlign = 0;
1035 getMaxByValAlign(ATy->getElementType(), EltAlign);
1036 if (EltAlign > MaxAlign)
1037 MaxAlign = EltAlign;
1038 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1039 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1040 unsigned EltAlign = 0;
1041 getMaxByValAlign(STy->getElementType(i), EltAlign);
1042 if (EltAlign > MaxAlign)
1043 MaxAlign = EltAlign;
1044 if (MaxAlign == 16)
1045 break;
1046 }
1047 }
1048 return;
1049}
1050
1051/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1052/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001053/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1054/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001055unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001056 if (Subtarget->is64Bit()) {
1057 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001058 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001059 if (TyAlign > 8)
1060 return TyAlign;
1061 return 8;
1062 }
1063
Evan Cheng29286502008-01-23 23:17:41 +00001064 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001065 if (Subtarget->hasSSE1())
1066 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001067 return Align;
1068}
Chris Lattner2b02a442007-02-25 08:29:00 +00001069
Evan Chengf0df0312008-05-15 08:39:06 +00001070/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001071/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001072/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001073/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001074EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001075X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001076 bool isSrcConst, bool isSrcStr,
1077 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001078 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1079 // linux. This is because the stack realignment code can't handle certain
1080 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001081 const Function *F = DAG.getMachineFunction().getFunction();
1082 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1083 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001084 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001085 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001086 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001088 }
Evan Chengf0df0312008-05-15 08:39:06 +00001089 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 return MVT::i64;
1091 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001092}
1093
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001094/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1095/// current function. The returned value is a member of the
1096/// MachineJumpTableInfo::JTEntryKind enum.
1097unsigned X86TargetLowering::getJumpTableEncoding() const {
1098 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1099 // symbol.
1100 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1101 Subtarget->isPICStyleGOT())
1102 return MachineJumpTableInfo::EK_GPRel32BlockAddress;
1103
1104 // Otherwise, use the normal jump table encoding heuristics.
1105 return TargetLowering::getJumpTableEncoding();
1106}
1107
Evan Chengcc415862007-11-09 01:32:10 +00001108/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1109/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001110SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001111 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001112 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001113 // This doesn't have DebugLoc associated with it, but is not really the
1114 // same as a Register.
1115 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1116 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001117 return Table;
1118}
1119
Bill Wendlingb4202b82009-07-01 18:50:55 +00001120/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001121unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001122 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001123}
1124
Chris Lattner2b02a442007-02-25 08:29:00 +00001125//===----------------------------------------------------------------------===//
1126// Return Value Calling Convention Implementation
1127//===----------------------------------------------------------------------===//
1128
Chris Lattner59ed56b2007-02-28 04:55:35 +00001129#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001130
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001131bool
1132X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1133 const SmallVectorImpl<EVT> &OutTys,
1134 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1135 SelectionDAG &DAG) {
1136 SmallVector<CCValAssign, 16> RVLocs;
1137 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1138 RVLocs, *DAG.getContext());
1139 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1140}
1141
Dan Gohman98ca4f22009-08-05 01:29:28 +00001142SDValue
1143X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001144 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001145 const SmallVectorImpl<ISD::OutputArg> &Outs,
1146 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001147
Chris Lattner9774c912007-02-27 05:28:59 +00001148 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001149 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1150 RVLocs, *DAG.getContext());
1151 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001153 // If this is the first return lowered for this function, add the regs to the
1154 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001155 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001156 for (unsigned i = 0; i != RVLocs.size(); ++i)
1157 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001158 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001159 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001160
Dan Gohman475871a2008-07-27 21:46:04 +00001161 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001162
Dan Gohman475871a2008-07-27 21:46:04 +00001163 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001164 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1165 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001166 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001167
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001168 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001169 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1170 CCValAssign &VA = RVLocs[i];
1171 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001172 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001173
Chris Lattner447ff682008-03-11 03:23:40 +00001174 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1175 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001176 if (VA.getLocReg() == X86::ST0 ||
1177 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001178 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1179 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001180 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001182 RetOps.push_back(ValToCopy);
1183 // Don't emit a copytoreg.
1184 continue;
1185 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001186
Evan Cheng242b38b2009-02-23 09:03:22 +00001187 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1188 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001189 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001190 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001191 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001192 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001193 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001194 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001195 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001196 }
1197
Dale Johannesendd64c412009-02-04 00:33:20 +00001198 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001199 Flag = Chain.getValue(1);
1200 }
Dan Gohman61a92132008-04-21 23:59:07 +00001201
1202 // The x86-64 ABI for returning structs by value requires that we copy
1203 // the sret argument into %rax for the return. We saved the argument into
1204 // a virtual register in the entry block, so now we copy the value out
1205 // and into %rax.
1206 if (Subtarget->is64Bit() &&
1207 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1208 MachineFunction &MF = DAG.getMachineFunction();
1209 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1210 unsigned Reg = FuncInfo->getSRetReturnReg();
1211 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001213 FuncInfo->setSRetReturnReg(Reg);
1214 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001215 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001216
Dale Johannesendd64c412009-02-04 00:33:20 +00001217 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001218 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001219
1220 // RAX now acts like a return value.
1221 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001222 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001223
Chris Lattner447ff682008-03-11 03:23:40 +00001224 RetOps[0] = Chain; // Update chain.
1225
1226 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001227 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001228 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
1230 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001231 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001232}
1233
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234/// LowerCallResult - Lower the result values of a call into the
1235/// appropriate copies out of appropriate physical registers.
1236///
1237SDValue
1238X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001239 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 const SmallVectorImpl<ISD::InputArg> &Ins,
1241 DebugLoc dl, SelectionDAG &DAG,
1242 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001243
Chris Lattnere32bbf62007-02-28 07:09:55 +00001244 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001245 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001246 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001247 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001248 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001249 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001250
Chris Lattner3085e152007-02-25 08:59:22 +00001251 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001252 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001253 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001254 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Torok Edwin3f142c32009-02-01 18:15:56 +00001256 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001258 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001259 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001260 }
1261
Chris Lattner8e6da152008-03-10 21:08:41 +00001262 // If this is a call to a function that returns an fp value on the floating
1263 // point stack, but where we prefer to use the value in xmm registers, copy
1264 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001265 if ((VA.getLocReg() == X86::ST0 ||
1266 VA.getLocReg() == X86::ST1) &&
1267 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001269 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Evan Cheng79fb3b42009-02-20 20:43:02 +00001271 SDValue Val;
1272 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001273 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1274 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1275 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001276 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001277 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001278 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1279 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001280 } else {
1281 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001283 Val = Chain.getValue(0);
1284 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001285 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1286 } else {
1287 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1288 CopyVT, InFlag).getValue(1);
1289 Val = Chain.getValue(0);
1290 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001291 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001292
Dan Gohman37eed792009-02-04 17:28:58 +00001293 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001294 // Round the F80 the right size, which also moves to the appropriate xmm
1295 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001296 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001297 // This truncation won't change the value.
1298 DAG.getIntPtrConstant(1));
1299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001300
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001302 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001303
Dan Gohman98ca4f22009-08-05 01:29:28 +00001304 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001305}
1306
1307
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001308//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001309// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001310//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001311// StdCall calling convention seems to be standard for many Windows' API
1312// routines and around. It differs from C calling convention just a little:
1313// callee should clean up the stack, not caller. Symbols should be also
1314// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001315// For info on fast calling convention see Fast Calling Convention (tail call)
1316// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001317
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001319/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001320static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1321 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001322 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001323
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001325}
1326
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001327/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001328/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001329static bool
1330ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1331 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001332 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001333
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001335}
1336
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001337/// IsCalleePop - Determines whether the callee is required to pop its
1338/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001339bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001340 if (IsVarArg)
1341 return false;
1342
Dan Gohman095cc292008-09-13 01:54:27 +00001343 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001344 default:
1345 return false;
1346 case CallingConv::X86_StdCall:
1347 return !Subtarget->is64Bit();
1348 case CallingConv::X86_FastCall:
1349 return !Subtarget->is64Bit();
1350 case CallingConv::Fast:
1351 return PerformTailCallOpt;
1352 }
1353}
1354
Dan Gohman095cc292008-09-13 01:54:27 +00001355/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1356/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001357CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001358 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001359 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001360 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001361 else
1362 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001363 }
1364
Gordon Henriksen86737662008-01-05 16:56:59 +00001365 if (CC == CallingConv::X86_FastCall)
1366 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001367 else if (CC == CallingConv::Fast)
1368 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001369 else
1370 return CC_X86_32_C;
1371}
1372
Dan Gohman98ca4f22009-08-05 01:29:28 +00001373/// NameDecorationForCallConv - Selects the appropriate decoration to
1374/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001375NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001376X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001378 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001379 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 return StdCall;
1381 return None;
1382}
1383
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001384
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001385/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1386/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001387/// the specific parameter attribute. The copy will be passed as a byval
1388/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001389static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001390CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001391 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1392 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001393 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001394 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001395 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001396}
1397
Dan Gohman98ca4f22009-08-05 01:29:28 +00001398SDValue
1399X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001400 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001401 const SmallVectorImpl<ISD::InputArg> &Ins,
1402 DebugLoc dl, SelectionDAG &DAG,
1403 const CCValAssign &VA,
1404 MachineFrameInfo *MFI,
1405 unsigned i) {
1406
Rafael Espindola7effac52007-09-14 15:48:13 +00001407 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001408 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1409 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001410 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001411 EVT ValVT;
1412
1413 // If value is passed by pointer we have address passed instead of the value
1414 // itself.
1415 if (VA.getLocInfo() == CCValAssign::Indirect)
1416 ValVT = VA.getLocVT();
1417 else
1418 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001419
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001420 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001421 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001422 // In case of tail call optimization mark all arguments mutable. Since they
1423 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001424 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001425 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001426 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001427 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001428 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001429 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001430 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001431}
1432
Dan Gohman475871a2008-07-27 21:46:04 +00001433SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001434X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001435 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 bool isVarArg,
1437 const SmallVectorImpl<ISD::InputArg> &Ins,
1438 DebugLoc dl,
1439 SelectionDAG &DAG,
1440 SmallVectorImpl<SDValue> &InVals) {
1441
Evan Cheng1bc78042006-04-26 01:20:17 +00001442 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001443 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001444
Gordon Henriksen86737662008-01-05 16:56:59 +00001445 const Function* Fn = MF.getFunction();
1446 if (Fn->hasExternalLinkage() &&
1447 Subtarget->isTargetCygMing() &&
1448 Fn->getName() == "main")
1449 FuncInfo->setForceFramePointer(true);
1450
1451 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001452 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001453
Evan Cheng1bc78042006-04-26 01:20:17 +00001454 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001455 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001456 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001457
Dan Gohman98ca4f22009-08-05 01:29:28 +00001458 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001459 "Var args not supported with calling convention fastcc");
1460
Chris Lattner638402b2007-02-28 07:00:42 +00001461 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001462 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001463 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1464 ArgLocs, *DAG.getContext());
1465 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001466
Chris Lattnerf39f7712007-02-28 05:46:49 +00001467 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001468 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001469 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1470 CCValAssign &VA = ArgLocs[i];
1471 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1472 // places.
1473 assert(VA.getValNo() != LastVal &&
1474 "Don't support value assigned to multiple locs yet");
1475 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001476
Chris Lattnerf39f7712007-02-28 05:46:49 +00001477 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001478 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001479 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001480 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001481 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001482 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001483 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001486 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001487 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001488 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001489 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001490 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1491 RC = X86::VR64RegisterClass;
1492 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001493 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001494
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001495 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001496 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Chris Lattnerf39f7712007-02-28 05:46:49 +00001498 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1499 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1500 // right size.
1501 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001502 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001503 DAG.getValueType(VA.getValVT()));
1504 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001505 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001506 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001507 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001508 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001509
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001510 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001511 // Handle MMX values passed in XMM regs.
1512 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001513 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1514 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001515 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1516 } else
1517 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001518 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001519 } else {
1520 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001522 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001523
1524 // If value is passed via pointer - do a load.
1525 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001526 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001527
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001529 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001530
Dan Gohman61a92132008-04-21 23:59:07 +00001531 // The x86-64 ABI for returning structs by value requires that we copy
1532 // the sret argument into %rax for the return. Save the argument into
1533 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001534 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001535 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1536 unsigned Reg = FuncInfo->getSRetReturnReg();
1537 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001538 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001539 FuncInfo->setSRetReturnReg(Reg);
1540 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001541 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001542 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001543 }
1544
Chris Lattnerf39f7712007-02-28 05:46:49 +00001545 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001546 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001548 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001549
Evan Cheng1bc78042006-04-26 01:20:17 +00001550 // If the function takes variable number of arguments, make a frame index for
1551 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001552 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001554 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 }
1556 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001557 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1558
1559 // FIXME: We should really autogenerate these arrays
1560 static const unsigned GPR64ArgRegsWin64[] = {
1561 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001562 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001563 static const unsigned XMMArgRegsWin64[] = {
1564 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1565 };
1566 static const unsigned GPR64ArgRegs64Bit[] = {
1567 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1568 };
1569 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001570 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1571 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1572 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001573 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1574
1575 if (IsWin64) {
1576 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1577 GPR64ArgRegs = GPR64ArgRegsWin64;
1578 XMMArgRegs = XMMArgRegsWin64;
1579 } else {
1580 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1581 GPR64ArgRegs = GPR64ArgRegs64Bit;
1582 XMMArgRegs = XMMArgRegs64Bit;
1583 }
1584 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1585 TotalNumIntRegs);
1586 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1587 TotalNumXMMRegs);
1588
Devang Patel578efa92009-06-05 21:57:13 +00001589 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001590 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001591 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001592 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001593 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001594 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001595 // Kernel mode asks for SSE to be disabled, so don't push them
1596 // on the stack.
1597 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001598
Gordon Henriksen86737662008-01-05 16:56:59 +00001599 // For X86-64, if there are vararg parameters that are passed via
1600 // registers, then we must store them to their spots on the stack so they
1601 // may be loaded by deferencing the result of va_next.
1602 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001603 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1604 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001605 TotalNumXMMRegs * 16, 16,
1606 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001607
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001609 SmallVector<SDValue, 8> MemOps;
1610 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001611 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001612 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001613 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1614 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001615 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1616 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001617 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001618 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001619 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001620 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001621 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001622 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001623 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001624 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001625
Dan Gohmanface41a2009-08-16 21:24:25 +00001626 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1627 // Now store the XMM (fp + vector) parameter registers.
1628 SmallVector<SDValue, 11> SaveXMMOps;
1629 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001630
Dan Gohmanface41a2009-08-16 21:24:25 +00001631 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1632 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1633 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001634
Dan Gohmanface41a2009-08-16 21:24:25 +00001635 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1636 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001637
Dan Gohmanface41a2009-08-16 21:24:25 +00001638 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1639 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1640 X86::VR128RegisterClass);
1641 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1642 SaveXMMOps.push_back(Val);
1643 }
1644 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1645 MVT::Other,
1646 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001648
1649 if (!MemOps.empty())
1650 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1651 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001653 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001654
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001658 BytesCallerReserves = 0;
1659 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001660 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001661 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001663 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001664 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001665 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001666
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 if (!Is64Bit) {
1668 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001669 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1671 }
Evan Cheng25caf632006-05-23 21:06:34 +00001672
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001673 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001674
Dan Gohman98ca4f22009-08-05 01:29:28 +00001675 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001676}
1677
Dan Gohman475871a2008-07-27 21:46:04 +00001678SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1680 SDValue StackPtr, SDValue Arg,
1681 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001682 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001683 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001684 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001685 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001686 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001687 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001688 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001689 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001690 }
Dale Johannesenace16102009-02-03 19:33:06 +00001691 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001692 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001693}
1694
Bill Wendling64e87322009-01-16 19:25:27 +00001695/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001696/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001697SDValue
1698X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001699 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001700 SDValue Chain,
1701 bool IsTailCall,
1702 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001703 int FPDiff,
1704 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001705 if (!IsTailCall || FPDiff==0) return Chain;
1706
1707 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001708 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001709 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001710
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001711 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001712 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001713 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001714}
1715
1716/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1717/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001718static SDValue
1719EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001721 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001722 // Store the return address to the appropriate stack slot.
1723 if (!FPDiff) return Chain;
1724 // Calculate the new stack slot for the return address.
1725 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001726 int NewReturnAddrFI =
David Greene3f2bf852009-11-12 20:49:22 +00001727 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1728 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001729 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001730 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001731 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001732 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001733 return Chain;
1734}
1735
Dan Gohman98ca4f22009-08-05 01:29:28 +00001736SDValue
1737X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001738 CallingConv::ID CallConv, bool isVarArg,
1739 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 const SmallVectorImpl<ISD::OutputArg> &Outs,
1741 const SmallVectorImpl<ISD::InputArg> &Ins,
1742 DebugLoc dl, SelectionDAG &DAG,
1743 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001744
Dan Gohman98ca4f22009-08-05 01:29:28 +00001745 MachineFunction &MF = DAG.getMachineFunction();
1746 bool Is64Bit = Subtarget->is64Bit();
1747 bool IsStructRet = CallIsStructReturn(Outs);
1748
1749 assert((!isTailCall ||
1750 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1751 "IsEligibleForTailCallOptimization missed a case!");
1752 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001753 "Var args not supported with calling convention fastcc");
1754
Chris Lattner638402b2007-02-28 07:00:42 +00001755 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001756 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1758 ArgLocs, *DAG.getContext());
1759 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001760
Chris Lattner423c5f42007-02-28 05:31:48 +00001761 // Get a count of how many bytes are to be pushed on the stack.
1762 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001763 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001764 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001765
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001767 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001769 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1771 FPDiff = NumBytesCallerPushed - NumBytes;
1772
1773 // Set the delta of movement of the returnaddr stackslot.
1774 // But only set if delta is greater than previous delta.
1775 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1776 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1777 }
1778
Chris Lattnere563bbc2008-10-11 22:08:30 +00001779 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001780
Dan Gohman475871a2008-07-27 21:46:04 +00001781 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001782 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001783 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001784 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001785
Dan Gohman475871a2008-07-27 21:46:04 +00001786 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1787 SmallVector<SDValue, 8> MemOpChains;
1788 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001789
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001790 // Walk the register/memloc assignments, inserting copies/loads. In the case
1791 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001792 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1793 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001794 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001795 SDValue Arg = Outs[i].Val;
1796 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001797 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001798
Chris Lattner423c5f42007-02-28 05:31:48 +00001799 // Promote the value if needed.
1800 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001801 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001802 case CCValAssign::Full: break;
1803 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001804 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001805 break;
1806 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001807 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001808 break;
1809 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001810 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1811 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001812 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1813 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1814 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001815 } else
1816 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1817 break;
1818 case CCValAssign::BCvt:
1819 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001820 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001821 case CCValAssign::Indirect: {
1822 // Store the argument.
1823 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001824 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001825 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001826 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001827 Arg = SpillSlot;
1828 break;
1829 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001830 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001831
Chris Lattner423c5f42007-02-28 05:31:48 +00001832 if (VA.isRegLoc()) {
1833 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1834 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001835 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001836 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001838 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001839
Dan Gohman98ca4f22009-08-05 01:29:28 +00001840 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1841 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001842 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001843 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Evan Cheng32fe1032006-05-25 00:59:30 +00001846 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001847 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001848 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001849
Evan Cheng347d5f72006-04-28 21:29:37 +00001850 // Build a sequence of copy-to-reg nodes chained together with token chain
1851 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001852 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001853 // Tail call byval lowering might overwrite argument registers so in case of
1854 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001855 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001856 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001857 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001858 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001859 InFlag = Chain.getValue(1);
1860 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001861
Eric Christopherfd179292009-08-27 18:07:15 +00001862
Chris Lattner88e1fd52009-07-09 04:24:46 +00001863 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001864 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1865 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001866 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001867 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1868 DAG.getNode(X86ISD::GlobalBaseReg,
1869 DebugLoc::getUnknownLoc(),
1870 getPointerTy()),
1871 InFlag);
1872 InFlag = Chain.getValue(1);
1873 } else {
1874 // If we are tail calling and generating PIC/GOT style code load the
1875 // address of the callee into ECX. The value in ecx is used as target of
1876 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1877 // for tail calls on PIC/GOT architectures. Normally we would just put the
1878 // address of GOT into ebx and then call target@PLT. But for tail calls
1879 // ebx would be restored (since ebx is callee saved) before jumping to the
1880 // target@PLT.
1881
1882 // Note: The actual moving to ECX is done further down.
1883 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1884 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1885 !G->getGlobal()->hasProtectedVisibility())
1886 Callee = LowerGlobalAddress(Callee, DAG);
1887 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001888 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001889 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001890 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001891
Gordon Henriksen86737662008-01-05 16:56:59 +00001892 if (Is64Bit && isVarArg) {
1893 // From AMD64 ABI document:
1894 // For calls that may call functions that use varargs or stdargs
1895 // (prototype-less calls or calls to functions containing ellipsis (...) in
1896 // the declaration) %al is used as hidden argument to specify the number
1897 // of SSE registers used. The contents of %al do not need to match exactly
1898 // the number of registers, but must be an ubound on the number of SSE
1899 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001900
1901 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001902 // Count the number of XMM registers allocated.
1903 static const unsigned XMMArgRegs[] = {
1904 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1905 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1906 };
1907 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001908 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001909 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001910
Dale Johannesendd64c412009-02-04 00:33:20 +00001911 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001912 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001913 InFlag = Chain.getValue(1);
1914 }
1915
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001916
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001917 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001918 if (isTailCall) {
1919 // Force all the incoming stack arguments to be loaded from the stack
1920 // before any new outgoing arguments are stored to the stack, because the
1921 // outgoing stack slots may alias the incoming argument stack slots, and
1922 // the alias isn't otherwise explicit. This is slightly more conservative
1923 // than necessary, because it means that each store effectively depends
1924 // on every argument instead of just those arguments it would clobber.
1925 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1926
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SmallVector<SDValue, 8> MemOpChains2;
1928 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001929 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001930 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001931 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001932 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1933 CCValAssign &VA = ArgLocs[i];
1934 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001935 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001936 SDValue Arg = Outs[i].Val;
1937 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001938 // Create frame index.
1939 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001940 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001941 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001942 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001943
Duncan Sands276dcbd2008-03-21 09:14:45 +00001944 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001945 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001947 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001948 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001949 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001950 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001951
Dan Gohman98ca4f22009-08-05 01:29:28 +00001952 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1953 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001954 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001956 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001957 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001959 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001960 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001961 }
1962 }
1963
1964 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001966 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001967
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001968 // Copy arguments to their registers.
1969 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001970 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001971 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001972 InFlag = Chain.getValue(1);
1973 }
Dan Gohman475871a2008-07-27 21:46:04 +00001974 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001975
Gordon Henriksen86737662008-01-05 16:56:59 +00001976 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001977 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001978 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 }
1980
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00001981 bool WasGlobalOrExternal = false;
1982 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
1983 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
1984 // In the 64-bit large code model, we have to make all calls
1985 // through a register, since the call instruction's 32-bit
1986 // pc-relative offset may not be large enough to hold the whole
1987 // address.
1988 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1989 WasGlobalOrExternal = true;
1990 // If the callee is a GlobalAddress node (quite common, every direct call
1991 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
1992 // it.
1993
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001994 // We should use extra load for direct calls to dllimported functions in
1995 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001996 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001997 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001998 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00001999
Chris Lattner48a7d022009-07-09 05:02:21 +00002000 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2001 // external symbols most go through the PLT in PIC mode. If the symbol
2002 // has hidden or protected visibility, or if it is static or local, then
2003 // we don't need to use the PLT - we can directly call it.
2004 if (Subtarget->isTargetELF() &&
2005 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002006 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002007 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002008 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002009 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2010 Subtarget->getDarwinVers() < 9) {
2011 // PC-relative references to external symbols should go through $stub,
2012 // unless we're building with the leopard linker or later, which
2013 // automatically synthesizes these stubs.
2014 OpFlags = X86II::MO_DARWIN_STUB;
2015 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002016
Chris Lattner74e726e2009-07-09 05:27:35 +00002017 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002018 G->getOffset(), OpFlags);
2019 }
Bill Wendling056292f2008-09-16 21:48:12 +00002020 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002021 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002022 unsigned char OpFlags = 0;
2023
2024 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2025 // symbols should go through the PLT.
2026 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002027 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002028 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002029 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002030 Subtarget->getDarwinVers() < 9) {
2031 // PC-relative references to external symbols should go through $stub,
2032 // unless we're building with the leopard linker or later, which
2033 // automatically synthesizes these stubs.
2034 OpFlags = X86II::MO_DARWIN_STUB;
2035 }
Eric Christopherfd179292009-08-27 18:07:15 +00002036
Chris Lattner48a7d022009-07-09 05:02:21 +00002037 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2038 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002039 }
2040
2041 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002042 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002043
Dale Johannesendd64c412009-02-04 00:33:20 +00002044 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002045 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 Callee,InFlag);
2047 Callee = DAG.getRegister(Opc, getPointerTy());
2048 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002049 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002050 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002051
Chris Lattnerd96d0722007-02-25 06:40:16 +00002052 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002054 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002055
Dan Gohman98ca4f22009-08-05 01:29:28 +00002056 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002057 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2058 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002059 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002061
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002062 Ops.push_back(Chain);
2063 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002064
Dan Gohman98ca4f22009-08-05 01:29:28 +00002065 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002066 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002067
Gordon Henriksen86737662008-01-05 16:56:59 +00002068 // Add argument registers to the end of the list so that they are known live
2069 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002070 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2071 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2072 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Evan Cheng586ccac2008-03-18 23:36:35 +00002074 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002076 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2077
2078 // Add an implicit use of AL for x86 vararg functions.
2079 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002080 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002081
Gabor Greifba36cb52008-08-28 21:40:38 +00002082 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002083 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002084
Dan Gohman98ca4f22009-08-05 01:29:28 +00002085 if (isTailCall) {
2086 // If this is the first return lowered for this function, add the regs
2087 // to the liveout set for the function.
2088 if (MF.getRegInfo().liveout_empty()) {
2089 SmallVector<CCValAssign, 16> RVLocs;
2090 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2091 *DAG.getContext());
2092 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2093 for (unsigned i = 0; i != RVLocs.size(); ++i)
2094 if (RVLocs[i].isRegLoc())
2095 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098 assert(((Callee.getOpcode() == ISD::Register &&
2099 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002100 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2102 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002103 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104
2105 return DAG.getNode(X86ISD::TC_RETURN, dl,
2106 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002107 }
2108
Dale Johannesenace16102009-02-03 19:33:06 +00002109 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002110 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002111
Chris Lattner2d297092006-05-23 18:50:38 +00002112 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002113 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002115 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002117 // If this is is a call to a struct-return function, the callee
2118 // pops the hidden struct pointer, so we have to push it back.
2119 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002120 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002121 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002122 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002123
Gordon Henriksenae636f82008-01-03 16:47:34 +00002124 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002125 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002126 DAG.getIntPtrConstant(NumBytes, true),
2127 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2128 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002129 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002130 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002131
Chris Lattner3085e152007-02-25 08:59:22 +00002132 // Handle result values, copying them out of physregs into vregs that we
2133 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002134 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2135 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002136}
2137
Evan Cheng25ab6902006-09-08 06:48:29 +00002138
2139//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002140// Fast Calling Convention (tail call) implementation
2141//===----------------------------------------------------------------------===//
2142
2143// Like std call, callee cleans arguments, convention except that ECX is
2144// reserved for storing the tail called function address. Only 2 registers are
2145// free for argument passing (inreg). Tail call optimization is performed
2146// provided:
2147// * tailcallopt is enabled
2148// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002149// On X86_64 architecture with GOT-style position independent code only local
2150// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002151// To keep the stack aligned according to platform abi the function
2152// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2153// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002154// If a tail called function callee has more arguments than the caller the
2155// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002156// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002157// original REtADDR, but before the saved framepointer or the spilled registers
2158// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2159// stack layout:
2160// arg1
2161// arg2
2162// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002163// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002164// move area ]
2165// (possible EBP)
2166// ESI
2167// EDI
2168// local1 ..
2169
2170/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2171/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002172unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002173 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002174 MachineFunction &MF = DAG.getMachineFunction();
2175 const TargetMachine &TM = MF.getTarget();
2176 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2177 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002178 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002179 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002180 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002181 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2182 // Number smaller than 12 so just add the difference.
2183 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2184 } else {
2185 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002186 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002187 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002188 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002189 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002190}
2191
Dan Gohman98ca4f22009-08-05 01:29:28 +00002192/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2193/// for tail call optimization. Targets which want to do tail call
2194/// optimization should implement this function.
2195bool
2196X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002197 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002198 bool isVarArg,
2199 const SmallVectorImpl<ISD::InputArg> &Ins,
2200 SelectionDAG& DAG) const {
2201 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002202 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002203 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002204}
2205
Dan Gohman3df24e62008-09-03 23:12:08 +00002206FastISel *
2207X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002208 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002209 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002210 DenseMap<const Value *, unsigned> &vm,
2211 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002212 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002213 DenseMap<const AllocaInst *, int> &am
2214#ifndef NDEBUG
2215 , SmallSet<Instruction*, 8> &cil
2216#endif
2217 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002218 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002219#ifndef NDEBUG
2220 , cil
2221#endif
2222 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002223}
2224
2225
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002226//===----------------------------------------------------------------------===//
2227// Other Lowering Hooks
2228//===----------------------------------------------------------------------===//
2229
2230
Dan Gohman475871a2008-07-27 21:46:04 +00002231SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002232 MachineFunction &MF = DAG.getMachineFunction();
2233 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2234 int ReturnAddrIndex = FuncInfo->getRAIndex();
2235
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002236 if (ReturnAddrIndex == 0) {
2237 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002238 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002239 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2240 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002241 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002242 }
2243
Evan Cheng25ab6902006-09-08 06:48:29 +00002244 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002245}
2246
2247
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002248bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2249 bool hasSymbolicDisplacement) {
2250 // Offset should fit into 32 bit immediate field.
2251 if (!isInt32(Offset))
2252 return false;
2253
2254 // If we don't have a symbolic displacement - we don't have any extra
2255 // restrictions.
2256 if (!hasSymbolicDisplacement)
2257 return true;
2258
2259 // FIXME: Some tweaks might be needed for medium code model.
2260 if (M != CodeModel::Small && M != CodeModel::Kernel)
2261 return false;
2262
2263 // For small code model we assume that latest object is 16MB before end of 31
2264 // bits boundary. We may also accept pretty large negative constants knowing
2265 // that all objects are in the positive half of address space.
2266 if (M == CodeModel::Small && Offset < 16*1024*1024)
2267 return true;
2268
2269 // For kernel code model we know that all object resist in the negative half
2270 // of 32bits address space. We may not accept negative offsets, since they may
2271 // be just off and we may accept pretty large positive ones.
2272 if (M == CodeModel::Kernel && Offset > 0)
2273 return true;
2274
2275 return false;
2276}
2277
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002278/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2279/// specific condition code, returning the condition code and the LHS/RHS of the
2280/// comparison to make.
2281static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2282 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002283 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002284 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2285 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2286 // X > -1 -> X == 0, jump !sign.
2287 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002288 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002289 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2290 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002291 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002292 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002293 // X < 1 -> X <= 0
2294 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002295 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002296 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002297 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002298
Evan Chengd9558e02006-01-06 00:43:03 +00002299 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002300 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002301 case ISD::SETEQ: return X86::COND_E;
2302 case ISD::SETGT: return X86::COND_G;
2303 case ISD::SETGE: return X86::COND_GE;
2304 case ISD::SETLT: return X86::COND_L;
2305 case ISD::SETLE: return X86::COND_LE;
2306 case ISD::SETNE: return X86::COND_NE;
2307 case ISD::SETULT: return X86::COND_B;
2308 case ISD::SETUGT: return X86::COND_A;
2309 case ISD::SETULE: return X86::COND_BE;
2310 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002311 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002312 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002313
Chris Lattner4c78e022008-12-23 23:42:27 +00002314 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002315
Chris Lattner4c78e022008-12-23 23:42:27 +00002316 // If LHS is a foldable load, but RHS is not, flip the condition.
2317 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2318 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2319 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2320 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002321 }
2322
Chris Lattner4c78e022008-12-23 23:42:27 +00002323 switch (SetCCOpcode) {
2324 default: break;
2325 case ISD::SETOLT:
2326 case ISD::SETOLE:
2327 case ISD::SETUGT:
2328 case ISD::SETUGE:
2329 std::swap(LHS, RHS);
2330 break;
2331 }
2332
2333 // On a floating point condition, the flags are set as follows:
2334 // ZF PF CF op
2335 // 0 | 0 | 0 | X > Y
2336 // 0 | 0 | 1 | X < Y
2337 // 1 | 0 | 0 | X == Y
2338 // 1 | 1 | 1 | unordered
2339 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002340 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002341 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002342 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002343 case ISD::SETOLT: // flipped
2344 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002345 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002346 case ISD::SETOLE: // flipped
2347 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002348 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002349 case ISD::SETUGT: // flipped
2350 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002351 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002352 case ISD::SETUGE: // flipped
2353 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002354 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002355 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002356 case ISD::SETNE: return X86::COND_NE;
2357 case ISD::SETUO: return X86::COND_P;
2358 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002359 case ISD::SETOEQ:
2360 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002361 }
Evan Chengd9558e02006-01-06 00:43:03 +00002362}
2363
Evan Cheng4a460802006-01-11 00:33:36 +00002364/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2365/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002366/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002367static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002368 switch (X86CC) {
2369 default:
2370 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002371 case X86::COND_B:
2372 case X86::COND_BE:
2373 case X86::COND_E:
2374 case X86::COND_P:
2375 case X86::COND_A:
2376 case X86::COND_AE:
2377 case X86::COND_NE:
2378 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002379 return true;
2380 }
2381}
2382
Evan Chengeb2f9692009-10-27 19:56:55 +00002383/// isFPImmLegal - Returns true if the target can instruction select the
2384/// specified FP immediate natively. If false, the legalizer will
2385/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002386bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002387 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2388 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2389 return true;
2390 }
2391 return false;
2392}
2393
Nate Begeman9008ca62009-04-27 18:41:29 +00002394/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2395/// the specified range (L, H].
2396static bool isUndefOrInRange(int Val, int Low, int Hi) {
2397 return (Val < 0) || (Val >= Low && Val < Hi);
2398}
2399
2400/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2401/// specified value.
2402static bool isUndefOrEqual(int Val, int CmpVal) {
2403 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002404 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002405 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002406}
2407
Nate Begeman9008ca62009-04-27 18:41:29 +00002408/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2409/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2410/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002411static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002412 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002413 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002414 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002415 return (Mask[0] < 2 && Mask[1] < 2);
2416 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002417}
2418
Nate Begeman9008ca62009-04-27 18:41:29 +00002419bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002420 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002421 N->getMask(M);
2422 return ::isPSHUFDMask(M, N->getValueType(0));
2423}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002424
Nate Begeman9008ca62009-04-27 18:41:29 +00002425/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2426/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002427static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002428 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002429 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002430
Nate Begeman9008ca62009-04-27 18:41:29 +00002431 // Lower quadword copied in order or undef.
2432 for (int i = 0; i != 4; ++i)
2433 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002434 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002435
Evan Cheng506d3df2006-03-29 23:07:14 +00002436 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002437 for (int i = 4; i != 8; ++i)
2438 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002439 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002440
Evan Cheng506d3df2006-03-29 23:07:14 +00002441 return true;
2442}
2443
Nate Begeman9008ca62009-04-27 18:41:29 +00002444bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002445 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002446 N->getMask(M);
2447 return ::isPSHUFHWMask(M, N->getValueType(0));
2448}
Evan Cheng506d3df2006-03-29 23:07:14 +00002449
Nate Begeman9008ca62009-04-27 18:41:29 +00002450/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2451/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002452static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002453 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002454 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002455
Rafael Espindola15684b22009-04-24 12:40:33 +00002456 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002457 for (int i = 4; i != 8; ++i)
2458 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002459 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002460
Rafael Espindola15684b22009-04-24 12:40:33 +00002461 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002462 for (int i = 0; i != 4; ++i)
2463 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002464 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002465
Rafael Espindola15684b22009-04-24 12:40:33 +00002466 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002467}
2468
Nate Begeman9008ca62009-04-27 18:41:29 +00002469bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002470 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002471 N->getMask(M);
2472 return ::isPSHUFLWMask(M, N->getValueType(0));
2473}
2474
Nate Begemana09008b2009-10-19 02:17:23 +00002475/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2476/// is suitable for input to PALIGNR.
2477static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2478 bool hasSSSE3) {
2479 int i, e = VT.getVectorNumElements();
2480
2481 // Do not handle v2i64 / v2f64 shuffles with palignr.
2482 if (e < 4 || !hasSSSE3)
2483 return false;
2484
2485 for (i = 0; i != e; ++i)
2486 if (Mask[i] >= 0)
2487 break;
2488
2489 // All undef, not a palignr.
2490 if (i == e)
2491 return false;
2492
2493 // Determine if it's ok to perform a palignr with only the LHS, since we
2494 // don't have access to the actual shuffle elements to see if RHS is undef.
2495 bool Unary = Mask[i] < (int)e;
2496 bool NeedsUnary = false;
2497
2498 int s = Mask[i] - i;
2499
2500 // Check the rest of the elements to see if they are consecutive.
2501 for (++i; i != e; ++i) {
2502 int m = Mask[i];
2503 if (m < 0)
2504 continue;
2505
2506 Unary = Unary && (m < (int)e);
2507 NeedsUnary = NeedsUnary || (m < s);
2508
2509 if (NeedsUnary && !Unary)
2510 return false;
2511 if (Unary && m != ((s+i) & (e-1)))
2512 return false;
2513 if (!Unary && m != (s+i))
2514 return false;
2515 }
2516 return true;
2517}
2518
2519bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2520 SmallVector<int, 8> M;
2521 N->getMask(M);
2522 return ::isPALIGNRMask(M, N->getValueType(0), true);
2523}
2524
Evan Cheng14aed5e2006-03-24 01:18:28 +00002525/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2526/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002527static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002528 int NumElems = VT.getVectorNumElements();
2529 if (NumElems != 2 && NumElems != 4)
2530 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002531
Nate Begeman9008ca62009-04-27 18:41:29 +00002532 int Half = NumElems / 2;
2533 for (int i = 0; i < Half; ++i)
2534 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002535 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 for (int i = Half; i < NumElems; ++i)
2537 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002538 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002539
Evan Cheng14aed5e2006-03-24 01:18:28 +00002540 return true;
2541}
2542
Nate Begeman9008ca62009-04-27 18:41:29 +00002543bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2544 SmallVector<int, 8> M;
2545 N->getMask(M);
2546 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002547}
2548
Evan Cheng213d2cf2007-05-17 18:45:50 +00002549/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002550/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2551/// half elements to come from vector 1 (which would equal the dest.) and
2552/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002553static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002554 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002555
2556 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002557 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002558
Nate Begeman9008ca62009-04-27 18:41:29 +00002559 int Half = NumElems / 2;
2560 for (int i = 0; i < Half; ++i)
2561 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002562 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002563 for (int i = Half; i < NumElems; ++i)
2564 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002565 return false;
2566 return true;
2567}
2568
Nate Begeman9008ca62009-04-27 18:41:29 +00002569static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2570 SmallVector<int, 8> M;
2571 N->getMask(M);
2572 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002573}
2574
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002575/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2576/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002577bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2578 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002579 return false;
2580
Evan Cheng2064a2b2006-03-28 06:50:32 +00002581 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002582 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2583 isUndefOrEqual(N->getMaskElt(1), 7) &&
2584 isUndefOrEqual(N->getMaskElt(2), 2) &&
2585 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002586}
2587
Nate Begeman0b10b912009-11-07 23:17:15 +00002588/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2589/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2590/// <2, 3, 2, 3>
2591bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2592 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2593
2594 if (NumElems != 4)
2595 return false;
2596
2597 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2598 isUndefOrEqual(N->getMaskElt(1), 3) &&
2599 isUndefOrEqual(N->getMaskElt(2), 2) &&
2600 isUndefOrEqual(N->getMaskElt(3), 3);
2601}
2602
Evan Cheng5ced1d82006-04-06 23:23:56 +00002603/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2604/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002605bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2606 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002607
Evan Cheng5ced1d82006-04-06 23:23:56 +00002608 if (NumElems != 2 && NumElems != 4)
2609 return false;
2610
Evan Chengc5cdff22006-04-07 21:53:05 +00002611 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002613 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002614
Evan Chengc5cdff22006-04-07 21:53:05 +00002615 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002617 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002618
2619 return true;
2620}
2621
Nate Begeman0b10b912009-11-07 23:17:15 +00002622/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2623/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2624bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002626
Evan Cheng5ced1d82006-04-06 23:23:56 +00002627 if (NumElems != 2 && NumElems != 4)
2628 return false;
2629
Evan Chengc5cdff22006-04-07 21:53:05 +00002630 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002631 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002632 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002633
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 for (unsigned i = 0; i < NumElems/2; ++i)
2635 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002636 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002637
2638 return true;
2639}
2640
Evan Cheng0038e592006-03-28 00:39:58 +00002641/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2642/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002643static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002644 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002645 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002646 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002647 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002648
Nate Begeman9008ca62009-04-27 18:41:29 +00002649 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2650 int BitI = Mask[i];
2651 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002652 if (!isUndefOrEqual(BitI, j))
2653 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002654 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002655 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002656 return false;
2657 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002658 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002659 return false;
2660 }
Evan Cheng0038e592006-03-28 00:39:58 +00002661 }
Evan Cheng0038e592006-03-28 00:39:58 +00002662 return true;
2663}
2664
Nate Begeman9008ca62009-04-27 18:41:29 +00002665bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2666 SmallVector<int, 8> M;
2667 N->getMask(M);
2668 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002669}
2670
Evan Cheng4fcb9222006-03-28 02:43:26 +00002671/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2672/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002673static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002674 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002675 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002676 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002677 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002678
Nate Begeman9008ca62009-04-27 18:41:29 +00002679 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2680 int BitI = Mask[i];
2681 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002682 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002683 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002684 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002685 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002686 return false;
2687 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002688 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002689 return false;
2690 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002691 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002692 return true;
2693}
2694
Nate Begeman9008ca62009-04-27 18:41:29 +00002695bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2696 SmallVector<int, 8> M;
2697 N->getMask(M);
2698 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002699}
2700
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002701/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2702/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2703/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002704static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002706 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002707 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002708
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2710 int BitI = Mask[i];
2711 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002712 if (!isUndefOrEqual(BitI, j))
2713 return false;
2714 if (!isUndefOrEqual(BitI1, j))
2715 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002716 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002717 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002718}
2719
Nate Begeman9008ca62009-04-27 18:41:29 +00002720bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2721 SmallVector<int, 8> M;
2722 N->getMask(M);
2723 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2724}
2725
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002726/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2727/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2728/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002729static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002730 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002731 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2732 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002733
Nate Begeman9008ca62009-04-27 18:41:29 +00002734 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2735 int BitI = Mask[i];
2736 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002737 if (!isUndefOrEqual(BitI, j))
2738 return false;
2739 if (!isUndefOrEqual(BitI1, j))
2740 return false;
2741 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002742 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002743}
2744
Nate Begeman9008ca62009-04-27 18:41:29 +00002745bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2746 SmallVector<int, 8> M;
2747 N->getMask(M);
2748 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2749}
2750
Evan Cheng017dcc62006-04-21 01:05:10 +00002751/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2752/// specifies a shuffle of elements that is suitable for input to MOVSS,
2753/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002754static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002755 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002756 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002757
2758 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002759
Nate Begeman9008ca62009-04-27 18:41:29 +00002760 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002761 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002762
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 for (int i = 1; i < NumElts; ++i)
2764 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002765 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002766
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002767 return true;
2768}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002769
Nate Begeman9008ca62009-04-27 18:41:29 +00002770bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2771 SmallVector<int, 8> M;
2772 N->getMask(M);
2773 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002774}
2775
Evan Cheng017dcc62006-04-21 01:05:10 +00002776/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2777/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002778/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002779static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002780 bool V2IsSplat = false, bool V2IsUndef = false) {
2781 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002782 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002783 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002784
Nate Begeman9008ca62009-04-27 18:41:29 +00002785 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002786 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002787
Nate Begeman9008ca62009-04-27 18:41:29 +00002788 for (int i = 1; i < NumOps; ++i)
2789 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2790 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2791 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002792 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002793
Evan Cheng39623da2006-04-20 08:58:49 +00002794 return true;
2795}
2796
Nate Begeman9008ca62009-04-27 18:41:29 +00002797static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002798 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002799 SmallVector<int, 8> M;
2800 N->getMask(M);
2801 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002802}
2803
Evan Chengd9539472006-04-14 21:59:03 +00002804/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2805/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002806bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2807 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002808 return false;
2809
2810 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002811 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002812 int Elt = N->getMaskElt(i);
2813 if (Elt >= 0 && Elt != 1)
2814 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002815 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002816
2817 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002818 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 int Elt = N->getMaskElt(i);
2820 if (Elt >= 0 && Elt != 3)
2821 return false;
2822 if (Elt == 3)
2823 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002824 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002825 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002826 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002827 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002828}
2829
2830/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2831/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002832bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2833 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002834 return false;
2835
2836 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 for (unsigned i = 0; i < 2; ++i)
2838 if (N->getMaskElt(i) > 0)
2839 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002840
2841 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002842 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 int Elt = N->getMaskElt(i);
2844 if (Elt >= 0 && Elt != 2)
2845 return false;
2846 if (Elt == 2)
2847 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002848 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002850 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002851}
2852
Evan Cheng0b457f02008-09-25 20:50:48 +00002853/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2854/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002855bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2856 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002857
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 for (int i = 0; i < e; ++i)
2859 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002860 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 for (int i = 0; i < e; ++i)
2862 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002863 return false;
2864 return true;
2865}
2866
Evan Cheng63d33002006-03-22 08:01:21 +00002867/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002868/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002869unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2871 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2872
Evan Chengb9df0ca2006-03-22 02:53:00 +00002873 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2874 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002875 for (int i = 0; i < NumOperands; ++i) {
2876 int Val = SVOp->getMaskElt(NumOperands-i-1);
2877 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002878 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002879 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002880 if (i != NumOperands - 1)
2881 Mask <<= Shift;
2882 }
Evan Cheng63d33002006-03-22 08:01:21 +00002883 return Mask;
2884}
2885
Evan Cheng506d3df2006-03-29 23:07:14 +00002886/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002887/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002888unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002890 unsigned Mask = 0;
2891 // 8 nodes, but we only care about the last 4.
2892 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002893 int Val = SVOp->getMaskElt(i);
2894 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002895 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002896 if (i != 4)
2897 Mask <<= 2;
2898 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002899 return Mask;
2900}
2901
2902/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002903/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002904unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002905 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002906 unsigned Mask = 0;
2907 // 8 nodes, but we only care about the first 4.
2908 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002909 int Val = SVOp->getMaskElt(i);
2910 if (Val >= 0)
2911 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002912 if (i != 0)
2913 Mask <<= 2;
2914 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002915 return Mask;
2916}
2917
Nate Begemana09008b2009-10-19 02:17:23 +00002918/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2919/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2920unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2921 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2922 EVT VVT = N->getValueType(0);
2923 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2924 int Val = 0;
2925
2926 unsigned i, e;
2927 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2928 Val = SVOp->getMaskElt(i);
2929 if (Val >= 0)
2930 break;
2931 }
2932 return (Val - i) * EltSize;
2933}
2934
Evan Cheng37b73872009-07-30 08:33:02 +00002935/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2936/// constant +0.0.
2937bool X86::isZeroNode(SDValue Elt) {
2938 return ((isa<ConstantSDNode>(Elt) &&
2939 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2940 (isa<ConstantFPSDNode>(Elt) &&
2941 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2942}
2943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2945/// their permute mask.
2946static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2947 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002948 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002949 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002951
Nate Begeman5a5ca152009-04-29 05:20:52 +00002952 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 int idx = SVOp->getMaskElt(i);
2954 if (idx < 0)
2955 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002956 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002958 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002960 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2962 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002963}
2964
Evan Cheng779ccea2007-12-07 21:30:01 +00002965/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2966/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00002967static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002968 unsigned NumElems = VT.getVectorNumElements();
2969 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 int idx = Mask[i];
2971 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002972 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002973 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002975 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002977 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002978}
2979
Evan Cheng533a0aa2006-04-19 20:35:22 +00002980/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2981/// match movhlps. The lower half elements should come from upper half of
2982/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002983/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002984static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2985 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002986 return false;
2987 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002988 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002989 return false;
2990 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002991 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002992 return false;
2993 return true;
2994}
2995
Evan Cheng5ced1d82006-04-06 23:23:56 +00002996/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002997/// is promoted to a vector. It also returns the LoadSDNode by reference if
2998/// required.
2999static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003000 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3001 return false;
3002 N = N->getOperand(0).getNode();
3003 if (!ISD::isNON_EXTLoad(N))
3004 return false;
3005 if (LD)
3006 *LD = cast<LoadSDNode>(N);
3007 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003008}
3009
Evan Cheng533a0aa2006-04-19 20:35:22 +00003010/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3011/// match movlp{s|d}. The lower half elements should come from lower half of
3012/// V1 (and in order), and the upper half elements should come from the upper
3013/// half of V2 (and in order). And since V1 will become the source of the
3014/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003015static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3016 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003017 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003018 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003019 // Is V2 is a vector load, don't do this transformation. We will try to use
3020 // load folding shufps op.
3021 if (ISD::isNON_EXTLoad(V2))
3022 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003023
Nate Begeman5a5ca152009-04-29 05:20:52 +00003024 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003025
Evan Cheng533a0aa2006-04-19 20:35:22 +00003026 if (NumElems != 2 && NumElems != 4)
3027 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003028 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003029 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003030 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003031 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003033 return false;
3034 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003035}
3036
Evan Cheng39623da2006-04-20 08:58:49 +00003037/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3038/// all the same.
3039static bool isSplatVector(SDNode *N) {
3040 if (N->getOpcode() != ISD::BUILD_VECTOR)
3041 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003042
Dan Gohman475871a2008-07-27 21:46:04 +00003043 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003044 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3045 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003046 return false;
3047 return true;
3048}
3049
Evan Cheng213d2cf2007-05-17 18:45:50 +00003050/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003051/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003052/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003053static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003054 SDValue V1 = N->getOperand(0);
3055 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003056 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3057 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003059 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003060 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003061 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3062 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003063 if (Opc != ISD::BUILD_VECTOR ||
3064 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 return false;
3066 } else if (Idx >= 0) {
3067 unsigned Opc = V1.getOpcode();
3068 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3069 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003070 if (Opc != ISD::BUILD_VECTOR ||
3071 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003072 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003073 }
3074 }
3075 return true;
3076}
3077
3078/// getZeroVector - Returns a vector of specified type with all zero elements.
3079///
Owen Andersone50ed302009-08-10 22:56:29 +00003080static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003081 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003082 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003083
Chris Lattner8a594482007-11-25 00:24:49 +00003084 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3085 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003086 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3089 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003090 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3092 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003093 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003094 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3095 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003096 }
Dale Johannesenace16102009-02-03 19:33:06 +00003097 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003098}
3099
Chris Lattner8a594482007-11-25 00:24:49 +00003100/// getOnesVector - Returns a vector of specified type with all bits set.
3101///
Owen Andersone50ed302009-08-10 22:56:29 +00003102static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003103 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003104
Chris Lattner8a594482007-11-25 00:24:49 +00003105 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3106 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003109 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003111 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003113 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003114}
3115
3116
Evan Cheng39623da2006-04-20 08:58:49 +00003117/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3118/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003119static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003120 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003121 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003122
Evan Cheng39623da2006-04-20 08:58:49 +00003123 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003124 SmallVector<int, 8> MaskVec;
3125 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003126
Nate Begeman5a5ca152009-04-29 05:20:52 +00003127 for (unsigned i = 0; i != NumElems; ++i) {
3128 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 MaskVec[i] = NumElems;
3130 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003131 }
Evan Cheng39623da2006-04-20 08:58:49 +00003132 }
Evan Cheng39623da2006-04-20 08:58:49 +00003133 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3135 SVOp->getOperand(1), &MaskVec[0]);
3136 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003137}
3138
Evan Cheng017dcc62006-04-21 01:05:10 +00003139/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3140/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003141static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 SDValue V2) {
3143 unsigned NumElems = VT.getVectorNumElements();
3144 SmallVector<int, 8> Mask;
3145 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003146 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 Mask.push_back(i);
3148 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003149}
3150
Nate Begeman9008ca62009-04-27 18:41:29 +00003151/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 SDValue V2) {
3154 unsigned NumElems = VT.getVectorNumElements();
3155 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003156 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 Mask.push_back(i);
3158 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003159 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003161}
3162
Nate Begeman9008ca62009-04-27 18:41:29 +00003163/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003164static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 SDValue V2) {
3166 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003167 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003169 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 Mask.push_back(i + Half);
3171 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003172 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003173 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003174}
3175
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003176/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003177static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 bool HasSSE2) {
3179 if (SV->getValueType(0).getVectorNumElements() <= 4)
3180 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003181
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003183 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 DebugLoc dl = SV->getDebugLoc();
3185 SDValue V1 = SV->getOperand(0);
3186 int NumElems = VT.getVectorNumElements();
3187 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003188
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 // unpack elements to the correct location
3190 while (NumElems > 4) {
3191 if (EltNo < NumElems/2) {
3192 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3193 } else {
3194 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3195 EltNo -= NumElems/2;
3196 }
3197 NumElems >>= 1;
3198 }
Eric Christopherfd179292009-08-27 18:07:15 +00003199
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 // Perform the splat.
3201 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003202 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3204 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003205}
3206
Evan Chengba05f722006-04-21 23:03:30 +00003207/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003208/// vector of zero or undef vector. This produces a shuffle where the low
3209/// element of V2 is swizzled into the zero/undef vector, landing at element
3210/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00003211static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003212 bool isZero, bool HasSSE2,
3213 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003214 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003215 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3217 unsigned NumElems = VT.getVectorNumElements();
3218 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003219 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 // If this is the insertion idx, put the low elt of V2 here.
3221 MaskVec.push_back(i == Idx ? NumElems : i);
3222 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003223}
3224
Evan Chengf26ffe92008-05-29 08:22:04 +00003225/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3226/// a shuffle that is zero.
3227static
Nate Begeman9008ca62009-04-27 18:41:29 +00003228unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3229 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003230 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003232 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 int Idx = SVOp->getMaskElt(Index);
3234 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003235 ++NumZeros;
3236 continue;
3237 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003239 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003240 ++NumZeros;
3241 else
3242 break;
3243 }
3244 return NumZeros;
3245}
3246
3247/// isVectorShift - Returns true if the shuffle can be implemented as a
3248/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003249/// FIXME: split into pslldqi, psrldqi, palignr variants.
3250static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003251 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003252 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003253
3254 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003256 if (!NumZeros) {
3257 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003259 if (!NumZeros)
3260 return false;
3261 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003262 bool SeenV1 = false;
3263 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 for (int i = NumZeros; i < NumElems; ++i) {
3265 int Val = isLeft ? (i - NumZeros) : i;
3266 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3267 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003268 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003270 SeenV1 = true;
3271 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003272 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003273 SeenV2 = true;
3274 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003276 return false;
3277 }
3278 if (SeenV1 && SeenV2)
3279 return false;
3280
Nate Begeman9008ca62009-04-27 18:41:29 +00003281 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003282 ShAmt = NumZeros;
3283 return true;
3284}
3285
3286
Evan Chengc78d3b42006-04-24 18:01:45 +00003287/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3288///
Dan Gohman475871a2008-07-27 21:46:04 +00003289static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003290 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003291 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003292 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003293 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003294
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003295 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003297 bool First = true;
3298 for (unsigned i = 0; i < 16; ++i) {
3299 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3300 if (ThisIsNonZero && First) {
3301 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003303 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003304 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003305 First = false;
3306 }
3307
3308 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003309 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003310 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3311 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003312 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003314 }
3315 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3317 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3318 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003319 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003320 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003321 } else
3322 ThisElt = LastElt;
3323
Gabor Greifba36cb52008-08-28 21:40:38 +00003324 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003326 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003327 }
3328 }
3329
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003331}
3332
Bill Wendlinga348c562007-03-22 18:42:45 +00003333/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003334///
Dan Gohman475871a2008-07-27 21:46:04 +00003335static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003336 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003337 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003338 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003339 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003340
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003341 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003343 bool First = true;
3344 for (unsigned i = 0; i < 8; ++i) {
3345 bool isNonZero = (NonZeros & (1 << i)) != 0;
3346 if (isNonZero) {
3347 if (First) {
3348 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003350 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003352 First = false;
3353 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003354 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003356 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003357 }
3358 }
3359
3360 return V;
3361}
3362
Evan Chengf26ffe92008-05-29 08:22:04 +00003363/// getVShift - Return a vector logical shift node.
3364///
Owen Andersone50ed302009-08-10 22:56:29 +00003365static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 unsigned NumBits, SelectionDAG &DAG,
3367 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003368 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003370 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003371 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3372 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3373 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003374 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003375}
3376
Dan Gohman475871a2008-07-27 21:46:04 +00003377SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003378X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3379 SelectionDAG &DAG) {
3380
3381 // Check if the scalar load can be widened into a vector load. And if
3382 // the address is "base + cst" see if the cst can be "absorbed" into
3383 // the shuffle mask.
3384 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3385 SDValue Ptr = LD->getBasePtr();
3386 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3387 return SDValue();
3388 EVT PVT = LD->getValueType(0);
3389 if (PVT != MVT::i32 && PVT != MVT::f32)
3390 return SDValue();
3391
3392 int FI = -1;
3393 int64_t Offset = 0;
3394 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3395 FI = FINode->getIndex();
3396 Offset = 0;
3397 } else if (Ptr.getOpcode() == ISD::ADD &&
3398 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3399 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3400 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3401 Offset = Ptr.getConstantOperandVal(1);
3402 Ptr = Ptr.getOperand(0);
3403 } else {
3404 return SDValue();
3405 }
3406
3407 SDValue Chain = LD->getChain();
3408 // Make sure the stack object alignment is at least 16.
3409 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3410 if (DAG.InferPtrAlignment(Ptr) < 16) {
3411 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003412 // Can't change the alignment. FIXME: It's possible to compute
3413 // the exact stack offset and reference FI + adjust offset instead.
3414 // If someone *really* cares about this. That's the way to implement it.
3415 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003416 } else {
3417 MFI->setObjectAlignment(FI, 16);
3418 }
3419 }
3420
3421 // (Offset % 16) must be multiple of 4. Then address is then
3422 // Ptr + (Offset & ~15).
3423 if (Offset < 0)
3424 return SDValue();
3425 if ((Offset % 16) & 3)
3426 return SDValue();
3427 int64_t StartOffset = Offset & ~15;
3428 if (StartOffset)
3429 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3430 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3431
3432 int EltNo = (Offset - StartOffset) >> 2;
3433 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3434 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3435 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3436 // Canonicalize it to a v4i32 shuffle.
3437 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3438 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3439 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3440 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3441 }
3442
3443 return SDValue();
3444}
3445
3446SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003447X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003448 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003449 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003450 if (ISD::isBuildVectorAllZeros(Op.getNode())
3451 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003452 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3453 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3454 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003456 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003457
Gabor Greifba36cb52008-08-28 21:40:38 +00003458 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003459 return getOnesVector(Op.getValueType(), DAG, dl);
3460 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003461 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003462
Owen Andersone50ed302009-08-10 22:56:29 +00003463 EVT VT = Op.getValueType();
3464 EVT ExtVT = VT.getVectorElementType();
3465 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003466
3467 unsigned NumElems = Op.getNumOperands();
3468 unsigned NumZero = 0;
3469 unsigned NumNonZero = 0;
3470 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003471 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003472 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003473 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003474 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003475 if (Elt.getOpcode() == ISD::UNDEF)
3476 continue;
3477 Values.insert(Elt);
3478 if (Elt.getOpcode() != ISD::Constant &&
3479 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003480 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003481 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003482 NumZero++;
3483 else {
3484 NonZeros |= (1 << i);
3485 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003486 }
3487 }
3488
Dan Gohman7f321562007-06-25 16:23:39 +00003489 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003490 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003491 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003492 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003493
Chris Lattner67f453a2008-03-09 05:42:06 +00003494 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003495 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003496 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003497 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003498
Chris Lattner62098042008-03-09 01:05:04 +00003499 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3500 // the value are obviously zero, truncate the value to i32 and do the
3501 // insertion that way. Only do this if the value is non-constant or if the
3502 // value is a constant being inserted into element 0. It is cheaper to do
3503 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003505 (!IsAllConstants || Idx == 0)) {
3506 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3507 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3509 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003510
Chris Lattner62098042008-03-09 01:05:04 +00003511 // Truncate the value (which may itself be a constant) to i32, and
3512 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003514 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003515 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3516 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003517
Chris Lattner62098042008-03-09 01:05:04 +00003518 // Now we have our 32-bit value zero extended in the low element of
3519 // a vector. If Idx != 0, swizzle it into place.
3520 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003521 SmallVector<int, 4> Mask;
3522 Mask.push_back(Idx);
3523 for (unsigned i = 1; i != VecElts; ++i)
3524 Mask.push_back(i);
3525 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003526 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003528 }
Dale Johannesenace16102009-02-03 19:33:06 +00003529 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003530 }
3531 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003532
Chris Lattner19f79692008-03-08 22:59:52 +00003533 // If we have a constant or non-constant insertion into the low element of
3534 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3535 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003536 // depending on what the source datatype is.
3537 if (Idx == 0) {
3538 if (NumZero == 0) {
3539 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3541 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003542 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3543 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3544 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3545 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3547 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3548 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003549 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3550 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3551 Subtarget->hasSSE2(), DAG);
3552 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3553 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003554 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003555
3556 // Is it a vector logical left shift?
3557 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003558 X86::isZeroNode(Op.getOperand(0)) &&
3559 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003560 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003561 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003562 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003563 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003564 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003566
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003567 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003568 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003569
Chris Lattner19f79692008-03-08 22:59:52 +00003570 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3571 // is a non-constant being inserted into an element other than the low one,
3572 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3573 // movd/movss) to move this into the low element, then shuffle it into
3574 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003575 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003576 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003577
Evan Cheng0db9fe62006-04-25 20:13:52 +00003578 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003579 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3580 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003581 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003582 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003583 MaskVec.push_back(i == Idx ? 0 : 1);
3584 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003585 }
3586 }
3587
Chris Lattner67f453a2008-03-09 05:42:06 +00003588 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003589 if (Values.size() == 1) {
3590 if (EVTBits == 32) {
3591 // Instead of a shuffle like this:
3592 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3593 // Check if it's possible to issue this instead.
3594 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3595 unsigned Idx = CountTrailingZeros_32(NonZeros);
3596 SDValue Item = Op.getOperand(Idx);
3597 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3598 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3599 }
Dan Gohman475871a2008-07-27 21:46:04 +00003600 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003602
Dan Gohmana3941172007-07-24 22:55:08 +00003603 // A vector full of immediates; various special cases are already
3604 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003605 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003606 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003607
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003608 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003609 if (EVTBits == 64) {
3610 if (NumNonZero == 1) {
3611 // One half is zero or undef.
3612 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003613 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003614 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003615 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3616 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003617 }
Dan Gohman475871a2008-07-27 21:46:04 +00003618 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003619 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003620
3621 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003622 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003623 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003624 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003625 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003626 }
3627
Bill Wendling826f36f2007-03-28 00:57:11 +00003628 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003629 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003630 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003631 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003632 }
3633
3634 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003635 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003636 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003637 if (NumElems == 4 && NumZero > 0) {
3638 for (unsigned i = 0; i < 4; ++i) {
3639 bool isZero = !(NonZeros & (1 << i));
3640 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003641 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003642 else
Dale Johannesenace16102009-02-03 19:33:06 +00003643 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003644 }
3645
3646 for (unsigned i = 0; i < 2; ++i) {
3647 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3648 default: break;
3649 case 0:
3650 V[i] = V[i*2]; // Must be a zero vector.
3651 break;
3652 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003653 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003654 break;
3655 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003656 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003657 break;
3658 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003659 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003660 break;
3661 }
3662 }
3663
Nate Begeman9008ca62009-04-27 18:41:29 +00003664 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003665 bool Reverse = (NonZeros & 0x3) == 2;
3666 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003668 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3669 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003670 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3671 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003672 }
3673
3674 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003675 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3676 // values to be inserted is equal to the number of elements, in which case
3677 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003678 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003679 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003680 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 getSubtarget()->hasSSE41()) {
3682 V[0] = DAG.getUNDEF(VT);
3683 for (unsigned i = 0; i < NumElems; ++i)
3684 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3685 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3686 Op.getOperand(i), DAG.getIntPtrConstant(i));
3687 return V[0];
3688 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003689 // Expand into a number of unpckl*.
3690 // e.g. for v4f32
3691 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3692 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3693 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003695 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003696 NumElems >>= 1;
3697 while (NumElems != 0) {
3698 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003700 NumElems >>= 1;
3701 }
3702 return V[0];
3703 }
3704
Dan Gohman475871a2008-07-27 21:46:04 +00003705 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003706}
3707
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003708SDValue
3709X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3710 // We support concatenate two MMX registers and place them in a MMX
3711 // register. This is better than doing a stack convert.
3712 DebugLoc dl = Op.getDebugLoc();
3713 EVT ResVT = Op.getValueType();
3714 assert(Op.getNumOperands() == 2);
3715 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3716 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3717 int Mask[2];
3718 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3719 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3720 InVec = Op.getOperand(1);
3721 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3722 unsigned NumElts = ResVT.getVectorNumElements();
3723 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3724 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3725 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3726 } else {
3727 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3728 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3729 Mask[0] = 0; Mask[1] = 2;
3730 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3731 }
3732 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3733}
3734
Nate Begemanb9a47b82009-02-23 08:49:38 +00003735// v8i16 shuffles - Prefer shuffles in the following order:
3736// 1. [all] pshuflw, pshufhw, optional move
3737// 2. [ssse3] 1 x pshufb
3738// 3. [ssse3] 2 x pshufb + 1 x por
3739// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003740static
Nate Begeman9008ca62009-04-27 18:41:29 +00003741SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3742 SelectionDAG &DAG, X86TargetLowering &TLI) {
3743 SDValue V1 = SVOp->getOperand(0);
3744 SDValue V2 = SVOp->getOperand(1);
3745 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003746 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003747
Nate Begemanb9a47b82009-02-23 08:49:38 +00003748 // Determine if more than 1 of the words in each of the low and high quadwords
3749 // of the result come from the same quadword of one of the two inputs. Undef
3750 // mask values count as coming from any quadword, for better codegen.
3751 SmallVector<unsigned, 4> LoQuad(4);
3752 SmallVector<unsigned, 4> HiQuad(4);
3753 BitVector InputQuads(4);
3754 for (unsigned i = 0; i < 8; ++i) {
3755 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003756 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003757 MaskVals.push_back(EltIdx);
3758 if (EltIdx < 0) {
3759 ++Quad[0];
3760 ++Quad[1];
3761 ++Quad[2];
3762 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003763 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003764 }
3765 ++Quad[EltIdx / 4];
3766 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003767 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003768
Nate Begemanb9a47b82009-02-23 08:49:38 +00003769 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003770 unsigned MaxQuad = 1;
3771 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003772 if (LoQuad[i] > MaxQuad) {
3773 BestLoQuad = i;
3774 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003775 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003776 }
3777
Nate Begemanb9a47b82009-02-23 08:49:38 +00003778 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003779 MaxQuad = 1;
3780 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003781 if (HiQuad[i] > MaxQuad) {
3782 BestHiQuad = i;
3783 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003784 }
3785 }
3786
Nate Begemanb9a47b82009-02-23 08:49:38 +00003787 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003788 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003789 // single pshufb instruction is necessary. If There are more than 2 input
3790 // quads, disable the next transformation since it does not help SSSE3.
3791 bool V1Used = InputQuads[0] || InputQuads[1];
3792 bool V2Used = InputQuads[2] || InputQuads[3];
3793 if (TLI.getSubtarget()->hasSSSE3()) {
3794 if (InputQuads.count() == 2 && V1Used && V2Used) {
3795 BestLoQuad = InputQuads.find_first();
3796 BestHiQuad = InputQuads.find_next(BestLoQuad);
3797 }
3798 if (InputQuads.count() > 2) {
3799 BestLoQuad = -1;
3800 BestHiQuad = -1;
3801 }
3802 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003803
Nate Begemanb9a47b82009-02-23 08:49:38 +00003804 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3805 // the shuffle mask. If a quad is scored as -1, that means that it contains
3806 // words from all 4 input quadwords.
3807 SDValue NewV;
3808 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003809 SmallVector<int, 8> MaskV;
3810 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3811 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003812 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3814 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3815 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003816
Nate Begemanb9a47b82009-02-23 08:49:38 +00003817 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3818 // source words for the shuffle, to aid later transformations.
3819 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003820 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003821 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003822 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003823 if (idx != (int)i)
3824 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003825 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003826 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003827 AllWordsInNewV = false;
3828 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003829 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003830
Nate Begemanb9a47b82009-02-23 08:49:38 +00003831 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3832 if (AllWordsInNewV) {
3833 for (int i = 0; i != 8; ++i) {
3834 int idx = MaskVals[i];
3835 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003836 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003837 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003838 if ((idx != i) && idx < 4)
3839 pshufhw = false;
3840 if ((idx != i) && idx > 3)
3841 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003842 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003843 V1 = NewV;
3844 V2Used = false;
3845 BestLoQuad = 0;
3846 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003847 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003848
Nate Begemanb9a47b82009-02-23 08:49:38 +00003849 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3850 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003851 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003852 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003853 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003854 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003855 }
Eric Christopherfd179292009-08-27 18:07:15 +00003856
Nate Begemanb9a47b82009-02-23 08:49:38 +00003857 // If we have SSSE3, and all words of the result are from 1 input vector,
3858 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3859 // is present, fall back to case 4.
3860 if (TLI.getSubtarget()->hasSSSE3()) {
3861 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003862
Nate Begemanb9a47b82009-02-23 08:49:38 +00003863 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003864 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003865 // mask, and elements that come from V1 in the V2 mask, so that the two
3866 // results can be OR'd together.
3867 bool TwoInputs = V1Used && V2Used;
3868 for (unsigned i = 0; i != 8; ++i) {
3869 int EltIdx = MaskVals[i] * 2;
3870 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3872 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003873 continue;
3874 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003875 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3876 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003877 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003879 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003880 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003881 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003882 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003884
Nate Begemanb9a47b82009-02-23 08:49:38 +00003885 // Calculate the shuffle mask for the second input, shuffle it, and
3886 // OR it with the first shuffled input.
3887 pshufbMask.clear();
3888 for (unsigned i = 0; i != 8; ++i) {
3889 int EltIdx = MaskVals[i] * 2;
3890 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003891 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3892 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003893 continue;
3894 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3896 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003897 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003899 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003900 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003901 MVT::v16i8, &pshufbMask[0], 16));
3902 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3903 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003904 }
3905
3906 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3907 // and update MaskVals with new element order.
3908 BitVector InOrder(8);
3909 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003910 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003911 for (int i = 0; i != 4; ++i) {
3912 int idx = MaskVals[i];
3913 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003914 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003915 InOrder.set(i);
3916 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003918 InOrder.set(i);
3919 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003920 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003921 }
3922 }
3923 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003926 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003927 }
Eric Christopherfd179292009-08-27 18:07:15 +00003928
Nate Begemanb9a47b82009-02-23 08:49:38 +00003929 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3930 // and update MaskVals with the new element order.
3931 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003933 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003934 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003935 for (unsigned i = 4; i != 8; ++i) {
3936 int idx = MaskVals[i];
3937 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003939 InOrder.set(i);
3940 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003942 InOrder.set(i);
3943 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003945 }
3946 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003947 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003949 }
Eric Christopherfd179292009-08-27 18:07:15 +00003950
Nate Begemanb9a47b82009-02-23 08:49:38 +00003951 // In case BestHi & BestLo were both -1, which means each quadword has a word
3952 // from each of the four input quadwords, calculate the InOrder bitvector now
3953 // before falling through to the insert/extract cleanup.
3954 if (BestLoQuad == -1 && BestHiQuad == -1) {
3955 NewV = V1;
3956 for (int i = 0; i != 8; ++i)
3957 if (MaskVals[i] < 0 || MaskVals[i] == i)
3958 InOrder.set(i);
3959 }
Eric Christopherfd179292009-08-27 18:07:15 +00003960
Nate Begemanb9a47b82009-02-23 08:49:38 +00003961 // The other elements are put in the right place using pextrw and pinsrw.
3962 for (unsigned i = 0; i != 8; ++i) {
3963 if (InOrder[i])
3964 continue;
3965 int EltIdx = MaskVals[i];
3966 if (EltIdx < 0)
3967 continue;
3968 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00003969 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003970 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003972 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003974 DAG.getIntPtrConstant(i));
3975 }
3976 return NewV;
3977}
3978
3979// v16i8 shuffles - Prefer shuffles in the following order:
3980// 1. [ssse3] 1 x pshufb
3981// 2. [ssse3] 2 x pshufb + 1 x por
3982// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3983static
Nate Begeman9008ca62009-04-27 18:41:29 +00003984SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3985 SelectionDAG &DAG, X86TargetLowering &TLI) {
3986 SDValue V1 = SVOp->getOperand(0);
3987 SDValue V2 = SVOp->getOperand(1);
3988 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003989 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003990 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00003991
Nate Begemanb9a47b82009-02-23 08:49:38 +00003992 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00003993 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00003994 // present, fall back to case 3.
3995 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3996 bool V1Only = true;
3997 bool V2Only = true;
3998 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003999 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004000 if (EltIdx < 0)
4001 continue;
4002 if (EltIdx < 16)
4003 V2Only = false;
4004 else
4005 V1Only = false;
4006 }
Eric Christopherfd179292009-08-27 18:07:15 +00004007
Nate Begemanb9a47b82009-02-23 08:49:38 +00004008 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4009 if (TLI.getSubtarget()->hasSSSE3()) {
4010 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004011
Nate Begemanb9a47b82009-02-23 08:49:38 +00004012 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004013 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004014 //
4015 // Otherwise, we have elements from both input vectors, and must zero out
4016 // elements that come from V2 in the first mask, and V1 in the second mask
4017 // so that we can OR them together.
4018 bool TwoInputs = !(V1Only || V2Only);
4019 for (unsigned i = 0; i != 16; ++i) {
4020 int EltIdx = MaskVals[i];
4021 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004023 continue;
4024 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004025 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004026 }
4027 // If all the elements are from V2, assign it to V1 and return after
4028 // building the first pshufb.
4029 if (V2Only)
4030 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004031 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004032 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004034 if (!TwoInputs)
4035 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004036
Nate Begemanb9a47b82009-02-23 08:49:38 +00004037 // Calculate the shuffle mask for the second input, shuffle it, and
4038 // OR it with the first shuffled input.
4039 pshufbMask.clear();
4040 for (unsigned i = 0; i != 16; ++i) {
4041 int EltIdx = MaskVals[i];
4042 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004043 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004044 continue;
4045 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004046 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004047 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004049 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 MVT::v16i8, &pshufbMask[0], 16));
4051 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004052 }
Eric Christopherfd179292009-08-27 18:07:15 +00004053
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 // No SSSE3 - Calculate in place words and then fix all out of place words
4055 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4056 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4058 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004059 SDValue NewV = V2Only ? V2 : V1;
4060 for (int i = 0; i != 8; ++i) {
4061 int Elt0 = MaskVals[i*2];
4062 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004063
Nate Begemanb9a47b82009-02-23 08:49:38 +00004064 // This word of the result is all undef, skip it.
4065 if (Elt0 < 0 && Elt1 < 0)
4066 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004067
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 // This word of the result is already in the correct place, skip it.
4069 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4070 continue;
4071 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4072 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004073
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4075 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4076 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004077
4078 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4079 // using a single extract together, load it and store it.
4080 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004081 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004082 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004084 DAG.getIntPtrConstant(i));
4085 continue;
4086 }
4087
Nate Begemanb9a47b82009-02-23 08:49:38 +00004088 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004089 // source byte is not also odd, shift the extracted word left 8 bits
4090 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004091 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004093 DAG.getIntPtrConstant(Elt1 / 2));
4094 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004095 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004097 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4099 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004100 }
4101 // If Elt0 is defined, extract it from the appropriate source. If the
4102 // source byte is not also even, shift the extracted word right 8 bits. If
4103 // Elt1 was also defined, OR the extracted values together before
4104 // inserting them in the result.
4105 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004107 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4108 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004110 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004111 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4113 DAG.getConstant(0x00FF, MVT::i16));
4114 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004115 : InsElt0;
4116 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004118 DAG.getIntPtrConstant(i));
4119 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004121}
4122
Evan Cheng7a831ce2007-12-15 03:00:47 +00004123/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4124/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4125/// done when every pair / quad of shuffle mask elements point to elements in
4126/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004127/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4128static
Nate Begeman9008ca62009-04-27 18:41:29 +00004129SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4130 SelectionDAG &DAG,
4131 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004132 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 SDValue V1 = SVOp->getOperand(0);
4134 SDValue V2 = SVOp->getOperand(1);
4135 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004136 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004138 EVT MaskEltVT = MaskVT.getVectorElementType();
4139 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004141 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 case MVT::v4f32: NewVT = MVT::v2f64; break;
4143 case MVT::v4i32: NewVT = MVT::v2i64; break;
4144 case MVT::v8i16: NewVT = MVT::v4i32; break;
4145 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004146 }
4147
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004148 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004151 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004153 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004154 int Scale = NumElems / NewWidth;
4155 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004156 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 int StartIdx = -1;
4158 for (int j = 0; j < Scale; ++j) {
4159 int EltIdx = SVOp->getMaskElt(i+j);
4160 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004161 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004162 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004163 StartIdx = EltIdx - (EltIdx % Scale);
4164 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004165 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004166 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004167 if (StartIdx == -1)
4168 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004169 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004171 }
4172
Dale Johannesenace16102009-02-03 19:33:06 +00004173 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4174 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004176}
4177
Evan Chengd880b972008-05-09 21:53:03 +00004178/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004179///
Owen Andersone50ed302009-08-10 22:56:29 +00004180static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 SDValue SrcOp, SelectionDAG &DAG,
4182 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004184 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004185 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004186 LD = dyn_cast<LoadSDNode>(SrcOp);
4187 if (!LD) {
4188 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4189 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004190 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4191 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004192 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4193 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004194 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004195 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004197 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4198 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4199 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4200 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004201 SrcOp.getOperand(0)
4202 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004203 }
4204 }
4205 }
4206
Dale Johannesenace16102009-02-03 19:33:06 +00004207 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4208 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004209 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004210 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004211}
4212
Evan Chengace3c172008-07-22 21:13:36 +00004213/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4214/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004215static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004216LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4217 SDValue V1 = SVOp->getOperand(0);
4218 SDValue V2 = SVOp->getOperand(1);
4219 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004220 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004221
Evan Chengace3c172008-07-22 21:13:36 +00004222 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004223 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 SmallVector<int, 8> Mask1(4U, -1);
4225 SmallVector<int, 8> PermMask;
4226 SVOp->getMask(PermMask);
4227
Evan Chengace3c172008-07-22 21:13:36 +00004228 unsigned NumHi = 0;
4229 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004230 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 int Idx = PermMask[i];
4232 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004233 Locs[i] = std::make_pair(-1, -1);
4234 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004235 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4236 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004237 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004238 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004239 NumLo++;
4240 } else {
4241 Locs[i] = std::make_pair(1, NumHi);
4242 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004244 NumHi++;
4245 }
4246 }
4247 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004248
Evan Chengace3c172008-07-22 21:13:36 +00004249 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004250 // If no more than two elements come from either vector. This can be
4251 // implemented with two shuffles. First shuffle gather the elements.
4252 // The second shuffle, which takes the first shuffle as both of its
4253 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004254 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004255
Nate Begeman9008ca62009-04-27 18:41:29 +00004256 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004257
Evan Chengace3c172008-07-22 21:13:36 +00004258 for (unsigned i = 0; i != 4; ++i) {
4259 if (Locs[i].first == -1)
4260 continue;
4261 else {
4262 unsigned Idx = (i < 2) ? 0 : 4;
4263 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004265 }
4266 }
4267
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004269 } else if (NumLo == 3 || NumHi == 3) {
4270 // Otherwise, we must have three elements from one vector, call it X, and
4271 // one element from the other, call it Y. First, use a shufps to build an
4272 // intermediate vector with the one element from Y and the element from X
4273 // that will be in the same half in the final destination (the indexes don't
4274 // matter). Then, use a shufps to build the final vector, taking the half
4275 // containing the element from Y from the intermediate, and the other half
4276 // from X.
4277 if (NumHi == 3) {
4278 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004279 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004280 std::swap(V1, V2);
4281 }
4282
4283 // Find the element from V2.
4284 unsigned HiIndex;
4285 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004286 int Val = PermMask[HiIndex];
4287 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004288 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004289 if (Val >= 4)
4290 break;
4291 }
4292
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 Mask1[0] = PermMask[HiIndex];
4294 Mask1[1] = -1;
4295 Mask1[2] = PermMask[HiIndex^1];
4296 Mask1[3] = -1;
4297 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004298
4299 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004300 Mask1[0] = PermMask[0];
4301 Mask1[1] = PermMask[1];
4302 Mask1[2] = HiIndex & 1 ? 6 : 4;
4303 Mask1[3] = HiIndex & 1 ? 4 : 6;
4304 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004305 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004306 Mask1[0] = HiIndex & 1 ? 2 : 0;
4307 Mask1[1] = HiIndex & 1 ? 0 : 2;
4308 Mask1[2] = PermMask[2];
4309 Mask1[3] = PermMask[3];
4310 if (Mask1[2] >= 0)
4311 Mask1[2] += 4;
4312 if (Mask1[3] >= 0)
4313 Mask1[3] += 4;
4314 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004315 }
Evan Chengace3c172008-07-22 21:13:36 +00004316 }
4317
4318 // Break it into (shuffle shuffle_hi, shuffle_lo).
4319 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004320 SmallVector<int,8> LoMask(4U, -1);
4321 SmallVector<int,8> HiMask(4U, -1);
4322
4323 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004324 unsigned MaskIdx = 0;
4325 unsigned LoIdx = 0;
4326 unsigned HiIdx = 2;
4327 for (unsigned i = 0; i != 4; ++i) {
4328 if (i == 2) {
4329 MaskPtr = &HiMask;
4330 MaskIdx = 1;
4331 LoIdx = 0;
4332 HiIdx = 2;
4333 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004334 int Idx = PermMask[i];
4335 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004336 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004338 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004339 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004340 LoIdx++;
4341 } else {
4342 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004344 HiIdx++;
4345 }
4346 }
4347
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4349 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4350 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004351 for (unsigned i = 0; i != 4; ++i) {
4352 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004354 } else {
4355 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004357 }
4358 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004360}
4361
Dan Gohman475871a2008-07-27 21:46:04 +00004362SDValue
4363X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004365 SDValue V1 = Op.getOperand(0);
4366 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004367 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004368 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004370 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004371 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4372 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004373 bool V1IsSplat = false;
4374 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004375
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004377 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004378
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 // Promote splats to v4f32.
4380 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004381 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 return Op;
4383 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004384 }
4385
Evan Cheng7a831ce2007-12-15 03:00:47 +00004386 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4387 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004390 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004391 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004392 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004393 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004394 // FIXME: Figure out a cleaner way to do this.
4395 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004396 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004398 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4400 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4401 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004402 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004403 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4405 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004406 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004407 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004408 }
4409 }
Eric Christopherfd179292009-08-27 18:07:15 +00004410
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 if (X86::isPSHUFDMask(SVOp))
4412 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004413
Evan Chengf26ffe92008-05-29 08:22:04 +00004414 // Check if this can be converted into a logical shift.
4415 bool isLeft = false;
4416 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004417 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004419 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004420 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004421 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004422 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004423 EVT EltVT = VT.getVectorElementType();
4424 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004425 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004426 }
Eric Christopherfd179292009-08-27 18:07:15 +00004427
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004429 if (V1IsUndef)
4430 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004431 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004432 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004433 if (!isMMX)
4434 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004435 }
Eric Christopherfd179292009-08-27 18:07:15 +00004436
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 // FIXME: fold these into legal mask.
4438 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4439 X86::isMOVSLDUPMask(SVOp) ||
4440 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004441 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004442 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004443 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004444
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 if (ShouldXformToMOVHLPS(SVOp) ||
4446 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4447 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004448
Evan Chengf26ffe92008-05-29 08:22:04 +00004449 if (isShift) {
4450 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004451 EVT EltVT = VT.getVectorElementType();
4452 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004453 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004454 }
Eric Christopherfd179292009-08-27 18:07:15 +00004455
Evan Cheng9eca5e82006-10-25 21:49:50 +00004456 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004457 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4458 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004459 V1IsSplat = isSplatVector(V1.getNode());
4460 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004461
Chris Lattner8a594482007-11-25 00:24:49 +00004462 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004463 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 Op = CommuteVectorShuffle(SVOp, DAG);
4465 SVOp = cast<ShuffleVectorSDNode>(Op);
4466 V1 = SVOp->getOperand(0);
4467 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004468 std::swap(V1IsSplat, V2IsSplat);
4469 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004470 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004471 }
4472
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4474 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004475 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004476 return V1;
4477 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4478 // the instruction selector will not match, so get a canonical MOVL with
4479 // swapped operands to undo the commute.
4480 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004481 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004482
Nate Begeman9008ca62009-04-27 18:41:29 +00004483 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4484 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4485 X86::isUNPCKLMask(SVOp) ||
4486 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004487 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004488
Evan Cheng9bbbb982006-10-25 20:48:19 +00004489 if (V2IsSplat) {
4490 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004491 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004492 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 SDValue NewMask = NormalizeMask(SVOp, DAG);
4494 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4495 if (NSVOp != SVOp) {
4496 if (X86::isUNPCKLMask(NSVOp, true)) {
4497 return NewMask;
4498 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4499 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500 }
4501 }
4502 }
4503
Evan Cheng9eca5e82006-10-25 21:49:50 +00004504 if (Commuted) {
4505 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 // FIXME: this seems wrong.
4507 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4508 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4509 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4510 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4511 X86::isUNPCKLMask(NewSVOp) ||
4512 X86::isUNPCKHMask(NewSVOp))
4513 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004514 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004515
Nate Begemanb9a47b82009-02-23 08:49:38 +00004516 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004517
4518 // Normalize the node to match x86 shuffle ops if needed
4519 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4520 return CommuteVectorShuffle(SVOp, DAG);
4521
4522 // Check for legal shuffle and return?
4523 SmallVector<int, 16> PermMask;
4524 SVOp->getMask(PermMask);
4525 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004526 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004527
Evan Cheng14b32e12007-12-11 01:46:18 +00004528 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004529 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004531 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004532 return NewOp;
4533 }
4534
Owen Anderson825b72b2009-08-11 20:47:22 +00004535 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004537 if (NewOp.getNode())
4538 return NewOp;
4539 }
Eric Christopherfd179292009-08-27 18:07:15 +00004540
Evan Chengace3c172008-07-22 21:13:36 +00004541 // Handle all 4 wide cases with a number of shuffles except for MMX.
4542 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004543 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004544
Dan Gohman475871a2008-07-27 21:46:04 +00004545 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004546}
4547
Dan Gohman475871a2008-07-27 21:46:04 +00004548SDValue
4549X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004550 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004551 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004552 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004553 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004555 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004556 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004557 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004558 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004559 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004560 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4561 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4562 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004563 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4564 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004565 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004566 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004567 Op.getOperand(0)),
4568 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004569 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004570 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004572 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004573 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004574 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004575 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4576 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004577 // result has a single use which is a store or a bitcast to i32. And in
4578 // the case of a store, it's not worth it if the index is a constant 0,
4579 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004580 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004581 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004582 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004583 if ((User->getOpcode() != ISD::STORE ||
4584 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4585 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004586 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004587 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004588 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4590 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004591 Op.getOperand(0)),
4592 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004593 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4594 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004595 // ExtractPS works with constant index.
4596 if (isa<ConstantSDNode>(Op.getOperand(1)))
4597 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004598 }
Dan Gohman475871a2008-07-27 21:46:04 +00004599 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004600}
4601
4602
Dan Gohman475871a2008-07-27 21:46:04 +00004603SDValue
4604X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004606 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004607
Evan Cheng62a3f152008-03-24 21:52:23 +00004608 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004609 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004610 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004611 return Res;
4612 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004613
Owen Andersone50ed302009-08-10 22:56:29 +00004614 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004615 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004617 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004618 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004619 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004620 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004621 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4622 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004623 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004624 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004625 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004626 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004627 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004628 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004630 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004631 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004632 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004633 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004634 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004635 if (Idx == 0)
4636 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004637
Evan Cheng0db9fe62006-04-25 20:13:52 +00004638 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004639 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004640 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004641 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004643 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004644 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004645 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004646 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4647 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4648 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004649 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650 if (Idx == 0)
4651 return Op;
4652
4653 // UNPCKHPD the element to the lowest double word, then movsd.
4654 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4655 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004657 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004658 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004659 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004660 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004661 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004662 }
4663
Dan Gohman475871a2008-07-27 21:46:04 +00004664 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004665}
4666
Dan Gohman475871a2008-07-27 21:46:04 +00004667SDValue
4668X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004669 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004670 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004671 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004672
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue N0 = Op.getOperand(0);
4674 SDValue N1 = Op.getOperand(1);
4675 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004676
Dan Gohman8a55ce42009-09-23 21:02:20 +00004677 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004678 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004679 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4680 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004681 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4682 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004683 if (N1.getValueType() != MVT::i32)
4684 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4685 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004686 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004687 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004688 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004689 // Bits [7:6] of the constant are the source select. This will always be
4690 // zero here. The DAG Combiner may combine an extract_elt index into these
4691 // bits. For example (insert (extract, 3), 2) could be matched by putting
4692 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004693 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004694 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004695 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004696 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004697 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004698 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004699 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004700 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004701 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004702 // PINSR* works with constant index.
4703 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004704 }
Dan Gohman475871a2008-07-27 21:46:04 +00004705 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004706}
4707
Dan Gohman475871a2008-07-27 21:46:04 +00004708SDValue
4709X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004710 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004711 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004712
4713 if (Subtarget->hasSSE41())
4714 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4715
Dan Gohman8a55ce42009-09-23 21:02:20 +00004716 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004717 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004718
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004719 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004720 SDValue N0 = Op.getOperand(0);
4721 SDValue N1 = Op.getOperand(1);
4722 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004723
Dan Gohman8a55ce42009-09-23 21:02:20 +00004724 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004725 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4726 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004727 if (N1.getValueType() != MVT::i32)
4728 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4729 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004730 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004731 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004732 }
Dan Gohman475871a2008-07-27 21:46:04 +00004733 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004734}
4735
Dan Gohman475871a2008-07-27 21:46:04 +00004736SDValue
4737X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004738 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 if (Op.getValueType() == MVT::v2f32)
4740 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4741 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4742 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004743 Op.getOperand(0))));
4744
Owen Anderson825b72b2009-08-11 20:47:22 +00004745 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4746 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004747
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4749 EVT VT = MVT::v2i32;
4750 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004751 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004752 case MVT::v16i8:
4753 case MVT::v8i16:
4754 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004755 break;
4756 }
Dale Johannesenace16102009-02-03 19:33:06 +00004757 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4758 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004759}
4760
Bill Wendling056292f2008-09-16 21:48:12 +00004761// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4762// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4763// one of the above mentioned nodes. It has to be wrapped because otherwise
4764// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4765// be used to form addressing mode. These wrapped nodes will be selected
4766// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004767SDValue
4768X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004769 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Chris Lattner41621a22009-06-26 19:22:52 +00004771 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4772 // global base reg.
4773 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004774 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004775 CodeModel::Model M = getTargetMachine().getCodeModel();
4776
Chris Lattner4f066492009-07-11 20:29:19 +00004777 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004778 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004779 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004780 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004781 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004782 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004783 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Evan Cheng1606e8e2009-03-13 07:51:59 +00004785 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004786 CP->getAlignment(),
4787 CP->getOffset(), OpFlag);
4788 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004789 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004790 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004791 if (OpFlag) {
4792 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004793 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004794 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004795 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004796 }
4797
4798 return Result;
4799}
4800
Chris Lattner18c59872009-06-27 04:16:01 +00004801SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4802 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004803
Chris Lattner18c59872009-06-27 04:16:01 +00004804 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4805 // global base reg.
4806 unsigned char OpFlag = 0;
4807 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004808 CodeModel::Model M = getTargetMachine().getCodeModel();
4809
Chris Lattner4f066492009-07-11 20:29:19 +00004810 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004811 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004812 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004813 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004814 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004815 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004816 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004817
Chris Lattner18c59872009-06-27 04:16:01 +00004818 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4819 OpFlag);
4820 DebugLoc DL = JT->getDebugLoc();
4821 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004822
Chris Lattner18c59872009-06-27 04:16:01 +00004823 // With PIC, the address is actually $g + Offset.
4824 if (OpFlag) {
4825 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4826 DAG.getNode(X86ISD::GlobalBaseReg,
4827 DebugLoc::getUnknownLoc(), getPointerTy()),
4828 Result);
4829 }
Eric Christopherfd179292009-08-27 18:07:15 +00004830
Chris Lattner18c59872009-06-27 04:16:01 +00004831 return Result;
4832}
4833
4834SDValue
4835X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4836 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004837
Chris Lattner18c59872009-06-27 04:16:01 +00004838 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4839 // global base reg.
4840 unsigned char OpFlag = 0;
4841 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004842 CodeModel::Model M = getTargetMachine().getCodeModel();
4843
Chris Lattner4f066492009-07-11 20:29:19 +00004844 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004845 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004846 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004847 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004848 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004849 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004850 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004851
Chris Lattner18c59872009-06-27 04:16:01 +00004852 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004853
Chris Lattner18c59872009-06-27 04:16:01 +00004854 DebugLoc DL = Op.getDebugLoc();
4855 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004856
4857
Chris Lattner18c59872009-06-27 04:16:01 +00004858 // With PIC, the address is actually $g + Offset.
4859 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004860 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004861 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4862 DAG.getNode(X86ISD::GlobalBaseReg,
4863 DebugLoc::getUnknownLoc(),
4864 getPointerTy()),
4865 Result);
4866 }
Eric Christopherfd179292009-08-27 18:07:15 +00004867
Chris Lattner18c59872009-06-27 04:16:01 +00004868 return Result;
4869}
4870
Dan Gohman475871a2008-07-27 21:46:04 +00004871SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004872X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004873 // Create the TargetBlockAddressAddress node.
4874 unsigned char OpFlags =
4875 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004876 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004877 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4878 DebugLoc dl = Op.getDebugLoc();
4879 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4880 /*isTarget=*/true, OpFlags);
4881
Dan Gohmanf705adb2009-10-30 01:28:02 +00004882 if (Subtarget->isPICStyleRIPRel() &&
4883 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004884 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4885 else
4886 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004887
Dan Gohman29cbade2009-11-20 23:18:13 +00004888 // With PIC, the address is actually $g + Offset.
4889 if (isGlobalRelativeToPICBase(OpFlags)) {
4890 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4891 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4892 Result);
4893 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004894
4895 return Result;
4896}
4897
4898SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004899X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004900 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004901 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004902 // Create the TargetGlobalAddress node, folding in the constant
4903 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004904 unsigned char OpFlags =
4905 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004906 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004907 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004908 if (OpFlags == X86II::MO_NO_FLAG &&
4909 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004910 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004911 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004912 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004913 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004914 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004915 }
Eric Christopherfd179292009-08-27 18:07:15 +00004916
Chris Lattner4f066492009-07-11 20:29:19 +00004917 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004918 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004919 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4920 else
4921 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004922
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004923 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004924 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004925 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4926 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004927 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004929
Chris Lattner36c25012009-07-10 07:34:39 +00004930 // For globals that require a load from a stub to get the address, emit the
4931 // load.
4932 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004933 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004934 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935
Dan Gohman6520e202008-10-18 02:06:02 +00004936 // If there was a non-zero offset that we didn't fold, create an explicit
4937 // addition for it.
4938 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004939 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004940 DAG.getConstant(Offset, getPointerTy()));
4941
Evan Cheng0db9fe62006-04-25 20:13:52 +00004942 return Result;
4943}
4944
Evan Chengda43bcf2008-09-24 00:05:32 +00004945SDValue
4946X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4947 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004948 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004949 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004950}
4951
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004952static SDValue
4953GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004954 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004955 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004956 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004957 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004958 DebugLoc dl = GA->getDebugLoc();
4959 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4960 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004961 GA->getOffset(),
4962 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004963 if (InFlag) {
4964 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004965 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004966 } else {
4967 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004968 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004969 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004970
4971 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
4972 MFI->setHasCalls(true);
4973
Rafael Espindola15f1b662009-04-24 12:59:40 +00004974 SDValue Flag = Chain.getValue(1);
4975 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004976}
4977
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004978// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004979static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004980LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004981 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004982 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004983 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4984 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004985 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004986 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004987 PtrVT), InFlag);
4988 InFlag = Chain.getValue(1);
4989
Chris Lattnerb903bed2009-06-26 21:20:29 +00004990 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004991}
4992
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004993// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004994static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004995LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004996 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004997 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4998 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004999}
5000
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005001// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5002// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005003static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005004 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005005 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005006 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005007 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005008 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5009 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005010 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005011 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005012
5013 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5014 NULL, 0);
5015
Chris Lattnerb903bed2009-06-26 21:20:29 +00005016 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005017 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5018 // initialexec.
5019 unsigned WrapperKind = X86ISD::Wrapper;
5020 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005021 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005022 } else if (is64Bit) {
5023 assert(model == TLSModel::InitialExec);
5024 OperandFlags = X86II::MO_GOTTPOFF;
5025 WrapperKind = X86ISD::WrapperRIP;
5026 } else {
5027 assert(model == TLSModel::InitialExec);
5028 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005029 }
Eric Christopherfd179292009-08-27 18:07:15 +00005030
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005031 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5032 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005033 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005034 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005035 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005036
Rafael Espindola9a580232009-02-27 13:37:18 +00005037 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005038 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005039 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005040
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005041 // The address of the thread local variable is the add of the thread
5042 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005043 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005044}
5045
Dan Gohman475871a2008-07-27 21:46:04 +00005046SDValue
5047X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005048 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005049 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005050 assert(Subtarget->isTargetELF() &&
5051 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005052 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005053 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005054
Chris Lattnerb903bed2009-06-26 21:20:29 +00005055 // If GV is an alias then use the aliasee for determining
5056 // thread-localness.
5057 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5058 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005059
Chris Lattnerb903bed2009-06-26 21:20:29 +00005060 TLSModel::Model model = getTLSModel(GV,
5061 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005062
Chris Lattnerb903bed2009-06-26 21:20:29 +00005063 switch (model) {
5064 case TLSModel::GeneralDynamic:
5065 case TLSModel::LocalDynamic: // not implemented
5066 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005067 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005068 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005069
Chris Lattnerb903bed2009-06-26 21:20:29 +00005070 case TLSModel::InitialExec:
5071 case TLSModel::LocalExec:
5072 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5073 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005074 }
Eric Christopherfd179292009-08-27 18:07:15 +00005075
Torok Edwinc23197a2009-07-14 16:55:14 +00005076 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005077 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005078}
5079
Evan Cheng0db9fe62006-04-25 20:13:52 +00005080
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005081/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005082/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005083SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005084 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005085 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005086 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005087 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005088 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005089 SDValue ShOpLo = Op.getOperand(0);
5090 SDValue ShOpHi = Op.getOperand(1);
5091 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005092 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005094 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005095
Dan Gohman475871a2008-07-27 21:46:04 +00005096 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005097 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005098 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5099 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005100 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005101 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5102 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005103 }
Evan Chenge3413162006-01-09 18:33:28 +00005104
Owen Anderson825b72b2009-08-11 20:47:22 +00005105 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5106 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005107 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005108 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005109
Dan Gohman475871a2008-07-27 21:46:04 +00005110 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005111 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005112 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5113 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005114
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005115 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005116 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5117 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005118 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005119 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5120 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005121 }
5122
Dan Gohman475871a2008-07-27 21:46:04 +00005123 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005124 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125}
Evan Chenga3195e82006-01-12 22:54:21 +00005126
Dan Gohman475871a2008-07-27 21:46:04 +00005127SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005128 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005129
5130 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005131 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005132 return Op;
5133 }
5134 return SDValue();
5135 }
5136
Owen Anderson825b72b2009-08-11 20:47:22 +00005137 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005138 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005139
Eli Friedman36df4992009-05-27 00:47:34 +00005140 // These are really Legal; return the operand so the caller accepts it as
5141 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005142 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005143 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005144 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005145 Subtarget->is64Bit()) {
5146 return Op;
5147 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005148
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005149 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005150 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005151 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005152 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005153 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005154 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005155 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005156 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005157 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5158}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005159
Owen Andersone50ed302009-08-10 22:56:29 +00005160SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005161 SDValue StackSlot,
5162 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005164 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005165 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005166 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005167 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005168 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005169 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005171 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005172 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005173 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005174
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005175 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005177 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005178
5179 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5180 // shouldn't be necessary except that RFP cannot be live across
5181 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005182 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005183 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005184 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005185 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005186 SDValue Ops[] = {
5187 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5188 };
5189 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005190 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005191 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005192 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005193
Evan Cheng0db9fe62006-04-25 20:13:52 +00005194 return Result;
5195}
5196
Bill Wendling8b8a6362009-01-17 03:56:04 +00005197// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5198SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5199 // This algorithm is not obvious. Here it is in C code, more or less:
5200 /*
5201 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5202 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5203 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005204
Bill Wendling8b8a6362009-01-17 03:56:04 +00005205 // Copy ints to xmm registers.
5206 __m128i xh = _mm_cvtsi32_si128( hi );
5207 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005208
Bill Wendling8b8a6362009-01-17 03:56:04 +00005209 // Combine into low half of a single xmm register.
5210 __m128i x = _mm_unpacklo_epi32( xh, xl );
5211 __m128d d;
5212 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005213
Bill Wendling8b8a6362009-01-17 03:56:04 +00005214 // Merge in appropriate exponents to give the integer bits the right
5215 // magnitude.
5216 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005217
Bill Wendling8b8a6362009-01-17 03:56:04 +00005218 // Subtract away the biases to deal with the IEEE-754 double precision
5219 // implicit 1.
5220 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005221
Bill Wendling8b8a6362009-01-17 03:56:04 +00005222 // All conversions up to here are exact. The correctly rounded result is
5223 // calculated using the current rounding mode using the following
5224 // horizontal add.
5225 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5226 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5227 // store doesn't really need to be here (except
5228 // maybe to zero the other double)
5229 return sd;
5230 }
5231 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005232
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005233 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005234 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005235
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005236 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005237 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005238 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5239 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5240 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5241 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005242 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005243 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005244
Bill Wendling8b8a6362009-01-17 03:56:04 +00005245 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005246 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005247 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005248 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005249 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005250 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005251 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005252
Owen Anderson825b72b2009-08-11 20:47:22 +00005253 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5254 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005255 Op.getOperand(0),
5256 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005257 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5258 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005259 Op.getOperand(0),
5260 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5262 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005263 PseudoSourceValue::getConstantPool(), 0,
5264 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5266 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5267 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005268 PseudoSourceValue::getConstantPool(), 0,
5269 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005270 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005271
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005272 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005273 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005274 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5275 DAG.getUNDEF(MVT::v2f64), ShufMask);
5276 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5277 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005278 DAG.getIntPtrConstant(0));
5279}
5280
Bill Wendling8b8a6362009-01-17 03:56:04 +00005281// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5282SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005283 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005284 // FP constant to bias correct the final result.
5285 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005286 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005287
5288 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005289 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5290 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005291 Op.getOperand(0),
5292 DAG.getIntPtrConstant(0)));
5293
Owen Anderson825b72b2009-08-11 20:47:22 +00005294 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5295 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005296 DAG.getIntPtrConstant(0));
5297
5298 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005299 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5300 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005301 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 MVT::v2f64, Load)),
5303 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005304 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005305 MVT::v2f64, Bias)));
5306 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5307 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005308 DAG.getIntPtrConstant(0));
5309
5310 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005312
5313 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005314 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005315
Owen Anderson825b72b2009-08-11 20:47:22 +00005316 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005317 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005318 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005320 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005321 }
5322
5323 // Handle final rounding.
5324 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005325}
5326
5327SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005328 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005329 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005330
Evan Chenga06ec9e2009-01-19 08:08:22 +00005331 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5332 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5333 // the optimization here.
5334 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005335 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005336
Owen Andersone50ed302009-08-10 22:56:29 +00005337 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005339 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005340 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005341 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005342
Bill Wendling8b8a6362009-01-17 03:56:04 +00005343 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005345 return LowerUINT_TO_FP_i32(Op, DAG);
5346 }
5347
Owen Anderson825b72b2009-08-11 20:47:22 +00005348 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005349
5350 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005352 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5353 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5354 getPointerTy(), StackSlot, WordOff);
5355 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5356 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005357 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005358 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005359 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005360}
5361
Dan Gohman475871a2008-07-27 21:46:04 +00005362std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005363FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005364 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005365
Owen Andersone50ed302009-08-10 22:56:29 +00005366 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005367
5368 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5370 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005371 }
5372
Owen Anderson825b72b2009-08-11 20:47:22 +00005373 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5374 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005377 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005378 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005379 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005380 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005381 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005382 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005383 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005384 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005385
Evan Cheng87c89352007-10-15 20:11:21 +00005386 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5387 // stack slot.
5388 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005389 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005390 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005391 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005392
Evan Cheng0db9fe62006-04-25 20:13:52 +00005393 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005395 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005396 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5397 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5398 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005399 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005400
Dan Gohman475871a2008-07-27 21:46:04 +00005401 SDValue Chain = DAG.getEntryNode();
5402 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005403 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005404 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005405 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005406 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005407 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005409 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5410 };
Dale Johannesenace16102009-02-03 19:33:06 +00005411 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005412 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005413 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005414 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5415 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005416
Evan Cheng0db9fe62006-04-25 20:13:52 +00005417 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005420
Chris Lattner27a6c732007-11-24 07:07:01 +00005421 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005422}
5423
Dan Gohman475871a2008-07-27 21:46:04 +00005424SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005425 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005426 if (Op.getValueType() == MVT::v2i32 &&
5427 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005428 return Op;
5429 }
5430 return SDValue();
5431 }
5432
Eli Friedman948e95a2009-05-23 09:59:16 +00005433 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005435 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5436 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005437
Chris Lattner27a6c732007-11-24 07:07:01 +00005438 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005439 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005440 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005441}
5442
Eli Friedman948e95a2009-05-23 09:59:16 +00005443SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5444 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5445 SDValue FIST = Vals.first, StackSlot = Vals.second;
5446 assert(FIST.getNode() && "Unexpected failure");
5447
5448 // Load the result.
5449 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5450 FIST, StackSlot, NULL, 0);
5451}
5452
Dan Gohman475871a2008-07-27 21:46:04 +00005453SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005454 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005455 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005456 EVT VT = Op.getValueType();
5457 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005458 if (VT.isVector())
5459 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005460 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005461 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005462 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005463 CV.push_back(C);
5464 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005465 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005466 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005467 CV.push_back(C);
5468 CV.push_back(C);
5469 CV.push_back(C);
5470 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005472 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005473 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005474 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005475 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005476 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005477 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005478}
5479
Dan Gohman475871a2008-07-27 21:46:04 +00005480SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005481 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005482 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005483 EVT VT = Op.getValueType();
5484 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005485 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005486 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005487 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005488 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005489 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005490 CV.push_back(C);
5491 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005493 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005494 CV.push_back(C);
5495 CV.push_back(C);
5496 CV.push_back(C);
5497 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005499 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005500 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005501 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005502 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005503 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005504 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005505 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005506 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5507 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005508 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005509 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005510 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005511 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005512 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005513}
5514
Dan Gohman475871a2008-07-27 21:46:04 +00005515SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005516 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005517 SDValue Op0 = Op.getOperand(0);
5518 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005519 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005520 EVT VT = Op.getValueType();
5521 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005522
5523 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005524 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005525 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005526 SrcVT = VT;
5527 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005528 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005529 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005530 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005531 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005532 }
5533
5534 // At this point the operands and the result should have the same
5535 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005536
Evan Cheng68c47cb2007-01-05 07:55:56 +00005537 // First get the sign bit of second operand.
5538 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005539 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005540 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5541 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005542 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005543 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5544 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5545 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5546 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005547 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005548 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005549 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005550 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005551 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005552 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005553 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005554
5555 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005556 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005557 // Op0 is MVT::f32, Op1 is MVT::f64.
5558 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5559 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5560 DAG.getConstant(32, MVT::i32));
5561 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5562 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005563 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005564 }
5565
Evan Cheng73d6cf12007-01-05 21:37:56 +00005566 // Clear first operand sign bit.
5567 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005568 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005569 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5570 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005571 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005572 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5573 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5574 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5575 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005576 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005577 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005578 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005579 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005580 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005581 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005582 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005583
5584 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005585 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005586}
5587
Dan Gohman076aee32009-03-04 19:44:21 +00005588/// Emit nodes that will be selected as "test Op0,Op0", or something
5589/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005590SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5591 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005592 DebugLoc dl = Op.getDebugLoc();
5593
Dan Gohman31125812009-03-07 01:58:32 +00005594 // CF and OF aren't always set the way we want. Determine which
5595 // of these we need.
5596 bool NeedCF = false;
5597 bool NeedOF = false;
5598 switch (X86CC) {
5599 case X86::COND_A: case X86::COND_AE:
5600 case X86::COND_B: case X86::COND_BE:
5601 NeedCF = true;
5602 break;
5603 case X86::COND_G: case X86::COND_GE:
5604 case X86::COND_L: case X86::COND_LE:
5605 case X86::COND_O: case X86::COND_NO:
5606 NeedOF = true;
5607 break;
5608 default: break;
5609 }
5610
Dan Gohman076aee32009-03-04 19:44:21 +00005611 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005612 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5613 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5614 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005615 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005616 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005617 switch (Op.getNode()->getOpcode()) {
5618 case ISD::ADD:
5619 // Due to an isel shortcoming, be conservative if this add is likely to
5620 // be selected as part of a load-modify-store instruction. When the root
5621 // node in a match is a store, isel doesn't know how to remap non-chain
5622 // non-flag uses of other nodes in the match, such as the ADD in this
5623 // case. This leads to the ADD being left around and reselected, with
5624 // the result being two adds in the output.
5625 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5626 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5627 if (UI->getOpcode() == ISD::STORE)
5628 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005629 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005630 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5631 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005632 if (C->getAPIntValue() == 1) {
5633 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005634 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005635 break;
5636 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005637 // An add of negative one (subtract of one) will be selected as a DEC.
5638 if (C->getAPIntValue().isAllOnesValue()) {
5639 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005640 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005641 break;
5642 }
5643 }
Dan Gohman076aee32009-03-04 19:44:21 +00005644 // Otherwise use a regular EFLAGS-setting add.
5645 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005646 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005647 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005648 case ISD::AND: {
5649 // If the primary and result isn't used, don't bother using X86ISD::AND,
5650 // because a TEST instruction will be better.
5651 bool NonFlagUse = false;
5652 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005653 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5654 SDNode *User = *UI;
5655 unsigned UOpNo = UI.getOperandNo();
5656 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5657 // Look pass truncate.
5658 UOpNo = User->use_begin().getOperandNo();
5659 User = *User->use_begin();
5660 }
5661 if (User->getOpcode() != ISD::BRCOND &&
5662 User->getOpcode() != ISD::SETCC &&
5663 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005664 NonFlagUse = true;
5665 break;
5666 }
Evan Cheng17751da2010-01-07 00:54:06 +00005667 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005668 if (!NonFlagUse)
5669 break;
5670 }
5671 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005672 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005673 case ISD::OR:
5674 case ISD::XOR:
5675 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005676 // likely to be selected as part of a load-modify-store instruction.
5677 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5678 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5679 if (UI->getOpcode() == ISD::STORE)
5680 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005681 // Otherwise use a regular EFLAGS-setting instruction.
5682 switch (Op.getNode()->getOpcode()) {
5683 case ISD::SUB: Opcode = X86ISD::SUB; break;
5684 case ISD::OR: Opcode = X86ISD::OR; break;
5685 case ISD::XOR: Opcode = X86ISD::XOR; break;
5686 case ISD::AND: Opcode = X86ISD::AND; break;
5687 default: llvm_unreachable("unexpected operator!");
5688 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005689 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005690 break;
5691 case X86ISD::ADD:
5692 case X86ISD::SUB:
5693 case X86ISD::INC:
5694 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005695 case X86ISD::OR:
5696 case X86ISD::XOR:
5697 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005698 return SDValue(Op.getNode(), 1);
5699 default:
5700 default_case:
5701 break;
5702 }
5703 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005704 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005705 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005706 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005707 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005708 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005709 DAG.ReplaceAllUsesWith(Op, New);
5710 return SDValue(New.getNode(), 1);
5711 }
5712 }
5713
5714 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005716 DAG.getConstant(0, Op.getValueType()));
5717}
5718
5719/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5720/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005721SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5722 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005723 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5724 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005725 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005726
5727 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005729}
5730
Evan Chengd40d03e2010-01-06 19:38:29 +00005731/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5732/// if it's possible.
5733static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005734 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005735 SDValue LHS, RHS;
5736 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5737 if (ConstantSDNode *Op010C =
5738 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5739 if (Op010C->getZExtValue() == 1) {
5740 LHS = Op0.getOperand(0);
5741 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005742 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005743 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5744 if (ConstantSDNode *Op000C =
5745 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5746 if (Op000C->getZExtValue() == 1) {
5747 LHS = Op0.getOperand(1);
5748 RHS = Op0.getOperand(0).getOperand(1);
5749 }
5750 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5751 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5752 SDValue AndLHS = Op0.getOperand(0);
5753 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5754 LHS = AndLHS.getOperand(0);
5755 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005756 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005757 }
Evan Cheng0488db92007-09-25 01:57:46 +00005758
Evan Chengd40d03e2010-01-06 19:38:29 +00005759 if (LHS.getNode()) {
5760 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5761 // instruction. Since the shift amount is in-range-or-undefined, we know
5762 // that doing a bittest on the i16 value is ok. We extend to i32 because
5763 // the encoding for the i16 version is larger than the i32 version.
5764 if (LHS.getValueType() == MVT::i8)
5765 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005766
Evan Chengd40d03e2010-01-06 19:38:29 +00005767 // If the operand types disagree, extend the shift amount to match. Since
5768 // BT ignores high bits (like shifts) we can use anyextend.
5769 if (LHS.getValueType() != RHS.getValueType())
5770 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005771
Evan Chengd40d03e2010-01-06 19:38:29 +00005772 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5773 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5774 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5775 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005776 }
5777
Evan Cheng54de3ea2010-01-05 06:52:31 +00005778 return SDValue();
5779}
5780
5781SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5782 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5783 SDValue Op0 = Op.getOperand(0);
5784 SDValue Op1 = Op.getOperand(1);
5785 DebugLoc dl = Op.getDebugLoc();
5786 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5787
5788 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005789 // Lower (X & (1 << N)) == 0 to BT(X, N).
5790 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5791 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5792 if (Op0.getOpcode() == ISD::AND &&
5793 Op0.hasOneUse() &&
5794 Op1.getOpcode() == ISD::Constant &&
5795 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5796 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5797 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5798 if (NewSetCC.getNode())
5799 return NewSetCC;
5800 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005801
Chris Lattnere55484e2008-12-25 05:34:37 +00005802 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5803 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005804 if (X86CC == X86::COND_INVALID)
5805 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005806
Dan Gohman31125812009-03-07 01:58:32 +00005807 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005808
5809 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005810 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005811 return DAG.getNode(ISD::AND, dl, MVT::i8,
5812 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5813 DAG.getConstant(X86CC, MVT::i8), Cond),
5814 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005815
Owen Anderson825b72b2009-08-11 20:47:22 +00005816 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5817 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005818}
5819
Dan Gohman475871a2008-07-27 21:46:04 +00005820SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5821 SDValue Cond;
5822 SDValue Op0 = Op.getOperand(0);
5823 SDValue Op1 = Op.getOperand(1);
5824 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005825 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005826 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5827 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005828 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005829
5830 if (isFP) {
5831 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005832 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5834 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005835 bool Swap = false;
5836
5837 switch (SetCCOpcode) {
5838 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005839 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005840 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005841 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005842 case ISD::SETGT: Swap = true; // Fallthrough
5843 case ISD::SETLT:
5844 case ISD::SETOLT: SSECC = 1; break;
5845 case ISD::SETOGE:
5846 case ISD::SETGE: Swap = true; // Fallthrough
5847 case ISD::SETLE:
5848 case ISD::SETOLE: SSECC = 2; break;
5849 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005850 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005851 case ISD::SETNE: SSECC = 4; break;
5852 case ISD::SETULE: Swap = true;
5853 case ISD::SETUGE: SSECC = 5; break;
5854 case ISD::SETULT: Swap = true;
5855 case ISD::SETUGT: SSECC = 6; break;
5856 case ISD::SETO: SSECC = 7; break;
5857 }
5858 if (Swap)
5859 std::swap(Op0, Op1);
5860
Nate Begemanfb8ead02008-07-25 19:05:58 +00005861 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005862 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005863 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005864 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5866 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005867 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005868 }
5869 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005870 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005871 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5872 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005873 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005874 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005875 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005876 }
5877 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005878 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005879 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005880
Nate Begeman30a0de92008-07-17 16:51:19 +00005881 // We are handling one of the integer comparisons here. Since SSE only has
5882 // GT and EQ comparisons for integer, swapping operands and multiple
5883 // operations may be required for some comparisons.
5884 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5885 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005886
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005888 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 case MVT::v8i8:
5890 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5891 case MVT::v4i16:
5892 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5893 case MVT::v2i32:
5894 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5895 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005897
Nate Begeman30a0de92008-07-17 16:51:19 +00005898 switch (SetCCOpcode) {
5899 default: break;
5900 case ISD::SETNE: Invert = true;
5901 case ISD::SETEQ: Opc = EQOpc; break;
5902 case ISD::SETLT: Swap = true;
5903 case ISD::SETGT: Opc = GTOpc; break;
5904 case ISD::SETGE: Swap = true;
5905 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5906 case ISD::SETULT: Swap = true;
5907 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5908 case ISD::SETUGE: Swap = true;
5909 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5910 }
5911 if (Swap)
5912 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005913
Nate Begeman30a0de92008-07-17 16:51:19 +00005914 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5915 // bits of the inputs before performing those operations.
5916 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005917 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005918 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5919 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005920 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005921 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5922 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005923 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5924 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005925 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005926
Dale Johannesenace16102009-02-03 19:33:06 +00005927 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005928
5929 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005930 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005931 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005932
Nate Begeman30a0de92008-07-17 16:51:19 +00005933 return Result;
5934}
Evan Cheng0488db92007-09-25 01:57:46 +00005935
Evan Cheng370e5342008-12-03 08:38:43 +00005936// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005937static bool isX86LogicalCmp(SDValue Op) {
5938 unsigned Opc = Op.getNode()->getOpcode();
5939 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5940 return true;
5941 if (Op.getResNo() == 1 &&
5942 (Opc == X86ISD::ADD ||
5943 Opc == X86ISD::SUB ||
5944 Opc == X86ISD::SMUL ||
5945 Opc == X86ISD::UMUL ||
5946 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005947 Opc == X86ISD::DEC ||
5948 Opc == X86ISD::OR ||
5949 Opc == X86ISD::XOR ||
5950 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005951 return true;
5952
5953 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005954}
5955
Dan Gohman475871a2008-07-27 21:46:04 +00005956SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005957 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005958 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005959 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005961
Dan Gohman1a492952009-10-20 16:22:37 +00005962 if (Cond.getOpcode() == ISD::SETCC) {
5963 SDValue NewCond = LowerSETCC(Cond, DAG);
5964 if (NewCond.getNode())
5965 Cond = NewCond;
5966 }
Evan Cheng734503b2006-09-11 02:19:56 +00005967
Evan Chengad9c0a32009-12-15 00:53:42 +00005968 // Look pass (and (setcc_carry (cmp ...)), 1).
5969 if (Cond.getOpcode() == ISD::AND &&
5970 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
5971 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
5972 if (C && C->getAPIntValue() == 1)
5973 Cond = Cond.getOperand(0);
5974 }
5975
Evan Cheng3f41d662007-10-08 22:16:29 +00005976 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5977 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00005978 if (Cond.getOpcode() == X86ISD::SETCC ||
5979 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00005980 CC = Cond.getOperand(0);
5981
Dan Gohman475871a2008-07-27 21:46:04 +00005982 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005983 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00005984 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005985
Evan Cheng3f41d662007-10-08 22:16:29 +00005986 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005987 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005988 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005989 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005990
Chris Lattnerd1980a52009-03-12 06:52:53 +00005991 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5992 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005993 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005994 addTest = false;
5995 }
5996 }
5997
5998 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005999 // Look pass the truncate.
6000 if (Cond.getOpcode() == ISD::TRUNCATE)
6001 Cond = Cond.getOperand(0);
6002
6003 // We know the result of AND is compared against zero. Try to match
6004 // it to BT.
6005 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6006 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6007 if (NewSetCC.getNode()) {
6008 CC = NewSetCC.getOperand(0);
6009 Cond = NewSetCC.getOperand(1);
6010 addTest = false;
6011 }
6012 }
6013 }
6014
6015 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006016 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006017 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006018 }
6019
Owen Anderson825b72b2009-08-11 20:47:22 +00006020 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Evan Cheng0488db92007-09-25 01:57:46 +00006021 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6022 // condition is true.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006023 SDValue Ops[] = { Op.getOperand(2), Op.getOperand(1), CC, Cond };
6024 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006025}
6026
Evan Cheng370e5342008-12-03 08:38:43 +00006027// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6028// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6029// from the AND / OR.
6030static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6031 Opc = Op.getOpcode();
6032 if (Opc != ISD::OR && Opc != ISD::AND)
6033 return false;
6034 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6035 Op.getOperand(0).hasOneUse() &&
6036 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6037 Op.getOperand(1).hasOneUse());
6038}
6039
Evan Cheng961d6d42009-02-02 08:19:07 +00006040// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6041// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006042static bool isXor1OfSetCC(SDValue Op) {
6043 if (Op.getOpcode() != ISD::XOR)
6044 return false;
6045 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6046 if (N1C && N1C->getAPIntValue() == 1) {
6047 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6048 Op.getOperand(0).hasOneUse();
6049 }
6050 return false;
6051}
6052
Dan Gohman475871a2008-07-27 21:46:04 +00006053SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006054 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006055 SDValue Chain = Op.getOperand(0);
6056 SDValue Cond = Op.getOperand(1);
6057 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006058 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006059 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006060
Dan Gohman1a492952009-10-20 16:22:37 +00006061 if (Cond.getOpcode() == ISD::SETCC) {
6062 SDValue NewCond = LowerSETCC(Cond, DAG);
6063 if (NewCond.getNode())
6064 Cond = NewCond;
6065 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006066#if 0
6067 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006068 else if (Cond.getOpcode() == X86ISD::ADD ||
6069 Cond.getOpcode() == X86ISD::SUB ||
6070 Cond.getOpcode() == X86ISD::SMUL ||
6071 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006072 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006073#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006074
Evan Chengad9c0a32009-12-15 00:53:42 +00006075 // Look pass (and (setcc_carry (cmp ...)), 1).
6076 if (Cond.getOpcode() == ISD::AND &&
6077 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6078 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6079 if (C && C->getAPIntValue() == 1)
6080 Cond = Cond.getOperand(0);
6081 }
6082
Evan Cheng3f41d662007-10-08 22:16:29 +00006083 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6084 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006085 if (Cond.getOpcode() == X86ISD::SETCC ||
6086 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006087 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006088
Dan Gohman475871a2008-07-27 21:46:04 +00006089 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006090 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006091 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006092 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006093 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006094 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006095 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006096 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006097 default: break;
6098 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006099 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006100 // These can only come from an arithmetic instruction with overflow,
6101 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006102 Cond = Cond.getNode()->getOperand(1);
6103 addTest = false;
6104 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006105 }
Evan Cheng0488db92007-09-25 01:57:46 +00006106 }
Evan Cheng370e5342008-12-03 08:38:43 +00006107 } else {
6108 unsigned CondOpc;
6109 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6110 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006111 if (CondOpc == ISD::OR) {
6112 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6113 // two branches instead of an explicit OR instruction with a
6114 // separate test.
6115 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006116 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006117 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006118 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006119 Chain, Dest, CC, Cmp);
6120 CC = Cond.getOperand(1).getOperand(0);
6121 Cond = Cmp;
6122 addTest = false;
6123 }
6124 } else { // ISD::AND
6125 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6126 // two branches instead of an explicit AND instruction with a
6127 // separate test. However, we only do this if this block doesn't
6128 // have a fall-through edge, because this requires an explicit
6129 // jmp when the condition is false.
6130 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006131 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006132 Op.getNode()->hasOneUse()) {
6133 X86::CondCode CCode =
6134 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6135 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006136 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006137 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6138 // Look for an unconditional branch following this conditional branch.
6139 // We need this because we need to reverse the successors in order
6140 // to implement FCMP_OEQ.
6141 if (User.getOpcode() == ISD::BR) {
6142 SDValue FalseBB = User.getOperand(1);
6143 SDValue NewBR =
6144 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6145 assert(NewBR == User);
6146 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006147
Dale Johannesene4d209d2009-02-03 20:21:25 +00006148 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006149 Chain, Dest, CC, Cmp);
6150 X86::CondCode CCode =
6151 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6152 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006153 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006154 Cond = Cmp;
6155 addTest = false;
6156 }
6157 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006158 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006159 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6160 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6161 // It should be transformed during dag combiner except when the condition
6162 // is set by a arithmetics with overflow node.
6163 X86::CondCode CCode =
6164 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6165 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006166 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006167 Cond = Cond.getOperand(0).getOperand(1);
6168 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006169 }
Evan Cheng0488db92007-09-25 01:57:46 +00006170 }
6171
6172 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006173 // Look pass the truncate.
6174 if (Cond.getOpcode() == ISD::TRUNCATE)
6175 Cond = Cond.getOperand(0);
6176
6177 // We know the result of AND is compared against zero. Try to match
6178 // it to BT.
6179 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6180 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6181 if (NewSetCC.getNode()) {
6182 CC = NewSetCC.getOperand(0);
6183 Cond = NewSetCC.getOperand(1);
6184 addTest = false;
6185 }
6186 }
6187 }
6188
6189 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006190 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006191 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006192 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006193 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006194 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006195}
6196
Anton Korobeynikove060b532007-04-17 19:34:00 +00006197
6198// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6199// Calls to _alloca is needed to probe the stack when allocating more than 4k
6200// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6201// that the guard pages used by the OS virtual memory manager are allocated in
6202// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006203SDValue
6204X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006205 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006206 assert(Subtarget->isTargetCygMing() &&
6207 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006208 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006209
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006210 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006211 SDValue Chain = Op.getOperand(0);
6212 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006213 // FIXME: Ensure alignment here
6214
Dan Gohman475871a2008-07-27 21:46:04 +00006215 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006216
Owen Andersone50ed302009-08-10 22:56:29 +00006217 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006219
Chris Lattnere563bbc2008-10-11 22:08:30 +00006220 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006221
Dale Johannesendd64c412009-02-04 00:33:20 +00006222 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006223 Flag = Chain.getValue(1);
6224
Owen Anderson825b72b2009-08-11 20:47:22 +00006225 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006226 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006227 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006228 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006229 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006230 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006231 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006232 Flag = Chain.getValue(1);
6233
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006234 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006235 DAG.getIntPtrConstant(0, true),
6236 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006237 Flag);
6238
Dale Johannesendd64c412009-02-04 00:33:20 +00006239 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006240
Dan Gohman475871a2008-07-27 21:46:04 +00006241 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006242 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006243}
6244
Dan Gohman475871a2008-07-27 21:46:04 +00006245SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006246X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006247 SDValue Chain,
6248 SDValue Dst, SDValue Src,
6249 SDValue Size, unsigned Align,
6250 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006251 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006252 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006253
Bill Wendling6f287b22008-09-30 21:22:07 +00006254 // If not DWORD aligned or size is more than the threshold, call the library.
6255 // The libc version is likely to be faster for these cases. It can use the
6256 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006257 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006258 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006259 ConstantSize->getZExtValue() >
6260 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006261 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006262
6263 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006264 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006265
Bill Wendling6158d842008-10-01 00:59:58 +00006266 if (const char *bzeroEntry = V &&
6267 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006268 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006269 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006270 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006271 TargetLowering::ArgListEntry Entry;
6272 Entry.Node = Dst;
6273 Entry.Ty = IntPtrTy;
6274 Args.push_back(Entry);
6275 Entry.Node = Size;
6276 Args.push_back(Entry);
6277 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006278 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6279 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006280 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006281 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6282 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006283 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006284 }
6285
Dan Gohman707e0182008-04-12 04:36:06 +00006286 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006287 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006288 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006289
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006290 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006291 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006292 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006293 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006294 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006295 unsigned BytesLeft = 0;
6296 bool TwoRepStos = false;
6297 if (ValC) {
6298 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006299 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006300
Evan Cheng0db9fe62006-04-25 20:13:52 +00006301 // If the value is a constant, then we can potentially use larger sets.
6302 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006303 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006304 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006305 ValReg = X86::AX;
6306 Val = (Val << 8) | Val;
6307 break;
6308 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006309 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006310 ValReg = X86::EAX;
6311 Val = (Val << 8) | Val;
6312 Val = (Val << 16) | Val;
6313 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006314 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006315 ValReg = X86::RAX;
6316 Val = (Val << 32) | Val;
6317 }
6318 break;
6319 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006320 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006321 ValReg = X86::AL;
6322 Count = DAG.getIntPtrConstant(SizeVal);
6323 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006324 }
6325
Owen Anderson825b72b2009-08-11 20:47:22 +00006326 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006327 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006328 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6329 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006330 }
6331
Dale Johannesen0f502f62009-02-03 22:26:09 +00006332 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006333 InFlag);
6334 InFlag = Chain.getValue(1);
6335 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006336 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006337 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006338 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006339 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006340 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006341
Scott Michelfdc40a02009-02-17 22:15:04 +00006342 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006343 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006344 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006345 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006346 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006347 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006348 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006349 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006350
Owen Anderson825b72b2009-08-11 20:47:22 +00006351 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006352 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6353 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006354
Evan Cheng0db9fe62006-04-25 20:13:52 +00006355 if (TwoRepStos) {
6356 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006357 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006358 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006359 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006360 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6361 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006362 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006363 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006364 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006365 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006366 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6367 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006368 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006369 // Handle the last 1 - 7 bytes.
6370 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006371 EVT AddrVT = Dst.getValueType();
6372 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006373
Dale Johannesen0f502f62009-02-03 22:26:09 +00006374 Chain = DAG.getMemset(Chain, dl,
6375 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006376 DAG.getConstant(Offset, AddrVT)),
6377 Src,
6378 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006379 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006380 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006381
Dan Gohman707e0182008-04-12 04:36:06 +00006382 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006383 return Chain;
6384}
Evan Cheng11e15b32006-04-03 20:53:28 +00006385
Dan Gohman475871a2008-07-27 21:46:04 +00006386SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006387X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006388 SDValue Chain, SDValue Dst, SDValue Src,
6389 SDValue Size, unsigned Align,
6390 bool AlwaysInline,
6391 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006392 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006393 // This requires the copy size to be a constant, preferrably
6394 // within a subtarget-specific limit.
6395 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6396 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006397 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006398 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006399 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006400 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006401
Evan Cheng1887c1c2008-08-21 21:00:15 +00006402 /// If not DWORD aligned, call the library.
6403 if ((Align & 3) != 0)
6404 return SDValue();
6405
6406 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006407 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006408 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006409 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006410
Duncan Sands83ec4b62008-06-06 12:08:01 +00006411 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006412 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006413 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006414 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006415
Dan Gohman475871a2008-07-27 21:46:04 +00006416 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006417 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006418 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006419 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006420 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006421 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006422 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006423 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006424 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006425 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006426 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006427 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006428 InFlag = Chain.getValue(1);
6429
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006431 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6432 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6433 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006434
Dan Gohman475871a2008-07-27 21:46:04 +00006435 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006436 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006437 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006438 // Handle the last 1 - 7 bytes.
6439 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006440 EVT DstVT = Dst.getValueType();
6441 EVT SrcVT = Src.getValueType();
6442 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006443 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006444 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006445 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006446 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006447 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006448 DAG.getConstant(BytesLeft, SizeVT),
6449 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006450 DstSV, DstSVOff + Offset,
6451 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006452 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006453
Owen Anderson825b72b2009-08-11 20:47:22 +00006454 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006455 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006456}
6457
Dan Gohman475871a2008-07-27 21:46:04 +00006458SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006459 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006460 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006461
Evan Cheng25ab6902006-09-08 06:48:29 +00006462 if (!Subtarget->is64Bit()) {
6463 // vastart just stores the address of the VarArgsFrameIndex slot into the
6464 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006465 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006466 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006467 }
6468
6469 // __va_list_tag:
6470 // gp_offset (0 - 6 * 8)
6471 // fp_offset (48 - 48 + 8 * 16)
6472 // overflow_arg_area (point to parameters coming in memory).
6473 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006474 SmallVector<SDValue, 8> MemOps;
6475 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006476 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006477 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006478 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006479 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006480 MemOps.push_back(Store);
6481
6482 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006483 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006484 FIN, DAG.getIntPtrConstant(4));
6485 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006486 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006487 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006488 MemOps.push_back(Store);
6489
6490 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006491 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006492 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006493 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006494 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006495 MemOps.push_back(Store);
6496
6497 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006498 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006499 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006500 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006501 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006502 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006503 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006504 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006505}
6506
Dan Gohman475871a2008-07-27 21:46:04 +00006507SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006508 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6509 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006510 SDValue Chain = Op.getOperand(0);
6511 SDValue SrcPtr = Op.getOperand(1);
6512 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006513
Torok Edwindac237e2009-07-08 20:53:28 +00006514 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006515 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006516}
6517
Dan Gohman475871a2008-07-27 21:46:04 +00006518SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006519 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006520 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006521 SDValue Chain = Op.getOperand(0);
6522 SDValue DstPtr = Op.getOperand(1);
6523 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006524 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6525 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006526 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006527
Dale Johannesendd64c412009-02-04 00:33:20 +00006528 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006529 DAG.getIntPtrConstant(24), 8, false,
6530 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006531}
6532
Dan Gohman475871a2008-07-27 21:46:04 +00006533SDValue
6534X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006535 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006536 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006537 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006538 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006539 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006540 case Intrinsic::x86_sse_comieq_ss:
6541 case Intrinsic::x86_sse_comilt_ss:
6542 case Intrinsic::x86_sse_comile_ss:
6543 case Intrinsic::x86_sse_comigt_ss:
6544 case Intrinsic::x86_sse_comige_ss:
6545 case Intrinsic::x86_sse_comineq_ss:
6546 case Intrinsic::x86_sse_ucomieq_ss:
6547 case Intrinsic::x86_sse_ucomilt_ss:
6548 case Intrinsic::x86_sse_ucomile_ss:
6549 case Intrinsic::x86_sse_ucomigt_ss:
6550 case Intrinsic::x86_sse_ucomige_ss:
6551 case Intrinsic::x86_sse_ucomineq_ss:
6552 case Intrinsic::x86_sse2_comieq_sd:
6553 case Intrinsic::x86_sse2_comilt_sd:
6554 case Intrinsic::x86_sse2_comile_sd:
6555 case Intrinsic::x86_sse2_comigt_sd:
6556 case Intrinsic::x86_sse2_comige_sd:
6557 case Intrinsic::x86_sse2_comineq_sd:
6558 case Intrinsic::x86_sse2_ucomieq_sd:
6559 case Intrinsic::x86_sse2_ucomilt_sd:
6560 case Intrinsic::x86_sse2_ucomile_sd:
6561 case Intrinsic::x86_sse2_ucomigt_sd:
6562 case Intrinsic::x86_sse2_ucomige_sd:
6563 case Intrinsic::x86_sse2_ucomineq_sd: {
6564 unsigned Opc = 0;
6565 ISD::CondCode CC = ISD::SETCC_INVALID;
6566 switch (IntNo) {
6567 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006568 case Intrinsic::x86_sse_comieq_ss:
6569 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570 Opc = X86ISD::COMI;
6571 CC = ISD::SETEQ;
6572 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006573 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006574 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006575 Opc = X86ISD::COMI;
6576 CC = ISD::SETLT;
6577 break;
6578 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006579 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 Opc = X86ISD::COMI;
6581 CC = ISD::SETLE;
6582 break;
6583 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006584 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006585 Opc = X86ISD::COMI;
6586 CC = ISD::SETGT;
6587 break;
6588 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006589 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006590 Opc = X86ISD::COMI;
6591 CC = ISD::SETGE;
6592 break;
6593 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006594 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006595 Opc = X86ISD::COMI;
6596 CC = ISD::SETNE;
6597 break;
6598 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006599 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 Opc = X86ISD::UCOMI;
6601 CC = ISD::SETEQ;
6602 break;
6603 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006604 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 Opc = X86ISD::UCOMI;
6606 CC = ISD::SETLT;
6607 break;
6608 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006609 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 Opc = X86ISD::UCOMI;
6611 CC = ISD::SETLE;
6612 break;
6613 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006614 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006615 Opc = X86ISD::UCOMI;
6616 CC = ISD::SETGT;
6617 break;
6618 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006619 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006620 Opc = X86ISD::UCOMI;
6621 CC = ISD::SETGE;
6622 break;
6623 case Intrinsic::x86_sse_ucomineq_ss:
6624 case Intrinsic::x86_sse2_ucomineq_sd:
6625 Opc = X86ISD::UCOMI;
6626 CC = ISD::SETNE;
6627 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006628 }
Evan Cheng734503b2006-09-11 02:19:56 +00006629
Dan Gohman475871a2008-07-27 21:46:04 +00006630 SDValue LHS = Op.getOperand(1);
6631 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006632 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006633 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006634 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6635 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6636 DAG.getConstant(X86CC, MVT::i8), Cond);
6637 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006638 }
Eric Christopher71c67532009-07-29 00:28:05 +00006639 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006640 // an integer value, not just an instruction so lower it to the ptest
6641 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006642 case Intrinsic::x86_sse41_ptestz:
6643 case Intrinsic::x86_sse41_ptestc:
6644 case Intrinsic::x86_sse41_ptestnzc:{
6645 unsigned X86CC = 0;
6646 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006647 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006648 case Intrinsic::x86_sse41_ptestz:
6649 // ZF = 1
6650 X86CC = X86::COND_E;
6651 break;
6652 case Intrinsic::x86_sse41_ptestc:
6653 // CF = 1
6654 X86CC = X86::COND_B;
6655 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006656 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006657 // ZF and CF = 0
6658 X86CC = X86::COND_A;
6659 break;
6660 }
Eric Christopherfd179292009-08-27 18:07:15 +00006661
Eric Christopher71c67532009-07-29 00:28:05 +00006662 SDValue LHS = Op.getOperand(1);
6663 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006664 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6665 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6666 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6667 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006668 }
Evan Cheng5759f972008-05-04 09:15:50 +00006669
6670 // Fix vector shift instructions where the last operand is a non-immediate
6671 // i32 value.
6672 case Intrinsic::x86_sse2_pslli_w:
6673 case Intrinsic::x86_sse2_pslli_d:
6674 case Intrinsic::x86_sse2_pslli_q:
6675 case Intrinsic::x86_sse2_psrli_w:
6676 case Intrinsic::x86_sse2_psrli_d:
6677 case Intrinsic::x86_sse2_psrli_q:
6678 case Intrinsic::x86_sse2_psrai_w:
6679 case Intrinsic::x86_sse2_psrai_d:
6680 case Intrinsic::x86_mmx_pslli_w:
6681 case Intrinsic::x86_mmx_pslli_d:
6682 case Intrinsic::x86_mmx_pslli_q:
6683 case Intrinsic::x86_mmx_psrli_w:
6684 case Intrinsic::x86_mmx_psrli_d:
6685 case Intrinsic::x86_mmx_psrli_q:
6686 case Intrinsic::x86_mmx_psrai_w:
6687 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006688 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006689 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006690 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006691
6692 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006694 switch (IntNo) {
6695 case Intrinsic::x86_sse2_pslli_w:
6696 NewIntNo = Intrinsic::x86_sse2_psll_w;
6697 break;
6698 case Intrinsic::x86_sse2_pslli_d:
6699 NewIntNo = Intrinsic::x86_sse2_psll_d;
6700 break;
6701 case Intrinsic::x86_sse2_pslli_q:
6702 NewIntNo = Intrinsic::x86_sse2_psll_q;
6703 break;
6704 case Intrinsic::x86_sse2_psrli_w:
6705 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6706 break;
6707 case Intrinsic::x86_sse2_psrli_d:
6708 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6709 break;
6710 case Intrinsic::x86_sse2_psrli_q:
6711 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6712 break;
6713 case Intrinsic::x86_sse2_psrai_w:
6714 NewIntNo = Intrinsic::x86_sse2_psra_w;
6715 break;
6716 case Intrinsic::x86_sse2_psrai_d:
6717 NewIntNo = Intrinsic::x86_sse2_psra_d;
6718 break;
6719 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006721 switch (IntNo) {
6722 case Intrinsic::x86_mmx_pslli_w:
6723 NewIntNo = Intrinsic::x86_mmx_psll_w;
6724 break;
6725 case Intrinsic::x86_mmx_pslli_d:
6726 NewIntNo = Intrinsic::x86_mmx_psll_d;
6727 break;
6728 case Intrinsic::x86_mmx_pslli_q:
6729 NewIntNo = Intrinsic::x86_mmx_psll_q;
6730 break;
6731 case Intrinsic::x86_mmx_psrli_w:
6732 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6733 break;
6734 case Intrinsic::x86_mmx_psrli_d:
6735 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6736 break;
6737 case Intrinsic::x86_mmx_psrli_q:
6738 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6739 break;
6740 case Intrinsic::x86_mmx_psrai_w:
6741 NewIntNo = Intrinsic::x86_mmx_psra_w;
6742 break;
6743 case Intrinsic::x86_mmx_psrai_d:
6744 NewIntNo = Intrinsic::x86_mmx_psra_d;
6745 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006746 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006747 }
6748 break;
6749 }
6750 }
Mon P Wangefa42202009-09-03 19:56:25 +00006751
6752 // The vector shift intrinsics with scalars uses 32b shift amounts but
6753 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6754 // to be zero.
6755 SDValue ShOps[4];
6756 ShOps[0] = ShAmt;
6757 ShOps[1] = DAG.getConstant(0, MVT::i32);
6758 if (ShAmtVT == MVT::v4i32) {
6759 ShOps[2] = DAG.getUNDEF(MVT::i32);
6760 ShOps[3] = DAG.getUNDEF(MVT::i32);
6761 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6762 } else {
6763 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6764 }
6765
Owen Andersone50ed302009-08-10 22:56:29 +00006766 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006767 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006768 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006769 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006770 Op.getOperand(1), ShAmt);
6771 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006772 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006773}
Evan Cheng72261582005-12-20 06:22:03 +00006774
Dan Gohman475871a2008-07-27 21:46:04 +00006775SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006776 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006777 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006778
6779 if (Depth > 0) {
6780 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6781 SDValue Offset =
6782 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006783 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006784 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006785 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006786 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006787 NULL, 0);
6788 }
6789
6790 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006791 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006792 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006793 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006794}
6795
Dan Gohman475871a2008-07-27 21:46:04 +00006796SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006797 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6798 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006799 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006800 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006801 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6802 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006803 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006804 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006805 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006806 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006807}
6808
Dan Gohman475871a2008-07-27 21:46:04 +00006809SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006810 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006811 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006812}
6813
Dan Gohman475871a2008-07-27 21:46:04 +00006814SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006815{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006816 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue Chain = Op.getOperand(0);
6818 SDValue Offset = Op.getOperand(1);
6819 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006820 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006821
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006822 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6823 getPointerTy());
6824 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006825
Dale Johannesene4d209d2009-02-03 20:21:25 +00006826 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006827 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006828 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6829 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006830 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006831 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006832
Dale Johannesene4d209d2009-02-03 20:21:25 +00006833 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006835 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006836}
6837
Dan Gohman475871a2008-07-27 21:46:04 +00006838SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006839 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006840 SDValue Root = Op.getOperand(0);
6841 SDValue Trmp = Op.getOperand(1); // trampoline
6842 SDValue FPtr = Op.getOperand(2); // nested function
6843 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006844 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006845
Dan Gohman69de1932008-02-06 22:27:42 +00006846 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006847
Duncan Sands339e14f2008-01-16 22:55:25 +00006848 const X86InstrInfo *TII =
6849 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6850
Duncan Sandsb116fac2007-07-27 20:02:49 +00006851 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006852 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006853
6854 // Large code-model.
6855
6856 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6857 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6858
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006859 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6860 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006861
6862 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6863
6864 // Load the pointer to the nested function into R11.
6865 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006866 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006867 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006868 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006869
Owen Anderson825b72b2009-08-11 20:47:22 +00006870 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6871 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006872 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006873
6874 // Load the 'nest' parameter value into R10.
6875 // R10 is specified in X86CallingConv.td
6876 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006877 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6878 DAG.getConstant(10, MVT::i64));
6879 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006880 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006881
Owen Anderson825b72b2009-08-11 20:47:22 +00006882 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6883 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006884 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006885
6886 // Jump to the nested function.
6887 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006888 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6889 DAG.getConstant(20, MVT::i64));
6890 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006891 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006892
6893 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6895 DAG.getConstant(22, MVT::i64));
6896 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006897 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006898
Dan Gohman475871a2008-07-27 21:46:04 +00006899 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006900 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006901 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006902 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006903 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006904 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006905 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006906 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006907
6908 switch (CC) {
6909 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006910 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006911 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006912 case CallingConv::X86_StdCall: {
6913 // Pass 'nest' parameter in ECX.
6914 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006915 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006916
6917 // Check that ECX wasn't needed by an 'inreg' parameter.
6918 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006919 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006920
Chris Lattner58d74912008-03-12 17:45:29 +00006921 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006922 unsigned InRegCount = 0;
6923 unsigned Idx = 1;
6924
6925 for (FunctionType::param_iterator I = FTy->param_begin(),
6926 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006927 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006928 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006929 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006930
6931 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006932 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006933 }
6934 }
6935 break;
6936 }
6937 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006938 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006939 // Pass 'nest' parameter in EAX.
6940 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006941 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006942 break;
6943 }
6944
Dan Gohman475871a2008-07-27 21:46:04 +00006945 SDValue OutChains[4];
6946 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006947
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6949 DAG.getConstant(10, MVT::i32));
6950 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006951
Duncan Sands339e14f2008-01-16 22:55:25 +00006952 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006953 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006954 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006956 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006957
Owen Anderson825b72b2009-08-11 20:47:22 +00006958 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6959 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006960 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006961
Duncan Sands339e14f2008-01-16 22:55:25 +00006962 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6964 DAG.getConstant(5, MVT::i32));
6965 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006966 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006967
Owen Anderson825b72b2009-08-11 20:47:22 +00006968 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6969 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006970 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006971
Dan Gohman475871a2008-07-27 21:46:04 +00006972 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006973 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006974 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006975 }
6976}
6977
Dan Gohman475871a2008-07-27 21:46:04 +00006978SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006979 /*
6980 The rounding mode is in bits 11:10 of FPSR, and has the following
6981 settings:
6982 00 Round to nearest
6983 01 Round to -inf
6984 10 Round to +inf
6985 11 Round to 0
6986
6987 FLT_ROUNDS, on the other hand, expects the following:
6988 -1 Undefined
6989 0 Round to 0
6990 1 Round to nearest
6991 2 Round to +inf
6992 3 Round to -inf
6993
6994 To perform the conversion, we do:
6995 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6996 */
6997
6998 MachineFunction &MF = DAG.getMachineFunction();
6999 const TargetMachine &TM = MF.getTarget();
7000 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7001 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007002 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007003 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007004
7005 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007006 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007007 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007008
Owen Anderson825b72b2009-08-11 20:47:22 +00007009 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007010 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007011
7012 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007014
7015 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007017 DAG.getNode(ISD::SRL, dl, MVT::i16,
7018 DAG.getNode(ISD::AND, dl, MVT::i16,
7019 CWD, DAG.getConstant(0x800, MVT::i16)),
7020 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007021 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007022 DAG.getNode(ISD::SRL, dl, MVT::i16,
7023 DAG.getNode(ISD::AND, dl, MVT::i16,
7024 CWD, DAG.getConstant(0x400, MVT::i16)),
7025 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007026
Dan Gohman475871a2008-07-27 21:46:04 +00007027 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 DAG.getNode(ISD::AND, dl, MVT::i16,
7029 DAG.getNode(ISD::ADD, dl, MVT::i16,
7030 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7031 DAG.getConstant(1, MVT::i16)),
7032 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007033
7034
Duncan Sands83ec4b62008-06-06 12:08:01 +00007035 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007036 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007037}
7038
Dan Gohman475871a2008-07-27 21:46:04 +00007039SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007040 EVT VT = Op.getValueType();
7041 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007042 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007043 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007044
7045 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007046 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007047 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007048 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007049 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007050 }
Evan Cheng18efe262007-12-14 02:13:44 +00007051
Evan Cheng152804e2007-12-14 08:30:15 +00007052 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007054 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007055
7056 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007057 SDValue Ops[] = {
7058 Op,
7059 DAG.getConstant(NumBits+NumBits-1, OpVT),
7060 DAG.getConstant(X86::COND_E, MVT::i8),
7061 Op.getValue(1)
7062 };
7063 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007064
7065 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007066 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007067
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 if (VT == MVT::i8)
7069 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007070 return Op;
7071}
7072
Dan Gohman475871a2008-07-27 21:46:04 +00007073SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007074 EVT VT = Op.getValueType();
7075 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007076 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007077 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007078
7079 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 if (VT == MVT::i8) {
7081 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007082 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007083 }
Evan Cheng152804e2007-12-14 08:30:15 +00007084
7085 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007087 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007088
7089 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007090 SDValue Ops[] = {
7091 Op,
7092 DAG.getConstant(NumBits, OpVT),
7093 DAG.getConstant(X86::COND_E, MVT::i8),
7094 Op.getValue(1)
7095 };
7096 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007097
Owen Anderson825b72b2009-08-11 20:47:22 +00007098 if (VT == MVT::i8)
7099 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007100 return Op;
7101}
7102
Mon P Wangaf9b9522008-12-18 21:42:19 +00007103SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007104 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007105 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007106 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Mon P Wangaf9b9522008-12-18 21:42:19 +00007108 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7109 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7110 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7111 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7112 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7113 //
7114 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7115 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7116 // return AloBlo + AloBhi + AhiBlo;
7117
7118 SDValue A = Op.getOperand(0);
7119 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007120
Dale Johannesene4d209d2009-02-03 20:21:25 +00007121 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7123 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007124 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7126 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007127 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007129 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007130 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007132 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007133 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007134 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007135 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007136 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7138 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007139 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7141 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007142 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7143 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007144 return Res;
7145}
7146
7147
Bill Wendling74c37652008-12-09 22:08:41 +00007148SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7149 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7150 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007151 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7152 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007153 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007154 SDValue LHS = N->getOperand(0);
7155 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007156 unsigned BaseOp = 0;
7157 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007158 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007159
7160 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007161 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007162 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007163 // A subtract of one will be selected as a INC. Note that INC doesn't
7164 // set CF, so we can't do this for UADDO.
7165 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7166 if (C->getAPIntValue() == 1) {
7167 BaseOp = X86ISD::INC;
7168 Cond = X86::COND_O;
7169 break;
7170 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007171 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007172 Cond = X86::COND_O;
7173 break;
7174 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007175 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007176 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007177 break;
7178 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007179 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7180 // set CF, so we can't do this for USUBO.
7181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7182 if (C->getAPIntValue() == 1) {
7183 BaseOp = X86ISD::DEC;
7184 Cond = X86::COND_O;
7185 break;
7186 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007187 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007188 Cond = X86::COND_O;
7189 break;
7190 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007191 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007192 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007193 break;
7194 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007195 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007196 Cond = X86::COND_O;
7197 break;
7198 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007199 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007200 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007201 break;
7202 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007203
Bill Wendling61edeb52008-12-02 01:06:39 +00007204 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007205 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007206 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007207
Bill Wendling61edeb52008-12-02 01:06:39 +00007208 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007209 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007210 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007211
Bill Wendling61edeb52008-12-02 01:06:39 +00007212 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7213 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007214}
7215
Dan Gohman475871a2008-07-27 21:46:04 +00007216SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007217 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007218 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007219 unsigned Reg = 0;
7220 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007222 default:
7223 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 case MVT::i8: Reg = X86::AL; size = 1; break;
7225 case MVT::i16: Reg = X86::AX; size = 2; break;
7226 case MVT::i32: Reg = X86::EAX; size = 4; break;
7227 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007228 assert(Subtarget->is64Bit() && "Node not type legal!");
7229 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007230 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007231 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007232 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007233 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007234 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007235 Op.getOperand(1),
7236 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007238 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007239 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007240 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007241 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007242 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007243 return cpOut;
7244}
7245
Duncan Sands1607f052008-12-01 11:39:25 +00007246SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007247 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007248 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007250 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007251 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007252 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007253 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7254 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007255 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007256 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7257 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007258 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007259 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007260 rdx.getValue(1)
7261 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007263}
7264
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007265SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7266 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007267 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007268 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007269 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007270 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007271 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007272 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007273 Node->getOperand(0),
7274 Node->getOperand(1), negOp,
7275 cast<AtomicSDNode>(Node)->getSrcValue(),
7276 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007277}
7278
Evan Cheng0db9fe62006-04-25 20:13:52 +00007279/// LowerOperation - Provide custom lowering hooks for some operations.
7280///
Dan Gohman475871a2008-07-27 21:46:04 +00007281SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007282 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007283 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007284 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7285 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007286 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007287 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007288 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7289 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7290 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7291 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7292 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7293 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007294 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007295 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007296 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007297 case ISD::SHL_PARTS:
7298 case ISD::SRA_PARTS:
7299 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7300 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007301 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007302 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007303 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007304 case ISD::FABS: return LowerFABS(Op, DAG);
7305 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007306 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007307 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007308 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007309 case ISD::SELECT: return LowerSELECT(Op, DAG);
7310 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007311 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007312 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007313 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007314 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007315 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007316 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7317 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007318 case ISD::FRAME_TO_ARGS_OFFSET:
7319 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007320 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007321 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007322 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007323 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007324 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7325 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007326 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007327 case ISD::SADDO:
7328 case ISD::UADDO:
7329 case ISD::SSUBO:
7330 case ISD::USUBO:
7331 case ISD::SMULO:
7332 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007333 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007334 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007335}
7336
Duncan Sands1607f052008-12-01 11:39:25 +00007337void X86TargetLowering::
7338ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7339 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007340 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007341 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007342 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007343
7344 SDValue Chain = Node->getOperand(0);
7345 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007346 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007347 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007348 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007349 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007350 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007351 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007352 SDValue Result =
7353 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7354 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007355 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007356 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007357 Results.push_back(Result.getValue(2));
7358}
7359
Duncan Sands126d9072008-07-04 11:47:58 +00007360/// ReplaceNodeResults - Replace a node with an illegal result type
7361/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007362void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7363 SmallVectorImpl<SDValue>&Results,
7364 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007365 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007366 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007367 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007368 assert(false && "Do not know how to custom type legalize this operation!");
7369 return;
7370 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007371 std::pair<SDValue,SDValue> Vals =
7372 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007373 SDValue FIST = Vals.first, StackSlot = Vals.second;
7374 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007375 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007376 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007377 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007378 }
7379 return;
7380 }
7381 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007382 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007383 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007384 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007385 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007386 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007387 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007388 eax.getValue(2));
7389 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7390 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007391 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007392 Results.push_back(edx.getValue(1));
7393 return;
7394 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007395 case ISD::SDIV:
7396 case ISD::UDIV:
7397 case ISD::SREM:
7398 case ISD::UREM: {
7399 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7400 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7401 return;
7402 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007403 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007404 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007405 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007406 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7408 DAG.getConstant(0, MVT::i32));
7409 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7410 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007411 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7412 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007413 cpInL.getValue(1));
7414 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007415 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7416 DAG.getConstant(0, MVT::i32));
7417 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7418 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007419 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007420 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007421 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007422 swapInL.getValue(1));
7423 SDValue Ops[] = { swapInH.getValue(0),
7424 N->getOperand(1),
7425 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007426 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007427 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007428 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007429 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007430 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007431 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007432 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007434 Results.push_back(cpOutH.getValue(1));
7435 return;
7436 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007437 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007438 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7439 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007440 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007441 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7442 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007443 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007444 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7445 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007446 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007447 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7448 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007449 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007450 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7451 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007452 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007453 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7454 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007455 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007456 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7457 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007458 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007459}
7460
Evan Cheng72261582005-12-20 06:22:03 +00007461const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7462 switch (Opcode) {
7463 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007464 case X86ISD::BSF: return "X86ISD::BSF";
7465 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007466 case X86ISD::SHLD: return "X86ISD::SHLD";
7467 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007468 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007469 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007470 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007471 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007472 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007473 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007474 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7475 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7476 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007477 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007478 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007479 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007480 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007481 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007482 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007483 case X86ISD::COMI: return "X86ISD::COMI";
7484 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007485 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007486 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007487 case X86ISD::CMOV: return "X86ISD::CMOV";
7488 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007489 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007490 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7491 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007492 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007493 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007494 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007495 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007496 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007497 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7498 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007499 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007500 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007501 case X86ISD::FMAX: return "X86ISD::FMAX";
7502 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007503 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7504 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007505 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007506 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007507 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007508 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007509 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007510 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7511 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007512 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7513 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7514 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7515 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7516 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7517 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007518 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7519 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007520 case X86ISD::VSHL: return "X86ISD::VSHL";
7521 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007522 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7523 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7524 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7525 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7526 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7527 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7528 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7529 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7530 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7531 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007532 case X86ISD::ADD: return "X86ISD::ADD";
7533 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007534 case X86ISD::SMUL: return "X86ISD::SMUL";
7535 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007536 case X86ISD::INC: return "X86ISD::INC";
7537 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007538 case X86ISD::OR: return "X86ISD::OR";
7539 case X86ISD::XOR: return "X86ISD::XOR";
7540 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007541 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007542 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007543 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007544 }
7545}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007546
Chris Lattnerc9addb72007-03-30 23:15:24 +00007547// isLegalAddressingMode - Return true if the addressing mode represented
7548// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007549bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007550 const Type *Ty) const {
7551 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007552 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007553
Chris Lattnerc9addb72007-03-30 23:15:24 +00007554 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007555 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007556 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007557
Chris Lattnerc9addb72007-03-30 23:15:24 +00007558 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007559 unsigned GVFlags =
7560 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007561
Chris Lattnerdfed4132009-07-10 07:38:24 +00007562 // If a reference to this global requires an extra load, we can't fold it.
7563 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007564 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007565
Chris Lattnerdfed4132009-07-10 07:38:24 +00007566 // If BaseGV requires a register for the PIC base, we cannot also have a
7567 // BaseReg specified.
7568 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007569 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007570
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007571 // If lower 4G is not available, then we must use rip-relative addressing.
7572 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7573 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007574 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007575
Chris Lattnerc9addb72007-03-30 23:15:24 +00007576 switch (AM.Scale) {
7577 case 0:
7578 case 1:
7579 case 2:
7580 case 4:
7581 case 8:
7582 // These scales always work.
7583 break;
7584 case 3:
7585 case 5:
7586 case 9:
7587 // These scales are formed with basereg+scalereg. Only accept if there is
7588 // no basereg yet.
7589 if (AM.HasBaseReg)
7590 return false;
7591 break;
7592 default: // Other stuff never works.
7593 return false;
7594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007595
Chris Lattnerc9addb72007-03-30 23:15:24 +00007596 return true;
7597}
7598
7599
Evan Cheng2bd122c2007-10-26 01:56:11 +00007600bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7601 if (!Ty1->isInteger() || !Ty2->isInteger())
7602 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007603 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7604 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007605 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007606 return false;
7607 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007608}
7609
Owen Andersone50ed302009-08-10 22:56:29 +00007610bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007611 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007612 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007613 unsigned NumBits1 = VT1.getSizeInBits();
7614 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007615 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007616 return false;
7617 return Subtarget->is64Bit() || NumBits1 < 64;
7618}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007619
Dan Gohman97121ba2009-04-08 00:15:30 +00007620bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007621 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007622 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007623}
7624
Owen Andersone50ed302009-08-10 22:56:29 +00007625bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007626 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007628}
7629
Owen Andersone50ed302009-08-10 22:56:29 +00007630bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007631 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007632 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007633}
7634
Evan Cheng60c07e12006-07-05 22:17:51 +00007635/// isShuffleMaskLegal - Targets can use this to indicate that they only
7636/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7637/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7638/// are assumed to be legal.
7639bool
Eric Christopherfd179292009-08-27 18:07:15 +00007640X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007641 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007642 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007643 if (VT.getSizeInBits() == 64)
7644 return false;
7645
Nate Begemana09008b2009-10-19 02:17:23 +00007646 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007647 return (VT.getVectorNumElements() == 2 ||
7648 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7649 isMOVLMask(M, VT) ||
7650 isSHUFPMask(M, VT) ||
7651 isPSHUFDMask(M, VT) ||
7652 isPSHUFHWMask(M, VT) ||
7653 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007654 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007655 isUNPCKLMask(M, VT) ||
7656 isUNPCKHMask(M, VT) ||
7657 isUNPCKL_v_undef_Mask(M, VT) ||
7658 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007659}
7660
Dan Gohman7d8143f2008-04-09 20:09:42 +00007661bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007662X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007663 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007664 unsigned NumElts = VT.getVectorNumElements();
7665 // FIXME: This collection of masks seems suspect.
7666 if (NumElts == 2)
7667 return true;
7668 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7669 return (isMOVLMask(Mask, VT) ||
7670 isCommutedMOVLMask(Mask, VT, true) ||
7671 isSHUFPMask(Mask, VT) ||
7672 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007673 }
7674 return false;
7675}
7676
7677//===----------------------------------------------------------------------===//
7678// X86 Scheduler Hooks
7679//===----------------------------------------------------------------------===//
7680
Mon P Wang63307c32008-05-05 19:05:59 +00007681// private utility function
7682MachineBasicBlock *
7683X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7684 MachineBasicBlock *MBB,
7685 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007686 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007687 unsigned LoadOpc,
7688 unsigned CXchgOpc,
7689 unsigned copyOpc,
7690 unsigned notOpc,
7691 unsigned EAXreg,
7692 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007693 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007694 // For the atomic bitwise operator, we generate
7695 // thisMBB:
7696 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007697 // ld t1 = [bitinstr.addr]
7698 // op t2 = t1, [bitinstr.val]
7699 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007700 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7701 // bz newMBB
7702 // fallthrough -->nextMBB
7703 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7704 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007705 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007706 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007707
Mon P Wang63307c32008-05-05 19:05:59 +00007708 /// First build the CFG
7709 MachineFunction *F = MBB->getParent();
7710 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007711 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7712 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7713 F->insert(MBBIter, newMBB);
7714 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007715
Mon P Wang63307c32008-05-05 19:05:59 +00007716 // Move all successors to thisMBB to nextMBB
7717 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007718
Mon P Wang63307c32008-05-05 19:05:59 +00007719 // Update thisMBB to fall through to newMBB
7720 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007721
Mon P Wang63307c32008-05-05 19:05:59 +00007722 // newMBB jumps to itself and fall through to nextMBB
7723 newMBB->addSuccessor(nextMBB);
7724 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007725
Mon P Wang63307c32008-05-05 19:05:59 +00007726 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007727 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007728 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007729 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007730 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007731 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007732 int numArgs = bInstr->getNumOperands() - 1;
7733 for (int i=0; i < numArgs; ++i)
7734 argOpers[i] = &bInstr->getOperand(i+1);
7735
7736 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007737 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7738 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007739
Dale Johannesen140be2d2008-08-19 18:47:28 +00007740 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007741 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007742 for (int i=0; i <= lastAddrIndx; ++i)
7743 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007744
Dale Johannesen140be2d2008-08-19 18:47:28 +00007745 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007746 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007747 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007748 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007749 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007750 tt = t1;
7751
Dale Johannesen140be2d2008-08-19 18:47:28 +00007752 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007753 assert((argOpers[valArgIndx]->isReg() ||
7754 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007755 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007756 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007757 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007758 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007759 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007760 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007761 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007762
Dale Johannesene4d209d2009-02-03 20:21:25 +00007763 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007764 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007765
Dale Johannesene4d209d2009-02-03 20:21:25 +00007766 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007767 for (int i=0; i <= lastAddrIndx; ++i)
7768 (*MIB).addOperand(*argOpers[i]);
7769 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007770 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007771 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7772 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007773
Dale Johannesene4d209d2009-02-03 20:21:25 +00007774 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007775 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007776
Mon P Wang63307c32008-05-05 19:05:59 +00007777 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007778 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007779
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007780 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007781 return nextMBB;
7782}
7783
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007784// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007785MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007786X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7787 MachineBasicBlock *MBB,
7788 unsigned regOpcL,
7789 unsigned regOpcH,
7790 unsigned immOpcL,
7791 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007792 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007793 // For the atomic bitwise operator, we generate
7794 // thisMBB (instructions are in pairs, except cmpxchg8b)
7795 // ld t1,t2 = [bitinstr.addr]
7796 // newMBB:
7797 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7798 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007799 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007800 // mov ECX, EBX <- t5, t6
7801 // mov EAX, EDX <- t1, t2
7802 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7803 // mov t3, t4 <- EAX, EDX
7804 // bz newMBB
7805 // result in out1, out2
7806 // fallthrough -->nextMBB
7807
7808 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7809 const unsigned LoadOpc = X86::MOV32rm;
7810 const unsigned copyOpc = X86::MOV32rr;
7811 const unsigned NotOpc = X86::NOT32r;
7812 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7813 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7814 MachineFunction::iterator MBBIter = MBB;
7815 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007816
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007817 /// First build the CFG
7818 MachineFunction *F = MBB->getParent();
7819 MachineBasicBlock *thisMBB = MBB;
7820 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7821 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7822 F->insert(MBBIter, newMBB);
7823 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007824
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007825 // Move all successors to thisMBB to nextMBB
7826 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007827
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007828 // Update thisMBB to fall through to newMBB
7829 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007830
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007831 // newMBB jumps to itself and fall through to nextMBB
7832 newMBB->addSuccessor(nextMBB);
7833 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007834
Dale Johannesene4d209d2009-02-03 20:21:25 +00007835 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007836 // Insert instructions into newMBB based on incoming instruction
7837 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007838 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007839 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007840 MachineOperand& dest1Oper = bInstr->getOperand(0);
7841 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007842 MachineOperand* argOpers[2 + X86AddrNumOperands];
7843 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007844 argOpers[i] = &bInstr->getOperand(i+2);
7845
Evan Chengad5b52f2010-01-08 19:14:57 +00007846 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007847 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007848
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007849 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007850 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007851 for (int i=0; i <= lastAddrIndx; ++i)
7852 (*MIB).addOperand(*argOpers[i]);
7853 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007854 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007855 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007856 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007857 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007858 MachineOperand newOp3 = *(argOpers[3]);
7859 if (newOp3.isImm())
7860 newOp3.setImm(newOp3.getImm()+4);
7861 else
7862 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007863 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007864 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007865
7866 // t3/4 are defined later, at the bottom of the loop
7867 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7868 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007869 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007870 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007871 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007872 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7873
Evan Cheng306b4ca2010-01-08 23:41:50 +00007874 // The subsequent operations should be using the destination registers of
7875 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007876 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007877 t1 = F->getRegInfo().createVirtualRegister(RC);
7878 t2 = F->getRegInfo().createVirtualRegister(RC);
7879 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7880 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007881 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007882 t1 = dest1Oper.getReg();
7883 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007884 }
7885
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007886 int valArgIndx = lastAddrIndx + 1;
7887 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007888 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007889 "invalid operand");
7890 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7891 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007892 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007893 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007894 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007895 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007896 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007897 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007898 (*MIB).addOperand(*argOpers[valArgIndx]);
7899 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007900 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007901 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007902 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007903 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007904 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007905 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007906 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007907 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007908 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007909 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007910
Dale Johannesene4d209d2009-02-03 20:21:25 +00007911 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007912 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007913 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007914 MIB.addReg(t2);
7915
Dale Johannesene4d209d2009-02-03 20:21:25 +00007916 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007917 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007918 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007919 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007920
Dale Johannesene4d209d2009-02-03 20:21:25 +00007921 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007922 for (int i=0; i <= lastAddrIndx; ++i)
7923 (*MIB).addOperand(*argOpers[i]);
7924
7925 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007926 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7927 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007928
Dale Johannesene4d209d2009-02-03 20:21:25 +00007929 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007930 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007931 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007932 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007933
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007934 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007935 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007936
7937 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7938 return nextMBB;
7939}
7940
7941// private utility function
7942MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007943X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7944 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007945 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007946 // For the atomic min/max operator, we generate
7947 // thisMBB:
7948 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007949 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007950 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007951 // cmp t1, t2
7952 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007953 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007954 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7955 // bz newMBB
7956 // fallthrough -->nextMBB
7957 //
7958 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7959 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007960 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007961 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007962
Mon P Wang63307c32008-05-05 19:05:59 +00007963 /// First build the CFG
7964 MachineFunction *F = MBB->getParent();
7965 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007966 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7967 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7968 F->insert(MBBIter, newMBB);
7969 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007970
Dan Gohmand6708ea2009-08-15 01:38:56 +00007971 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00007972 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007973
Mon P Wang63307c32008-05-05 19:05:59 +00007974 // Update thisMBB to fall through to newMBB
7975 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007976
Mon P Wang63307c32008-05-05 19:05:59 +00007977 // newMBB jumps to newMBB and fall through to nextMBB
7978 newMBB->addSuccessor(nextMBB);
7979 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007980
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007982 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007983 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007984 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007985 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007986 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007987 int numArgs = mInstr->getNumOperands() - 1;
7988 for (int i=0; i < numArgs; ++i)
7989 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007990
Mon P Wang63307c32008-05-05 19:05:59 +00007991 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007992 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7993 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007994
Mon P Wangab3e7472008-05-05 22:56:23 +00007995 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007997 for (int i=0; i <= lastAddrIndx; ++i)
7998 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007999
Mon P Wang63307c32008-05-05 19:05:59 +00008000 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008001 assert((argOpers[valArgIndx]->isReg() ||
8002 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008003 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008004
8005 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008006 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008007 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008008 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008009 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008010 (*MIB).addOperand(*argOpers[valArgIndx]);
8011
Dale Johannesene4d209d2009-02-03 20:21:25 +00008012 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008013 MIB.addReg(t1);
8014
Dale Johannesene4d209d2009-02-03 20:21:25 +00008015 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008016 MIB.addReg(t1);
8017 MIB.addReg(t2);
8018
8019 // Generate movc
8020 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008021 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008022 MIB.addReg(t2);
8023 MIB.addReg(t1);
8024
8025 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008026 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008027 for (int i=0; i <= lastAddrIndx; ++i)
8028 (*MIB).addOperand(*argOpers[i]);
8029 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008030 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008031 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8032 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008033
Dale Johannesene4d209d2009-02-03 20:21:25 +00008034 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008035 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008036
Mon P Wang63307c32008-05-05 19:05:59 +00008037 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008038 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008039
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008040 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008041 return nextMBB;
8042}
8043
Eric Christopherf83a5de2009-08-27 18:08:16 +00008044// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8045// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008046MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008047X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008048 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008049
8050 MachineFunction *F = BB->getParent();
8051 DebugLoc dl = MI->getDebugLoc();
8052 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8053
8054 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008055 if (memArg)
8056 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8057 else
8058 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008059
8060 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8061
8062 for (unsigned i = 0; i < numArgs; ++i) {
8063 MachineOperand &Op = MI->getOperand(i+1);
8064
8065 if (!(Op.isReg() && Op.isImplicit()))
8066 MIB.addOperand(Op);
8067 }
8068
8069 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8070 .addReg(X86::XMM0);
8071
8072 F->DeleteMachineInstr(MI);
8073
8074 return BB;
8075}
8076
8077MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008078X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8079 MachineInstr *MI,
8080 MachineBasicBlock *MBB) const {
8081 // Emit code to save XMM registers to the stack. The ABI says that the
8082 // number of registers to save is given in %al, so it's theoretically
8083 // possible to do an indirect jump trick to avoid saving all of them,
8084 // however this code takes a simpler approach and just executes all
8085 // of the stores if %al is non-zero. It's less code, and it's probably
8086 // easier on the hardware branch predictor, and stores aren't all that
8087 // expensive anyway.
8088
8089 // Create the new basic blocks. One block contains all the XMM stores,
8090 // and one block is the final destination regardless of whether any
8091 // stores were performed.
8092 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8093 MachineFunction *F = MBB->getParent();
8094 MachineFunction::iterator MBBIter = MBB;
8095 ++MBBIter;
8096 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8097 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8098 F->insert(MBBIter, XMMSaveMBB);
8099 F->insert(MBBIter, EndMBB);
8100
8101 // Set up the CFG.
8102 // Move any original successors of MBB to the end block.
8103 EndMBB->transferSuccessors(MBB);
8104 // The original block will now fall through to the XMM save block.
8105 MBB->addSuccessor(XMMSaveMBB);
8106 // The XMMSaveMBB will fall through to the end block.
8107 XMMSaveMBB->addSuccessor(EndMBB);
8108
8109 // Now add the instructions.
8110 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8111 DebugLoc DL = MI->getDebugLoc();
8112
8113 unsigned CountReg = MI->getOperand(0).getReg();
8114 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8115 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8116
8117 if (!Subtarget->isTargetWin64()) {
8118 // If %al is 0, branch around the XMM save block.
8119 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8120 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8121 MBB->addSuccessor(EndMBB);
8122 }
8123
8124 // In the XMM save block, save all the XMM argument registers.
8125 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8126 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008127 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008128 F->getMachineMemOperand(
8129 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8130 MachineMemOperand::MOStore, Offset,
8131 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008132 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8133 .addFrameIndex(RegSaveFrameIndex)
8134 .addImm(/*Scale=*/1)
8135 .addReg(/*IndexReg=*/0)
8136 .addImm(/*Disp=*/Offset)
8137 .addReg(/*Segment=*/0)
8138 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008139 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008140 }
8141
8142 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8143
8144 return EndMBB;
8145}
Mon P Wang63307c32008-05-05 19:05:59 +00008146
Evan Cheng60c07e12006-07-05 22:17:51 +00008147MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008148X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008149 MachineBasicBlock *BB,
8150 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008151 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8152 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008153
Chris Lattner52600972009-09-02 05:57:00 +00008154 // To "insert" a SELECT_CC instruction, we actually have to insert the
8155 // diamond control-flow pattern. The incoming instruction knows the
8156 // destination vreg to set, the condition code register to branch on, the
8157 // true/false values to select between, and a branch opcode to use.
8158 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8159 MachineFunction::iterator It = BB;
8160 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008161
Chris Lattner52600972009-09-02 05:57:00 +00008162 // thisMBB:
8163 // ...
8164 // TrueVal = ...
8165 // cmpTY ccX, r1, r2
8166 // bCC copy1MBB
8167 // fallthrough --> copy0MBB
8168 MachineBasicBlock *thisMBB = BB;
8169 MachineFunction *F = BB->getParent();
8170 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8171 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8172 unsigned Opc =
8173 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8174 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8175 F->insert(It, copy0MBB);
8176 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008177 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008178 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008179 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008180 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008181 E = BB->succ_end(); I != E; ++I) {
8182 EM->insert(std::make_pair(*I, sinkMBB));
8183 sinkMBB->addSuccessor(*I);
8184 }
8185 // Next, remove all successors of the current block, and add the true
8186 // and fallthrough blocks as its successors.
8187 while (!BB->succ_empty())
8188 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008189 // Add the true and fallthrough blocks as its successors.
8190 BB->addSuccessor(copy0MBB);
8191 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008192
Chris Lattner52600972009-09-02 05:57:00 +00008193 // copy0MBB:
8194 // %FalseValue = ...
8195 // # fallthrough to sinkMBB
8196 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008197
Chris Lattner52600972009-09-02 05:57:00 +00008198 // Update machine-CFG edges
8199 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008200
Chris Lattner52600972009-09-02 05:57:00 +00008201 // sinkMBB:
8202 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8203 // ...
8204 BB = sinkMBB;
8205 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8206 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8207 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8208
8209 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8210 return BB;
8211}
8212
8213
8214MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008215X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008216 MachineBasicBlock *BB,
8217 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008218 switch (MI->getOpcode()) {
8219 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008220 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008221 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008222 case X86::CMOV_FR32:
8223 case X86::CMOV_FR64:
8224 case X86::CMOV_V4F32:
8225 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008226 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008227 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008228
Dale Johannesen849f2142007-07-03 00:53:03 +00008229 case X86::FP32_TO_INT16_IN_MEM:
8230 case X86::FP32_TO_INT32_IN_MEM:
8231 case X86::FP32_TO_INT64_IN_MEM:
8232 case X86::FP64_TO_INT16_IN_MEM:
8233 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008234 case X86::FP64_TO_INT64_IN_MEM:
8235 case X86::FP80_TO_INT16_IN_MEM:
8236 case X86::FP80_TO_INT32_IN_MEM:
8237 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008238 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8239 DebugLoc DL = MI->getDebugLoc();
8240
Evan Cheng60c07e12006-07-05 22:17:51 +00008241 // Change the floating point control register to use "round towards zero"
8242 // mode when truncating to an integer value.
8243 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008244 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008245 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008246
8247 // Load the old value of the high byte of the control word...
8248 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008249 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008250 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008251 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008252
8253 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008254 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008255 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008256
8257 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008258 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008259
8260 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008261 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008262 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008263
8264 // Get the X86 opcode to use.
8265 unsigned Opc;
8266 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008267 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008268 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8269 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8270 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8271 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8272 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8273 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008274 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8275 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8276 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008277 }
8278
8279 X86AddressMode AM;
8280 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008281 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008282 AM.BaseType = X86AddressMode::RegBase;
8283 AM.Base.Reg = Op.getReg();
8284 } else {
8285 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008286 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008287 }
8288 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008289 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008290 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008291 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008292 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008293 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008294 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008295 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008296 AM.GV = Op.getGlobal();
8297 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008298 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008299 }
Chris Lattner52600972009-09-02 05:57:00 +00008300 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008301 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008302
8303 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008304 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008305
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008306 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008307 return BB;
8308 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008309 // String/text processing lowering.
8310 case X86::PCMPISTRM128REG:
8311 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8312 case X86::PCMPISTRM128MEM:
8313 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8314 case X86::PCMPESTRM128REG:
8315 return EmitPCMP(MI, BB, 5, false /* in mem */);
8316 case X86::PCMPESTRM128MEM:
8317 return EmitPCMP(MI, BB, 5, true /* in mem */);
8318
8319 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008320 case X86::ATOMAND32:
8321 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008322 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008323 X86::LCMPXCHG32, X86::MOV32rr,
8324 X86::NOT32r, X86::EAX,
8325 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008326 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008327 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8328 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008329 X86::LCMPXCHG32, X86::MOV32rr,
8330 X86::NOT32r, X86::EAX,
8331 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008332 case X86::ATOMXOR32:
8333 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008334 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008335 X86::LCMPXCHG32, X86::MOV32rr,
8336 X86::NOT32r, X86::EAX,
8337 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008338 case X86::ATOMNAND32:
8339 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008340 X86::AND32ri, X86::MOV32rm,
8341 X86::LCMPXCHG32, X86::MOV32rr,
8342 X86::NOT32r, X86::EAX,
8343 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008344 case X86::ATOMMIN32:
8345 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8346 case X86::ATOMMAX32:
8347 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8348 case X86::ATOMUMIN32:
8349 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8350 case X86::ATOMUMAX32:
8351 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008352
8353 case X86::ATOMAND16:
8354 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8355 X86::AND16ri, X86::MOV16rm,
8356 X86::LCMPXCHG16, X86::MOV16rr,
8357 X86::NOT16r, X86::AX,
8358 X86::GR16RegisterClass);
8359 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008360 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008361 X86::OR16ri, X86::MOV16rm,
8362 X86::LCMPXCHG16, X86::MOV16rr,
8363 X86::NOT16r, X86::AX,
8364 X86::GR16RegisterClass);
8365 case X86::ATOMXOR16:
8366 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8367 X86::XOR16ri, X86::MOV16rm,
8368 X86::LCMPXCHG16, X86::MOV16rr,
8369 X86::NOT16r, X86::AX,
8370 X86::GR16RegisterClass);
8371 case X86::ATOMNAND16:
8372 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8373 X86::AND16ri, X86::MOV16rm,
8374 X86::LCMPXCHG16, X86::MOV16rr,
8375 X86::NOT16r, X86::AX,
8376 X86::GR16RegisterClass, true);
8377 case X86::ATOMMIN16:
8378 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8379 case X86::ATOMMAX16:
8380 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8381 case X86::ATOMUMIN16:
8382 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8383 case X86::ATOMUMAX16:
8384 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8385
8386 case X86::ATOMAND8:
8387 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8388 X86::AND8ri, X86::MOV8rm,
8389 X86::LCMPXCHG8, X86::MOV8rr,
8390 X86::NOT8r, X86::AL,
8391 X86::GR8RegisterClass);
8392 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008393 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008394 X86::OR8ri, X86::MOV8rm,
8395 X86::LCMPXCHG8, X86::MOV8rr,
8396 X86::NOT8r, X86::AL,
8397 X86::GR8RegisterClass);
8398 case X86::ATOMXOR8:
8399 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8400 X86::XOR8ri, X86::MOV8rm,
8401 X86::LCMPXCHG8, X86::MOV8rr,
8402 X86::NOT8r, X86::AL,
8403 X86::GR8RegisterClass);
8404 case X86::ATOMNAND8:
8405 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8406 X86::AND8ri, X86::MOV8rm,
8407 X86::LCMPXCHG8, X86::MOV8rr,
8408 X86::NOT8r, X86::AL,
8409 X86::GR8RegisterClass, true);
8410 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008411 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008412 case X86::ATOMAND64:
8413 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008414 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008415 X86::LCMPXCHG64, X86::MOV64rr,
8416 X86::NOT64r, X86::RAX,
8417 X86::GR64RegisterClass);
8418 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8420 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008421 X86::LCMPXCHG64, X86::MOV64rr,
8422 X86::NOT64r, X86::RAX,
8423 X86::GR64RegisterClass);
8424 case X86::ATOMXOR64:
8425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008426 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008427 X86::LCMPXCHG64, X86::MOV64rr,
8428 X86::NOT64r, X86::RAX,
8429 X86::GR64RegisterClass);
8430 case X86::ATOMNAND64:
8431 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8432 X86::AND64ri32, X86::MOV64rm,
8433 X86::LCMPXCHG64, X86::MOV64rr,
8434 X86::NOT64r, X86::RAX,
8435 X86::GR64RegisterClass, true);
8436 case X86::ATOMMIN64:
8437 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8438 case X86::ATOMMAX64:
8439 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8440 case X86::ATOMUMIN64:
8441 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8442 case X86::ATOMUMAX64:
8443 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008444
8445 // This group does 64-bit operations on a 32-bit host.
8446 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008447 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008448 X86::AND32rr, X86::AND32rr,
8449 X86::AND32ri, X86::AND32ri,
8450 false);
8451 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008452 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008453 X86::OR32rr, X86::OR32rr,
8454 X86::OR32ri, X86::OR32ri,
8455 false);
8456 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008457 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008458 X86::XOR32rr, X86::XOR32rr,
8459 X86::XOR32ri, X86::XOR32ri,
8460 false);
8461 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008462 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008463 X86::AND32rr, X86::AND32rr,
8464 X86::AND32ri, X86::AND32ri,
8465 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008466 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008467 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008468 X86::ADD32rr, X86::ADC32rr,
8469 X86::ADD32ri, X86::ADC32ri,
8470 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008471 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008472 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008473 X86::SUB32rr, X86::SBB32rr,
8474 X86::SUB32ri, X86::SBB32ri,
8475 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008476 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008477 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008478 X86::MOV32rr, X86::MOV32rr,
8479 X86::MOV32ri, X86::MOV32ri,
8480 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008481 case X86::VASTART_SAVE_XMM_REGS:
8482 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008483 }
8484}
8485
8486//===----------------------------------------------------------------------===//
8487// X86 Optimization Hooks
8488//===----------------------------------------------------------------------===//
8489
Dan Gohman475871a2008-07-27 21:46:04 +00008490void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008491 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008492 APInt &KnownZero,
8493 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008494 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008495 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008496 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008497 assert((Opc >= ISD::BUILTIN_OP_END ||
8498 Opc == ISD::INTRINSIC_WO_CHAIN ||
8499 Opc == ISD::INTRINSIC_W_CHAIN ||
8500 Opc == ISD::INTRINSIC_VOID) &&
8501 "Should use MaskedValueIsZero if you don't know whether Op"
8502 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008503
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008504 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008505 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008506 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008507 case X86ISD::ADD:
8508 case X86ISD::SUB:
8509 case X86ISD::SMUL:
8510 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008511 case X86ISD::INC:
8512 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008513 case X86ISD::OR:
8514 case X86ISD::XOR:
8515 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008516 // These nodes' second result is a boolean.
8517 if (Op.getResNo() == 0)
8518 break;
8519 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008520 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008521 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8522 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008523 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008524 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008525}
Chris Lattner259e97c2006-01-31 19:43:35 +00008526
Evan Cheng206ee9d2006-07-07 08:33:52 +00008527/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008528/// node is a GlobalAddress + offset.
8529bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8530 GlobalValue* &GA, int64_t &Offset) const{
8531 if (N->getOpcode() == X86ISD::Wrapper) {
8532 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008533 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008534 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008535 return true;
8536 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008537 }
Evan Chengad4196b2008-05-12 19:56:52 +00008538 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008539}
8540
Nate Begeman9008ca62009-04-27 18:41:29 +00008541static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008542 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008543 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008544 SelectionDAG &DAG, MachineFrameInfo *MFI,
8545 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008546 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008547 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008548 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008549 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008550 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008551 return false;
8552 continue;
8553 }
8554
Dan Gohman475871a2008-07-27 21:46:04 +00008555 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008556 if (!Elt.getNode() ||
8557 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008558 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008559 if (!LDBase) {
8560 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008561 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008562 LDBase = cast<LoadSDNode>(Elt.getNode());
8563 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008564 continue;
8565 }
8566 if (Elt.getOpcode() == ISD::UNDEF)
8567 continue;
8568
Nate Begemanabc01992009-06-05 21:37:30 +00008569 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008570 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008571 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008572 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008573 }
8574 return true;
8575}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008576
8577/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8578/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8579/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008580/// order. In the case of v2i64, it will see if it can rewrite the
8581/// shuffle to be an appropriate build vector so it can take advantage of
8582// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008583static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008584 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008585 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008586 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008587 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008588 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8589 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008590
Eli Friedman7a5e5552009-06-07 06:52:44 +00008591 if (VT.getSizeInBits() != 128)
8592 return SDValue();
8593
Mon P Wang1e955802009-04-03 02:43:30 +00008594 // Try to combine a vector_shuffle into a 128-bit load.
8595 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008596 LoadSDNode *LD = NULL;
8597 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008598 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008599 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008600 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008601
Eli Friedman7a5e5552009-06-07 06:52:44 +00008602 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008603 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008604 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8605 LD->getSrcValue(), LD->getSrcValueOffset(),
8606 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008607 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008608 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008609 LD->isVolatile(), LD->getAlignment());
8610 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008611 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008612 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8613 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008614 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8615 }
8616 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008617}
Evan Chengd880b972008-05-09 21:53:03 +00008618
Chris Lattner83e6c992006-10-04 06:57:07 +00008619/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008620static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008621 const X86Subtarget *Subtarget) {
8622 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008623 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008624 // Get the LHS/RHS of the select.
8625 SDValue LHS = N->getOperand(1);
8626 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008627
Dan Gohman670e5392009-09-21 18:03:22 +00008628 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8629 // instructions have the peculiarity that if either operand is a NaN,
8630 // they chose what we call the RHS operand (and as such are not symmetric).
8631 // It happens that this matches the semantics of the common C idiom
8632 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008633 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008634 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008635 Cond.getOpcode() == ISD::SETCC) {
8636 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008637
Chris Lattner47b4ce82009-03-11 05:48:52 +00008638 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008639 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008640 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8641 switch (CC) {
8642 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008643 case ISD::SETULT:
8644 // This can be a min if we can prove that at least one of the operands
8645 // is not a nan.
8646 if (!FiniteOnlyFPMath()) {
8647 if (DAG.isKnownNeverNaN(RHS)) {
8648 // Put the potential NaN in the RHS so that SSE will preserve it.
8649 std::swap(LHS, RHS);
8650 } else if (!DAG.isKnownNeverNaN(LHS))
8651 break;
8652 }
8653 Opcode = X86ISD::FMIN;
8654 break;
8655 case ISD::SETOLE:
8656 // This can be a min if we can prove that at least one of the operands
8657 // is not a nan.
8658 if (!FiniteOnlyFPMath()) {
8659 if (DAG.isKnownNeverNaN(LHS)) {
8660 // Put the potential NaN in the RHS so that SSE will preserve it.
8661 std::swap(LHS, RHS);
8662 } else if (!DAG.isKnownNeverNaN(RHS))
8663 break;
8664 }
8665 Opcode = X86ISD::FMIN;
8666 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008667 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008668 // This can be a min, but if either operand is a NaN we need it to
8669 // preserve the original LHS.
8670 std::swap(LHS, RHS);
8671 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008672 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008673 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008674 Opcode = X86ISD::FMIN;
8675 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008676
Dan Gohman670e5392009-09-21 18:03:22 +00008677 case ISD::SETOGE:
8678 // This can be a max if we can prove that at least one of the operands
8679 // is not a nan.
8680 if (!FiniteOnlyFPMath()) {
8681 if (DAG.isKnownNeverNaN(LHS)) {
8682 // Put the potential NaN in the RHS so that SSE will preserve it.
8683 std::swap(LHS, RHS);
8684 } else if (!DAG.isKnownNeverNaN(RHS))
8685 break;
8686 }
8687 Opcode = X86ISD::FMAX;
8688 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008689 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008690 // This can be a max if we can prove that at least one of the operands
8691 // is not a nan.
8692 if (!FiniteOnlyFPMath()) {
8693 if (DAG.isKnownNeverNaN(RHS)) {
8694 // Put the potential NaN in the RHS so that SSE will preserve it.
8695 std::swap(LHS, RHS);
8696 } else if (!DAG.isKnownNeverNaN(LHS))
8697 break;
8698 }
8699 Opcode = X86ISD::FMAX;
8700 break;
8701 case ISD::SETUGE:
8702 // This can be a max, but if either operand is a NaN we need it to
8703 // preserve the original LHS.
8704 std::swap(LHS, RHS);
8705 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008706 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008707 case ISD::SETGE:
8708 Opcode = X86ISD::FMAX;
8709 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008710 }
Dan Gohman670e5392009-09-21 18:03:22 +00008711 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008712 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8713 switch (CC) {
8714 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008715 case ISD::SETOGE:
8716 // This can be a min if we can prove that at least one of the operands
8717 // is not a nan.
8718 if (!FiniteOnlyFPMath()) {
8719 if (DAG.isKnownNeverNaN(RHS)) {
8720 // Put the potential NaN in the RHS so that SSE will preserve it.
8721 std::swap(LHS, RHS);
8722 } else if (!DAG.isKnownNeverNaN(LHS))
8723 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008724 }
Dan Gohman670e5392009-09-21 18:03:22 +00008725 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008726 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008727 case ISD::SETUGT:
8728 // This can be a min if we can prove that at least one of the operands
8729 // is not a nan.
8730 if (!FiniteOnlyFPMath()) {
8731 if (DAG.isKnownNeverNaN(LHS)) {
8732 // Put the potential NaN in the RHS so that SSE will preserve it.
8733 std::swap(LHS, RHS);
8734 } else if (!DAG.isKnownNeverNaN(RHS))
8735 break;
8736 }
8737 Opcode = X86ISD::FMIN;
8738 break;
8739 case ISD::SETUGE:
8740 // This can be a min, but if either operand is a NaN we need it to
8741 // preserve the original LHS.
8742 std::swap(LHS, RHS);
8743 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008744 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008745 case ISD::SETGE:
8746 Opcode = X86ISD::FMIN;
8747 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008748
Dan Gohman670e5392009-09-21 18:03:22 +00008749 case ISD::SETULT:
8750 // This can be a max if we can prove that at least one of the operands
8751 // is not a nan.
8752 if (!FiniteOnlyFPMath()) {
8753 if (DAG.isKnownNeverNaN(LHS)) {
8754 // Put the potential NaN in the RHS so that SSE will preserve it.
8755 std::swap(LHS, RHS);
8756 } else if (!DAG.isKnownNeverNaN(RHS))
8757 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008758 }
Dan Gohman670e5392009-09-21 18:03:22 +00008759 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008760 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008761 case ISD::SETOLE:
8762 // This can be a max if we can prove that at least one of the operands
8763 // is not a nan.
8764 if (!FiniteOnlyFPMath()) {
8765 if (DAG.isKnownNeverNaN(RHS)) {
8766 // Put the potential NaN in the RHS so that SSE will preserve it.
8767 std::swap(LHS, RHS);
8768 } else if (!DAG.isKnownNeverNaN(LHS))
8769 break;
8770 }
8771 Opcode = X86ISD::FMAX;
8772 break;
8773 case ISD::SETULE:
8774 // This can be a max, but if either operand is a NaN we need it to
8775 // preserve the original LHS.
8776 std::swap(LHS, RHS);
8777 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008778 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008779 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008780 Opcode = X86ISD::FMAX;
8781 break;
8782 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008783 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008784
Chris Lattner47b4ce82009-03-11 05:48:52 +00008785 if (Opcode)
8786 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008787 }
Eric Christopherfd179292009-08-27 18:07:15 +00008788
Chris Lattnerd1980a52009-03-12 06:52:53 +00008789 // If this is a select between two integer constants, try to do some
8790 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008791 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8792 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008793 // Don't do this for crazy integer types.
8794 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8795 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008796 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008797 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008798
Chris Lattnercee56e72009-03-13 05:53:31 +00008799 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008800 // Efficiently invertible.
8801 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8802 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8803 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8804 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008805 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008806 }
Eric Christopherfd179292009-08-27 18:07:15 +00008807
Chris Lattnerd1980a52009-03-12 06:52:53 +00008808 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008809 if (FalseC->getAPIntValue() == 0 &&
8810 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008811 if (NeedsCondInvert) // Invert the condition if needed.
8812 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8813 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008814
Chris Lattnerd1980a52009-03-12 06:52:53 +00008815 // Zero extend the condition if needed.
8816 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008817
Chris Lattnercee56e72009-03-13 05:53:31 +00008818 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008819 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008820 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008821 }
Eric Christopherfd179292009-08-27 18:07:15 +00008822
Chris Lattner97a29a52009-03-13 05:22:11 +00008823 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008824 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008825 if (NeedsCondInvert) // Invert the condition if needed.
8826 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8827 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008828
Chris Lattner97a29a52009-03-13 05:22:11 +00008829 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008830 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8831 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008832 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008833 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008834 }
Eric Christopherfd179292009-08-27 18:07:15 +00008835
Chris Lattnercee56e72009-03-13 05:53:31 +00008836 // Optimize cases that will turn into an LEA instruction. This requires
8837 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008838 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008839 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008840 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008841
Chris Lattnercee56e72009-03-13 05:53:31 +00008842 bool isFastMultiplier = false;
8843 if (Diff < 10) {
8844 switch ((unsigned char)Diff) {
8845 default: break;
8846 case 1: // result = add base, cond
8847 case 2: // result = lea base( , cond*2)
8848 case 3: // result = lea base(cond, cond*2)
8849 case 4: // result = lea base( , cond*4)
8850 case 5: // result = lea base(cond, cond*4)
8851 case 8: // result = lea base( , cond*8)
8852 case 9: // result = lea base(cond, cond*8)
8853 isFastMultiplier = true;
8854 break;
8855 }
8856 }
Eric Christopherfd179292009-08-27 18:07:15 +00008857
Chris Lattnercee56e72009-03-13 05:53:31 +00008858 if (isFastMultiplier) {
8859 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8860 if (NeedsCondInvert) // Invert the condition if needed.
8861 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8862 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008863
Chris Lattnercee56e72009-03-13 05:53:31 +00008864 // Zero extend the condition if needed.
8865 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8866 Cond);
8867 // Scale the condition by the difference.
8868 if (Diff != 1)
8869 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8870 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008871
Chris Lattnercee56e72009-03-13 05:53:31 +00008872 // Add the base if non-zero.
8873 if (FalseC->getAPIntValue() != 0)
8874 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8875 SDValue(FalseC, 0));
8876 return Cond;
8877 }
Eric Christopherfd179292009-08-27 18:07:15 +00008878 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008879 }
8880 }
Eric Christopherfd179292009-08-27 18:07:15 +00008881
Dan Gohman475871a2008-07-27 21:46:04 +00008882 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008883}
8884
Chris Lattnerd1980a52009-03-12 06:52:53 +00008885/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8886static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8887 TargetLowering::DAGCombinerInfo &DCI) {
8888 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008889
Chris Lattnerd1980a52009-03-12 06:52:53 +00008890 // If the flag operand isn't dead, don't touch this CMOV.
8891 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8892 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008893
Chris Lattnerd1980a52009-03-12 06:52:53 +00008894 // If this is a select between two integer constants, try to do some
8895 // optimizations. Note that the operands are ordered the opposite of SELECT
8896 // operands.
8897 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8898 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8899 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8900 // larger than FalseC (the false value).
8901 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008902
Chris Lattnerd1980a52009-03-12 06:52:53 +00008903 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8904 CC = X86::GetOppositeBranchCondition(CC);
8905 std::swap(TrueC, FalseC);
8906 }
Eric Christopherfd179292009-08-27 18:07:15 +00008907
Chris Lattnerd1980a52009-03-12 06:52:53 +00008908 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008909 // This is efficient for any integer data type (including i8/i16) and
8910 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008911 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8912 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008913 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8914 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008915
Chris Lattnerd1980a52009-03-12 06:52:53 +00008916 // Zero extend the condition if needed.
8917 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008918
Chris Lattnerd1980a52009-03-12 06:52:53 +00008919 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8920 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008921 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008922 if (N->getNumValues() == 2) // Dead flag value?
8923 return DCI.CombineTo(N, Cond, SDValue());
8924 return Cond;
8925 }
Eric Christopherfd179292009-08-27 18:07:15 +00008926
Chris Lattnercee56e72009-03-13 05:53:31 +00008927 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8928 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008929 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8930 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008931 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8932 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008933
Chris Lattner97a29a52009-03-13 05:22:11 +00008934 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008935 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8936 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008937 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8938 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008939
Chris Lattner97a29a52009-03-13 05:22:11 +00008940 if (N->getNumValues() == 2) // Dead flag value?
8941 return DCI.CombineTo(N, Cond, SDValue());
8942 return Cond;
8943 }
Eric Christopherfd179292009-08-27 18:07:15 +00008944
Chris Lattnercee56e72009-03-13 05:53:31 +00008945 // Optimize cases that will turn into an LEA instruction. This requires
8946 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008947 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008948 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008949 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008950
Chris Lattnercee56e72009-03-13 05:53:31 +00008951 bool isFastMultiplier = false;
8952 if (Diff < 10) {
8953 switch ((unsigned char)Diff) {
8954 default: break;
8955 case 1: // result = add base, cond
8956 case 2: // result = lea base( , cond*2)
8957 case 3: // result = lea base(cond, cond*2)
8958 case 4: // result = lea base( , cond*4)
8959 case 5: // result = lea base(cond, cond*4)
8960 case 8: // result = lea base( , cond*8)
8961 case 9: // result = lea base(cond, cond*8)
8962 isFastMultiplier = true;
8963 break;
8964 }
8965 }
Eric Christopherfd179292009-08-27 18:07:15 +00008966
Chris Lattnercee56e72009-03-13 05:53:31 +00008967 if (isFastMultiplier) {
8968 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8969 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008970 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8971 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00008972 // Zero extend the condition if needed.
8973 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8974 Cond);
8975 // Scale the condition by the difference.
8976 if (Diff != 1)
8977 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8978 DAG.getConstant(Diff, Cond.getValueType()));
8979
8980 // Add the base if non-zero.
8981 if (FalseC->getAPIntValue() != 0)
8982 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8983 SDValue(FalseC, 0));
8984 if (N->getNumValues() == 2) // Dead flag value?
8985 return DCI.CombineTo(N, Cond, SDValue());
8986 return Cond;
8987 }
Eric Christopherfd179292009-08-27 18:07:15 +00008988 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008989 }
8990 }
8991 return SDValue();
8992}
8993
8994
Evan Cheng0b0cd912009-03-28 05:57:29 +00008995/// PerformMulCombine - Optimize a single multiply with constant into two
8996/// in order to implement it with two cheaper instructions, e.g.
8997/// LEA + SHL, LEA + LEA.
8998static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8999 TargetLowering::DAGCombinerInfo &DCI) {
9000 if (DAG.getMachineFunction().
9001 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9002 return SDValue();
9003
9004 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9005 return SDValue();
9006
Owen Andersone50ed302009-08-10 22:56:29 +00009007 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009008 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009009 return SDValue();
9010
9011 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9012 if (!C)
9013 return SDValue();
9014 uint64_t MulAmt = C->getZExtValue();
9015 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9016 return SDValue();
9017
9018 uint64_t MulAmt1 = 0;
9019 uint64_t MulAmt2 = 0;
9020 if ((MulAmt % 9) == 0) {
9021 MulAmt1 = 9;
9022 MulAmt2 = MulAmt / 9;
9023 } else if ((MulAmt % 5) == 0) {
9024 MulAmt1 = 5;
9025 MulAmt2 = MulAmt / 5;
9026 } else if ((MulAmt % 3) == 0) {
9027 MulAmt1 = 3;
9028 MulAmt2 = MulAmt / 3;
9029 }
9030 if (MulAmt2 &&
9031 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9032 DebugLoc DL = N->getDebugLoc();
9033
9034 if (isPowerOf2_64(MulAmt2) &&
9035 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9036 // If second multiplifer is pow2, issue it first. We want the multiply by
9037 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9038 // is an add.
9039 std::swap(MulAmt1, MulAmt2);
9040
9041 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009042 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009043 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009044 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009045 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009046 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009047 DAG.getConstant(MulAmt1, VT));
9048
Eric Christopherfd179292009-08-27 18:07:15 +00009049 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009050 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009051 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009052 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009053 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009054 DAG.getConstant(MulAmt2, VT));
9055
9056 // Do not add new nodes to DAG combiner worklist.
9057 DCI.CombineTo(N, NewMul, false);
9058 }
9059 return SDValue();
9060}
9061
Evan Chengad9c0a32009-12-15 00:53:42 +00009062static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9063 SDValue N0 = N->getOperand(0);
9064 SDValue N1 = N->getOperand(1);
9065 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9066 EVT VT = N0.getValueType();
9067
9068 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9069 // since the result of setcc_c is all zero's or all ones.
9070 if (N1C && N0.getOpcode() == ISD::AND &&
9071 N0.getOperand(1).getOpcode() == ISD::Constant) {
9072 SDValue N00 = N0.getOperand(0);
9073 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9074 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9075 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9076 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9077 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9078 APInt ShAmt = N1C->getAPIntValue();
9079 Mask = Mask.shl(ShAmt);
9080 if (Mask != 0)
9081 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9082 N00, DAG.getConstant(Mask, VT));
9083 }
9084 }
9085
9086 return SDValue();
9087}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009088
Nate Begeman740ab032009-01-26 00:52:55 +00009089/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9090/// when possible.
9091static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9092 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009093 EVT VT = N->getValueType(0);
9094 if (!VT.isVector() && VT.isInteger() &&
9095 N->getOpcode() == ISD::SHL)
9096 return PerformSHLCombine(N, DAG);
9097
Nate Begeman740ab032009-01-26 00:52:55 +00009098 // On X86 with SSE2 support, we can transform this to a vector shift if
9099 // all elements are shifted by the same amount. We can't do this in legalize
9100 // because the a constant vector is typically transformed to a constant pool
9101 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009102 if (!Subtarget->hasSSE2())
9103 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009104
Owen Anderson825b72b2009-08-11 20:47:22 +00009105 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009106 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009107
Mon P Wang3becd092009-01-28 08:12:05 +00009108 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009109 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009110 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009111 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009112 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9113 unsigned NumElts = VT.getVectorNumElements();
9114 unsigned i = 0;
9115 for (; i != NumElts; ++i) {
9116 SDValue Arg = ShAmtOp.getOperand(i);
9117 if (Arg.getOpcode() == ISD::UNDEF) continue;
9118 BaseShAmt = Arg;
9119 break;
9120 }
9121 for (; i != NumElts; ++i) {
9122 SDValue Arg = ShAmtOp.getOperand(i);
9123 if (Arg.getOpcode() == ISD::UNDEF) continue;
9124 if (Arg != BaseShAmt) {
9125 return SDValue();
9126 }
9127 }
9128 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009129 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009130 SDValue InVec = ShAmtOp.getOperand(0);
9131 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9132 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9133 unsigned i = 0;
9134 for (; i != NumElts; ++i) {
9135 SDValue Arg = InVec.getOperand(i);
9136 if (Arg.getOpcode() == ISD::UNDEF) continue;
9137 BaseShAmt = Arg;
9138 break;
9139 }
9140 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9141 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9142 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9143 if (C->getZExtValue() == SplatIdx)
9144 BaseShAmt = InVec.getOperand(1);
9145 }
9146 }
9147 if (BaseShAmt.getNode() == 0)
9148 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9149 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009150 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009151 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009152
Mon P Wangefa42202009-09-03 19:56:25 +00009153 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009154 if (EltVT.bitsGT(MVT::i32))
9155 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9156 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009157 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009158
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009159 // The shift amount is identical so we can do a vector shift.
9160 SDValue ValOp = N->getOperand(0);
9161 switch (N->getOpcode()) {
9162 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009163 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009164 break;
9165 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009166 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009167 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009168 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009169 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009171 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009172 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009173 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009174 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009175 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009176 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009177 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009178 break;
9179 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009180 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009181 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009182 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009183 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009184 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009185 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009186 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009187 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009188 break;
9189 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009191 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009192 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009193 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009194 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009195 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009196 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009197 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009199 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009200 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009201 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009202 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009203 }
9204 return SDValue();
9205}
9206
Evan Cheng760d1942010-01-04 21:22:48 +00009207static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9208 const X86Subtarget *Subtarget) {
9209 EVT VT = N->getValueType(0);
9210 if (VT != MVT::i64 || !Subtarget->is64Bit())
9211 return SDValue();
9212
9213 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9214 SDValue N0 = N->getOperand(0);
9215 SDValue N1 = N->getOperand(1);
9216 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9217 std::swap(N0, N1);
9218 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9219 return SDValue();
9220
9221 SDValue ShAmt0 = N0.getOperand(1);
9222 if (ShAmt0.getValueType() != MVT::i8)
9223 return SDValue();
9224 SDValue ShAmt1 = N1.getOperand(1);
9225 if (ShAmt1.getValueType() != MVT::i8)
9226 return SDValue();
9227 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9228 ShAmt0 = ShAmt0.getOperand(0);
9229 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9230 ShAmt1 = ShAmt1.getOperand(0);
9231
9232 DebugLoc DL = N->getDebugLoc();
9233 unsigned Opc = X86ISD::SHLD;
9234 SDValue Op0 = N0.getOperand(0);
9235 SDValue Op1 = N1.getOperand(0);
9236 if (ShAmt0.getOpcode() == ISD::SUB) {
9237 Opc = X86ISD::SHRD;
9238 std::swap(Op0, Op1);
9239 std::swap(ShAmt0, ShAmt1);
9240 }
9241
9242 if (ShAmt1.getOpcode() == ISD::SUB) {
9243 SDValue Sum = ShAmt1.getOperand(0);
9244 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9245 if (SumC->getSExtValue() == 64 &&
9246 ShAmt1.getOperand(1) == ShAmt0)
9247 return DAG.getNode(Opc, DL, VT,
9248 Op0, Op1,
9249 DAG.getNode(ISD::TRUNCATE, DL,
9250 MVT::i8, ShAmt0));
9251 }
9252 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9253 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9254 if (ShAmt0C &&
9255 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9256 return DAG.getNode(Opc, DL, VT,
9257 N0.getOperand(0), N1.getOperand(0),
9258 DAG.getNode(ISD::TRUNCATE, DL,
9259 MVT::i8, ShAmt0));
9260 }
9261
9262 return SDValue();
9263}
9264
Chris Lattner149a4e52008-02-22 02:09:43 +00009265/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009266static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009267 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009268 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9269 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009270 // A preferable solution to the general problem is to figure out the right
9271 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009272
9273 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009274 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009275 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009276 if (VT.getSizeInBits() != 64)
9277 return SDValue();
9278
Devang Patel578efa92009-06-05 21:57:13 +00009279 const Function *F = DAG.getMachineFunction().getFunction();
9280 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009281 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009282 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009283 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009284 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009285 isa<LoadSDNode>(St->getValue()) &&
9286 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9287 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009288 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009289 LoadSDNode *Ld = 0;
9290 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009291 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009292 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009293 // Must be a store of a load. We currently handle two cases: the load
9294 // is a direct child, and it's under an intervening TokenFactor. It is
9295 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009296 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009297 Ld = cast<LoadSDNode>(St->getChain());
9298 else if (St->getValue().hasOneUse() &&
9299 ChainVal->getOpcode() == ISD::TokenFactor) {
9300 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009301 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009302 TokenFactorIndex = i;
9303 Ld = cast<LoadSDNode>(St->getValue());
9304 } else
9305 Ops.push_back(ChainVal->getOperand(i));
9306 }
9307 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009308
Evan Cheng536e6672009-03-12 05:59:15 +00009309 if (!Ld || !ISD::isNormalLoad(Ld))
9310 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009311
Evan Cheng536e6672009-03-12 05:59:15 +00009312 // If this is not the MMX case, i.e. we are just turning i64 load/store
9313 // into f64 load/store, avoid the transformation if there are multiple
9314 // uses of the loaded value.
9315 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9316 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009317
Evan Cheng536e6672009-03-12 05:59:15 +00009318 DebugLoc LdDL = Ld->getDebugLoc();
9319 DebugLoc StDL = N->getDebugLoc();
9320 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9321 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9322 // pair instead.
9323 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009324 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009325 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9326 Ld->getBasePtr(), Ld->getSrcValue(),
9327 Ld->getSrcValueOffset(), Ld->isVolatile(),
9328 Ld->getAlignment());
9329 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009330 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009331 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009332 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009333 Ops.size());
9334 }
Evan Cheng536e6672009-03-12 05:59:15 +00009335 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009336 St->getSrcValue(), St->getSrcValueOffset(),
9337 St->isVolatile(), St->getAlignment());
9338 }
Evan Cheng536e6672009-03-12 05:59:15 +00009339
9340 // Otherwise, lower to two pairs of 32-bit loads / stores.
9341 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009342 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9343 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009344
Owen Anderson825b72b2009-08-11 20:47:22 +00009345 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009346 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9347 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009348 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009349 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9350 Ld->isVolatile(),
9351 MinAlign(Ld->getAlignment(), 4));
9352
9353 SDValue NewChain = LoLd.getValue(1);
9354 if (TokenFactorIndex != -1) {
9355 Ops.push_back(LoLd);
9356 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009357 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009358 Ops.size());
9359 }
9360
9361 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009362 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9363 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009364
9365 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9366 St->getSrcValue(), St->getSrcValueOffset(),
9367 St->isVolatile(), St->getAlignment());
9368 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9369 St->getSrcValue(),
9370 St->getSrcValueOffset() + 4,
9371 St->isVolatile(),
9372 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009374 }
Dan Gohman475871a2008-07-27 21:46:04 +00009375 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009376}
9377
Chris Lattner6cf73262008-01-25 06:14:17 +00009378/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9379/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009380static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009381 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9382 // F[X]OR(0.0, x) -> x
9383 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009384 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9385 if (C->getValueAPF().isPosZero())
9386 return N->getOperand(1);
9387 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9388 if (C->getValueAPF().isPosZero())
9389 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009390 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009391}
9392
9393/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009394static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009395 // FAND(0.0, x) -> 0.0
9396 // FAND(x, 0.0) -> 0.0
9397 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9398 if (C->getValueAPF().isPosZero())
9399 return N->getOperand(0);
9400 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9401 if (C->getValueAPF().isPosZero())
9402 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009403 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009404}
9405
Dan Gohmane5af2d32009-01-29 01:59:02 +00009406static SDValue PerformBTCombine(SDNode *N,
9407 SelectionDAG &DAG,
9408 TargetLowering::DAGCombinerInfo &DCI) {
9409 // BT ignores high bits in the bit index operand.
9410 SDValue Op1 = N->getOperand(1);
9411 if (Op1.hasOneUse()) {
9412 unsigned BitWidth = Op1.getValueSizeInBits();
9413 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9414 APInt KnownZero, KnownOne;
9415 TargetLowering::TargetLoweringOpt TLO(DAG);
9416 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9417 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9418 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9419 DCI.CommitTargetLoweringOpt(TLO);
9420 }
9421 return SDValue();
9422}
Chris Lattner83e6c992006-10-04 06:57:07 +00009423
Eli Friedman7a5e5552009-06-07 06:52:44 +00009424static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9425 SDValue Op = N->getOperand(0);
9426 if (Op.getOpcode() == ISD::BIT_CONVERT)
9427 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009428 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009429 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009430 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009431 OpVT.getVectorElementType().getSizeInBits()) {
9432 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9433 }
9434 return SDValue();
9435}
9436
Owen Anderson99177002009-06-29 18:04:45 +00009437// On X86 and X86-64, atomic operations are lowered to locked instructions.
9438// Locked instructions, in turn, have implicit fence semantics (all memory
9439// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009440// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009441// fence-atomic-fence.
9442static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9443 SDValue atomic = N->getOperand(0);
9444 switch (atomic.getOpcode()) {
9445 case ISD::ATOMIC_CMP_SWAP:
9446 case ISD::ATOMIC_SWAP:
9447 case ISD::ATOMIC_LOAD_ADD:
9448 case ISD::ATOMIC_LOAD_SUB:
9449 case ISD::ATOMIC_LOAD_AND:
9450 case ISD::ATOMIC_LOAD_OR:
9451 case ISD::ATOMIC_LOAD_XOR:
9452 case ISD::ATOMIC_LOAD_NAND:
9453 case ISD::ATOMIC_LOAD_MIN:
9454 case ISD::ATOMIC_LOAD_MAX:
9455 case ISD::ATOMIC_LOAD_UMIN:
9456 case ISD::ATOMIC_LOAD_UMAX:
9457 break;
9458 default:
9459 return SDValue();
9460 }
Eric Christopherfd179292009-08-27 18:07:15 +00009461
Owen Anderson99177002009-06-29 18:04:45 +00009462 SDValue fence = atomic.getOperand(0);
9463 if (fence.getOpcode() != ISD::MEMBARRIER)
9464 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009465
Owen Anderson99177002009-06-29 18:04:45 +00009466 switch (atomic.getOpcode()) {
9467 case ISD::ATOMIC_CMP_SWAP:
9468 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9469 atomic.getOperand(1), atomic.getOperand(2),
9470 atomic.getOperand(3));
9471 case ISD::ATOMIC_SWAP:
9472 case ISD::ATOMIC_LOAD_ADD:
9473 case ISD::ATOMIC_LOAD_SUB:
9474 case ISD::ATOMIC_LOAD_AND:
9475 case ISD::ATOMIC_LOAD_OR:
9476 case ISD::ATOMIC_LOAD_XOR:
9477 case ISD::ATOMIC_LOAD_NAND:
9478 case ISD::ATOMIC_LOAD_MIN:
9479 case ISD::ATOMIC_LOAD_MAX:
9480 case ISD::ATOMIC_LOAD_UMIN:
9481 case ISD::ATOMIC_LOAD_UMAX:
9482 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9483 atomic.getOperand(1), atomic.getOperand(2));
9484 default:
9485 return SDValue();
9486 }
9487}
9488
Evan Cheng2e489c42009-12-16 00:53:11 +00009489static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9490 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9491 // (and (i32 x86isd::setcc_carry), 1)
9492 // This eliminates the zext. This transformation is necessary because
9493 // ISD::SETCC is always legalized to i8.
9494 DebugLoc dl = N->getDebugLoc();
9495 SDValue N0 = N->getOperand(0);
9496 EVT VT = N->getValueType(0);
9497 if (N0.getOpcode() == ISD::AND &&
9498 N0.hasOneUse() &&
9499 N0.getOperand(0).hasOneUse()) {
9500 SDValue N00 = N0.getOperand(0);
9501 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9502 return SDValue();
9503 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9504 if (!C || C->getZExtValue() != 1)
9505 return SDValue();
9506 return DAG.getNode(ISD::AND, dl, VT,
9507 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9508 N00.getOperand(0), N00.getOperand(1)),
9509 DAG.getConstant(1, VT));
9510 }
9511
9512 return SDValue();
9513}
9514
Dan Gohman475871a2008-07-27 21:46:04 +00009515SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009516 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009517 SelectionDAG &DAG = DCI.DAG;
9518 switch (N->getOpcode()) {
9519 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009520 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009521 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009522 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009523 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009524 case ISD::SHL:
9525 case ISD::SRA:
9526 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009527 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009528 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009529 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009530 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9531 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009532 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009533 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009534 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009535 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009536 }
9537
Dan Gohman475871a2008-07-27 21:46:04 +00009538 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009539}
9540
Evan Cheng60c07e12006-07-05 22:17:51 +00009541//===----------------------------------------------------------------------===//
9542// X86 Inline Assembly Support
9543//===----------------------------------------------------------------------===//
9544
Chris Lattnerb8105652009-07-20 17:51:36 +00009545static bool LowerToBSwap(CallInst *CI) {
9546 // FIXME: this should verify that we are targetting a 486 or better. If not,
9547 // we will turn this bswap into something that will be lowered to logical ops
9548 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9549 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009550
Chris Lattnerb8105652009-07-20 17:51:36 +00009551 // Verify this is a simple bswap.
9552 if (CI->getNumOperands() != 2 ||
9553 CI->getType() != CI->getOperand(1)->getType() ||
9554 !CI->getType()->isInteger())
9555 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009556
Chris Lattnerb8105652009-07-20 17:51:36 +00009557 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9558 if (!Ty || Ty->getBitWidth() % 16 != 0)
9559 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009560
Chris Lattnerb8105652009-07-20 17:51:36 +00009561 // Okay, we can do this xform, do so now.
9562 const Type *Tys[] = { Ty };
9563 Module *M = CI->getParent()->getParent()->getParent();
9564 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009565
Chris Lattnerb8105652009-07-20 17:51:36 +00009566 Value *Op = CI->getOperand(1);
9567 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009568
Chris Lattnerb8105652009-07-20 17:51:36 +00009569 CI->replaceAllUsesWith(Op);
9570 CI->eraseFromParent();
9571 return true;
9572}
9573
9574bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9575 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9576 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9577
9578 std::string AsmStr = IA->getAsmString();
9579
9580 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009581 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009582 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9583
9584 switch (AsmPieces.size()) {
9585 default: return false;
9586 case 1:
9587 AsmStr = AsmPieces[0];
9588 AsmPieces.clear();
9589 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9590
9591 // bswap $0
9592 if (AsmPieces.size() == 2 &&
9593 (AsmPieces[0] == "bswap" ||
9594 AsmPieces[0] == "bswapq" ||
9595 AsmPieces[0] == "bswapl") &&
9596 (AsmPieces[1] == "$0" ||
9597 AsmPieces[1] == "${0:q}")) {
9598 // No need to check constraints, nothing other than the equivalent of
9599 // "=r,0" would be valid here.
9600 return LowerToBSwap(CI);
9601 }
9602 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009603 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009604 AsmPieces.size() == 3 &&
9605 AsmPieces[0] == "rorw" &&
9606 AsmPieces[1] == "$$8," &&
9607 AsmPieces[2] == "${0:w}" &&
9608 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9609 return LowerToBSwap(CI);
9610 }
9611 break;
9612 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009613 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009614 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009615 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9616 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9617 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009618 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009619 SplitString(AsmPieces[0], Words, " \t");
9620 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9621 Words.clear();
9622 SplitString(AsmPieces[1], Words, " \t");
9623 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9624 Words.clear();
9625 SplitString(AsmPieces[2], Words, " \t,");
9626 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9627 Words[2] == "%edx") {
9628 return LowerToBSwap(CI);
9629 }
9630 }
9631 }
9632 }
9633 break;
9634 }
9635 return false;
9636}
9637
9638
9639
Chris Lattnerf4dff842006-07-11 02:54:03 +00009640/// getConstraintType - Given a constraint letter, return the type of
9641/// constraint it is for this target.
9642X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009643X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9644 if (Constraint.size() == 1) {
9645 switch (Constraint[0]) {
9646 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009647 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009648 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009649 case 'r':
9650 case 'R':
9651 case 'l':
9652 case 'q':
9653 case 'Q':
9654 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009655 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009656 case 'Y':
9657 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009658 case 'e':
9659 case 'Z':
9660 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009661 default:
9662 break;
9663 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009664 }
Chris Lattner4234f572007-03-25 02:14:49 +00009665 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009666}
9667
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009668/// LowerXConstraint - try to replace an X constraint, which matches anything,
9669/// with another that has more specific requirements based on the type of the
9670/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009671const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009672LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009673 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9674 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009675 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009676 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009677 return "Y";
9678 if (Subtarget->hasSSE1())
9679 return "x";
9680 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009681
Chris Lattner5e764232008-04-26 23:02:14 +00009682 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009683}
9684
Chris Lattner48884cd2007-08-25 00:47:38 +00009685/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9686/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009687void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009688 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009689 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009690 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009691 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009692 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009693
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009694 switch (Constraint) {
9695 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009696 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009697 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009698 if (C->getZExtValue() <= 31) {
9699 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009700 break;
9701 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009702 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009703 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009704 case 'J':
9705 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009706 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009707 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9708 break;
9709 }
9710 }
9711 return;
9712 case 'K':
9713 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009714 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009715 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9716 break;
9717 }
9718 }
9719 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009720 case 'N':
9721 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009722 if (C->getZExtValue() <= 255) {
9723 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009724 break;
9725 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009726 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009727 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009728 case 'e': {
9729 // 32-bit signed value
9730 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9731 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009732 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9733 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009734 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009736 break;
9737 }
9738 // FIXME gcc accepts some relocatable values here too, but only in certain
9739 // memory models; it's complicated.
9740 }
9741 return;
9742 }
9743 case 'Z': {
9744 // 32-bit unsigned value
9745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9746 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009747 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9748 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009749 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9750 break;
9751 }
9752 }
9753 // FIXME gcc accepts some relocatable values here too, but only in certain
9754 // memory models; it's complicated.
9755 return;
9756 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009757 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009758 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009759 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009760 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009762 break;
9763 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009764
Chris Lattnerdc43a882007-05-03 16:52:29 +00009765 // If we are in non-pic codegen mode, we allow the address of a global (with
9766 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009767 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009768 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009769
Chris Lattner49921962009-05-08 18:23:14 +00009770 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9771 while (1) {
9772 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9773 Offset += GA->getOffset();
9774 break;
9775 } else if (Op.getOpcode() == ISD::ADD) {
9776 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9777 Offset += C->getZExtValue();
9778 Op = Op.getOperand(0);
9779 continue;
9780 }
9781 } else if (Op.getOpcode() == ISD::SUB) {
9782 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9783 Offset += -C->getZExtValue();
9784 Op = Op.getOperand(0);
9785 continue;
9786 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009787 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009788
Chris Lattner49921962009-05-08 18:23:14 +00009789 // Otherwise, this isn't something we can handle, reject it.
9790 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009791 }
Eric Christopherfd179292009-08-27 18:07:15 +00009792
Chris Lattner36c25012009-07-10 07:34:39 +00009793 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009794 // If we require an extra load to get this address, as in PIC mode, we
9795 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009796 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9797 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009798 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009799
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009800 if (hasMemory)
9801 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9802 else
9803 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009804 Result = Op;
9805 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009806 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009808
Gabor Greifba36cb52008-08-28 21:40:38 +00009809 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009810 Ops.push_back(Result);
9811 return;
9812 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009813 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9814 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009815}
9816
Chris Lattner259e97c2006-01-31 19:43:35 +00009817std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009818getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009819 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009820 if (Constraint.size() == 1) {
9821 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009822 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009823 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009824 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9825 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009826 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009827 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9828 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9829 X86::R10D,X86::R11D,X86::R12D,
9830 X86::R13D,X86::R14D,X86::R15D,
9831 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009833 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9834 X86::SI, X86::DI, X86::R8W,X86::R9W,
9835 X86::R10W,X86::R11W,X86::R12W,
9836 X86::R13W,X86::R14W,X86::R15W,
9837 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009838 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009839 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9840 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9841 X86::R10B,X86::R11B,X86::R12B,
9842 X86::R13B,X86::R14B,X86::R15B,
9843 X86::BPL, X86::SPL, 0);
9844
Owen Anderson825b72b2009-08-11 20:47:22 +00009845 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009846 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9847 X86::RSI, X86::RDI, X86::R8, X86::R9,
9848 X86::R10, X86::R11, X86::R12,
9849 X86::R13, X86::R14, X86::R15,
9850 X86::RBP, X86::RSP, 0);
9851
9852 break;
9853 }
Eric Christopherfd179292009-08-27 18:07:15 +00009854 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009855 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009856 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009857 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009858 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009859 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009860 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009861 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009862 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009863 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9864 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009865 }
9866 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009867
Chris Lattner1efa40f2006-02-22 00:56:39 +00009868 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009869}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009870
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009871std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009872X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009873 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009874 // First, see if this is a constraint that directly corresponds to an LLVM
9875 // register class.
9876 if (Constraint.size() == 1) {
9877 // GCC Constraint Letters
9878 switch (Constraint[0]) {
9879 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009880 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009881 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009882 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009883 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009884 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009885 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009886 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009887 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009888 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009889 case 'R': // LEGACY_REGS
9890 if (VT == MVT::i8)
9891 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9892 if (VT == MVT::i16)
9893 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9894 if (VT == MVT::i32 || !Subtarget->is64Bit())
9895 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9896 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009897 case 'f': // FP Stack registers.
9898 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9899 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009900 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009901 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009902 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009903 return std::make_pair(0U, X86::RFP64RegisterClass);
9904 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009905 case 'y': // MMX_REGS if MMX allowed.
9906 if (!Subtarget->hasMMX()) break;
9907 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009908 case 'Y': // SSE_REGS if SSE2 allowed
9909 if (!Subtarget->hasSSE2()) break;
9910 // FALL THROUGH.
9911 case 'x': // SSE_REGS if SSE1 allowed
9912 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009913
Owen Anderson825b72b2009-08-11 20:47:22 +00009914 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009915 default: break;
9916 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009917 case MVT::f32:
9918 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009919 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009920 case MVT::f64:
9921 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009922 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009923 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009924 case MVT::v16i8:
9925 case MVT::v8i16:
9926 case MVT::v4i32:
9927 case MVT::v2i64:
9928 case MVT::v4f32:
9929 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009930 return std::make_pair(0U, X86::VR128RegisterClass);
9931 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009932 break;
9933 }
9934 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009935
Chris Lattnerf76d1802006-07-31 23:26:50 +00009936 // Use the default implementation in TargetLowering to convert the register
9937 // constraint into a member of a register class.
9938 std::pair<unsigned, const TargetRegisterClass*> Res;
9939 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009940
9941 // Not found as a standard register?
9942 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009943 // Map st(0) -> st(7) -> ST0
9944 if (Constraint.size() == 7 && Constraint[0] == '{' &&
9945 tolower(Constraint[1]) == 's' &&
9946 tolower(Constraint[2]) == 't' &&
9947 Constraint[3] == '(' &&
9948 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
9949 Constraint[5] == ')' &&
9950 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +00009951
Chris Lattner56d77c72009-09-13 22:41:48 +00009952 Res.first = X86::ST0+Constraint[4]-'0';
9953 Res.second = X86::RFP80RegisterClass;
9954 return Res;
9955 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009956
Chris Lattner56d77c72009-09-13 22:41:48 +00009957 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009958 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +00009959 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009960 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009961 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009962 }
Chris Lattner56d77c72009-09-13 22:41:48 +00009963
9964 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009965 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009966 Res.first = X86::EFLAGS;
9967 Res.second = X86::CCRRegisterClass;
9968 return Res;
9969 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009970
Dale Johannesen330169f2008-11-13 21:52:36 +00009971 // 'A' means EAX + EDX.
9972 if (Constraint == "A") {
9973 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009974 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009975 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +00009976 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009977 return Res;
9978 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009979
Chris Lattnerf76d1802006-07-31 23:26:50 +00009980 // Otherwise, check to see if this is a register class of the wrong value
9981 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9982 // turn into {ax},{dx}.
9983 if (Res.second->hasType(VT))
9984 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009985
Chris Lattnerf76d1802006-07-31 23:26:50 +00009986 // All of the single-register GCC register classes map their values onto
9987 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9988 // really want an 8-bit or 32-bit register, map to the appropriate register
9989 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009990 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009991 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009992 unsigned DestReg = 0;
9993 switch (Res.first) {
9994 default: break;
9995 case X86::AX: DestReg = X86::AL; break;
9996 case X86::DX: DestReg = X86::DL; break;
9997 case X86::CX: DestReg = X86::CL; break;
9998 case X86::BX: DestReg = X86::BL; break;
9999 }
10000 if (DestReg) {
10001 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010002 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010003 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010004 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010005 unsigned DestReg = 0;
10006 switch (Res.first) {
10007 default: break;
10008 case X86::AX: DestReg = X86::EAX; break;
10009 case X86::DX: DestReg = X86::EDX; break;
10010 case X86::CX: DestReg = X86::ECX; break;
10011 case X86::BX: DestReg = X86::EBX; break;
10012 case X86::SI: DestReg = X86::ESI; break;
10013 case X86::DI: DestReg = X86::EDI; break;
10014 case X86::BP: DestReg = X86::EBP; break;
10015 case X86::SP: DestReg = X86::ESP; break;
10016 }
10017 if (DestReg) {
10018 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010019 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010020 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010021 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010022 unsigned DestReg = 0;
10023 switch (Res.first) {
10024 default: break;
10025 case X86::AX: DestReg = X86::RAX; break;
10026 case X86::DX: DestReg = X86::RDX; break;
10027 case X86::CX: DestReg = X86::RCX; break;
10028 case X86::BX: DestReg = X86::RBX; break;
10029 case X86::SI: DestReg = X86::RSI; break;
10030 case X86::DI: DestReg = X86::RDI; break;
10031 case X86::BP: DestReg = X86::RBP; break;
10032 case X86::SP: DestReg = X86::RSP; break;
10033 }
10034 if (DestReg) {
10035 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010036 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010037 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010038 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010039 } else if (Res.second == X86::FR32RegisterClass ||
10040 Res.second == X86::FR64RegisterClass ||
10041 Res.second == X86::VR128RegisterClass) {
10042 // Handle references to XMM physical registers that got mapped into the
10043 // wrong class. This can happen with constraints like {xmm0} where the
10044 // target independent register mapper will just pick the first match it can
10045 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010046 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010047 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010048 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010049 Res.second = X86::FR64RegisterClass;
10050 else if (X86::VR128RegisterClass->hasType(VT))
10051 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010052 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010053
Chris Lattnerf76d1802006-07-31 23:26:50 +000010054 return Res;
10055}
Mon P Wang0c397192008-10-30 08:01:45 +000010056
10057//===----------------------------------------------------------------------===//
10058// X86 Widen vector type
10059//===----------------------------------------------------------------------===//
10060
10061/// getWidenVectorType: given a vector type, returns the type to widen
10062/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010063/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010064/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010065/// scalarizing vs using the wider vector type.
10066
Owen Andersone50ed302009-08-10 22:56:29 +000010067EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010068 assert(VT.isVector());
10069 if (isTypeLegal(VT))
10070 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010071
Mon P Wang0c397192008-10-30 08:01:45 +000010072 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10073 // type based on element type. This would speed up our search (though
10074 // it may not be worth it since the size of the list is relatively
10075 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010076 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010077 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010078
Mon P Wang0c397192008-10-30 08:01:45 +000010079 // On X86, it make sense to widen any vector wider than 1
10080 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010081 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010082
Owen Anderson825b72b2009-08-11 20:47:22 +000010083 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10084 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10085 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010086
10087 if (isTypeLegal(SVT) &&
10088 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010089 SVT.getVectorNumElements() > NElts)
10090 return SVT;
10091 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010092 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010093}