blob: 02582a044f12931518a54b6073b108a90ec50855 [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"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000039#include "llvm/Support/ErrorHandling.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000041#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000042#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000043#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000044#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000045using namespace llvm;
46
Mon P Wang3c81d352008-11-23 04:37:22 +000047static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000048DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000049
Dan Gohman2f67df72009-09-03 17:18:51 +000050// Disable16Bit - 16-bit operations typically have a larger encoding than
51// corresponding 32-bit instructions, and 16-bit code is slow on some
52// processors. This is an experimental flag to disable 16-bit operations
53// (which forces them to be Legalized to 32-bit operations).
54static cl::opt<bool>
55Disable16Bit("disable-16bit", cl::Hidden,
56 cl::desc("Disable use of 16-bit instructions"));
57
Evan Cheng10e86422008-04-25 19:11:04 +000058// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000059static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000060 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000061
Chris Lattnerf0144122009-07-28 03:13:23 +000062static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
63 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
64 default: llvm_unreachable("unknown subtarget type");
65 case X86Subtarget::isDarwin:
Chris Lattner8c6ed052009-09-16 01:46:41 +000066 if (TM.getSubtarget<X86Subtarget>().is64Bit())
67 return new X8664_MachoTargetObjectFile();
Chris Lattner228252f2009-09-18 20:22:52 +000068 return new X8632_MachoTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +000069 case X86Subtarget::isELF:
70 return new TargetLoweringObjectFileELF();
71 case X86Subtarget::isMingw:
72 case X86Subtarget::isCygwin:
73 case X86Subtarget::isWindows:
74 return new TargetLoweringObjectFileCOFF();
75 }
Eric Christopherfd179292009-08-27 18:07:15 +000076
Chris Lattnerf0144122009-07-28 03:13:23 +000077}
78
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000079X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000080 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000081 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000082 X86ScalarSSEf64 = Subtarget->hasSSE2();
83 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000084 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000085
Anton Korobeynikov2365f512007-07-14 14:06:15 +000086 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000087 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000088
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000089 // Set up the TargetLowering object.
90
91 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000092 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000093 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000094 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000095 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000096
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000097 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000098 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000099 setUseUnderscoreSetJmp(false);
100 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000101 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000102 // MS runtime is weird: it exports _setjmp, but longjmp!
103 setUseUnderscoreSetJmp(true);
104 setUseUnderscoreLongJmp(false);
105 } else {
106 setUseUnderscoreSetJmp(true);
107 setUseUnderscoreLongJmp(true);
108 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000109
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000112 if (!Disable16Bit)
113 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000115 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000119
Scott Michelfdc40a02009-02-17 22:15:04 +0000120 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000122 if (!Disable16Bit)
123 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000124 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000125 if (!Disable16Bit)
126 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
128 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000129
130 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000137
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000138 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
139 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000143
Evan Cheng25ab6902006-09-08 06:48:29 +0000144 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000147 } else if (!UseSoftFloat) {
148 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000149 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000151 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000152 // We have an algorithm for SSE2, and we turn this into a 64-bit
153 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000156
157 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
158 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
160 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000161
Devang Patel6a784892009-06-05 18:48:29 +0000162 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000163 // SSE has no i16 to fp conversion, only i32
164 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000168 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
170 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000171 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000172 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
174 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000175 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176
Dale Johannesen73328d12007-09-19 23:55:34 +0000177 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
178 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
180 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000181
Evan Cheng02568ff2006-01-30 22:13:22 +0000182 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
183 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
185 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000186
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000187 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000189 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000191 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
193 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000194 }
195
196 // Handle FP_TO_UINT by promoting the destination to a larger signed
197 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
199 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
200 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000201
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
204 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000205 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000206 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 // Expand FP_TO_UINT into a select.
208 // FIXME: We would like to use a Custom expander here eventually to do
209 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000212 // With SSE3 we can use fisttpll to convert to a signed i64; without
213 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216
Chris Lattner399610a2006-12-05 18:22:22 +0000217 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000218 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
220 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000221 }
Chris Lattner21f66852005-12-23 05:15:23 +0000222
Dan Gohmanb00ee212008-02-18 19:34:53 +0000223 // Scalar integer divide and remainder are lowered to use operations that
224 // produce two results, to match the available instructions. This exposes
225 // the two-result form to trivial CSE, which is able to combine x/y and x%y
226 // into a single instruction.
227 //
228 // Scalar integer multiply-high is also lowered to use two-result
229 // operations, to match the available instructions. However, plain multiply
230 // (low) operations are left as Legal, as there are single-result
231 // instructions for this in x86. Using the two-result multiply instructions
232 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
234 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
235 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
236 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
237 setOperationAction(ISD::SREM , MVT::i8 , Expand);
238 setOperationAction(ISD::UREM , MVT::i8 , Expand);
239 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
240 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
241 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
242 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
243 setOperationAction(ISD::SREM , MVT::i16 , Expand);
244 setOperationAction(ISD::UREM , MVT::i16 , Expand);
245 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
246 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
247 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
248 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
249 setOperationAction(ISD::SREM , MVT::i32 , Expand);
250 setOperationAction(ISD::UREM , MVT::i32 , Expand);
251 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
252 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
253 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
254 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
255 setOperationAction(ISD::SREM , MVT::i64 , Expand);
256 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000257
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
259 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
260 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
261 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000262 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
264 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
265 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
266 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
267 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
268 setOperationAction(ISD::FREM , MVT::f32 , Expand);
269 setOperationAction(ISD::FREM , MVT::f64 , Expand);
270 setOperationAction(ISD::FREM , MVT::f80 , Expand);
271 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Owen Anderson825b72b2009-08-11 20:47:22 +0000273 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
274 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
275 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
276 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000277 if (Disable16Bit) {
278 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
279 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
280 } else {
281 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
282 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
283 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
285 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
294 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000295
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000297 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000298 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000299 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000300 if (Disable16Bit)
301 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
302 else
303 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
305 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
307 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
308 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000309 if (Disable16Bit)
310 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
311 else
312 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
314 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
315 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
316 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000317 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
319 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000322
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000323 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
325 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
326 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
327 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000328 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
330 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000331 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000332 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
334 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
335 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
336 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000337 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000338 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000339 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
341 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
342 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000348
Evan Chengd2cde682008-03-10 19:38:10 +0000349 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000351
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000352 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000354
Mon P Wang63307c32008-05-05 19:05:59 +0000355 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
357 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
358 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
359 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000360
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
362 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
363 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000366 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
371 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000374 }
375
Evan Cheng3c992d22006-03-07 02:02:57 +0000376 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000377 if (!Subtarget->isTargetDarwin() &&
378 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000379 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000381 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000382
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
384 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
385 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
386 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000387 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000388 setExceptionPointerRegister(X86::RAX);
389 setExceptionSelectorRegister(X86::RDX);
390 } else {
391 setExceptionPointerRegister(X86::EAX);
392 setExceptionSelectorRegister(X86::EDX);
393 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
395 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000396
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000398
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000400
Nate Begemanacc398c2006-01-25 18:21:52 +0000401 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::VASTART , MVT::Other, Custom);
403 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000404 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::VAARG , MVT::Other, Custom);
406 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000407 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::VAARG , MVT::Other, Expand);
409 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000410 }
Evan Chengae642192007-03-02 23:16:35 +0000411
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
413 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000414 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000416 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000418 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000420
Evan Chengc7ce29b2009-02-13 22:36:38 +0000421 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000422 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000423 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
425 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000426
Evan Cheng223547a2006-01-31 22:28:30 +0000427 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::FABS , MVT::f64, Custom);
429 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000430
431 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::FNEG , MVT::f64, Custom);
433 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000434
Evan Cheng68c47cb2007-01-05 07:55:56 +0000435 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
437 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000438
Evan Chengd25e9e82006-02-02 00:28:23 +0000439 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FSIN , MVT::f64, Expand);
441 setOperationAction(ISD::FCOS , MVT::f64, Expand);
442 setOperationAction(ISD::FSIN , MVT::f32, Expand);
443 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000444
Chris Lattnera54aa942006-01-29 06:26:08 +0000445 // Expand FP immediates into loads from the stack, except for the special
446 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447 addLegalFPImmediate(APFloat(+0.0)); // xorpd
448 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000449 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450 // Use SSE for f32, x87 for f64.
451 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
453 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454
455 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457
458 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
463 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
465 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466
467 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::FSIN , MVT::f32, Expand);
469 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000470
Nate Begemane1795842008-02-14 08:57:00 +0000471 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000472 addLegalFPImmediate(APFloat(+0.0f)); // xorps
473 addLegalFPImmediate(APFloat(+0.0)); // FLD0
474 addLegalFPImmediate(APFloat(+1.0)); // FLD1
475 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
476 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
477
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
480 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000481 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000482 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000484 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000485 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
486 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000487
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
489 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
490 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
491 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000492
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000493 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000494 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
495 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000496 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000497 addLegalFPImmediate(APFloat(+0.0)); // FLD0
498 addLegalFPImmediate(APFloat(+1.0)); // FLD1
499 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
500 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000501 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
502 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
503 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
504 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000505 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000506
Dale Johannesen59a58732007-08-05 18:49:15 +0000507 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000508 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
510 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
511 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000512 {
513 bool ignored;
514 APFloat TmpFlt(+0.0);
515 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
516 &ignored);
517 addLegalFPImmediate(TmpFlt); // FLD0
518 TmpFlt.changeSign();
519 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
520 APFloat TmpFlt2(+1.0);
521 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
522 &ignored);
523 addLegalFPImmediate(TmpFlt2); // FLD1
524 TmpFlt2.changeSign();
525 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
526 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000527
Evan Chengc7ce29b2009-02-13 22:36:38 +0000528 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000529 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
530 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000531 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000532 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000533
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000534 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
536 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
537 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000538
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::FLOG, MVT::f80, Expand);
540 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
541 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
542 setOperationAction(ISD::FEXP, MVT::f80, Expand);
543 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000544
Mon P Wangf007a8b2008-11-06 05:31:54 +0000545 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000546 // (for widening) or expand (for scalarization). Then we will selectively
547 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000548 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
549 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
550 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
565 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
566 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000598 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000599 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
603 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
604 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
605 setTruncStoreAction((MVT::SimpleValueType)VT,
606 (MVT::SimpleValueType)InnerVT, Expand);
607 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
608 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
609 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000610 }
611
Evan Chengc7ce29b2009-02-13 22:36:38 +0000612 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
613 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000614 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
616 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
617 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
618 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
619 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000620
Owen Anderson825b72b2009-08-11 20:47:22 +0000621 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
622 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
623 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
624 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
627 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
628 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
629 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
632 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000633
Owen Anderson825b72b2009-08-11 20:47:22 +0000634 setOperationAction(ISD::AND, MVT::v8i8, Promote);
635 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
636 setOperationAction(ISD::AND, MVT::v4i16, Promote);
637 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
638 setOperationAction(ISD::AND, MVT::v2i32, Promote);
639 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
640 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000641
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::OR, MVT::v8i8, Promote);
643 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
644 setOperationAction(ISD::OR, MVT::v4i16, Promote);
645 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
646 setOperationAction(ISD::OR, MVT::v2i32, Promote);
647 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
648 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000649
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
651 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
652 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
653 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
654 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
655 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
656 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000657
Owen Anderson825b72b2009-08-11 20:47:22 +0000658 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
659 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
660 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
661 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
662 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
663 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
664 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
665 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
666 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000667
Owen Anderson825b72b2009-08-11 20:47:22 +0000668 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
669 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
670 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
671 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
676 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
680 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
681 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
682 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000683
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000685
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
687 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
688 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
689 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
690 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
691 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
692 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000693 }
694
Evan Cheng92722532009-03-26 23:06:32 +0000695 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000696 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000697
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
699 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
700 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
701 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
702 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
703 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
704 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
705 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
706 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
707 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
708 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
709 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000710 }
711
Evan Cheng92722532009-03-26 23:06:32 +0000712 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000713 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000714
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000715 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
716 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
718 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
719 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
720 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000721
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
723 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
724 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
725 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
726 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
727 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
728 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
729 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
730 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
731 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
732 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
733 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
734 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
735 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
736 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
737 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000738
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
740 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
741 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
742 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000743
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
745 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
746 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
747 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
748 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000749
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000750 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
751 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
752 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
753 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
755
Evan Cheng2c3ae372006-04-12 21:21:57 +0000756 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
758 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000759 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000760 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000761 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000762 // Do not attempt to custom lower non-128-bit vectors
763 if (!VT.is128BitVector())
764 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::BUILD_VECTOR,
766 VT.getSimpleVT().SimpleTy, Custom);
767 setOperationAction(ISD::VECTOR_SHUFFLE,
768 VT.getSimpleVT().SimpleTy, Custom);
769 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
770 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000772
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
774 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
775 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
776 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
777 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
778 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000779
Nate Begemancdd1eec2008-02-12 22:51:28 +0000780 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
782 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000783 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000784
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000785 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
787 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000788 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000789
790 // Do not attempt to promote non-128-bit vectors
791 if (!VT.is128BitVector()) {
792 continue;
793 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000794 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000796 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000798 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000800 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000802 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000804 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000805
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000807
Evan Cheng2c3ae372006-04-12 21:21:57 +0000808 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
810 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
811 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
812 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
815 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000816 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
818 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000819 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000820 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000821
Nate Begeman14d12ca2008-02-11 04:19:36 +0000822 if (Subtarget->hasSSE41()) {
823 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000825
826 // i8 and i16 vectors are custom , because the source register and source
827 // source memory operand types are not the same width. f32 vectors are
828 // custom since the immediate controlling the insert encodes additional
829 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
831 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
832 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000834
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
836 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
837 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000839
840 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000843 }
844 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000845
Nate Begeman30a0de92008-07-17 16:51:19 +0000846 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000848 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000849
David Greene9b9838d2009-06-29 16:47:10 +0000850 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
852 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
853 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
854 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000855
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
857 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
858 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
859 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
860 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
861 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
862 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
863 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
864 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
865 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
866 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
867 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
868 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
869 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
870 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000871
872 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
874 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
875 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
876 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
877 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
878 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
879 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
880 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
881 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
882 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
883 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
884 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
885 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
886 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000887
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
889 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
890 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
891 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
894 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
895 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
896 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
900 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
901 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
902 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
903 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
904 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000905
906#if 0
907 // Not sure we want to do this since there are no 256-bit integer
908 // operations in AVX
909
910 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
911 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
913 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000914
915 // Do not attempt to custom lower non-power-of-2 vectors
916 if (!isPowerOf2_32(VT.getVectorNumElements()))
917 continue;
918
919 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
920 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
921 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
922 }
923
924 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
926 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000927 }
David Greene9b9838d2009-06-29 16:47:10 +0000928#endif
929
930#if 0
931 // Not sure we want to do this since there are no 256-bit integer
932 // operations in AVX
933
934 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
935 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
937 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000938
939 if (!VT.is256BitVector()) {
940 continue;
941 }
942 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000943 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000944 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000945 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000946 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000948 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000950 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000952 }
953
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000955#endif
956 }
957
Evan Cheng6be2c582006-04-05 23:38:46 +0000958 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000960
Bill Wendling74c37652008-12-09 22:08:41 +0000961 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 setOperationAction(ISD::SADDO, MVT::i32, Custom);
963 setOperationAction(ISD::SADDO, MVT::i64, Custom);
964 setOperationAction(ISD::UADDO, MVT::i32, Custom);
965 setOperationAction(ISD::UADDO, MVT::i64, Custom);
966 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
967 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
968 setOperationAction(ISD::USUBO, MVT::i32, Custom);
969 setOperationAction(ISD::USUBO, MVT::i64, Custom);
970 setOperationAction(ISD::SMULO, MVT::i32, Custom);
971 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000972
Evan Chengd54f2d52009-03-31 19:38:51 +0000973 if (!Subtarget->is64Bit()) {
974 // These libcalls are not available in 32-bit.
975 setLibcallName(RTLIB::SHL_I128, 0);
976 setLibcallName(RTLIB::SRL_I128, 0);
977 setLibcallName(RTLIB::SRA_I128, 0);
978 }
979
Evan Cheng206ee9d2006-07-07 08:33:52 +0000980 // We have target-specific dag combine patterns for the following nodes:
981 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000982 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000983 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000984 setTargetDAGCombine(ISD::SHL);
985 setTargetDAGCombine(ISD::SRA);
986 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000987 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000988 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000989 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000990 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000991 if (Subtarget->is64Bit())
992 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000993
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000994 computeRegisterProperties();
995
Mon P Wangcd6e7252009-11-30 02:42:02 +0000996 // Divide and reminder operations have no vector equivalent and can
997 // trap. Do a custom widening for these operations in which we never
998 // generate more divides/remainder than the original vector width.
999 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1000 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1001 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1002 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1003 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1004 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1005 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1006 }
1007 }
1008
Evan Cheng87ed7162006-02-14 08:25:08 +00001009 // FIXME: These should be based on subtarget info. Plus, the values should
1010 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001011 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1012 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1013 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001014 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001015 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001016}
1017
Scott Michel5b8f82e2008-03-10 15:42:14 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1020 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001021}
1022
1023
Evan Cheng29286502008-01-23 23:17:41 +00001024/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1025/// the desired ByVal argument alignment.
1026static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1027 if (MaxAlign == 16)
1028 return;
1029 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1030 if (VTy->getBitWidth() == 128)
1031 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001032 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1033 unsigned EltAlign = 0;
1034 getMaxByValAlign(ATy->getElementType(), EltAlign);
1035 if (EltAlign > MaxAlign)
1036 MaxAlign = EltAlign;
1037 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1038 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1039 unsigned EltAlign = 0;
1040 getMaxByValAlign(STy->getElementType(i), EltAlign);
1041 if (EltAlign > MaxAlign)
1042 MaxAlign = EltAlign;
1043 if (MaxAlign == 16)
1044 break;
1045 }
1046 }
1047 return;
1048}
1049
1050/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1051/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001052/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1053/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001054unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001055 if (Subtarget->is64Bit()) {
1056 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001057 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001058 if (TyAlign > 8)
1059 return TyAlign;
1060 return 8;
1061 }
1062
Evan Cheng29286502008-01-23 23:17:41 +00001063 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001064 if (Subtarget->hasSSE1())
1065 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001066 return Align;
1067}
Chris Lattner2b02a442007-02-25 08:29:00 +00001068
Evan Chengf0df0312008-05-15 08:39:06 +00001069/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001070/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001071/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001072/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001073EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001074X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001075 bool isSrcConst, bool isSrcStr,
1076 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001077 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1078 // linux. This is because the stack realignment code can't handle certain
1079 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001080 const Function *F = DAG.getMachineFunction().getFunction();
1081 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1082 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001083 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001085 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001086 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 }
Evan Chengf0df0312008-05-15 08:39:06 +00001088 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::i64;
1090 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001091}
1092
Evan Chengcc415862007-11-09 01:32:10 +00001093/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1094/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001096 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001097 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 // This doesn't have DebugLoc associated with it, but is not really the
1099 // same as a Register.
1100 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1101 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001102 return Table;
1103}
1104
Bill Wendlingb4202b82009-07-01 18:50:55 +00001105/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001106unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001107 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001108}
1109
Chris Lattner2b02a442007-02-25 08:29:00 +00001110//===----------------------------------------------------------------------===//
1111// Return Value Calling Convention Implementation
1112//===----------------------------------------------------------------------===//
1113
Chris Lattner59ed56b2007-02-28 04:55:35 +00001114#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001115
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001116bool
1117X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1118 const SmallVectorImpl<EVT> &OutTys,
1119 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1120 SelectionDAG &DAG) {
1121 SmallVector<CCValAssign, 16> RVLocs;
1122 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1123 RVLocs, *DAG.getContext());
1124 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1125}
1126
Dan Gohman98ca4f22009-08-05 01:29:28 +00001127SDValue
1128X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001129 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001130 const SmallVectorImpl<ISD::OutputArg> &Outs,
1131 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001132
Chris Lattner9774c912007-02-27 05:28:59 +00001133 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001134 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1135 RVLocs, *DAG.getContext());
1136 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001137
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001138 // If this is the first return lowered for this function, add the regs to the
1139 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001140 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001141 for (unsigned i = 0; i != RVLocs.size(); ++i)
1142 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001143 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001145
Dan Gohman475871a2008-07-27 21:46:04 +00001146 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001147
Dan Gohman475871a2008-07-27 21:46:04 +00001148 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001149 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1150 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001151 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001152
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001153 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001154 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1155 CCValAssign &VA = RVLocs[i];
1156 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001157 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001158
Chris Lattner447ff682008-03-11 03:23:40 +00001159 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1160 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001161 if (VA.getLocReg() == X86::ST0 ||
1162 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001163 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1164 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001165 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001166 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001167 RetOps.push_back(ValToCopy);
1168 // Don't emit a copytoreg.
1169 continue;
1170 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001171
Evan Cheng242b38b2009-02-23 09:03:22 +00001172 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1173 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001174 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001175 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001176 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001177 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001178 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001180 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001181 }
1182
Dale Johannesendd64c412009-02-04 00:33:20 +00001183 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001184 Flag = Chain.getValue(1);
1185 }
Dan Gohman61a92132008-04-21 23:59:07 +00001186
1187 // The x86-64 ABI for returning structs by value requires that we copy
1188 // the sret argument into %rax for the return. We saved the argument into
1189 // a virtual register in the entry block, so now we copy the value out
1190 // and into %rax.
1191 if (Subtarget->is64Bit() &&
1192 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1193 MachineFunction &MF = DAG.getMachineFunction();
1194 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1195 unsigned Reg = FuncInfo->getSRetReturnReg();
1196 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001197 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001198 FuncInfo->setSRetReturnReg(Reg);
1199 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001200 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001201
Dale Johannesendd64c412009-02-04 00:33:20 +00001202 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001203 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001204
1205 // RAX now acts like a return value.
1206 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001208
Chris Lattner447ff682008-03-11 03:23:40 +00001209 RetOps[0] = Chain; // Update chain.
1210
1211 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001212 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001213 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001214
1215 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001217}
1218
Dan Gohman98ca4f22009-08-05 01:29:28 +00001219/// LowerCallResult - Lower the result values of a call into the
1220/// appropriate copies out of appropriate physical registers.
1221///
1222SDValue
1223X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001224 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001225 const SmallVectorImpl<ISD::InputArg> &Ins,
1226 DebugLoc dl, SelectionDAG &DAG,
1227 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001228
Chris Lattnere32bbf62007-02-28 07:09:55 +00001229 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001230 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001231 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001232 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001233 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001235
Chris Lattner3085e152007-02-25 08:59:22 +00001236 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001237 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001238 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001239 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001240
Torok Edwin3f142c32009-02-01 18:15:56 +00001241 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001243 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001244 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001245 }
1246
Chris Lattner8e6da152008-03-10 21:08:41 +00001247 // If this is a call to a function that returns an fp value on the floating
1248 // point stack, but where we prefer to use the value in xmm registers, copy
1249 // 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 +00001250 if ((VA.getLocReg() == X86::ST0 ||
1251 VA.getLocReg() == X86::ST1) &&
1252 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Evan Cheng79fb3b42009-02-20 20:43:02 +00001256 SDValue Val;
1257 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001258 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1259 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1260 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001262 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1264 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001265 } else {
1266 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001268 Val = Chain.getValue(0);
1269 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001270 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1271 } else {
1272 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1273 CopyVT, InFlag).getValue(1);
1274 Val = Chain.getValue(0);
1275 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001276 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001277
Dan Gohman37eed792009-02-04 17:28:58 +00001278 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001279 // Round the F80 the right size, which also moves to the appropriate xmm
1280 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001281 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001282 // This truncation won't change the value.
1283 DAG.getIntPtrConstant(1));
1284 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001285
Dan Gohman98ca4f22009-08-05 01:29:28 +00001286 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001287 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001290}
1291
1292
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001293//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001294// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001295//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001296// StdCall calling convention seems to be standard for many Windows' API
1297// routines and around. It differs from C calling convention just a little:
1298// callee should clean up the stack, not caller. Symbols should be also
1299// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001300// For info on fast calling convention see Fast Calling Convention (tail call)
1301// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001302
Dan Gohman98ca4f22009-08-05 01:29:28 +00001303/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001304/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001305static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1306 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001307 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001308
Dan Gohman98ca4f22009-08-05 01:29:28 +00001309 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001310}
1311
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001312/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001313/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314static bool
1315ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1316 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001317 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001318
Dan Gohman98ca4f22009-08-05 01:29:28 +00001319 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001320}
1321
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001322/// IsCalleePop - Determines whether the callee is required to pop its
1323/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001324bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001325 if (IsVarArg)
1326 return false;
1327
Dan Gohman095cc292008-09-13 01:54:27 +00001328 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001329 default:
1330 return false;
1331 case CallingConv::X86_StdCall:
1332 return !Subtarget->is64Bit();
1333 case CallingConv::X86_FastCall:
1334 return !Subtarget->is64Bit();
1335 case CallingConv::Fast:
1336 return PerformTailCallOpt;
1337 }
1338}
1339
Dan Gohman095cc292008-09-13 01:54:27 +00001340/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1341/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001342CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001343 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001344 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001345 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001346 else
1347 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001348 }
1349
Gordon Henriksen86737662008-01-05 16:56:59 +00001350 if (CC == CallingConv::X86_FastCall)
1351 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001352 else if (CC == CallingConv::Fast)
1353 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001354 else
1355 return CC_X86_32_C;
1356}
1357
Dan Gohman98ca4f22009-08-05 01:29:28 +00001358/// NameDecorationForCallConv - Selects the appropriate decoration to
1359/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001360NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001361X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001363 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001365 return StdCall;
1366 return None;
1367}
1368
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001369
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001370/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1371/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001372/// the specific parameter attribute. The copy will be passed as a byval
1373/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001374static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001375CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001376 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1377 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001378 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001379 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001380 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001381}
1382
Dan Gohman98ca4f22009-08-05 01:29:28 +00001383SDValue
1384X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001385 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001386 const SmallVectorImpl<ISD::InputArg> &Ins,
1387 DebugLoc dl, SelectionDAG &DAG,
1388 const CCValAssign &VA,
1389 MachineFrameInfo *MFI,
1390 unsigned i) {
1391
Rafael Espindola7effac52007-09-14 15:48:13 +00001392 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001393 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1394 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001395 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001396 EVT ValVT;
1397
1398 // If value is passed by pointer we have address passed instead of the value
1399 // itself.
1400 if (VA.getLocInfo() == CCValAssign::Indirect)
1401 ValVT = VA.getLocVT();
1402 else
1403 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001404
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001405 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001406 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001407 // In case of tail call optimization mark all arguments mutable. Since they
1408 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001409 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001410 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001411 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001412 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001413 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001414 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001415 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001416}
1417
Dan Gohman475871a2008-07-27 21:46:04 +00001418SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001420 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421 bool isVarArg,
1422 const SmallVectorImpl<ISD::InputArg> &Ins,
1423 DebugLoc dl,
1424 SelectionDAG &DAG,
1425 SmallVectorImpl<SDValue> &InVals) {
1426
Evan Cheng1bc78042006-04-26 01:20:17 +00001427 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001429
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 const Function* Fn = MF.getFunction();
1431 if (Fn->hasExternalLinkage() &&
1432 Subtarget->isTargetCygMing() &&
1433 Fn->getName() == "main")
1434 FuncInfo->setForceFramePointer(true);
1435
1436 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001437 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001438
Evan Cheng1bc78042006-04-26 01:20:17 +00001439 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001440 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001441 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001442
Dan Gohman98ca4f22009-08-05 01:29:28 +00001443 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001444 "Var args not supported with calling convention fastcc");
1445
Chris Lattner638402b2007-02-28 07:00:42 +00001446 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001447 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1449 ArgLocs, *DAG.getContext());
1450 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001451
Chris Lattnerf39f7712007-02-28 05:46:49 +00001452 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001453 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001454 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1455 CCValAssign &VA = ArgLocs[i];
1456 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1457 // places.
1458 assert(VA.getValNo() != LastVal &&
1459 "Don't support value assigned to multiple locs yet");
1460 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Chris Lattnerf39f7712007-02-28 05:46:49 +00001462 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001463 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001464 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001465 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001466 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001467 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001470 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001471 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001472 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001473 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001474 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001475 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1476 RC = X86::VR64RegisterClass;
1477 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001478 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001479
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001480 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001482
Chris Lattnerf39f7712007-02-28 05:46:49 +00001483 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1484 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1485 // right size.
1486 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001487 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001488 DAG.getValueType(VA.getValVT()));
1489 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001490 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001491 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001492 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001493 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001494
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001495 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001496 // Handle MMX values passed in XMM regs.
1497 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001498 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1499 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001500 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1501 } else
1502 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001503 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001504 } else {
1505 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001507 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001508
1509 // If value is passed via pointer - do a load.
1510 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001511 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001512
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001514 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001515
Dan Gohman61a92132008-04-21 23:59:07 +00001516 // The x86-64 ABI for returning structs by value requires that we copy
1517 // the sret argument into %rax for the return. Save the argument into
1518 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001519 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001520 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1521 unsigned Reg = FuncInfo->getSRetReturnReg();
1522 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001524 FuncInfo->setSRetReturnReg(Reg);
1525 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001526 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001528 }
1529
Chris Lattnerf39f7712007-02-28 05:46:49 +00001530 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001531 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001532 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001533 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001534
Evan Cheng1bc78042006-04-26 01:20:17 +00001535 // If the function takes variable number of arguments, make a frame index for
1536 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001537 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001539 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001540 }
1541 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001542 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1543
1544 // FIXME: We should really autogenerate these arrays
1545 static const unsigned GPR64ArgRegsWin64[] = {
1546 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001547 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001548 static const unsigned XMMArgRegsWin64[] = {
1549 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1550 };
1551 static const unsigned GPR64ArgRegs64Bit[] = {
1552 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1553 };
1554 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1556 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1557 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001558 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1559
1560 if (IsWin64) {
1561 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1562 GPR64ArgRegs = GPR64ArgRegsWin64;
1563 XMMArgRegs = XMMArgRegsWin64;
1564 } else {
1565 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1566 GPR64ArgRegs = GPR64ArgRegs64Bit;
1567 XMMArgRegs = XMMArgRegs64Bit;
1568 }
1569 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1570 TotalNumIntRegs);
1571 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1572 TotalNumXMMRegs);
1573
Devang Patel578efa92009-06-05 21:57:13 +00001574 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001575 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001576 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001577 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001578 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001579 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001580 // Kernel mode asks for SSE to be disabled, so don't push them
1581 // on the stack.
1582 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001583
Gordon Henriksen86737662008-01-05 16:56:59 +00001584 // For X86-64, if there are vararg parameters that are passed via
1585 // registers, then we must store them to their spots on the stack so they
1586 // may be loaded by deferencing the result of va_next.
1587 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001588 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1589 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001590 TotalNumXMMRegs * 16, 16,
1591 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001592
Gordon Henriksen86737662008-01-05 16:56:59 +00001593 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SmallVector<SDValue, 8> MemOps;
1595 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001596 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001597 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001598 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1599 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001600 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1601 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001603 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001604 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001605 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001606 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001607 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001608 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001609 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001610
Dan Gohmanface41a2009-08-16 21:24:25 +00001611 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1612 // Now store the XMM (fp + vector) parameter registers.
1613 SmallVector<SDValue, 11> SaveXMMOps;
1614 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001615
Dan Gohmanface41a2009-08-16 21:24:25 +00001616 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1617 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1618 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001619
Dan Gohmanface41a2009-08-16 21:24:25 +00001620 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1621 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001622
Dan Gohmanface41a2009-08-16 21:24:25 +00001623 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1624 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1625 X86::VR128RegisterClass);
1626 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1627 SaveXMMOps.push_back(Val);
1628 }
1629 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1630 MVT::Other,
1631 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001632 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001633
1634 if (!MemOps.empty())
1635 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1636 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001637 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001639
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001641 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001643 BytesCallerReserves = 0;
1644 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001645 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001646 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001647 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001648 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001649 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001650 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001651
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 if (!Is64Bit) {
1653 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001654 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1656 }
Evan Cheng25caf632006-05-23 21:06:34 +00001657
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001658 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001659
Dan Gohman98ca4f22009-08-05 01:29:28 +00001660 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001661}
1662
Dan Gohman475871a2008-07-27 21:46:04 +00001663SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001664X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1665 SDValue StackPtr, SDValue Arg,
1666 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001667 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001668 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001669 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001670 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001671 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001672 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001673 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001674 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001675 }
Dale Johannesenace16102009-02-03 19:33:06 +00001676 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001677 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001678}
1679
Bill Wendling64e87322009-01-16 19:25:27 +00001680/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001681/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001682SDValue
1683X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001685 SDValue Chain,
1686 bool IsTailCall,
1687 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001688 int FPDiff,
1689 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001690 if (!IsTailCall || FPDiff==0) return Chain;
1691
1692 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001693 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001694 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001695
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001696 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001697 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001698 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001699}
1700
1701/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1702/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001703static SDValue
1704EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001705 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001706 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001707 // Store the return address to the appropriate stack slot.
1708 if (!FPDiff) return Chain;
1709 // Calculate the new stack slot for the return address.
1710 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001711 int NewReturnAddrFI =
David Greene3f2bf852009-11-12 20:49:22 +00001712 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1713 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001714 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001715 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001716 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001717 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001718 return Chain;
1719}
1720
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721SDValue
1722X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001723 CallingConv::ID CallConv, bool isVarArg,
1724 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 const SmallVectorImpl<ISD::OutputArg> &Outs,
1726 const SmallVectorImpl<ISD::InputArg> &Ins,
1727 DebugLoc dl, SelectionDAG &DAG,
1728 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001729
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 MachineFunction &MF = DAG.getMachineFunction();
1731 bool Is64Bit = Subtarget->is64Bit();
1732 bool IsStructRet = CallIsStructReturn(Outs);
1733
1734 assert((!isTailCall ||
1735 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1736 "IsEligibleForTailCallOptimization missed a case!");
1737 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001738 "Var args not supported with calling convention fastcc");
1739
Chris Lattner638402b2007-02-28 07:00:42 +00001740 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001741 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1743 ArgLocs, *DAG.getContext());
1744 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Chris Lattner423c5f42007-02-28 05:31:48 +00001746 // Get a count of how many bytes are to be pushed on the stack.
1747 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001748 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001749 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001750
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001752 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001754 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1756 FPDiff = NumBytesCallerPushed - NumBytes;
1757
1758 // Set the delta of movement of the returnaddr stackslot.
1759 // But only set if delta is greater than previous delta.
1760 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1761 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1762 }
1763
Chris Lattnere563bbc2008-10-11 22:08:30 +00001764 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001765
Dan Gohman475871a2008-07-27 21:46:04 +00001766 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001767 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001768 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001769 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770
Dan Gohman475871a2008-07-27 21:46:04 +00001771 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1772 SmallVector<SDValue, 8> MemOpChains;
1773 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001774
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001775 // Walk the register/memloc assignments, inserting copies/loads. In the case
1776 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001777 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1778 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001779 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 SDValue Arg = Outs[i].Val;
1781 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001782 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001783
Chris Lattner423c5f42007-02-28 05:31:48 +00001784 // Promote the value if needed.
1785 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001786 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001787 case CCValAssign::Full: break;
1788 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001789 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001790 break;
1791 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001792 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001793 break;
1794 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001795 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1796 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001797 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1798 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1799 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001800 } else
1801 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1802 break;
1803 case CCValAssign::BCvt:
1804 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001805 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001806 case CCValAssign::Indirect: {
1807 // Store the argument.
1808 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001809 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001810 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001811 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001812 Arg = SpillSlot;
1813 break;
1814 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001815 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001816
Chris Lattner423c5f42007-02-28 05:31:48 +00001817 if (VA.isRegLoc()) {
1818 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1819 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001820 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001821 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001822 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001823 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1826 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001827 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001828 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001829 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001830
Evan Cheng32fe1032006-05-25 00:59:30 +00001831 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001832 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001833 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001834
Evan Cheng347d5f72006-04-28 21:29:37 +00001835 // Build a sequence of copy-to-reg nodes chained together with token chain
1836 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001837 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001838 // Tail call byval lowering might overwrite argument registers so in case of
1839 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001840 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001841 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001842 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001843 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001844 InFlag = Chain.getValue(1);
1845 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001846
Eric Christopherfd179292009-08-27 18:07:15 +00001847
Chris Lattner88e1fd52009-07-09 04:24:46 +00001848 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001849 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1850 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001852 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1853 DAG.getNode(X86ISD::GlobalBaseReg,
1854 DebugLoc::getUnknownLoc(),
1855 getPointerTy()),
1856 InFlag);
1857 InFlag = Chain.getValue(1);
1858 } else {
1859 // If we are tail calling and generating PIC/GOT style code load the
1860 // address of the callee into ECX. The value in ecx is used as target of
1861 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1862 // for tail calls on PIC/GOT architectures. Normally we would just put the
1863 // address of GOT into ebx and then call target@PLT. But for tail calls
1864 // ebx would be restored (since ebx is callee saved) before jumping to the
1865 // target@PLT.
1866
1867 // Note: The actual moving to ECX is done further down.
1868 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1869 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1870 !G->getGlobal()->hasProtectedVisibility())
1871 Callee = LowerGlobalAddress(Callee, DAG);
1872 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001873 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001874 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001875 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001876
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 if (Is64Bit && isVarArg) {
1878 // From AMD64 ABI document:
1879 // For calls that may call functions that use varargs or stdargs
1880 // (prototype-less calls or calls to functions containing ellipsis (...) in
1881 // the declaration) %al is used as hidden argument to specify the number
1882 // of SSE registers used. The contents of %al do not need to match exactly
1883 // the number of registers, but must be an ubound on the number of SSE
1884 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001885
1886 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001887 // Count the number of XMM registers allocated.
1888 static const unsigned XMMArgRegs[] = {
1889 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1890 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1891 };
1892 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001893 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001894 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001895
Dale Johannesendd64c412009-02-04 00:33:20 +00001896 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001898 InFlag = Chain.getValue(1);
1899 }
1900
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001901
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001902 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 if (isTailCall) {
1904 // Force all the incoming stack arguments to be loaded from the stack
1905 // before any new outgoing arguments are stored to the stack, because the
1906 // outgoing stack slots may alias the incoming argument stack slots, and
1907 // the alias isn't otherwise explicit. This is slightly more conservative
1908 // than necessary, because it means that each store effectively depends
1909 // on every argument instead of just those arguments it would clobber.
1910 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1911
Dan Gohman475871a2008-07-27 21:46:04 +00001912 SmallVector<SDValue, 8> MemOpChains2;
1913 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001914 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001915 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001916 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001917 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1918 CCValAssign &VA = ArgLocs[i];
1919 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001920 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001921 SDValue Arg = Outs[i].Val;
1922 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001923 // Create frame index.
1924 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001926 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001927 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001928
Duncan Sands276dcbd2008-03-21 09:14:45 +00001929 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001930 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001931 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001932 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001933 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001934 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001935 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001936
Dan Gohman98ca4f22009-08-05 01:29:28 +00001937 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1938 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001939 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001940 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001941 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001942 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001943 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001944 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001945 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001946 }
1947 }
1948
1949 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001951 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001952
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001953 // Copy arguments to their registers.
1954 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001956 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001957 InFlag = Chain.getValue(1);
1958 }
Dan Gohman475871a2008-07-27 21:46:04 +00001959 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001960
Gordon Henriksen86737662008-01-05 16:56:59 +00001961 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001962 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001963 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 }
1965
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00001966 bool WasGlobalOrExternal = false;
1967 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
1968 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
1969 // In the 64-bit large code model, we have to make all calls
1970 // through a register, since the call instruction's 32-bit
1971 // pc-relative offset may not be large enough to hold the whole
1972 // address.
1973 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1974 WasGlobalOrExternal = true;
1975 // If the callee is a GlobalAddress node (quite common, every direct call
1976 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
1977 // it.
1978
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001979 // We should use extra load for direct calls to dllimported functions in
1980 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001981 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001982 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001983 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00001984
Chris Lattner48a7d022009-07-09 05:02:21 +00001985 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1986 // external symbols most go through the PLT in PIC mode. If the symbol
1987 // has hidden or protected visibility, or if it is static or local, then
1988 // we don't need to use the PLT - we can directly call it.
1989 if (Subtarget->isTargetELF() &&
1990 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001991 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001992 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001993 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001994 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1995 Subtarget->getDarwinVers() < 9) {
1996 // PC-relative references to external symbols should go through $stub,
1997 // unless we're building with the leopard linker or later, which
1998 // automatically synthesizes these stubs.
1999 OpFlags = X86II::MO_DARWIN_STUB;
2000 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002001
Chris Lattner74e726e2009-07-09 05:27:35 +00002002 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002003 G->getOffset(), OpFlags);
2004 }
Bill Wendling056292f2008-09-16 21:48:12 +00002005 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002006 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002007 unsigned char OpFlags = 0;
2008
2009 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2010 // symbols should go through the PLT.
2011 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002012 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002013 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002014 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002015 Subtarget->getDarwinVers() < 9) {
2016 // PC-relative references to external symbols should go through $stub,
2017 // unless we're building with the leopard linker or later, which
2018 // automatically synthesizes these stubs.
2019 OpFlags = X86II::MO_DARWIN_STUB;
2020 }
Eric Christopherfd179292009-08-27 18:07:15 +00002021
Chris Lattner48a7d022009-07-09 05:02:21 +00002022 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2023 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002024 }
2025
2026 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002027 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002028
Dale Johannesendd64c412009-02-04 00:33:20 +00002029 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002030 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002031 Callee,InFlag);
2032 Callee = DAG.getRegister(Opc, getPointerTy());
2033 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002034 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002035 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002036
Chris Lattnerd96d0722007-02-25 06:40:16 +00002037 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002038 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002040
Dan Gohman98ca4f22009-08-05 01:29:28 +00002041 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002042 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2043 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002044 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002046
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002047 Ops.push_back(Chain);
2048 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002049
Dan Gohman98ca4f22009-08-05 01:29:28 +00002050 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002051 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002052
Gordon Henriksen86737662008-01-05 16:56:59 +00002053 // Add argument registers to the end of the list so that they are known live
2054 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002055 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2056 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2057 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Evan Cheng586ccac2008-03-18 23:36:35 +00002059 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002061 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2062
2063 // Add an implicit use of AL for x86 vararg functions.
2064 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002065 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002066
Gabor Greifba36cb52008-08-28 21:40:38 +00002067 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002068 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002069
Dan Gohman98ca4f22009-08-05 01:29:28 +00002070 if (isTailCall) {
2071 // If this is the first return lowered for this function, add the regs
2072 // to the liveout set for the function.
2073 if (MF.getRegInfo().liveout_empty()) {
2074 SmallVector<CCValAssign, 16> RVLocs;
2075 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2076 *DAG.getContext());
2077 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2078 for (unsigned i = 0; i != RVLocs.size(); ++i)
2079 if (RVLocs[i].isRegLoc())
2080 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2081 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002082
Dan Gohman98ca4f22009-08-05 01:29:28 +00002083 assert(((Callee.getOpcode() == ISD::Register &&
2084 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002085 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002086 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2087 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002088 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002089
2090 return DAG.getNode(X86ISD::TC_RETURN, dl,
2091 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 }
2093
Dale Johannesenace16102009-02-03 19:33:06 +00002094 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002095 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002096
Chris Lattner2d297092006-05-23 18:50:38 +00002097 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002098 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002099 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002102 // If this is is a call to a struct-return function, the callee
2103 // pops the hidden struct pointer, so we have to push it back.
2104 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002105 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002106 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002107 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002108
Gordon Henriksenae636f82008-01-03 16:47:34 +00002109 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002110 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002111 DAG.getIntPtrConstant(NumBytes, true),
2112 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2113 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002114 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002115 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002116
Chris Lattner3085e152007-02-25 08:59:22 +00002117 // Handle result values, copying them out of physregs into vregs that we
2118 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002119 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2120 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002121}
2122
Evan Cheng25ab6902006-09-08 06:48:29 +00002123
2124//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002125// Fast Calling Convention (tail call) implementation
2126//===----------------------------------------------------------------------===//
2127
2128// Like std call, callee cleans arguments, convention except that ECX is
2129// reserved for storing the tail called function address. Only 2 registers are
2130// free for argument passing (inreg). Tail call optimization is performed
2131// provided:
2132// * tailcallopt is enabled
2133// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002134// On X86_64 architecture with GOT-style position independent code only local
2135// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002136// To keep the stack aligned according to platform abi the function
2137// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2138// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002139// If a tail called function callee has more arguments than the caller the
2140// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002141// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002142// original REtADDR, but before the saved framepointer or the spilled registers
2143// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2144// stack layout:
2145// arg1
2146// arg2
2147// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002148// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002149// move area ]
2150// (possible EBP)
2151// ESI
2152// EDI
2153// local1 ..
2154
2155/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2156/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002157unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002158 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002159 MachineFunction &MF = DAG.getMachineFunction();
2160 const TargetMachine &TM = MF.getTarget();
2161 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2162 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002163 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002164 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002165 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002166 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2167 // Number smaller than 12 so just add the difference.
2168 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2169 } else {
2170 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002171 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002172 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002173 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002174 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002175}
2176
Dan Gohman98ca4f22009-08-05 01:29:28 +00002177/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2178/// for tail call optimization. Targets which want to do tail call
2179/// optimization should implement this function.
2180bool
2181X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002182 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002183 bool isVarArg,
2184 const SmallVectorImpl<ISD::InputArg> &Ins,
2185 SelectionDAG& DAG) const {
2186 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002187 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002188 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002189}
2190
Dan Gohman3df24e62008-09-03 23:12:08 +00002191FastISel *
2192X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002193 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002194 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002195 DenseMap<const Value *, unsigned> &vm,
2196 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002197 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002198 DenseMap<const AllocaInst *, int> &am
2199#ifndef NDEBUG
2200 , SmallSet<Instruction*, 8> &cil
2201#endif
2202 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002203 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002204#ifndef NDEBUG
2205 , cil
2206#endif
2207 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002208}
2209
2210
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002211//===----------------------------------------------------------------------===//
2212// Other Lowering Hooks
2213//===----------------------------------------------------------------------===//
2214
2215
Dan Gohman475871a2008-07-27 21:46:04 +00002216SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002217 MachineFunction &MF = DAG.getMachineFunction();
2218 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2219 int ReturnAddrIndex = FuncInfo->getRAIndex();
2220
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002221 if (ReturnAddrIndex == 0) {
2222 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002223 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002224 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2225 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002226 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002227 }
2228
Evan Cheng25ab6902006-09-08 06:48:29 +00002229 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002230}
2231
2232
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002233bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2234 bool hasSymbolicDisplacement) {
2235 // Offset should fit into 32 bit immediate field.
2236 if (!isInt32(Offset))
2237 return false;
2238
2239 // If we don't have a symbolic displacement - we don't have any extra
2240 // restrictions.
2241 if (!hasSymbolicDisplacement)
2242 return true;
2243
2244 // FIXME: Some tweaks might be needed for medium code model.
2245 if (M != CodeModel::Small && M != CodeModel::Kernel)
2246 return false;
2247
2248 // For small code model we assume that latest object is 16MB before end of 31
2249 // bits boundary. We may also accept pretty large negative constants knowing
2250 // that all objects are in the positive half of address space.
2251 if (M == CodeModel::Small && Offset < 16*1024*1024)
2252 return true;
2253
2254 // For kernel code model we know that all object resist in the negative half
2255 // of 32bits address space. We may not accept negative offsets, since they may
2256 // be just off and we may accept pretty large positive ones.
2257 if (M == CodeModel::Kernel && Offset > 0)
2258 return true;
2259
2260 return false;
2261}
2262
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002263/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2264/// specific condition code, returning the condition code and the LHS/RHS of the
2265/// comparison to make.
2266static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2267 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002268 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002269 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2270 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2271 // X > -1 -> X == 0, jump !sign.
2272 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002273 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002274 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2275 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002276 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002277 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002278 // X < 1 -> X <= 0
2279 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002280 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002281 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002282 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002283
Evan Chengd9558e02006-01-06 00:43:03 +00002284 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002285 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002286 case ISD::SETEQ: return X86::COND_E;
2287 case ISD::SETGT: return X86::COND_G;
2288 case ISD::SETGE: return X86::COND_GE;
2289 case ISD::SETLT: return X86::COND_L;
2290 case ISD::SETLE: return X86::COND_LE;
2291 case ISD::SETNE: return X86::COND_NE;
2292 case ISD::SETULT: return X86::COND_B;
2293 case ISD::SETUGT: return X86::COND_A;
2294 case ISD::SETULE: return X86::COND_BE;
2295 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002296 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002297 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002298
Chris Lattner4c78e022008-12-23 23:42:27 +00002299 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002300
Chris Lattner4c78e022008-12-23 23:42:27 +00002301 // If LHS is a foldable load, but RHS is not, flip the condition.
2302 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2303 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2304 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2305 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002306 }
2307
Chris Lattner4c78e022008-12-23 23:42:27 +00002308 switch (SetCCOpcode) {
2309 default: break;
2310 case ISD::SETOLT:
2311 case ISD::SETOLE:
2312 case ISD::SETUGT:
2313 case ISD::SETUGE:
2314 std::swap(LHS, RHS);
2315 break;
2316 }
2317
2318 // On a floating point condition, the flags are set as follows:
2319 // ZF PF CF op
2320 // 0 | 0 | 0 | X > Y
2321 // 0 | 0 | 1 | X < Y
2322 // 1 | 0 | 0 | X == Y
2323 // 1 | 1 | 1 | unordered
2324 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002325 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002326 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002327 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002328 case ISD::SETOLT: // flipped
2329 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002330 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002331 case ISD::SETOLE: // flipped
2332 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002333 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002334 case ISD::SETUGT: // flipped
2335 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002336 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002337 case ISD::SETUGE: // flipped
2338 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002339 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002340 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002341 case ISD::SETNE: return X86::COND_NE;
2342 case ISD::SETUO: return X86::COND_P;
2343 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002344 case ISD::SETOEQ:
2345 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002346 }
Evan Chengd9558e02006-01-06 00:43:03 +00002347}
2348
Evan Cheng4a460802006-01-11 00:33:36 +00002349/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2350/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002351/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002352static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002353 switch (X86CC) {
2354 default:
2355 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002356 case X86::COND_B:
2357 case X86::COND_BE:
2358 case X86::COND_E:
2359 case X86::COND_P:
2360 case X86::COND_A:
2361 case X86::COND_AE:
2362 case X86::COND_NE:
2363 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002364 return true;
2365 }
2366}
2367
Evan Chengeb2f9692009-10-27 19:56:55 +00002368/// isFPImmLegal - Returns true if the target can instruction select the
2369/// specified FP immediate natively. If false, the legalizer will
2370/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002371bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002372 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2373 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2374 return true;
2375 }
2376 return false;
2377}
2378
Nate Begeman9008ca62009-04-27 18:41:29 +00002379/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2380/// the specified range (L, H].
2381static bool isUndefOrInRange(int Val, int Low, int Hi) {
2382 return (Val < 0) || (Val >= Low && Val < Hi);
2383}
2384
2385/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2386/// specified value.
2387static bool isUndefOrEqual(int Val, int CmpVal) {
2388 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002389 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002390 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002391}
2392
Nate Begeman9008ca62009-04-27 18:41:29 +00002393/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2394/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2395/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002396static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002397 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002398 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002400 return (Mask[0] < 2 && Mask[1] < 2);
2401 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002402}
2403
Nate Begeman9008ca62009-04-27 18:41:29 +00002404bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002405 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002406 N->getMask(M);
2407 return ::isPSHUFDMask(M, N->getValueType(0));
2408}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002409
Nate Begeman9008ca62009-04-27 18:41:29 +00002410/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2411/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002412static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002413 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002414 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002415
Nate Begeman9008ca62009-04-27 18:41:29 +00002416 // Lower quadword copied in order or undef.
2417 for (int i = 0; i != 4; ++i)
2418 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002419 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002420
Evan Cheng506d3df2006-03-29 23:07:14 +00002421 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002422 for (int i = 4; i != 8; ++i)
2423 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002424 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002425
Evan Cheng506d3df2006-03-29 23:07:14 +00002426 return true;
2427}
2428
Nate Begeman9008ca62009-04-27 18:41:29 +00002429bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002430 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002431 N->getMask(M);
2432 return ::isPSHUFHWMask(M, N->getValueType(0));
2433}
Evan Cheng506d3df2006-03-29 23:07:14 +00002434
Nate Begeman9008ca62009-04-27 18:41:29 +00002435/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2436/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002437static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002438 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002439 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002440
Rafael Espindola15684b22009-04-24 12:40:33 +00002441 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 for (int i = 4; i != 8; ++i)
2443 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002444 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002445
Rafael Espindola15684b22009-04-24 12:40:33 +00002446 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002447 for (int i = 0; i != 4; ++i)
2448 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002449 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002450
Rafael Espindola15684b22009-04-24 12:40:33 +00002451 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002452}
2453
Nate Begeman9008ca62009-04-27 18:41:29 +00002454bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002455 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002456 N->getMask(M);
2457 return ::isPSHUFLWMask(M, N->getValueType(0));
2458}
2459
Nate Begemana09008b2009-10-19 02:17:23 +00002460/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2461/// is suitable for input to PALIGNR.
2462static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2463 bool hasSSSE3) {
2464 int i, e = VT.getVectorNumElements();
2465
2466 // Do not handle v2i64 / v2f64 shuffles with palignr.
2467 if (e < 4 || !hasSSSE3)
2468 return false;
2469
2470 for (i = 0; i != e; ++i)
2471 if (Mask[i] >= 0)
2472 break;
2473
2474 // All undef, not a palignr.
2475 if (i == e)
2476 return false;
2477
2478 // Determine if it's ok to perform a palignr with only the LHS, since we
2479 // don't have access to the actual shuffle elements to see if RHS is undef.
2480 bool Unary = Mask[i] < (int)e;
2481 bool NeedsUnary = false;
2482
2483 int s = Mask[i] - i;
2484
2485 // Check the rest of the elements to see if they are consecutive.
2486 for (++i; i != e; ++i) {
2487 int m = Mask[i];
2488 if (m < 0)
2489 continue;
2490
2491 Unary = Unary && (m < (int)e);
2492 NeedsUnary = NeedsUnary || (m < s);
2493
2494 if (NeedsUnary && !Unary)
2495 return false;
2496 if (Unary && m != ((s+i) & (e-1)))
2497 return false;
2498 if (!Unary && m != (s+i))
2499 return false;
2500 }
2501 return true;
2502}
2503
2504bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2505 SmallVector<int, 8> M;
2506 N->getMask(M);
2507 return ::isPALIGNRMask(M, N->getValueType(0), true);
2508}
2509
Evan Cheng14aed5e2006-03-24 01:18:28 +00002510/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2511/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002512static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002513 int NumElems = VT.getVectorNumElements();
2514 if (NumElems != 2 && NumElems != 4)
2515 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002516
Nate Begeman9008ca62009-04-27 18:41:29 +00002517 int Half = NumElems / 2;
2518 for (int i = 0; i < Half; ++i)
2519 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002520 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002521 for (int i = Half; i < NumElems; ++i)
2522 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002523 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002524
Evan Cheng14aed5e2006-03-24 01:18:28 +00002525 return true;
2526}
2527
Nate Begeman9008ca62009-04-27 18:41:29 +00002528bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2529 SmallVector<int, 8> M;
2530 N->getMask(M);
2531 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002532}
2533
Evan Cheng213d2cf2007-05-17 18:45:50 +00002534/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002535/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2536/// half elements to come from vector 1 (which would equal the dest.) and
2537/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002538static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002540
2541 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002542 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002543
Nate Begeman9008ca62009-04-27 18:41:29 +00002544 int Half = NumElems / 2;
2545 for (int i = 0; i < Half; ++i)
2546 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002547 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002548 for (int i = Half; i < NumElems; ++i)
2549 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002550 return false;
2551 return true;
2552}
2553
Nate Begeman9008ca62009-04-27 18:41:29 +00002554static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2555 SmallVector<int, 8> M;
2556 N->getMask(M);
2557 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002558}
2559
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002560/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2561/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002562bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2563 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002564 return false;
2565
Evan Cheng2064a2b2006-03-28 06:50:32 +00002566 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002567 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2568 isUndefOrEqual(N->getMaskElt(1), 7) &&
2569 isUndefOrEqual(N->getMaskElt(2), 2) &&
2570 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002571}
2572
Nate Begeman0b10b912009-11-07 23:17:15 +00002573/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2574/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2575/// <2, 3, 2, 3>
2576bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2577 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2578
2579 if (NumElems != 4)
2580 return false;
2581
2582 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2583 isUndefOrEqual(N->getMaskElt(1), 3) &&
2584 isUndefOrEqual(N->getMaskElt(2), 2) &&
2585 isUndefOrEqual(N->getMaskElt(3), 3);
2586}
2587
Evan Cheng5ced1d82006-04-06 23:23:56 +00002588/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2589/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002590bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2591 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002592
Evan Cheng5ced1d82006-04-06 23:23:56 +00002593 if (NumElems != 2 && NumElems != 4)
2594 return false;
2595
Evan Chengc5cdff22006-04-07 21:53:05 +00002596 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002597 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002598 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002599
Evan Chengc5cdff22006-04-07 21:53:05 +00002600 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002602 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002603
2604 return true;
2605}
2606
Nate Begeman0b10b912009-11-07 23:17:15 +00002607/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2608/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2609bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002610 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002611
Evan Cheng5ced1d82006-04-06 23:23:56 +00002612 if (NumElems != 2 && NumElems != 4)
2613 return false;
2614
Evan Chengc5cdff22006-04-07 21:53:05 +00002615 for (unsigned i = 0; i < NumElems/2; ++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
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 for (unsigned i = 0; i < NumElems/2; ++i)
2620 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002621 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002622
2623 return true;
2624}
2625
Evan Cheng0038e592006-03-28 00:39:58 +00002626/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2627/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002628static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002629 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002631 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002632 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002633
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2635 int BitI = Mask[i];
2636 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002637 if (!isUndefOrEqual(BitI, j))
2638 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002639 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002640 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002641 return false;
2642 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002643 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002644 return false;
2645 }
Evan Cheng0038e592006-03-28 00:39:58 +00002646 }
Evan Cheng0038e592006-03-28 00:39:58 +00002647 return true;
2648}
2649
Nate Begeman9008ca62009-04-27 18:41:29 +00002650bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2651 SmallVector<int, 8> M;
2652 N->getMask(M);
2653 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002654}
2655
Evan Cheng4fcb9222006-03-28 02:43:26 +00002656/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2657/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002658static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002659 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002660 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002661 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002662 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002663
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2665 int BitI = Mask[i];
2666 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002667 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002668 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002669 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002670 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002671 return false;
2672 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002673 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002674 return false;
2675 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002676 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002677 return true;
2678}
2679
Nate Begeman9008ca62009-04-27 18:41:29 +00002680bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2681 SmallVector<int, 8> M;
2682 N->getMask(M);
2683 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002684}
2685
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002686/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2687/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2688/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002689static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002690 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002691 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002692 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002693
Nate Begeman9008ca62009-04-27 18:41:29 +00002694 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2695 int BitI = Mask[i];
2696 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002697 if (!isUndefOrEqual(BitI, j))
2698 return false;
2699 if (!isUndefOrEqual(BitI1, j))
2700 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002701 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002702 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002703}
2704
Nate Begeman9008ca62009-04-27 18:41:29 +00002705bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2706 SmallVector<int, 8> M;
2707 N->getMask(M);
2708 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2709}
2710
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002711/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2712/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2713/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002714static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002715 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002716 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2717 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002718
Nate Begeman9008ca62009-04-27 18:41:29 +00002719 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2720 int BitI = Mask[i];
2721 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002722 if (!isUndefOrEqual(BitI, j))
2723 return false;
2724 if (!isUndefOrEqual(BitI1, j))
2725 return false;
2726 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002727 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002728}
2729
Nate Begeman9008ca62009-04-27 18:41:29 +00002730bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2731 SmallVector<int, 8> M;
2732 N->getMask(M);
2733 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2734}
2735
Evan Cheng017dcc62006-04-21 01:05:10 +00002736/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2737/// specifies a shuffle of elements that is suitable for input to MOVSS,
2738/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002739static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002740 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002741 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002742
2743 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002744
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002746 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002747
Nate Begeman9008ca62009-04-27 18:41:29 +00002748 for (int i = 1; i < NumElts; ++i)
2749 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002750 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002751
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002752 return true;
2753}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002754
Nate Begeman9008ca62009-04-27 18:41:29 +00002755bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2756 SmallVector<int, 8> M;
2757 N->getMask(M);
2758 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002759}
2760
Evan Cheng017dcc62006-04-21 01:05:10 +00002761/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2762/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002763/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002764static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002765 bool V2IsSplat = false, bool V2IsUndef = false) {
2766 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002767 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002768 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002769
Nate Begeman9008ca62009-04-27 18:41:29 +00002770 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002771 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002772
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 for (int i = 1; i < NumOps; ++i)
2774 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2775 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2776 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002777 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002778
Evan Cheng39623da2006-04-20 08:58:49 +00002779 return true;
2780}
2781
Nate Begeman9008ca62009-04-27 18:41:29 +00002782static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002783 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002784 SmallVector<int, 8> M;
2785 N->getMask(M);
2786 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002787}
2788
Evan Chengd9539472006-04-14 21:59:03 +00002789/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2790/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002791bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2792 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002793 return false;
2794
2795 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002796 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 int Elt = N->getMaskElt(i);
2798 if (Elt >= 0 && Elt != 1)
2799 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002800 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002801
2802 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002803 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002804 int Elt = N->getMaskElt(i);
2805 if (Elt >= 0 && Elt != 3)
2806 return false;
2807 if (Elt == 3)
2808 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002809 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002810 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002811 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002812 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002813}
2814
2815/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2816/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002817bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2818 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002819 return false;
2820
2821 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 for (unsigned i = 0; i < 2; ++i)
2823 if (N->getMaskElt(i) > 0)
2824 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002825
2826 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002827 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 int Elt = N->getMaskElt(i);
2829 if (Elt >= 0 && Elt != 2)
2830 return false;
2831 if (Elt == 2)
2832 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002833 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002834 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002835 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002836}
2837
Evan Cheng0b457f02008-09-25 20:50:48 +00002838/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2839/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002840bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2841 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 for (int i = 0; i < e; ++i)
2844 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002845 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002846 for (int i = 0; i < e; ++i)
2847 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002848 return false;
2849 return true;
2850}
2851
Evan Cheng63d33002006-03-22 08:01:21 +00002852/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002853/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002854unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2856 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2857
Evan Chengb9df0ca2006-03-22 02:53:00 +00002858 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2859 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 for (int i = 0; i < NumOperands; ++i) {
2861 int Val = SVOp->getMaskElt(NumOperands-i-1);
2862 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002863 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002864 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002865 if (i != NumOperands - 1)
2866 Mask <<= Shift;
2867 }
Evan Cheng63d33002006-03-22 08:01:21 +00002868 return Mask;
2869}
2870
Evan Cheng506d3df2006-03-29 23:07:14 +00002871/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002872/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002873unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002875 unsigned Mask = 0;
2876 // 8 nodes, but we only care about the last 4.
2877 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 int Val = SVOp->getMaskElt(i);
2879 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002880 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002881 if (i != 4)
2882 Mask <<= 2;
2883 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002884 return Mask;
2885}
2886
2887/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002888/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002889unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002890 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002891 unsigned Mask = 0;
2892 // 8 nodes, but we only care about the first 4.
2893 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002894 int Val = SVOp->getMaskElt(i);
2895 if (Val >= 0)
2896 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002897 if (i != 0)
2898 Mask <<= 2;
2899 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002900 return Mask;
2901}
2902
Nate Begemana09008b2009-10-19 02:17:23 +00002903/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2904/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2905unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2906 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2907 EVT VVT = N->getValueType(0);
2908 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2909 int Val = 0;
2910
2911 unsigned i, e;
2912 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2913 Val = SVOp->getMaskElt(i);
2914 if (Val >= 0)
2915 break;
2916 }
2917 return (Val - i) * EltSize;
2918}
2919
Evan Cheng37b73872009-07-30 08:33:02 +00002920/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2921/// constant +0.0.
2922bool X86::isZeroNode(SDValue Elt) {
2923 return ((isa<ConstantSDNode>(Elt) &&
2924 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2925 (isa<ConstantFPSDNode>(Elt) &&
2926 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2927}
2928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2930/// their permute mask.
2931static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2932 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002933 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002934 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002936
Nate Begeman5a5ca152009-04-29 05:20:52 +00002937 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 int idx = SVOp->getMaskElt(i);
2939 if (idx < 0)
2940 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002941 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002942 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002943 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002945 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2947 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002948}
2949
Evan Cheng779ccea2007-12-07 21:30:01 +00002950/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2951/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00002952static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002953 unsigned NumElems = VT.getVectorNumElements();
2954 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 int idx = Mask[i];
2956 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002957 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002958 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002960 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002962 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002963}
2964
Evan Cheng533a0aa2006-04-19 20:35:22 +00002965/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2966/// match movhlps. The lower half elements should come from upper half of
2967/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002968/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002969static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2970 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002971 return false;
2972 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002973 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002974 return false;
2975 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002977 return false;
2978 return true;
2979}
2980
Evan Cheng5ced1d82006-04-06 23:23:56 +00002981/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002982/// is promoted to a vector. It also returns the LoadSDNode by reference if
2983/// required.
2984static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002985 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2986 return false;
2987 N = N->getOperand(0).getNode();
2988 if (!ISD::isNON_EXTLoad(N))
2989 return false;
2990 if (LD)
2991 *LD = cast<LoadSDNode>(N);
2992 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002993}
2994
Evan Cheng533a0aa2006-04-19 20:35:22 +00002995/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2996/// match movlp{s|d}. The lower half elements should come from lower half of
2997/// V1 (and in order), and the upper half elements should come from the upper
2998/// half of V2 (and in order). And since V1 will become the source of the
2999/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003000static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3001 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003002 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003003 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003004 // Is V2 is a vector load, don't do this transformation. We will try to use
3005 // load folding shufps op.
3006 if (ISD::isNON_EXTLoad(V2))
3007 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003008
Nate Begeman5a5ca152009-04-29 05:20:52 +00003009 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003010
Evan Cheng533a0aa2006-04-19 20:35:22 +00003011 if (NumElems != 2 && NumElems != 4)
3012 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003013 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003015 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003016 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003017 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003018 return false;
3019 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003020}
3021
Evan Cheng39623da2006-04-20 08:58:49 +00003022/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3023/// all the same.
3024static bool isSplatVector(SDNode *N) {
3025 if (N->getOpcode() != ISD::BUILD_VECTOR)
3026 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003027
Dan Gohman475871a2008-07-27 21:46:04 +00003028 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003029 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3030 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003031 return false;
3032 return true;
3033}
3034
Evan Cheng213d2cf2007-05-17 18:45:50 +00003035/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003036/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003037/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003038static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003039 SDValue V1 = N->getOperand(0);
3040 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003041 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3042 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003044 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003045 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003046 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3047 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003048 if (Opc != ISD::BUILD_VECTOR ||
3049 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 return false;
3051 } else if (Idx >= 0) {
3052 unsigned Opc = V1.getOpcode();
3053 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3054 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003055 if (Opc != ISD::BUILD_VECTOR ||
3056 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003057 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003058 }
3059 }
3060 return true;
3061}
3062
3063/// getZeroVector - Returns a vector of specified type with all zero elements.
3064///
Owen Andersone50ed302009-08-10 22:56:29 +00003065static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003066 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003067 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003068
Chris Lattner8a594482007-11-25 00:24:49 +00003069 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3070 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003071 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003072 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3074 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003075 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3077 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003078 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3080 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003081 }
Dale Johannesenace16102009-02-03 19:33:06 +00003082 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003083}
3084
Chris Lattner8a594482007-11-25 00:24:49 +00003085/// getOnesVector - Returns a vector of specified type with all bits set.
3086///
Owen Andersone50ed302009-08-10 22:56:29 +00003087static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003089
Chris Lattner8a594482007-11-25 00:24:49 +00003090 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3091 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003092 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003093 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003094 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003096 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003097 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003098 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003099}
3100
3101
Evan Cheng39623da2006-04-20 08:58:49 +00003102/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3103/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003104static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003105 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003106 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003107
Evan Cheng39623da2006-04-20 08:58:49 +00003108 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 SmallVector<int, 8> MaskVec;
3110 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003111
Nate Begeman5a5ca152009-04-29 05:20:52 +00003112 for (unsigned i = 0; i != NumElems; ++i) {
3113 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 MaskVec[i] = NumElems;
3115 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003116 }
Evan Cheng39623da2006-04-20 08:58:49 +00003117 }
Evan Cheng39623da2006-04-20 08:58:49 +00003118 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003119 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3120 SVOp->getOperand(1), &MaskVec[0]);
3121 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003122}
3123
Evan Cheng017dcc62006-04-21 01:05:10 +00003124/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3125/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003126static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003127 SDValue V2) {
3128 unsigned NumElems = VT.getVectorNumElements();
3129 SmallVector<int, 8> Mask;
3130 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003131 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 Mask.push_back(i);
3133 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003134}
3135
Nate Begeman9008ca62009-04-27 18:41:29 +00003136/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003137static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 SDValue V2) {
3139 unsigned NumElems = VT.getVectorNumElements();
3140 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003141 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 Mask.push_back(i);
3143 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003144 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003145 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003146}
3147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003149static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 SDValue V2) {
3151 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003152 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003153 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003154 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 Mask.push_back(i + Half);
3156 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003157 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003159}
3160
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003161/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003162static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 bool HasSSE2) {
3164 if (SV->getValueType(0).getVectorNumElements() <= 4)
3165 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003166
Owen Anderson825b72b2009-08-11 20:47:22 +00003167 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003168 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 DebugLoc dl = SV->getDebugLoc();
3170 SDValue V1 = SV->getOperand(0);
3171 int NumElems = VT.getVectorNumElements();
3172 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003173
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 // unpack elements to the correct location
3175 while (NumElems > 4) {
3176 if (EltNo < NumElems/2) {
3177 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3178 } else {
3179 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3180 EltNo -= NumElems/2;
3181 }
3182 NumElems >>= 1;
3183 }
Eric Christopherfd179292009-08-27 18:07:15 +00003184
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 // Perform the splat.
3186 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003187 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003188 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3189 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003190}
3191
Evan Chengba05f722006-04-21 23:03:30 +00003192/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003193/// vector of zero or undef vector. This produces a shuffle where the low
3194/// element of V2 is swizzled into the zero/undef vector, landing at element
3195/// 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 +00003196static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003197 bool isZero, bool HasSSE2,
3198 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003199 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3202 unsigned NumElems = VT.getVectorNumElements();
3203 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003204 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 // If this is the insertion idx, put the low elt of V2 here.
3206 MaskVec.push_back(i == Idx ? NumElems : i);
3207 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003208}
3209
Evan Chengf26ffe92008-05-29 08:22:04 +00003210/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3211/// a shuffle that is zero.
3212static
Nate Begeman9008ca62009-04-27 18:41:29 +00003213unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3214 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003215 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003217 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 int Idx = SVOp->getMaskElt(Index);
3219 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003220 ++NumZeros;
3221 continue;
3222 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003224 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003225 ++NumZeros;
3226 else
3227 break;
3228 }
3229 return NumZeros;
3230}
3231
3232/// isVectorShift - Returns true if the shuffle can be implemented as a
3233/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003234/// FIXME: split into pslldqi, psrldqi, palignr variants.
3235static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003236 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003238
3239 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003241 if (!NumZeros) {
3242 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003244 if (!NumZeros)
3245 return false;
3246 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003247 bool SeenV1 = false;
3248 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 for (int i = NumZeros; i < NumElems; ++i) {
3250 int Val = isLeft ? (i - NumZeros) : i;
3251 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3252 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003253 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003254 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003255 SeenV1 = true;
3256 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003258 SeenV2 = true;
3259 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003261 return false;
3262 }
3263 if (SeenV1 && SeenV2)
3264 return false;
3265
Nate Begeman9008ca62009-04-27 18:41:29 +00003266 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003267 ShAmt = NumZeros;
3268 return true;
3269}
3270
3271
Evan Chengc78d3b42006-04-24 18:01:45 +00003272/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3273///
Dan Gohman475871a2008-07-27 21:46:04 +00003274static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003275 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003276 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003277 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003278 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003279
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003280 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003281 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003282 bool First = true;
3283 for (unsigned i = 0; i < 16; ++i) {
3284 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3285 if (ThisIsNonZero && First) {
3286 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003288 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003290 First = false;
3291 }
3292
3293 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003294 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003295 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3296 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003297 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003299 }
3300 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3302 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3303 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003304 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003306 } else
3307 ThisElt = LastElt;
3308
Gabor Greifba36cb52008-08-28 21:40:38 +00003309 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003311 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003312 }
3313 }
3314
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003316}
3317
Bill Wendlinga348c562007-03-22 18:42:45 +00003318/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003319///
Dan Gohman475871a2008-07-27 21:46:04 +00003320static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003321 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003322 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003323 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003324 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003325
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003326 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003327 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003328 bool First = true;
3329 for (unsigned i = 0; i < 8; ++i) {
3330 bool isNonZero = (NonZeros & (1 << i)) != 0;
3331 if (isNonZero) {
3332 if (First) {
3333 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003335 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003337 First = false;
3338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003339 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003341 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003342 }
3343 }
3344
3345 return V;
3346}
3347
Evan Chengf26ffe92008-05-29 08:22:04 +00003348/// getVShift - Return a vector logical shift node.
3349///
Owen Andersone50ed302009-08-10 22:56:29 +00003350static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 unsigned NumBits, SelectionDAG &DAG,
3352 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003353 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003355 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003356 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3357 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3358 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003359 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003360}
3361
Dan Gohman475871a2008-07-27 21:46:04 +00003362SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003363X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3364 SelectionDAG &DAG) {
3365
3366 // Check if the scalar load can be widened into a vector load. And if
3367 // the address is "base + cst" see if the cst can be "absorbed" into
3368 // the shuffle mask.
3369 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3370 SDValue Ptr = LD->getBasePtr();
3371 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3372 return SDValue();
3373 EVT PVT = LD->getValueType(0);
3374 if (PVT != MVT::i32 && PVT != MVT::f32)
3375 return SDValue();
3376
3377 int FI = -1;
3378 int64_t Offset = 0;
3379 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3380 FI = FINode->getIndex();
3381 Offset = 0;
3382 } else if (Ptr.getOpcode() == ISD::ADD &&
3383 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3384 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3385 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3386 Offset = Ptr.getConstantOperandVal(1);
3387 Ptr = Ptr.getOperand(0);
3388 } else {
3389 return SDValue();
3390 }
3391
3392 SDValue Chain = LD->getChain();
3393 // Make sure the stack object alignment is at least 16.
3394 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3395 if (DAG.InferPtrAlignment(Ptr) < 16) {
3396 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003397 // Can't change the alignment. FIXME: It's possible to compute
3398 // the exact stack offset and reference FI + adjust offset instead.
3399 // If someone *really* cares about this. That's the way to implement it.
3400 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003401 } else {
3402 MFI->setObjectAlignment(FI, 16);
3403 }
3404 }
3405
3406 // (Offset % 16) must be multiple of 4. Then address is then
3407 // Ptr + (Offset & ~15).
3408 if (Offset < 0)
3409 return SDValue();
3410 if ((Offset % 16) & 3)
3411 return SDValue();
3412 int64_t StartOffset = Offset & ~15;
3413 if (StartOffset)
3414 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3415 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3416
3417 int EltNo = (Offset - StartOffset) >> 2;
3418 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3419 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3420 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3421 // Canonicalize it to a v4i32 shuffle.
3422 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3423 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3424 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3425 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3426 }
3427
3428 return SDValue();
3429}
3430
3431SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003432X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003433 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003434 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003435 if (ISD::isBuildVectorAllZeros(Op.getNode())
3436 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003437 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3438 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3439 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003441 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003442
Gabor Greifba36cb52008-08-28 21:40:38 +00003443 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003444 return getOnesVector(Op.getValueType(), DAG, dl);
3445 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003446 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003447
Owen Andersone50ed302009-08-10 22:56:29 +00003448 EVT VT = Op.getValueType();
3449 EVT ExtVT = VT.getVectorElementType();
3450 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003451
3452 unsigned NumElems = Op.getNumOperands();
3453 unsigned NumZero = 0;
3454 unsigned NumNonZero = 0;
3455 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003456 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003457 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003458 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003460 if (Elt.getOpcode() == ISD::UNDEF)
3461 continue;
3462 Values.insert(Elt);
3463 if (Elt.getOpcode() != ISD::Constant &&
3464 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003465 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003466 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003467 NumZero++;
3468 else {
3469 NonZeros |= (1 << i);
3470 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003471 }
3472 }
3473
Dan Gohman7f321562007-06-25 16:23:39 +00003474 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003475 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003476 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003477 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003478
Chris Lattner67f453a2008-03-09 05:42:06 +00003479 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003480 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003481 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003482 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003483
Chris Lattner62098042008-03-09 01:05:04 +00003484 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3485 // the value are obviously zero, truncate the value to i32 and do the
3486 // insertion that way. Only do this if the value is non-constant or if the
3487 // value is a constant being inserted into element 0. It is cheaper to do
3488 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003490 (!IsAllConstants || Idx == 0)) {
3491 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3492 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3494 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003495
Chris Lattner62098042008-03-09 01:05:04 +00003496 // Truncate the value (which may itself be a constant) to i32, and
3497 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003499 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003500 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3501 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003502
Chris Lattner62098042008-03-09 01:05:04 +00003503 // Now we have our 32-bit value zero extended in the low element of
3504 // a vector. If Idx != 0, swizzle it into place.
3505 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003506 SmallVector<int, 4> Mask;
3507 Mask.push_back(Idx);
3508 for (unsigned i = 1; i != VecElts; ++i)
3509 Mask.push_back(i);
3510 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003511 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003513 }
Dale Johannesenace16102009-02-03 19:33:06 +00003514 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003515 }
3516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003517
Chris Lattner19f79692008-03-08 22:59:52 +00003518 // If we have a constant or non-constant insertion into the low element of
3519 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3520 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003521 // depending on what the source datatype is.
3522 if (Idx == 0) {
3523 if (NumZero == 0) {
3524 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3526 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003527 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3528 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3529 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3530 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3532 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3533 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003534 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3535 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3536 Subtarget->hasSSE2(), DAG);
3537 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3538 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003539 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003540
3541 // Is it a vector logical left shift?
3542 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003543 X86::isZeroNode(Op.getOperand(0)) &&
3544 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003545 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003546 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003547 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003548 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003549 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003550 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003551
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003552 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003553 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003554
Chris Lattner19f79692008-03-08 22:59:52 +00003555 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3556 // is a non-constant being inserted into an element other than the low one,
3557 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3558 // movd/movss) to move this into the low element, then shuffle it into
3559 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003560 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003561 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003562
Evan Cheng0db9fe62006-04-25 20:13:52 +00003563 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003564 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3565 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003566 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003567 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 MaskVec.push_back(i == Idx ? 0 : 1);
3569 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003570 }
3571 }
3572
Chris Lattner67f453a2008-03-09 05:42:06 +00003573 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003574 if (Values.size() == 1) {
3575 if (EVTBits == 32) {
3576 // Instead of a shuffle like this:
3577 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3578 // Check if it's possible to issue this instead.
3579 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3580 unsigned Idx = CountTrailingZeros_32(NonZeros);
3581 SDValue Item = Op.getOperand(Idx);
3582 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3583 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3584 }
Dan Gohman475871a2008-07-27 21:46:04 +00003585 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003587
Dan Gohmana3941172007-07-24 22:55:08 +00003588 // A vector full of immediates; various special cases are already
3589 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003590 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003591 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003592
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003593 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003594 if (EVTBits == 64) {
3595 if (NumNonZero == 1) {
3596 // One half is zero or undef.
3597 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003598 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003599 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003600 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3601 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003602 }
Dan Gohman475871a2008-07-27 21:46:04 +00003603 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003604 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003605
3606 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003607 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003608 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003609 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003610 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611 }
3612
Bill Wendling826f36f2007-03-28 00:57:11 +00003613 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003614 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003615 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003616 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003617 }
3618
3619 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003620 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003621 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003622 if (NumElems == 4 && NumZero > 0) {
3623 for (unsigned i = 0; i < 4; ++i) {
3624 bool isZero = !(NonZeros & (1 << i));
3625 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003626 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003627 else
Dale Johannesenace16102009-02-03 19:33:06 +00003628 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003629 }
3630
3631 for (unsigned i = 0; i < 2; ++i) {
3632 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3633 default: break;
3634 case 0:
3635 V[i] = V[i*2]; // Must be a zero vector.
3636 break;
3637 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003638 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003639 break;
3640 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003641 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003642 break;
3643 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003644 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003645 break;
3646 }
3647 }
3648
Nate Begeman9008ca62009-04-27 18:41:29 +00003649 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003650 bool Reverse = (NonZeros & 0x3) == 2;
3651 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003652 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003653 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3654 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003655 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3656 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003657 }
3658
3659 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003660 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3661 // values to be inserted is equal to the number of elements, in which case
3662 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003663 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003664 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003665 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003666 getSubtarget()->hasSSE41()) {
3667 V[0] = DAG.getUNDEF(VT);
3668 for (unsigned i = 0; i < NumElems; ++i)
3669 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3670 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3671 Op.getOperand(i), DAG.getIntPtrConstant(i));
3672 return V[0];
3673 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 // Expand into a number of unpckl*.
3675 // e.g. for v4f32
3676 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3677 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3678 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003679 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003680 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681 NumElems >>= 1;
3682 while (NumElems != 0) {
3683 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003685 NumElems >>= 1;
3686 }
3687 return V[0];
3688 }
3689
Dan Gohman475871a2008-07-27 21:46:04 +00003690 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003691}
3692
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003693SDValue
3694X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3695 // We support concatenate two MMX registers and place them in a MMX
3696 // register. This is better than doing a stack convert.
3697 DebugLoc dl = Op.getDebugLoc();
3698 EVT ResVT = Op.getValueType();
3699 assert(Op.getNumOperands() == 2);
3700 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3701 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3702 int Mask[2];
3703 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3704 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3705 InVec = Op.getOperand(1);
3706 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3707 unsigned NumElts = ResVT.getVectorNumElements();
3708 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3709 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3710 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3711 } else {
3712 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3713 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3714 Mask[0] = 0; Mask[1] = 2;
3715 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3716 }
3717 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3718}
3719
Nate Begemanb9a47b82009-02-23 08:49:38 +00003720// v8i16 shuffles - Prefer shuffles in the following order:
3721// 1. [all] pshuflw, pshufhw, optional move
3722// 2. [ssse3] 1 x pshufb
3723// 3. [ssse3] 2 x pshufb + 1 x por
3724// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003725static
Nate Begeman9008ca62009-04-27 18:41:29 +00003726SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3727 SelectionDAG &DAG, X86TargetLowering &TLI) {
3728 SDValue V1 = SVOp->getOperand(0);
3729 SDValue V2 = SVOp->getOperand(1);
3730 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003731 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003732
Nate Begemanb9a47b82009-02-23 08:49:38 +00003733 // Determine if more than 1 of the words in each of the low and high quadwords
3734 // of the result come from the same quadword of one of the two inputs. Undef
3735 // mask values count as coming from any quadword, for better codegen.
3736 SmallVector<unsigned, 4> LoQuad(4);
3737 SmallVector<unsigned, 4> HiQuad(4);
3738 BitVector InputQuads(4);
3739 for (unsigned i = 0; i < 8; ++i) {
3740 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003742 MaskVals.push_back(EltIdx);
3743 if (EltIdx < 0) {
3744 ++Quad[0];
3745 ++Quad[1];
3746 ++Quad[2];
3747 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003748 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003749 }
3750 ++Quad[EltIdx / 4];
3751 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003752 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003753
Nate Begemanb9a47b82009-02-23 08:49:38 +00003754 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003755 unsigned MaxQuad = 1;
3756 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003757 if (LoQuad[i] > MaxQuad) {
3758 BestLoQuad = i;
3759 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003760 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003761 }
3762
Nate Begemanb9a47b82009-02-23 08:49:38 +00003763 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003764 MaxQuad = 1;
3765 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003766 if (HiQuad[i] > MaxQuad) {
3767 BestHiQuad = i;
3768 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003769 }
3770 }
3771
Nate Begemanb9a47b82009-02-23 08:49:38 +00003772 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003773 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003774 // single pshufb instruction is necessary. If There are more than 2 input
3775 // quads, disable the next transformation since it does not help SSSE3.
3776 bool V1Used = InputQuads[0] || InputQuads[1];
3777 bool V2Used = InputQuads[2] || InputQuads[3];
3778 if (TLI.getSubtarget()->hasSSSE3()) {
3779 if (InputQuads.count() == 2 && V1Used && V2Used) {
3780 BestLoQuad = InputQuads.find_first();
3781 BestHiQuad = InputQuads.find_next(BestLoQuad);
3782 }
3783 if (InputQuads.count() > 2) {
3784 BestLoQuad = -1;
3785 BestHiQuad = -1;
3786 }
3787 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003788
Nate Begemanb9a47b82009-02-23 08:49:38 +00003789 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3790 // the shuffle mask. If a quad is scored as -1, that means that it contains
3791 // words from all 4 input quadwords.
3792 SDValue NewV;
3793 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003794 SmallVector<int, 8> MaskV;
3795 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3796 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003797 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3799 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3800 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003801
Nate Begemanb9a47b82009-02-23 08:49:38 +00003802 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3803 // source words for the shuffle, to aid later transformations.
3804 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003805 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003806 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003807 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003808 if (idx != (int)i)
3809 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003810 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003811 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003812 AllWordsInNewV = false;
3813 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003814 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003815
Nate Begemanb9a47b82009-02-23 08:49:38 +00003816 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3817 if (AllWordsInNewV) {
3818 for (int i = 0; i != 8; ++i) {
3819 int idx = MaskVals[i];
3820 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003821 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003822 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003823 if ((idx != i) && idx < 4)
3824 pshufhw = false;
3825 if ((idx != i) && idx > 3)
3826 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003827 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003828 V1 = NewV;
3829 V2Used = false;
3830 BestLoQuad = 0;
3831 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003832 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003833
Nate Begemanb9a47b82009-02-23 08:49:38 +00003834 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3835 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003836 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003837 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003839 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003840 }
Eric Christopherfd179292009-08-27 18:07:15 +00003841
Nate Begemanb9a47b82009-02-23 08:49:38 +00003842 // If we have SSSE3, and all words of the result are from 1 input vector,
3843 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3844 // is present, fall back to case 4.
3845 if (TLI.getSubtarget()->hasSSSE3()) {
3846 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003847
Nate Begemanb9a47b82009-02-23 08:49:38 +00003848 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003849 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003850 // mask, and elements that come from V1 in the V2 mask, so that the two
3851 // results can be OR'd together.
3852 bool TwoInputs = V1Used && V2Used;
3853 for (unsigned i = 0; i != 8; ++i) {
3854 int EltIdx = MaskVals[i] * 2;
3855 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3857 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003858 continue;
3859 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3861 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003862 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003863 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003864 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003865 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003866 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003867 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003869
Nate Begemanb9a47b82009-02-23 08:49:38 +00003870 // Calculate the shuffle mask for the second input, shuffle it, and
3871 // OR it with the first shuffled input.
3872 pshufbMask.clear();
3873 for (unsigned i = 0; i != 8; ++i) {
3874 int EltIdx = MaskVals[i] * 2;
3875 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3877 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003878 continue;
3879 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003880 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3881 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003882 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003884 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003885 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 MVT::v16i8, &pshufbMask[0], 16));
3887 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3888 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003889 }
3890
3891 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3892 // and update MaskVals with new element order.
3893 BitVector InOrder(8);
3894 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003896 for (int i = 0; i != 4; ++i) {
3897 int idx = MaskVals[i];
3898 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003900 InOrder.set(i);
3901 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003902 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003903 InOrder.set(i);
3904 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003905 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003906 }
3907 }
3908 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003910 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003912 }
Eric Christopherfd179292009-08-27 18:07:15 +00003913
Nate Begemanb9a47b82009-02-23 08:49:38 +00003914 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3915 // and update MaskVals with the new element order.
3916 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003917 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003918 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003920 for (unsigned i = 4; i != 8; ++i) {
3921 int idx = MaskVals[i];
3922 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003923 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003924 InOrder.set(i);
3925 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003926 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003927 InOrder.set(i);
3928 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003929 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003930 }
3931 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003934 }
Eric Christopherfd179292009-08-27 18:07:15 +00003935
Nate Begemanb9a47b82009-02-23 08:49:38 +00003936 // In case BestHi & BestLo were both -1, which means each quadword has a word
3937 // from each of the four input quadwords, calculate the InOrder bitvector now
3938 // before falling through to the insert/extract cleanup.
3939 if (BestLoQuad == -1 && BestHiQuad == -1) {
3940 NewV = V1;
3941 for (int i = 0; i != 8; ++i)
3942 if (MaskVals[i] < 0 || MaskVals[i] == i)
3943 InOrder.set(i);
3944 }
Eric Christopherfd179292009-08-27 18:07:15 +00003945
Nate Begemanb9a47b82009-02-23 08:49:38 +00003946 // The other elements are put in the right place using pextrw and pinsrw.
3947 for (unsigned i = 0; i != 8; ++i) {
3948 if (InOrder[i])
3949 continue;
3950 int EltIdx = MaskVals[i];
3951 if (EltIdx < 0)
3952 continue;
3953 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003955 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003957 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003959 DAG.getIntPtrConstant(i));
3960 }
3961 return NewV;
3962}
3963
3964// v16i8 shuffles - Prefer shuffles in the following order:
3965// 1. [ssse3] 1 x pshufb
3966// 2. [ssse3] 2 x pshufb + 1 x por
3967// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3968static
Nate Begeman9008ca62009-04-27 18:41:29 +00003969SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3970 SelectionDAG &DAG, X86TargetLowering &TLI) {
3971 SDValue V1 = SVOp->getOperand(0);
3972 SDValue V2 = SVOp->getOperand(1);
3973 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003974 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003975 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00003976
Nate Begemanb9a47b82009-02-23 08:49:38 +00003977 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00003978 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00003979 // present, fall back to case 3.
3980 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3981 bool V1Only = true;
3982 bool V2Only = true;
3983 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003984 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003985 if (EltIdx < 0)
3986 continue;
3987 if (EltIdx < 16)
3988 V2Only = false;
3989 else
3990 V1Only = false;
3991 }
Eric Christopherfd179292009-08-27 18:07:15 +00003992
Nate Begemanb9a47b82009-02-23 08:49:38 +00003993 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3994 if (TLI.getSubtarget()->hasSSSE3()) {
3995 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003996
Nate Begemanb9a47b82009-02-23 08:49:38 +00003997 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00003998 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003999 //
4000 // Otherwise, we have elements from both input vectors, and must zero out
4001 // elements that come from V2 in the first mask, and V1 in the second mask
4002 // so that we can OR them together.
4003 bool TwoInputs = !(V1Only || V2Only);
4004 for (unsigned i = 0; i != 16; ++i) {
4005 int EltIdx = MaskVals[i];
4006 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004007 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004008 continue;
4009 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004010 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004011 }
4012 // If all the elements are from V2, assign it to V1 and return after
4013 // building the first pshufb.
4014 if (V2Only)
4015 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004016 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004017 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004019 if (!TwoInputs)
4020 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004021
Nate Begemanb9a47b82009-02-23 08:49:38 +00004022 // Calculate the shuffle mask for the second input, shuffle it, and
4023 // OR it with the first shuffled input.
4024 pshufbMask.clear();
4025 for (unsigned i = 0; i != 16; ++i) {
4026 int EltIdx = MaskVals[i];
4027 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 continue;
4030 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004031 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004032 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004034 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 MVT::v16i8, &pshufbMask[0], 16));
4036 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004037 }
Eric Christopherfd179292009-08-27 18:07:15 +00004038
Nate Begemanb9a47b82009-02-23 08:49:38 +00004039 // No SSSE3 - Calculate in place words and then fix all out of place words
4040 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4041 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4043 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004044 SDValue NewV = V2Only ? V2 : V1;
4045 for (int i = 0; i != 8; ++i) {
4046 int Elt0 = MaskVals[i*2];
4047 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004048
Nate Begemanb9a47b82009-02-23 08:49:38 +00004049 // This word of the result is all undef, skip it.
4050 if (Elt0 < 0 && Elt1 < 0)
4051 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004052
Nate Begemanb9a47b82009-02-23 08:49:38 +00004053 // This word of the result is already in the correct place, skip it.
4054 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4055 continue;
4056 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4057 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004058
Nate Begemanb9a47b82009-02-23 08:49:38 +00004059 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4060 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4061 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004062
4063 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4064 // using a single extract together, load it and store it.
4065 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004067 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004069 DAG.getIntPtrConstant(i));
4070 continue;
4071 }
4072
Nate Begemanb9a47b82009-02-23 08:49:38 +00004073 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004074 // source byte is not also odd, shift the extracted word left 8 bits
4075 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004076 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004077 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 DAG.getIntPtrConstant(Elt1 / 2));
4079 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004081 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004082 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4084 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 }
4086 // If Elt0 is defined, extract it from the appropriate source. If the
4087 // source byte is not also even, shift the extracted word right 8 bits. If
4088 // Elt1 was also defined, OR the extracted values together before
4089 // inserting them in the result.
4090 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004091 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4093 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004094 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004095 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004096 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4098 DAG.getConstant(0x00FF, MVT::i16));
4099 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004100 : InsElt0;
4101 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004102 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 DAG.getIntPtrConstant(i));
4104 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004106}
4107
Evan Cheng7a831ce2007-12-15 03:00:47 +00004108/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4109/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4110/// done when every pair / quad of shuffle mask elements point to elements in
4111/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004112/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4113static
Nate Begeman9008ca62009-04-27 18:41:29 +00004114SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4115 SelectionDAG &DAG,
4116 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004117 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004118 SDValue V1 = SVOp->getOperand(0);
4119 SDValue V2 = SVOp->getOperand(1);
4120 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004121 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004123 EVT MaskEltVT = MaskVT.getVectorElementType();
4124 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004126 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 case MVT::v4f32: NewVT = MVT::v2f64; break;
4128 case MVT::v4i32: NewVT = MVT::v2i64; break;
4129 case MVT::v8i16: NewVT = MVT::v4i32; break;
4130 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004131 }
4132
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004133 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004134 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004136 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004138 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004139 int Scale = NumElems / NewWidth;
4140 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004141 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004142 int StartIdx = -1;
4143 for (int j = 0; j < Scale; ++j) {
4144 int EltIdx = SVOp->getMaskElt(i+j);
4145 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004146 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004147 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004148 StartIdx = EltIdx - (EltIdx % Scale);
4149 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004150 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004151 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004152 if (StartIdx == -1)
4153 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004154 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004155 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004156 }
4157
Dale Johannesenace16102009-02-03 19:33:06 +00004158 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4159 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004160 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004161}
4162
Evan Chengd880b972008-05-09 21:53:03 +00004163/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004164///
Owen Andersone50ed302009-08-10 22:56:29 +00004165static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 SDValue SrcOp, SelectionDAG &DAG,
4167 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004169 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004170 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004171 LD = dyn_cast<LoadSDNode>(SrcOp);
4172 if (!LD) {
4173 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4174 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004175 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4176 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004177 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4178 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004179 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004180 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004182 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4183 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4184 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4185 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004186 SrcOp.getOperand(0)
4187 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004188 }
4189 }
4190 }
4191
Dale Johannesenace16102009-02-03 19:33:06 +00004192 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4193 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004194 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004195 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004196}
4197
Evan Chengace3c172008-07-22 21:13:36 +00004198/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4199/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004200static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004201LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4202 SDValue V1 = SVOp->getOperand(0);
4203 SDValue V2 = SVOp->getOperand(1);
4204 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004205 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004206
Evan Chengace3c172008-07-22 21:13:36 +00004207 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004208 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 SmallVector<int, 8> Mask1(4U, -1);
4210 SmallVector<int, 8> PermMask;
4211 SVOp->getMask(PermMask);
4212
Evan Chengace3c172008-07-22 21:13:36 +00004213 unsigned NumHi = 0;
4214 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004215 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004216 int Idx = PermMask[i];
4217 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004218 Locs[i] = std::make_pair(-1, -1);
4219 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004220 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4221 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004222 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004223 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004224 NumLo++;
4225 } else {
4226 Locs[i] = std::make_pair(1, NumHi);
4227 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004229 NumHi++;
4230 }
4231 }
4232 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004233
Evan Chengace3c172008-07-22 21:13:36 +00004234 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004235 // If no more than two elements come from either vector. This can be
4236 // implemented with two shuffles. First shuffle gather the elements.
4237 // The second shuffle, which takes the first shuffle as both of its
4238 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004240
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004242
Evan Chengace3c172008-07-22 21:13:36 +00004243 for (unsigned i = 0; i != 4; ++i) {
4244 if (Locs[i].first == -1)
4245 continue;
4246 else {
4247 unsigned Idx = (i < 2) ? 0 : 4;
4248 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004250 }
4251 }
4252
Nate Begeman9008ca62009-04-27 18:41:29 +00004253 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004254 } else if (NumLo == 3 || NumHi == 3) {
4255 // Otherwise, we must have three elements from one vector, call it X, and
4256 // one element from the other, call it Y. First, use a shufps to build an
4257 // intermediate vector with the one element from Y and the element from X
4258 // that will be in the same half in the final destination (the indexes don't
4259 // matter). Then, use a shufps to build the final vector, taking the half
4260 // containing the element from Y from the intermediate, and the other half
4261 // from X.
4262 if (NumHi == 3) {
4263 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004265 std::swap(V1, V2);
4266 }
4267
4268 // Find the element from V2.
4269 unsigned HiIndex;
4270 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 int Val = PermMask[HiIndex];
4272 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004273 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004274 if (Val >= 4)
4275 break;
4276 }
4277
Nate Begeman9008ca62009-04-27 18:41:29 +00004278 Mask1[0] = PermMask[HiIndex];
4279 Mask1[1] = -1;
4280 Mask1[2] = PermMask[HiIndex^1];
4281 Mask1[3] = -1;
4282 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004283
4284 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 Mask1[0] = PermMask[0];
4286 Mask1[1] = PermMask[1];
4287 Mask1[2] = HiIndex & 1 ? 6 : 4;
4288 Mask1[3] = HiIndex & 1 ? 4 : 6;
4289 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004290 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 Mask1[0] = HiIndex & 1 ? 2 : 0;
4292 Mask1[1] = HiIndex & 1 ? 0 : 2;
4293 Mask1[2] = PermMask[2];
4294 Mask1[3] = PermMask[3];
4295 if (Mask1[2] >= 0)
4296 Mask1[2] += 4;
4297 if (Mask1[3] >= 0)
4298 Mask1[3] += 4;
4299 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004300 }
Evan Chengace3c172008-07-22 21:13:36 +00004301 }
4302
4303 // Break it into (shuffle shuffle_hi, shuffle_lo).
4304 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 SmallVector<int,8> LoMask(4U, -1);
4306 SmallVector<int,8> HiMask(4U, -1);
4307
4308 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004309 unsigned MaskIdx = 0;
4310 unsigned LoIdx = 0;
4311 unsigned HiIdx = 2;
4312 for (unsigned i = 0; i != 4; ++i) {
4313 if (i == 2) {
4314 MaskPtr = &HiMask;
4315 MaskIdx = 1;
4316 LoIdx = 0;
4317 HiIdx = 2;
4318 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004319 int Idx = PermMask[i];
4320 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004321 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004323 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004325 LoIdx++;
4326 } else {
4327 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004329 HiIdx++;
4330 }
4331 }
4332
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4334 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4335 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004336 for (unsigned i = 0; i != 4; ++i) {
4337 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004339 } else {
4340 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004341 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004342 }
4343 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004344 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004345}
4346
Dan Gohman475871a2008-07-27 21:46:04 +00004347SDValue
4348X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004350 SDValue V1 = Op.getOperand(0);
4351 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004352 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004353 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004355 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4357 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004358 bool V1IsSplat = false;
4359 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004360
Nate Begeman9008ca62009-04-27 18:41:29 +00004361 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004362 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004363
Nate Begeman9008ca62009-04-27 18:41:29 +00004364 // Promote splats to v4f32.
4365 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004366 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 return Op;
4368 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004369 }
4370
Evan Cheng7a831ce2007-12-15 03:00:47 +00004371 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4372 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004375 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004376 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004377 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004378 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004379 // FIXME: Figure out a cleaner way to do this.
4380 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004381 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004383 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4385 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4386 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004387 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004388 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4390 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004391 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004393 }
4394 }
Eric Christopherfd179292009-08-27 18:07:15 +00004395
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 if (X86::isPSHUFDMask(SVOp))
4397 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004398
Evan Chengf26ffe92008-05-29 08:22:04 +00004399 // Check if this can be converted into a logical shift.
4400 bool isLeft = false;
4401 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004402 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004404 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004405 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004406 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004407 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004408 EVT EltVT = VT.getVectorElementType();
4409 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004410 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004411 }
Eric Christopherfd179292009-08-27 18:07:15 +00004412
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004414 if (V1IsUndef)
4415 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004416 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004417 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004418 if (!isMMX)
4419 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004420 }
Eric Christopherfd179292009-08-27 18:07:15 +00004421
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 // FIXME: fold these into legal mask.
4423 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4424 X86::isMOVSLDUPMask(SVOp) ||
4425 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004426 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004428 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004429
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 if (ShouldXformToMOVHLPS(SVOp) ||
4431 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4432 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004433
Evan Chengf26ffe92008-05-29 08:22:04 +00004434 if (isShift) {
4435 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004436 EVT EltVT = VT.getVectorElementType();
4437 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004438 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004439 }
Eric Christopherfd179292009-08-27 18:07:15 +00004440
Evan Cheng9eca5e82006-10-25 21:49:50 +00004441 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004442 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4443 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004444 V1IsSplat = isSplatVector(V1.getNode());
4445 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004446
Chris Lattner8a594482007-11-25 00:24:49 +00004447 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004448 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 Op = CommuteVectorShuffle(SVOp, DAG);
4450 SVOp = cast<ShuffleVectorSDNode>(Op);
4451 V1 = SVOp->getOperand(0);
4452 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004453 std::swap(V1IsSplat, V2IsSplat);
4454 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004455 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004456 }
4457
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4459 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004460 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004461 return V1;
4462 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4463 // the instruction selector will not match, so get a canonical MOVL with
4464 // swapped operands to undo the commute.
4465 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004466 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004467
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4469 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4470 X86::isUNPCKLMask(SVOp) ||
4471 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004472 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004473
Evan Cheng9bbbb982006-10-25 20:48:19 +00004474 if (V2IsSplat) {
4475 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004476 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004477 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 SDValue NewMask = NormalizeMask(SVOp, DAG);
4479 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4480 if (NSVOp != SVOp) {
4481 if (X86::isUNPCKLMask(NSVOp, true)) {
4482 return NewMask;
4483 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4484 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 }
4486 }
4487 }
4488
Evan Cheng9eca5e82006-10-25 21:49:50 +00004489 if (Commuted) {
4490 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 // FIXME: this seems wrong.
4492 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4493 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4494 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4495 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4496 X86::isUNPCKLMask(NewSVOp) ||
4497 X86::isUNPCKHMask(NewSVOp))
4498 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004499 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500
Nate Begemanb9a47b82009-02-23 08:49:38 +00004501 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004502
4503 // Normalize the node to match x86 shuffle ops if needed
4504 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4505 return CommuteVectorShuffle(SVOp, DAG);
4506
4507 // Check for legal shuffle and return?
4508 SmallVector<int, 16> PermMask;
4509 SVOp->getMask(PermMask);
4510 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004511 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004512
Evan Cheng14b32e12007-12-11 01:46:18 +00004513 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004514 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004516 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004517 return NewOp;
4518 }
4519
Owen Anderson825b72b2009-08-11 20:47:22 +00004520 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004521 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004522 if (NewOp.getNode())
4523 return NewOp;
4524 }
Eric Christopherfd179292009-08-27 18:07:15 +00004525
Evan Chengace3c172008-07-22 21:13:36 +00004526 // Handle all 4 wide cases with a number of shuffles except for MMX.
4527 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004528 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004529
Dan Gohman475871a2008-07-27 21:46:04 +00004530 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004531}
4532
Dan Gohman475871a2008-07-27 21:46:04 +00004533SDValue
4534X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004535 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004536 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004537 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004538 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004539 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004540 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004541 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004542 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004543 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004544 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004545 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4546 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4547 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004548 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4549 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004550 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004551 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004552 Op.getOperand(0)),
4553 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, 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);
Owen Anderson825b72b2009-08-11 20:47:22 +00004559 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004560 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4561 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004562 // result has a single use which is a store or a bitcast to i32. And in
4563 // the case of a store, it's not worth it if the index is a constant 0,
4564 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004565 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004566 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004567 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004568 if ((User->getOpcode() != ISD::STORE ||
4569 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4570 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004571 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004572 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004573 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004574 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4575 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004576 Op.getOperand(0)),
4577 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004578 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4579 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004580 // ExtractPS works with constant index.
4581 if (isa<ConstantSDNode>(Op.getOperand(1)))
4582 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004583 }
Dan Gohman475871a2008-07-27 21:46:04 +00004584 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004585}
4586
4587
Dan Gohman475871a2008-07-27 21:46:04 +00004588SDValue
4589X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004591 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592
Evan Cheng62a3f152008-03-24 21:52:23 +00004593 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004595 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004596 return Res;
4597 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004598
Owen Andersone50ed302009-08-10 22:56:29 +00004599 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004600 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004601 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004602 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004604 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004605 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4607 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004608 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004609 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004610 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004611 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004612 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004613 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004615 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004617 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004618 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004619 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 if (Idx == 0)
4621 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004622
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004625 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004626 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004627 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004628 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004629 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004630 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004631 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4632 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4633 // to match extract_elt for f64.
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;
4637
4638 // UNPCKHPD the element to the lowest double word, then movsd.
4639 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4640 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004641 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004642 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004643 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004644 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004645 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004646 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 }
4648
Dan Gohman475871a2008-07-27 21:46:04 +00004649 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004650}
4651
Dan Gohman475871a2008-07-27 21:46:04 +00004652SDValue
4653X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004654 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004655 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004656 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004657
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue N0 = Op.getOperand(0);
4659 SDValue N1 = Op.getOperand(1);
4660 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004661
Dan Gohman8a55ce42009-09-23 21:02:20 +00004662 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004663 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004664 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4665 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004666 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4667 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004668 if (N1.getValueType() != MVT::i32)
4669 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4670 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004671 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004672 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004673 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004674 // Bits [7:6] of the constant are the source select. This will always be
4675 // zero here. The DAG Combiner may combine an extract_elt index into these
4676 // bits. For example (insert (extract, 3), 2) could be matched by putting
4677 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004678 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004679 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004680 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004681 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004682 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004683 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004685 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004686 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004687 // PINSR* works with constant index.
4688 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004689 }
Dan Gohman475871a2008-07-27 21:46:04 +00004690 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004691}
4692
Dan Gohman475871a2008-07-27 21:46:04 +00004693SDValue
4694X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004695 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004696 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004697
4698 if (Subtarget->hasSSE41())
4699 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4700
Dan Gohman8a55ce42009-09-23 21:02:20 +00004701 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004702 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004703
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004704 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004705 SDValue N0 = Op.getOperand(0);
4706 SDValue N1 = Op.getOperand(1);
4707 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004708
Dan Gohman8a55ce42009-09-23 21:02:20 +00004709 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004710 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4711 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 if (N1.getValueType() != MVT::i32)
4713 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4714 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004715 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004716 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004717 }
Dan Gohman475871a2008-07-27 21:46:04 +00004718 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004719}
4720
Dan Gohman475871a2008-07-27 21:46:04 +00004721SDValue
4722X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004723 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004724 if (Op.getValueType() == MVT::v2f32)
4725 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4726 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4727 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004728 Op.getOperand(0))));
4729
Owen Anderson825b72b2009-08-11 20:47:22 +00004730 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4731 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004732
Owen Anderson825b72b2009-08-11 20:47:22 +00004733 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4734 EVT VT = MVT::v2i32;
4735 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004736 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004737 case MVT::v16i8:
4738 case MVT::v8i16:
4739 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004740 break;
4741 }
Dale Johannesenace16102009-02-03 19:33:06 +00004742 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4743 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004744}
4745
Bill Wendling056292f2008-09-16 21:48:12 +00004746// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4747// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4748// one of the above mentioned nodes. It has to be wrapped because otherwise
4749// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4750// be used to form addressing mode. These wrapped nodes will be selected
4751// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004752SDValue
4753X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004754 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004755
Chris Lattner41621a22009-06-26 19:22:52 +00004756 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4757 // global base reg.
4758 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004759 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004760 CodeModel::Model M = getTargetMachine().getCodeModel();
4761
Chris Lattner4f066492009-07-11 20:29:19 +00004762 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004763 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004764 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004765 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004766 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004767 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004768 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004769
Evan Cheng1606e8e2009-03-13 07:51:59 +00004770 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004771 CP->getAlignment(),
4772 CP->getOffset(), OpFlag);
4773 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004774 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004775 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004776 if (OpFlag) {
4777 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004778 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004779 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004780 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004781 }
4782
4783 return Result;
4784}
4785
Chris Lattner18c59872009-06-27 04:16:01 +00004786SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4787 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004788
Chris Lattner18c59872009-06-27 04:16:01 +00004789 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4790 // global base reg.
4791 unsigned char OpFlag = 0;
4792 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004793 CodeModel::Model M = getTargetMachine().getCodeModel();
4794
Chris Lattner4f066492009-07-11 20:29:19 +00004795 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004796 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004797 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004798 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004799 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004800 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004801 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004802
Chris Lattner18c59872009-06-27 04:16:01 +00004803 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4804 OpFlag);
4805 DebugLoc DL = JT->getDebugLoc();
4806 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004807
Chris Lattner18c59872009-06-27 04:16:01 +00004808 // With PIC, the address is actually $g + Offset.
4809 if (OpFlag) {
4810 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4811 DAG.getNode(X86ISD::GlobalBaseReg,
4812 DebugLoc::getUnknownLoc(), getPointerTy()),
4813 Result);
4814 }
Eric Christopherfd179292009-08-27 18:07:15 +00004815
Chris Lattner18c59872009-06-27 04:16:01 +00004816 return Result;
4817}
4818
4819SDValue
4820X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4821 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004822
Chris Lattner18c59872009-06-27 04:16:01 +00004823 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4824 // global base reg.
4825 unsigned char OpFlag = 0;
4826 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004827 CodeModel::Model M = getTargetMachine().getCodeModel();
4828
Chris Lattner4f066492009-07-11 20:29:19 +00004829 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004830 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004831 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004832 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004833 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004834 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004835 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004836
Chris Lattner18c59872009-06-27 04:16:01 +00004837 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Chris Lattner18c59872009-06-27 04:16:01 +00004839 DebugLoc DL = Op.getDebugLoc();
4840 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004841
4842
Chris Lattner18c59872009-06-27 04:16:01 +00004843 // With PIC, the address is actually $g + Offset.
4844 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004845 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004846 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4847 DAG.getNode(X86ISD::GlobalBaseReg,
4848 DebugLoc::getUnknownLoc(),
4849 getPointerTy()),
4850 Result);
4851 }
Eric Christopherfd179292009-08-27 18:07:15 +00004852
Chris Lattner18c59872009-06-27 04:16:01 +00004853 return Result;
4854}
4855
Dan Gohman475871a2008-07-27 21:46:04 +00004856SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004857X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004858 // Create the TargetBlockAddressAddress node.
4859 unsigned char OpFlags =
4860 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004861 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004862 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4863 DebugLoc dl = Op.getDebugLoc();
4864 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4865 /*isTarget=*/true, OpFlags);
4866
Dan Gohmanf705adb2009-10-30 01:28:02 +00004867 if (Subtarget->isPICStyleRIPRel() &&
4868 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004869 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4870 else
4871 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004872
Dan Gohman29cbade2009-11-20 23:18:13 +00004873 // With PIC, the address is actually $g + Offset.
4874 if (isGlobalRelativeToPICBase(OpFlags)) {
4875 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4876 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4877 Result);
4878 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004879
4880 return Result;
4881}
4882
4883SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004884X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004885 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004886 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004887 // Create the TargetGlobalAddress node, folding in the constant
4888 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004889 unsigned char OpFlags =
4890 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004891 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004892 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004893 if (OpFlags == X86II::MO_NO_FLAG &&
4894 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004895 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004896 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004897 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004898 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004899 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004900 }
Eric Christopherfd179292009-08-27 18:07:15 +00004901
Chris Lattner4f066492009-07-11 20:29:19 +00004902 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004903 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004904 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4905 else
4906 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004907
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004908 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004909 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004910 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4911 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004912 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004914
Chris Lattner36c25012009-07-10 07:34:39 +00004915 // For globals that require a load from a stub to get the address, emit the
4916 // load.
4917 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004918 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004919 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004920
Dan Gohman6520e202008-10-18 02:06:02 +00004921 // If there was a non-zero offset that we didn't fold, create an explicit
4922 // addition for it.
4923 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004924 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004925 DAG.getConstant(Offset, getPointerTy()));
4926
Evan Cheng0db9fe62006-04-25 20:13:52 +00004927 return Result;
4928}
4929
Evan Chengda43bcf2008-09-24 00:05:32 +00004930SDValue
4931X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4932 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004933 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004934 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004935}
4936
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004937static SDValue
4938GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004939 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004940 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004941 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004942 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004943 DebugLoc dl = GA->getDebugLoc();
4944 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4945 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004946 GA->getOffset(),
4947 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004948 if (InFlag) {
4949 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004950 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004951 } else {
4952 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004953 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004954 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004955
4956 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
4957 MFI->setHasCalls(true);
4958
Rafael Espindola15f1b662009-04-24 12:59:40 +00004959 SDValue Flag = Chain.getValue(1);
4960 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004961}
4962
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004963// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004964static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004965LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004966 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004968 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4969 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004970 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004971 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004972 PtrVT), InFlag);
4973 InFlag = Chain.getValue(1);
4974
Chris Lattnerb903bed2009-06-26 21:20:29 +00004975 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004976}
4977
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004978// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004979static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004980LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004981 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004982 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4983 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004984}
4985
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004986// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4987// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004988static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004989 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004990 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004991 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004992 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004993 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4994 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004995 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00004996 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004997
4998 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4999 NULL, 0);
5000
Chris Lattnerb903bed2009-06-26 21:20:29 +00005001 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005002 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5003 // initialexec.
5004 unsigned WrapperKind = X86ISD::Wrapper;
5005 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005006 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005007 } else if (is64Bit) {
5008 assert(model == TLSModel::InitialExec);
5009 OperandFlags = X86II::MO_GOTTPOFF;
5010 WrapperKind = X86ISD::WrapperRIP;
5011 } else {
5012 assert(model == TLSModel::InitialExec);
5013 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005014 }
Eric Christopherfd179292009-08-27 18:07:15 +00005015
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005016 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5017 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005018 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005019 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005020 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005021
Rafael Espindola9a580232009-02-27 13:37:18 +00005022 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005023 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005024 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005025
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005026 // The address of the thread local variable is the add of the thread
5027 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005028 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005029}
5030
Dan Gohman475871a2008-07-27 21:46:04 +00005031SDValue
5032X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005033 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005034 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005035 assert(Subtarget->isTargetELF() &&
5036 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005037 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005038 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005039
Chris Lattnerb903bed2009-06-26 21:20:29 +00005040 // If GV is an alias then use the aliasee for determining
5041 // thread-localness.
5042 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5043 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005044
Chris Lattnerb903bed2009-06-26 21:20:29 +00005045 TLSModel::Model model = getTLSModel(GV,
5046 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005047
Chris Lattnerb903bed2009-06-26 21:20:29 +00005048 switch (model) {
5049 case TLSModel::GeneralDynamic:
5050 case TLSModel::LocalDynamic: // not implemented
5051 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005052 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005053 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005054
Chris Lattnerb903bed2009-06-26 21:20:29 +00005055 case TLSModel::InitialExec:
5056 case TLSModel::LocalExec:
5057 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5058 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005059 }
Eric Christopherfd179292009-08-27 18:07:15 +00005060
Torok Edwinc23197a2009-07-14 16:55:14 +00005061 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005062 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005063}
5064
Evan Cheng0db9fe62006-04-25 20:13:52 +00005065
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005066/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005067/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005068SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005069 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005070 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005071 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005072 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005073 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005074 SDValue ShOpLo = Op.getOperand(0);
5075 SDValue ShOpHi = Op.getOperand(1);
5076 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005077 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005078 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005079 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005080
Dan Gohman475871a2008-07-27 21:46:04 +00005081 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005082 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005083 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5084 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005085 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005086 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5087 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005088 }
Evan Chenge3413162006-01-09 18:33:28 +00005089
Owen Anderson825b72b2009-08-11 20:47:22 +00005090 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5091 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005092 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005093 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005094
Dan Gohman475871a2008-07-27 21:46:04 +00005095 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005096 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005097 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5098 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005099
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005100 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005101 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5102 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005103 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005104 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5105 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005106 }
5107
Dan Gohman475871a2008-07-27 21:46:04 +00005108 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005109 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005110}
Evan Chenga3195e82006-01-12 22:54:21 +00005111
Dan Gohman475871a2008-07-27 21:46:04 +00005112SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005113 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005114
5115 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005116 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005117 return Op;
5118 }
5119 return SDValue();
5120 }
5121
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005123 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005124
Eli Friedman36df4992009-05-27 00:47:34 +00005125 // These are really Legal; return the operand so the caller accepts it as
5126 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005127 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005128 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005129 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005130 Subtarget->is64Bit()) {
5131 return Op;
5132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005133
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005134 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005135 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005136 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005137 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005138 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005139 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005140 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005141 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005142 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5143}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005144
Owen Andersone50ed302009-08-10 22:56:29 +00005145SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005146 SDValue StackSlot,
5147 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005148 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005149 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005150 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005151 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005152 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005154 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005156 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005157 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005158 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005159
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005160 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005162 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163
5164 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5165 // shouldn't be necessary except that RFP cannot be live across
5166 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005167 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005168 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005169 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005171 SDValue Ops[] = {
5172 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5173 };
5174 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005175 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005176 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005177 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005178
Evan Cheng0db9fe62006-04-25 20:13:52 +00005179 return Result;
5180}
5181
Bill Wendling8b8a6362009-01-17 03:56:04 +00005182// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5183SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5184 // This algorithm is not obvious. Here it is in C code, more or less:
5185 /*
5186 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5187 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5188 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005189
Bill Wendling8b8a6362009-01-17 03:56:04 +00005190 // Copy ints to xmm registers.
5191 __m128i xh = _mm_cvtsi32_si128( hi );
5192 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005193
Bill Wendling8b8a6362009-01-17 03:56:04 +00005194 // Combine into low half of a single xmm register.
5195 __m128i x = _mm_unpacklo_epi32( xh, xl );
5196 __m128d d;
5197 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005198
Bill Wendling8b8a6362009-01-17 03:56:04 +00005199 // Merge in appropriate exponents to give the integer bits the right
5200 // magnitude.
5201 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005202
Bill Wendling8b8a6362009-01-17 03:56:04 +00005203 // Subtract away the biases to deal with the IEEE-754 double precision
5204 // implicit 1.
5205 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005206
Bill Wendling8b8a6362009-01-17 03:56:04 +00005207 // All conversions up to here are exact. The correctly rounded result is
5208 // calculated using the current rounding mode using the following
5209 // horizontal add.
5210 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5211 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5212 // store doesn't really need to be here (except
5213 // maybe to zero the other double)
5214 return sd;
5215 }
5216 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005217
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005218 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005219 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005220
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005221 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005222 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005223 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5224 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5225 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5226 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005227 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005228 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005229
Bill Wendling8b8a6362009-01-17 03:56:04 +00005230 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005231 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005232 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005233 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005234 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005235 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005236 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005237
Owen Anderson825b72b2009-08-11 20:47:22 +00005238 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5239 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005240 Op.getOperand(0),
5241 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005242 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5243 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005244 Op.getOperand(0),
5245 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5247 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005248 PseudoSourceValue::getConstantPool(), 0,
5249 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005250 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5251 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5252 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005253 PseudoSourceValue::getConstantPool(), 0,
5254 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005255 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005256
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005257 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005258 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005259 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5260 DAG.getUNDEF(MVT::v2f64), ShufMask);
5261 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5262 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005263 DAG.getIntPtrConstant(0));
5264}
5265
Bill Wendling8b8a6362009-01-17 03:56:04 +00005266// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5267SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005268 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005269 // FP constant to bias correct the final result.
5270 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005271 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005272
5273 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005274 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5275 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005276 Op.getOperand(0),
5277 DAG.getIntPtrConstant(0)));
5278
Owen Anderson825b72b2009-08-11 20:47:22 +00005279 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5280 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005281 DAG.getIntPtrConstant(0));
5282
5283 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005284 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5285 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005286 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 MVT::v2f64, Load)),
5288 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005289 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005290 MVT::v2f64, Bias)));
5291 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5292 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005293 DAG.getIntPtrConstant(0));
5294
5295 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005296 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005297
5298 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005299 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005300
Owen Anderson825b72b2009-08-11 20:47:22 +00005301 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005302 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005303 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005304 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005305 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005306 }
5307
5308 // Handle final rounding.
5309 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005310}
5311
5312SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005313 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005314 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005315
Evan Chenga06ec9e2009-01-19 08:08:22 +00005316 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5317 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5318 // the optimization here.
5319 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005320 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005321
Owen Andersone50ed302009-08-10 22:56:29 +00005322 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005323 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005324 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005326 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005327
Bill Wendling8b8a6362009-01-17 03:56:04 +00005328 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005329 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005330 return LowerUINT_TO_FP_i32(Op, DAG);
5331 }
5332
Owen Anderson825b72b2009-08-11 20:47:22 +00005333 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005334
5335 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005336 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005337 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5338 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5339 getPointerTy(), StackSlot, WordOff);
5340 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5341 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005343 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005345}
5346
Dan Gohman475871a2008-07-27 21:46:04 +00005347std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005348FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005349 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005350
Owen Andersone50ed302009-08-10 22:56:29 +00005351 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005352
5353 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005354 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5355 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005356 }
5357
Owen Anderson825b72b2009-08-11 20:47:22 +00005358 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5359 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005360 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005362 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005364 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005365 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005366 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005367 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005368 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005369 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005370
Evan Cheng87c89352007-10-15 20:11:21 +00005371 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5372 // stack slot.
5373 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005374 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005375 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005376 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005377
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005379 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005380 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5382 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5383 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005384 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005385
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue Chain = DAG.getEntryNode();
5387 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005388 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005389 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005390 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005391 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005392 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005393 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005394 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5395 };
Dale Johannesenace16102009-02-03 19:33:06 +00005396 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005397 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005398 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005399 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5400 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005401
Evan Cheng0db9fe62006-04-25 20:13:52 +00005402 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005404 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005405
Chris Lattner27a6c732007-11-24 07:07:01 +00005406 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407}
5408
Dan Gohman475871a2008-07-27 21:46:04 +00005409SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005410 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 if (Op.getValueType() == MVT::v2i32 &&
5412 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005413 return Op;
5414 }
5415 return SDValue();
5416 }
5417
Eli Friedman948e95a2009-05-23 09:59:16 +00005418 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005419 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005420 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5421 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005422
Chris Lattner27a6c732007-11-24 07:07:01 +00005423 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005424 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005425 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005426}
5427
Eli Friedman948e95a2009-05-23 09:59:16 +00005428SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5429 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5430 SDValue FIST = Vals.first, StackSlot = Vals.second;
5431 assert(FIST.getNode() && "Unexpected failure");
5432
5433 // Load the result.
5434 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5435 FIST, StackSlot, NULL, 0);
5436}
5437
Dan Gohman475871a2008-07-27 21:46:04 +00005438SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005439 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005440 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005441 EVT VT = Op.getValueType();
5442 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005443 if (VT.isVector())
5444 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005447 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005448 CV.push_back(C);
5449 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005451 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005452 CV.push_back(C);
5453 CV.push_back(C);
5454 CV.push_back(C);
5455 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005457 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005458 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005459 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005460 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005461 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005462 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005463}
5464
Dan Gohman475871a2008-07-27 21:46:04 +00005465SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005466 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005467 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005468 EVT VT = Op.getValueType();
5469 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005470 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005471 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005472 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005473 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005474 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005475 CV.push_back(C);
5476 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005478 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005479 CV.push_back(C);
5480 CV.push_back(C);
5481 CV.push_back(C);
5482 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005483 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005484 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005485 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005486 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005487 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005488 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005489 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005490 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005491 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5492 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005493 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005494 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005495 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005496 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005497 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498}
5499
Dan Gohman475871a2008-07-27 21:46:04 +00005500SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005501 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005502 SDValue Op0 = Op.getOperand(0);
5503 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005504 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005505 EVT VT = Op.getValueType();
5506 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005507
5508 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005509 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005510 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005511 SrcVT = VT;
5512 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005513 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005514 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005515 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005516 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005517 }
5518
5519 // At this point the operands and the result should have the same
5520 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005521
Evan Cheng68c47cb2007-01-05 07:55:56 +00005522 // First get the sign bit of second operand.
5523 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005524 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005525 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5526 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005527 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005528 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5529 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5530 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5531 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005532 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005533 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005534 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005535 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005536 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005537 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005538 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005539
5540 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005541 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 // Op0 is MVT::f32, Op1 is MVT::f64.
5543 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5544 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5545 DAG.getConstant(32, MVT::i32));
5546 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5547 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005548 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005549 }
5550
Evan Cheng73d6cf12007-01-05 21:37:56 +00005551 // Clear first operand sign bit.
5552 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005554 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5555 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005556 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005557 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5558 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5559 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5560 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005561 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005562 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005563 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005564 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005565 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005566 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005567 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005568
5569 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005570 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005571}
5572
Dan Gohman076aee32009-03-04 19:44:21 +00005573/// Emit nodes that will be selected as "test Op0,Op0", or something
5574/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005575SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5576 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005577 DebugLoc dl = Op.getDebugLoc();
5578
Dan Gohman31125812009-03-07 01:58:32 +00005579 // CF and OF aren't always set the way we want. Determine which
5580 // of these we need.
5581 bool NeedCF = false;
5582 bool NeedOF = false;
5583 switch (X86CC) {
5584 case X86::COND_A: case X86::COND_AE:
5585 case X86::COND_B: case X86::COND_BE:
5586 NeedCF = true;
5587 break;
5588 case X86::COND_G: case X86::COND_GE:
5589 case X86::COND_L: case X86::COND_LE:
5590 case X86::COND_O: case X86::COND_NO:
5591 NeedOF = true;
5592 break;
5593 default: break;
5594 }
5595
Dan Gohman076aee32009-03-04 19:44:21 +00005596 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005597 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5598 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5599 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005600 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005601 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005602 switch (Op.getNode()->getOpcode()) {
5603 case ISD::ADD:
5604 // Due to an isel shortcoming, be conservative if this add is likely to
5605 // be selected as part of a load-modify-store instruction. When the root
5606 // node in a match is a store, isel doesn't know how to remap non-chain
5607 // non-flag uses of other nodes in the match, such as the ADD in this
5608 // case. This leads to the ADD being left around and reselected, with
5609 // the result being two adds in the output.
5610 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5611 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5612 if (UI->getOpcode() == ISD::STORE)
5613 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005614 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005615 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5616 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005617 if (C->getAPIntValue() == 1) {
5618 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005619 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005620 break;
5621 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005622 // An add of negative one (subtract of one) will be selected as a DEC.
5623 if (C->getAPIntValue().isAllOnesValue()) {
5624 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005625 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005626 break;
5627 }
5628 }
Dan Gohman076aee32009-03-04 19:44:21 +00005629 // Otherwise use a regular EFLAGS-setting add.
5630 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005631 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005632 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005633 case ISD::AND: {
5634 // If the primary and result isn't used, don't bother using X86ISD::AND,
5635 // because a TEST instruction will be better.
5636 bool NonFlagUse = false;
5637 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005638 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5639 SDNode *User = *UI;
5640 unsigned UOpNo = UI.getOperandNo();
5641 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5642 // Look pass truncate.
5643 UOpNo = User->use_begin().getOperandNo();
5644 User = *User->use_begin();
5645 }
5646 if (User->getOpcode() != ISD::BRCOND &&
5647 User->getOpcode() != ISD::SETCC &&
5648 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005649 NonFlagUse = true;
5650 break;
5651 }
Evan Cheng17751da2010-01-07 00:54:06 +00005652 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005653 if (!NonFlagUse)
5654 break;
5655 }
5656 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005657 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005658 case ISD::OR:
5659 case ISD::XOR:
5660 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005661 // likely to be selected as part of a load-modify-store instruction.
5662 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5663 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5664 if (UI->getOpcode() == ISD::STORE)
5665 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005666 // Otherwise use a regular EFLAGS-setting instruction.
5667 switch (Op.getNode()->getOpcode()) {
5668 case ISD::SUB: Opcode = X86ISD::SUB; break;
5669 case ISD::OR: Opcode = X86ISD::OR; break;
5670 case ISD::XOR: Opcode = X86ISD::XOR; break;
5671 case ISD::AND: Opcode = X86ISD::AND; break;
5672 default: llvm_unreachable("unexpected operator!");
5673 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005674 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005675 break;
5676 case X86ISD::ADD:
5677 case X86ISD::SUB:
5678 case X86ISD::INC:
5679 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005680 case X86ISD::OR:
5681 case X86ISD::XOR:
5682 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005683 return SDValue(Op.getNode(), 1);
5684 default:
5685 default_case:
5686 break;
5687 }
5688 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005689 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005690 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005691 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005692 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005693 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005694 DAG.ReplaceAllUsesWith(Op, New);
5695 return SDValue(New.getNode(), 1);
5696 }
5697 }
5698
5699 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005700 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005701 DAG.getConstant(0, Op.getValueType()));
5702}
5703
5704/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5705/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005706SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5707 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005708 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5709 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005710 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005711
5712 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005714}
5715
Evan Chengd40d03e2010-01-06 19:38:29 +00005716/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5717/// if it's possible.
5718static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005719 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005720 SDValue LHS, RHS;
5721 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5722 if (ConstantSDNode *Op010C =
5723 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5724 if (Op010C->getZExtValue() == 1) {
5725 LHS = Op0.getOperand(0);
5726 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005727 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005728 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5729 if (ConstantSDNode *Op000C =
5730 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5731 if (Op000C->getZExtValue() == 1) {
5732 LHS = Op0.getOperand(1);
5733 RHS = Op0.getOperand(0).getOperand(1);
5734 }
5735 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5736 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5737 SDValue AndLHS = Op0.getOperand(0);
5738 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5739 LHS = AndLHS.getOperand(0);
5740 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005741 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005742 }
Evan Cheng0488db92007-09-25 01:57:46 +00005743
Evan Chengd40d03e2010-01-06 19:38:29 +00005744 if (LHS.getNode()) {
5745 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5746 // instruction. Since the shift amount is in-range-or-undefined, we know
5747 // that doing a bittest on the i16 value is ok. We extend to i32 because
5748 // the encoding for the i16 version is larger than the i32 version.
5749 if (LHS.getValueType() == MVT::i8)
5750 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005751
Evan Chengd40d03e2010-01-06 19:38:29 +00005752 // If the operand types disagree, extend the shift amount to match. Since
5753 // BT ignores high bits (like shifts) we can use anyextend.
5754 if (LHS.getValueType() != RHS.getValueType())
5755 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005756
Evan Chengd40d03e2010-01-06 19:38:29 +00005757 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5758 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5759 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5760 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005761 }
5762
Evan Cheng54de3ea2010-01-05 06:52:31 +00005763 return SDValue();
5764}
5765
5766SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5767 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5768 SDValue Op0 = Op.getOperand(0);
5769 SDValue Op1 = Op.getOperand(1);
5770 DebugLoc dl = Op.getDebugLoc();
5771 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5772
5773 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005774 // Lower (X & (1 << N)) == 0 to BT(X, N).
5775 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5776 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5777 if (Op0.getOpcode() == ISD::AND &&
5778 Op0.hasOneUse() &&
5779 Op1.getOpcode() == ISD::Constant &&
5780 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5781 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5782 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5783 if (NewSetCC.getNode())
5784 return NewSetCC;
5785 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005786
Chris Lattnere55484e2008-12-25 05:34:37 +00005787 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5788 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005789 if (X86CC == X86::COND_INVALID)
5790 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005791
Dan Gohman31125812009-03-07 01:58:32 +00005792 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005793
5794 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005795 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005796 return DAG.getNode(ISD::AND, dl, MVT::i8,
5797 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5798 DAG.getConstant(X86CC, MVT::i8), Cond),
5799 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005800
Owen Anderson825b72b2009-08-11 20:47:22 +00005801 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5802 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005803}
5804
Dan Gohman475871a2008-07-27 21:46:04 +00005805SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5806 SDValue Cond;
5807 SDValue Op0 = Op.getOperand(0);
5808 SDValue Op1 = Op.getOperand(1);
5809 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005810 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005811 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5812 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005813 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005814
5815 if (isFP) {
5816 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005817 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5819 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005820 bool Swap = false;
5821
5822 switch (SetCCOpcode) {
5823 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005824 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005825 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005826 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005827 case ISD::SETGT: Swap = true; // Fallthrough
5828 case ISD::SETLT:
5829 case ISD::SETOLT: SSECC = 1; break;
5830 case ISD::SETOGE:
5831 case ISD::SETGE: Swap = true; // Fallthrough
5832 case ISD::SETLE:
5833 case ISD::SETOLE: SSECC = 2; break;
5834 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005835 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005836 case ISD::SETNE: SSECC = 4; break;
5837 case ISD::SETULE: Swap = true;
5838 case ISD::SETUGE: SSECC = 5; break;
5839 case ISD::SETULT: Swap = true;
5840 case ISD::SETUGT: SSECC = 6; break;
5841 case ISD::SETO: SSECC = 7; break;
5842 }
5843 if (Swap)
5844 std::swap(Op0, Op1);
5845
Nate Begemanfb8ead02008-07-25 19:05:58 +00005846 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005847 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005848 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005849 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5851 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005852 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005853 }
5854 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005855 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5857 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005858 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005859 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005860 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005861 }
5862 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005865
Nate Begeman30a0de92008-07-17 16:51:19 +00005866 // We are handling one of the integer comparisons here. Since SSE only has
5867 // GT and EQ comparisons for integer, swapping operands and multiple
5868 // operations may be required for some comparisons.
5869 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5870 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005871
Owen Anderson825b72b2009-08-11 20:47:22 +00005872 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005873 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 case MVT::v8i8:
5875 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5876 case MVT::v4i16:
5877 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5878 case MVT::v2i32:
5879 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5880 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005881 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005882
Nate Begeman30a0de92008-07-17 16:51:19 +00005883 switch (SetCCOpcode) {
5884 default: break;
5885 case ISD::SETNE: Invert = true;
5886 case ISD::SETEQ: Opc = EQOpc; break;
5887 case ISD::SETLT: Swap = true;
5888 case ISD::SETGT: Opc = GTOpc; break;
5889 case ISD::SETGE: Swap = true;
5890 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5891 case ISD::SETULT: Swap = true;
5892 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5893 case ISD::SETUGE: Swap = true;
5894 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5895 }
5896 if (Swap)
5897 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005898
Nate Begeman30a0de92008-07-17 16:51:19 +00005899 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5900 // bits of the inputs before performing those operations.
5901 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005902 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005903 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5904 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005905 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005906 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5907 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005908 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5909 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005911
Dale Johannesenace16102009-02-03 19:33:06 +00005912 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005913
5914 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005915 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005916 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005917
Nate Begeman30a0de92008-07-17 16:51:19 +00005918 return Result;
5919}
Evan Cheng0488db92007-09-25 01:57:46 +00005920
Evan Cheng370e5342008-12-03 08:38:43 +00005921// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005922static bool isX86LogicalCmp(SDValue Op) {
5923 unsigned Opc = Op.getNode()->getOpcode();
5924 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5925 return true;
5926 if (Op.getResNo() == 1 &&
5927 (Opc == X86ISD::ADD ||
5928 Opc == X86ISD::SUB ||
5929 Opc == X86ISD::SMUL ||
5930 Opc == X86ISD::UMUL ||
5931 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005932 Opc == X86ISD::DEC ||
5933 Opc == X86ISD::OR ||
5934 Opc == X86ISD::XOR ||
5935 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005936 return true;
5937
5938 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005939}
5940
Dan Gohman475871a2008-07-27 21:46:04 +00005941SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005942 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005943 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005944 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005945 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005946
Dan Gohman1a492952009-10-20 16:22:37 +00005947 if (Cond.getOpcode() == ISD::SETCC) {
5948 SDValue NewCond = LowerSETCC(Cond, DAG);
5949 if (NewCond.getNode())
5950 Cond = NewCond;
5951 }
Evan Cheng734503b2006-09-11 02:19:56 +00005952
Evan Chengad9c0a32009-12-15 00:53:42 +00005953 // Look pass (and (setcc_carry (cmp ...)), 1).
5954 if (Cond.getOpcode() == ISD::AND &&
5955 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
5956 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
5957 if (C && C->getAPIntValue() == 1)
5958 Cond = Cond.getOperand(0);
5959 }
5960
Evan Cheng3f41d662007-10-08 22:16:29 +00005961 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5962 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00005963 if (Cond.getOpcode() == X86ISD::SETCC ||
5964 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00005965 CC = Cond.getOperand(0);
5966
Dan Gohman475871a2008-07-27 21:46:04 +00005967 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005968 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00005969 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005970
Evan Cheng3f41d662007-10-08 22:16:29 +00005971 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005972 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005973 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005974 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005975
Chris Lattnerd1980a52009-03-12 06:52:53 +00005976 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5977 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005978 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005979 addTest = false;
5980 }
5981 }
5982
5983 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005984 // Look pass the truncate.
5985 if (Cond.getOpcode() == ISD::TRUNCATE)
5986 Cond = Cond.getOperand(0);
5987
5988 // We know the result of AND is compared against zero. Try to match
5989 // it to BT.
5990 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
5991 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
5992 if (NewSetCC.getNode()) {
5993 CC = NewSetCC.getOperand(0);
5994 Cond = NewSetCC.getOperand(1);
5995 addTest = false;
5996 }
5997 }
5998 }
5999
6000 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006001 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006002 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006003 }
6004
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Evan Cheng0488db92007-09-25 01:57:46 +00006006 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6007 // condition is true.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006008 SDValue Ops[] = { Op.getOperand(2), Op.getOperand(1), CC, Cond };
6009 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006010}
6011
Evan Cheng370e5342008-12-03 08:38:43 +00006012// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6013// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6014// from the AND / OR.
6015static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6016 Opc = Op.getOpcode();
6017 if (Opc != ISD::OR && Opc != ISD::AND)
6018 return false;
6019 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6020 Op.getOperand(0).hasOneUse() &&
6021 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6022 Op.getOperand(1).hasOneUse());
6023}
6024
Evan Cheng961d6d42009-02-02 08:19:07 +00006025// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6026// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006027static bool isXor1OfSetCC(SDValue Op) {
6028 if (Op.getOpcode() != ISD::XOR)
6029 return false;
6030 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6031 if (N1C && N1C->getAPIntValue() == 1) {
6032 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6033 Op.getOperand(0).hasOneUse();
6034 }
6035 return false;
6036}
6037
Dan Gohman475871a2008-07-27 21:46:04 +00006038SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006039 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006040 SDValue Chain = Op.getOperand(0);
6041 SDValue Cond = Op.getOperand(1);
6042 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006043 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006045
Dan Gohman1a492952009-10-20 16:22:37 +00006046 if (Cond.getOpcode() == ISD::SETCC) {
6047 SDValue NewCond = LowerSETCC(Cond, DAG);
6048 if (NewCond.getNode())
6049 Cond = NewCond;
6050 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006051#if 0
6052 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006053 else if (Cond.getOpcode() == X86ISD::ADD ||
6054 Cond.getOpcode() == X86ISD::SUB ||
6055 Cond.getOpcode() == X86ISD::SMUL ||
6056 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006057 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006058#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006059
Evan Chengad9c0a32009-12-15 00:53:42 +00006060 // Look pass (and (setcc_carry (cmp ...)), 1).
6061 if (Cond.getOpcode() == ISD::AND &&
6062 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6063 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6064 if (C && C->getAPIntValue() == 1)
6065 Cond = Cond.getOperand(0);
6066 }
6067
Evan Cheng3f41d662007-10-08 22:16:29 +00006068 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6069 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006070 if (Cond.getOpcode() == X86ISD::SETCC ||
6071 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006072 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006073
Dan Gohman475871a2008-07-27 21:46:04 +00006074 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006075 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006076 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006077 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006078 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006079 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006080 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006081 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006082 default: break;
6083 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006084 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006085 // These can only come from an arithmetic instruction with overflow,
6086 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006087 Cond = Cond.getNode()->getOperand(1);
6088 addTest = false;
6089 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006090 }
Evan Cheng0488db92007-09-25 01:57:46 +00006091 }
Evan Cheng370e5342008-12-03 08:38:43 +00006092 } else {
6093 unsigned CondOpc;
6094 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6095 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006096 if (CondOpc == ISD::OR) {
6097 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6098 // two branches instead of an explicit OR instruction with a
6099 // separate test.
6100 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006101 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006102 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006103 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006104 Chain, Dest, CC, Cmp);
6105 CC = Cond.getOperand(1).getOperand(0);
6106 Cond = Cmp;
6107 addTest = false;
6108 }
6109 } else { // ISD::AND
6110 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6111 // two branches instead of an explicit AND instruction with a
6112 // separate test. However, we only do this if this block doesn't
6113 // have a fall-through edge, because this requires an explicit
6114 // jmp when the condition is false.
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 Op.getNode()->hasOneUse()) {
6118 X86::CondCode CCode =
6119 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6120 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006121 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006122 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6123 // Look for an unconditional branch following this conditional branch.
6124 // We need this because we need to reverse the successors in order
6125 // to implement FCMP_OEQ.
6126 if (User.getOpcode() == ISD::BR) {
6127 SDValue FalseBB = User.getOperand(1);
6128 SDValue NewBR =
6129 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6130 assert(NewBR == User);
6131 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006132
Dale Johannesene4d209d2009-02-03 20:21:25 +00006133 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006134 Chain, Dest, CC, Cmp);
6135 X86::CondCode CCode =
6136 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6137 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006138 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006139 Cond = Cmp;
6140 addTest = false;
6141 }
6142 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006143 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006144 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6145 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6146 // It should be transformed during dag combiner except when the condition
6147 // is set by a arithmetics with overflow node.
6148 X86::CondCode CCode =
6149 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6150 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006151 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006152 Cond = Cond.getOperand(0).getOperand(1);
6153 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006154 }
Evan Cheng0488db92007-09-25 01:57:46 +00006155 }
6156
6157 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006158 // Look pass the truncate.
6159 if (Cond.getOpcode() == ISD::TRUNCATE)
6160 Cond = Cond.getOperand(0);
6161
6162 // We know the result of AND is compared against zero. Try to match
6163 // it to BT.
6164 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6165 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6166 if (NewSetCC.getNode()) {
6167 CC = NewSetCC.getOperand(0);
6168 Cond = NewSetCC.getOperand(1);
6169 addTest = false;
6170 }
6171 }
6172 }
6173
6174 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006175 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006176 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006177 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006178 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006179 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006180}
6181
Anton Korobeynikove060b532007-04-17 19:34:00 +00006182
6183// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6184// Calls to _alloca is needed to probe the stack when allocating more than 4k
6185// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6186// that the guard pages used by the OS virtual memory manager are allocated in
6187// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006188SDValue
6189X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006190 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006191 assert(Subtarget->isTargetCygMing() &&
6192 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006193 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006194
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006195 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006196 SDValue Chain = Op.getOperand(0);
6197 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006198 // FIXME: Ensure alignment here
6199
Dan Gohman475871a2008-07-27 21:46:04 +00006200 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006201
Owen Andersone50ed302009-08-10 22:56:29 +00006202 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006203 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006204
Chris Lattnere563bbc2008-10-11 22:08:30 +00006205 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006206
Dale Johannesendd64c412009-02-04 00:33:20 +00006207 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006208 Flag = Chain.getValue(1);
6209
Owen Anderson825b72b2009-08-11 20:47:22 +00006210 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006211 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006212 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006213 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006214 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006215 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006216 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006217 Flag = Chain.getValue(1);
6218
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006219 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006220 DAG.getIntPtrConstant(0, true),
6221 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006222 Flag);
6223
Dale Johannesendd64c412009-02-04 00:33:20 +00006224 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006225
Dan Gohman475871a2008-07-27 21:46:04 +00006226 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006227 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006228}
6229
Dan Gohman475871a2008-07-27 21:46:04 +00006230SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006231X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006232 SDValue Chain,
6233 SDValue Dst, SDValue Src,
6234 SDValue Size, unsigned Align,
6235 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006236 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006237 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006238
Bill Wendling6f287b22008-09-30 21:22:07 +00006239 // If not DWORD aligned or size is more than the threshold, call the library.
6240 // The libc version is likely to be faster for these cases. It can use the
6241 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006242 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006243 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006244 ConstantSize->getZExtValue() >
6245 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006246 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006247
6248 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006249 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006250
Bill Wendling6158d842008-10-01 00:59:58 +00006251 if (const char *bzeroEntry = V &&
6252 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006253 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006254 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006255 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006256 TargetLowering::ArgListEntry Entry;
6257 Entry.Node = Dst;
6258 Entry.Ty = IntPtrTy;
6259 Args.push_back(Entry);
6260 Entry.Node = Size;
6261 Args.push_back(Entry);
6262 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006263 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6264 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006265 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006266 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6267 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006268 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006269 }
6270
Dan Gohman707e0182008-04-12 04:36:06 +00006271 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006272 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006273 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006274
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006275 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006276 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006277 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006278 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006279 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006280 unsigned BytesLeft = 0;
6281 bool TwoRepStos = false;
6282 if (ValC) {
6283 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006284 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006285
Evan Cheng0db9fe62006-04-25 20:13:52 +00006286 // If the value is a constant, then we can potentially use larger sets.
6287 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006288 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006289 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006290 ValReg = X86::AX;
6291 Val = (Val << 8) | Val;
6292 break;
6293 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006294 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006295 ValReg = X86::EAX;
6296 Val = (Val << 8) | Val;
6297 Val = (Val << 16) | Val;
6298 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006299 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006300 ValReg = X86::RAX;
6301 Val = (Val << 32) | Val;
6302 }
6303 break;
6304 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006305 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006306 ValReg = X86::AL;
6307 Count = DAG.getIntPtrConstant(SizeVal);
6308 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006309 }
6310
Owen Anderson825b72b2009-08-11 20:47:22 +00006311 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006312 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006313 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6314 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006315 }
6316
Dale Johannesen0f502f62009-02-03 22:26:09 +00006317 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006318 InFlag);
6319 InFlag = Chain.getValue(1);
6320 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006321 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006322 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006323 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006324 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006325 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006326
Scott Michelfdc40a02009-02-17 22:15:04 +00006327 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006328 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006329 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006330 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006331 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006332 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006333 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006334 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006335
Owen Anderson825b72b2009-08-11 20:47:22 +00006336 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006337 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6338 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006339
Evan Cheng0db9fe62006-04-25 20:13:52 +00006340 if (TwoRepStos) {
6341 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006342 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006343 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006344 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006345 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6346 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006347 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006348 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006349 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006350 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006351 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6352 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006353 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006354 // Handle the last 1 - 7 bytes.
6355 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006356 EVT AddrVT = Dst.getValueType();
6357 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006358
Dale Johannesen0f502f62009-02-03 22:26:09 +00006359 Chain = DAG.getMemset(Chain, dl,
6360 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006361 DAG.getConstant(Offset, AddrVT)),
6362 Src,
6363 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006364 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006365 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006366
Dan Gohman707e0182008-04-12 04:36:06 +00006367 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006368 return Chain;
6369}
Evan Cheng11e15b32006-04-03 20:53:28 +00006370
Dan Gohman475871a2008-07-27 21:46:04 +00006371SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006372X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006373 SDValue Chain, SDValue Dst, SDValue Src,
6374 SDValue Size, unsigned Align,
6375 bool AlwaysInline,
6376 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006377 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006378 // This requires the copy size to be a constant, preferrably
6379 // within a subtarget-specific limit.
6380 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6381 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006382 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006383 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006384 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006385 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006386
Evan Cheng1887c1c2008-08-21 21:00:15 +00006387 /// If not DWORD aligned, call the library.
6388 if ((Align & 3) != 0)
6389 return SDValue();
6390
6391 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006393 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006394 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006395
Duncan Sands83ec4b62008-06-06 12:08:01 +00006396 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006397 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006398 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006399 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006400
Dan Gohman475871a2008-07-27 21:46:04 +00006401 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006402 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006403 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006404 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006405 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006406 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006407 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006408 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006409 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006410 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006411 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006412 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006413 InFlag = Chain.getValue(1);
6414
Owen Anderson825b72b2009-08-11 20:47:22 +00006415 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006416 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6417 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6418 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006419
Dan Gohman475871a2008-07-27 21:46:04 +00006420 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006421 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006422 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006423 // Handle the last 1 - 7 bytes.
6424 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006425 EVT DstVT = Dst.getValueType();
6426 EVT SrcVT = Src.getValueType();
6427 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006428 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006429 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006430 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006431 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006432 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006433 DAG.getConstant(BytesLeft, SizeVT),
6434 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006435 DstSV, DstSVOff + Offset,
6436 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006437 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006438
Owen Anderson825b72b2009-08-11 20:47:22 +00006439 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006440 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006441}
6442
Dan Gohman475871a2008-07-27 21:46:04 +00006443SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006444 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006445 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006446
Evan Cheng25ab6902006-09-08 06:48:29 +00006447 if (!Subtarget->is64Bit()) {
6448 // vastart just stores the address of the VarArgsFrameIndex slot into the
6449 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006450 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006451 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006452 }
6453
6454 // __va_list_tag:
6455 // gp_offset (0 - 6 * 8)
6456 // fp_offset (48 - 48 + 8 * 16)
6457 // overflow_arg_area (point to parameters coming in memory).
6458 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006459 SmallVector<SDValue, 8> MemOps;
6460 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006461 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006462 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006464 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006465 MemOps.push_back(Store);
6466
6467 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006468 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006469 FIN, DAG.getIntPtrConstant(4));
6470 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006471 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006472 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006473 MemOps.push_back(Store);
6474
6475 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006476 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006477 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006478 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006479 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006480 MemOps.push_back(Store);
6481
6482 // Store ptr to reg_save_area.
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(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006485 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006486 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006487 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006489 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006490}
6491
Dan Gohman475871a2008-07-27 21:46:04 +00006492SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006493 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6494 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006495 SDValue Chain = Op.getOperand(0);
6496 SDValue SrcPtr = Op.getOperand(1);
6497 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006498
Torok Edwindac237e2009-07-08 20:53:28 +00006499 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006500 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006501}
6502
Dan Gohman475871a2008-07-27 21:46:04 +00006503SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006504 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006505 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006506 SDValue Chain = Op.getOperand(0);
6507 SDValue DstPtr = Op.getOperand(1);
6508 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006509 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6510 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006511 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006512
Dale Johannesendd64c412009-02-04 00:33:20 +00006513 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006514 DAG.getIntPtrConstant(24), 8, false,
6515 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006516}
6517
Dan Gohman475871a2008-07-27 21:46:04 +00006518SDValue
6519X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006520 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006521 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006522 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006523 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006524 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006525 case Intrinsic::x86_sse_comieq_ss:
6526 case Intrinsic::x86_sse_comilt_ss:
6527 case Intrinsic::x86_sse_comile_ss:
6528 case Intrinsic::x86_sse_comigt_ss:
6529 case Intrinsic::x86_sse_comige_ss:
6530 case Intrinsic::x86_sse_comineq_ss:
6531 case Intrinsic::x86_sse_ucomieq_ss:
6532 case Intrinsic::x86_sse_ucomilt_ss:
6533 case Intrinsic::x86_sse_ucomile_ss:
6534 case Intrinsic::x86_sse_ucomigt_ss:
6535 case Intrinsic::x86_sse_ucomige_ss:
6536 case Intrinsic::x86_sse_ucomineq_ss:
6537 case Intrinsic::x86_sse2_comieq_sd:
6538 case Intrinsic::x86_sse2_comilt_sd:
6539 case Intrinsic::x86_sse2_comile_sd:
6540 case Intrinsic::x86_sse2_comigt_sd:
6541 case Intrinsic::x86_sse2_comige_sd:
6542 case Intrinsic::x86_sse2_comineq_sd:
6543 case Intrinsic::x86_sse2_ucomieq_sd:
6544 case Intrinsic::x86_sse2_ucomilt_sd:
6545 case Intrinsic::x86_sse2_ucomile_sd:
6546 case Intrinsic::x86_sse2_ucomigt_sd:
6547 case Intrinsic::x86_sse2_ucomige_sd:
6548 case Intrinsic::x86_sse2_ucomineq_sd: {
6549 unsigned Opc = 0;
6550 ISD::CondCode CC = ISD::SETCC_INVALID;
6551 switch (IntNo) {
6552 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006553 case Intrinsic::x86_sse_comieq_ss:
6554 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006555 Opc = X86ISD::COMI;
6556 CC = ISD::SETEQ;
6557 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006558 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006559 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006560 Opc = X86ISD::COMI;
6561 CC = ISD::SETLT;
6562 break;
6563 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006564 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006565 Opc = X86ISD::COMI;
6566 CC = ISD::SETLE;
6567 break;
6568 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006569 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570 Opc = X86ISD::COMI;
6571 CC = ISD::SETGT;
6572 break;
6573 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006574 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006575 Opc = X86ISD::COMI;
6576 CC = ISD::SETGE;
6577 break;
6578 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006579 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 Opc = X86ISD::COMI;
6581 CC = ISD::SETNE;
6582 break;
6583 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006584 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006585 Opc = X86ISD::UCOMI;
6586 CC = ISD::SETEQ;
6587 break;
6588 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006589 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006590 Opc = X86ISD::UCOMI;
6591 CC = ISD::SETLT;
6592 break;
6593 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006594 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006595 Opc = X86ISD::UCOMI;
6596 CC = ISD::SETLE;
6597 break;
6598 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006599 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 Opc = X86ISD::UCOMI;
6601 CC = ISD::SETGT;
6602 break;
6603 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006604 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 Opc = X86ISD::UCOMI;
6606 CC = ISD::SETGE;
6607 break;
6608 case Intrinsic::x86_sse_ucomineq_ss:
6609 case Intrinsic::x86_sse2_ucomineq_sd:
6610 Opc = X86ISD::UCOMI;
6611 CC = ISD::SETNE;
6612 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006613 }
Evan Cheng734503b2006-09-11 02:19:56 +00006614
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue LHS = Op.getOperand(1);
6616 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006617 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006618 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006619 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6620 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6621 DAG.getConstant(X86CC, MVT::i8), Cond);
6622 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006623 }
Eric Christopher71c67532009-07-29 00:28:05 +00006624 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006625 // an integer value, not just an instruction so lower it to the ptest
6626 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006627 case Intrinsic::x86_sse41_ptestz:
6628 case Intrinsic::x86_sse41_ptestc:
6629 case Intrinsic::x86_sse41_ptestnzc:{
6630 unsigned X86CC = 0;
6631 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006632 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006633 case Intrinsic::x86_sse41_ptestz:
6634 // ZF = 1
6635 X86CC = X86::COND_E;
6636 break;
6637 case Intrinsic::x86_sse41_ptestc:
6638 // CF = 1
6639 X86CC = X86::COND_B;
6640 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006641 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006642 // ZF and CF = 0
6643 X86CC = X86::COND_A;
6644 break;
6645 }
Eric Christopherfd179292009-08-27 18:07:15 +00006646
Eric Christopher71c67532009-07-29 00:28:05 +00006647 SDValue LHS = Op.getOperand(1);
6648 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006649 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6650 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6651 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6652 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006653 }
Evan Cheng5759f972008-05-04 09:15:50 +00006654
6655 // Fix vector shift instructions where the last operand is a non-immediate
6656 // i32 value.
6657 case Intrinsic::x86_sse2_pslli_w:
6658 case Intrinsic::x86_sse2_pslli_d:
6659 case Intrinsic::x86_sse2_pslli_q:
6660 case Intrinsic::x86_sse2_psrli_w:
6661 case Intrinsic::x86_sse2_psrli_d:
6662 case Intrinsic::x86_sse2_psrli_q:
6663 case Intrinsic::x86_sse2_psrai_w:
6664 case Intrinsic::x86_sse2_psrai_d:
6665 case Intrinsic::x86_mmx_pslli_w:
6666 case Intrinsic::x86_mmx_pslli_d:
6667 case Intrinsic::x86_mmx_pslli_q:
6668 case Intrinsic::x86_mmx_psrli_w:
6669 case Intrinsic::x86_mmx_psrli_d:
6670 case Intrinsic::x86_mmx_psrli_q:
6671 case Intrinsic::x86_mmx_psrai_w:
6672 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006673 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006674 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006675 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006676
6677 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006678 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006679 switch (IntNo) {
6680 case Intrinsic::x86_sse2_pslli_w:
6681 NewIntNo = Intrinsic::x86_sse2_psll_w;
6682 break;
6683 case Intrinsic::x86_sse2_pslli_d:
6684 NewIntNo = Intrinsic::x86_sse2_psll_d;
6685 break;
6686 case Intrinsic::x86_sse2_pslli_q:
6687 NewIntNo = Intrinsic::x86_sse2_psll_q;
6688 break;
6689 case Intrinsic::x86_sse2_psrli_w:
6690 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6691 break;
6692 case Intrinsic::x86_sse2_psrli_d:
6693 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6694 break;
6695 case Intrinsic::x86_sse2_psrli_q:
6696 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6697 break;
6698 case Intrinsic::x86_sse2_psrai_w:
6699 NewIntNo = Intrinsic::x86_sse2_psra_w;
6700 break;
6701 case Intrinsic::x86_sse2_psrai_d:
6702 NewIntNo = Intrinsic::x86_sse2_psra_d;
6703 break;
6704 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006705 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006706 switch (IntNo) {
6707 case Intrinsic::x86_mmx_pslli_w:
6708 NewIntNo = Intrinsic::x86_mmx_psll_w;
6709 break;
6710 case Intrinsic::x86_mmx_pslli_d:
6711 NewIntNo = Intrinsic::x86_mmx_psll_d;
6712 break;
6713 case Intrinsic::x86_mmx_pslli_q:
6714 NewIntNo = Intrinsic::x86_mmx_psll_q;
6715 break;
6716 case Intrinsic::x86_mmx_psrli_w:
6717 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6718 break;
6719 case Intrinsic::x86_mmx_psrli_d:
6720 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6721 break;
6722 case Intrinsic::x86_mmx_psrli_q:
6723 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6724 break;
6725 case Intrinsic::x86_mmx_psrai_w:
6726 NewIntNo = Intrinsic::x86_mmx_psra_w;
6727 break;
6728 case Intrinsic::x86_mmx_psrai_d:
6729 NewIntNo = Intrinsic::x86_mmx_psra_d;
6730 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006731 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006732 }
6733 break;
6734 }
6735 }
Mon P Wangefa42202009-09-03 19:56:25 +00006736
6737 // The vector shift intrinsics with scalars uses 32b shift amounts but
6738 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6739 // to be zero.
6740 SDValue ShOps[4];
6741 ShOps[0] = ShAmt;
6742 ShOps[1] = DAG.getConstant(0, MVT::i32);
6743 if (ShAmtVT == MVT::v4i32) {
6744 ShOps[2] = DAG.getUNDEF(MVT::i32);
6745 ShOps[3] = DAG.getUNDEF(MVT::i32);
6746 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6747 } else {
6748 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6749 }
6750
Owen Andersone50ed302009-08-10 22:56:29 +00006751 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006752 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006753 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006754 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006755 Op.getOperand(1), ShAmt);
6756 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006757 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006758}
Evan Cheng72261582005-12-20 06:22:03 +00006759
Dan Gohman475871a2008-07-27 21:46:04 +00006760SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006761 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006762 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006763
6764 if (Depth > 0) {
6765 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6766 SDValue Offset =
6767 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006768 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006769 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006770 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006771 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006772 NULL, 0);
6773 }
6774
6775 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006776 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006777 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006778 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006779}
6780
Dan Gohman475871a2008-07-27 21:46:04 +00006781SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006782 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6783 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006784 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006785 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006786 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6787 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006788 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006789 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006790 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006791 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006792}
6793
Dan Gohman475871a2008-07-27 21:46:04 +00006794SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006795 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006796 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006797}
6798
Dan Gohman475871a2008-07-27 21:46:04 +00006799SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006800{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006801 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue Chain = Op.getOperand(0);
6803 SDValue Offset = Op.getOperand(1);
6804 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006805 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006806
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006807 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6808 getPointerTy());
6809 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006810
Dale Johannesene4d209d2009-02-03 20:21:25 +00006811 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006812 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006813 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6814 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006815 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006816 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006817
Dale Johannesene4d209d2009-02-03 20:21:25 +00006818 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006819 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006820 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006821}
6822
Dan Gohman475871a2008-07-27 21:46:04 +00006823SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006824 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006825 SDValue Root = Op.getOperand(0);
6826 SDValue Trmp = Op.getOperand(1); // trampoline
6827 SDValue FPtr = Op.getOperand(2); // nested function
6828 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006829 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006830
Dan Gohman69de1932008-02-06 22:27:42 +00006831 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006832
Duncan Sands339e14f2008-01-16 22:55:25 +00006833 const X86InstrInfo *TII =
6834 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6835
Duncan Sandsb116fac2007-07-27 20:02:49 +00006836 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006837 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006838
6839 // Large code-model.
6840
6841 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6842 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6843
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006844 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6845 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006846
6847 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6848
6849 // Load the pointer to the nested function into R11.
6850 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006851 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006852 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006853 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006854
Owen Anderson825b72b2009-08-11 20:47:22 +00006855 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6856 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006857 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006858
6859 // Load the 'nest' parameter value into R10.
6860 // R10 is specified in X86CallingConv.td
6861 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6863 DAG.getConstant(10, MVT::i64));
6864 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006865 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006866
Owen Anderson825b72b2009-08-11 20:47:22 +00006867 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6868 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006869 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006870
6871 // Jump to the nested function.
6872 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006873 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6874 DAG.getConstant(20, MVT::i64));
6875 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006876 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006877
6878 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006879 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6880 DAG.getConstant(22, MVT::i64));
6881 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006882 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006883
Dan Gohman475871a2008-07-27 21:46:04 +00006884 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006886 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006887 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006888 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006889 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006890 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006891 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006892
6893 switch (CC) {
6894 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006895 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006896 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006897 case CallingConv::X86_StdCall: {
6898 // Pass 'nest' parameter in ECX.
6899 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006900 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006901
6902 // Check that ECX wasn't needed by an 'inreg' parameter.
6903 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006904 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006905
Chris Lattner58d74912008-03-12 17:45:29 +00006906 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006907 unsigned InRegCount = 0;
6908 unsigned Idx = 1;
6909
6910 for (FunctionType::param_iterator I = FTy->param_begin(),
6911 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006912 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006913 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006914 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006915
6916 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006917 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006918 }
6919 }
6920 break;
6921 }
6922 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006923 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006924 // Pass 'nest' parameter in EAX.
6925 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006926 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006927 break;
6928 }
6929
Dan Gohman475871a2008-07-27 21:46:04 +00006930 SDValue OutChains[4];
6931 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006932
Owen Anderson825b72b2009-08-11 20:47:22 +00006933 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6934 DAG.getConstant(10, MVT::i32));
6935 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006936
Duncan Sands339e14f2008-01-16 22:55:25 +00006937 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006938 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006939 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006941 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006942
Owen Anderson825b72b2009-08-11 20:47:22 +00006943 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6944 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006945 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006946
Duncan Sands339e14f2008-01-16 22:55:25 +00006947 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6949 DAG.getConstant(5, MVT::i32));
6950 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006951 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006952
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6954 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006955 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006956
Dan Gohman475871a2008-07-27 21:46:04 +00006957 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006958 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006959 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006960 }
6961}
6962
Dan Gohman475871a2008-07-27 21:46:04 +00006963SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006964 /*
6965 The rounding mode is in bits 11:10 of FPSR, and has the following
6966 settings:
6967 00 Round to nearest
6968 01 Round to -inf
6969 10 Round to +inf
6970 11 Round to 0
6971
6972 FLT_ROUNDS, on the other hand, expects the following:
6973 -1 Undefined
6974 0 Round to 0
6975 1 Round to nearest
6976 2 Round to +inf
6977 3 Round to -inf
6978
6979 To perform the conversion, we do:
6980 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6981 */
6982
6983 MachineFunction &MF = DAG.getMachineFunction();
6984 const TargetMachine &TM = MF.getTarget();
6985 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6986 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00006987 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006988 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006989
6990 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00006991 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006993
Owen Anderson825b72b2009-08-11 20:47:22 +00006994 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006995 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006996
6997 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006999
7000 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007001 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007002 DAG.getNode(ISD::SRL, dl, MVT::i16,
7003 DAG.getNode(ISD::AND, dl, MVT::i16,
7004 CWD, DAG.getConstant(0x800, MVT::i16)),
7005 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007006 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 DAG.getNode(ISD::SRL, dl, MVT::i16,
7008 DAG.getNode(ISD::AND, dl, MVT::i16,
7009 CWD, DAG.getConstant(0x400, MVT::i16)),
7010 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007011
Dan Gohman475871a2008-07-27 21:46:04 +00007012 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 DAG.getNode(ISD::AND, dl, MVT::i16,
7014 DAG.getNode(ISD::ADD, dl, MVT::i16,
7015 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7016 DAG.getConstant(1, MVT::i16)),
7017 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007018
7019
Duncan Sands83ec4b62008-06-06 12:08:01 +00007020 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007021 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007022}
7023
Dan Gohman475871a2008-07-27 21:46:04 +00007024SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007025 EVT VT = Op.getValueType();
7026 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007027 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007028 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007029
7030 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007031 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007032 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007034 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007035 }
Evan Cheng18efe262007-12-14 02:13:44 +00007036
Evan Cheng152804e2007-12-14 08:30:15 +00007037 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007038 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007039 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007040
7041 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007042 SDValue Ops[] = {
7043 Op,
7044 DAG.getConstant(NumBits+NumBits-1, OpVT),
7045 DAG.getConstant(X86::COND_E, MVT::i8),
7046 Op.getValue(1)
7047 };
7048 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007049
7050 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007051 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007052
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 if (VT == MVT::i8)
7054 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007055 return Op;
7056}
7057
Dan Gohman475871a2008-07-27 21:46:04 +00007058SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007059 EVT VT = Op.getValueType();
7060 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007061 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007062 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007063
7064 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 if (VT == MVT::i8) {
7066 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007067 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007068 }
Evan Cheng152804e2007-12-14 08:30:15 +00007069
7070 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007071 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007072 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007073
7074 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007075 SDValue Ops[] = {
7076 Op,
7077 DAG.getConstant(NumBits, OpVT),
7078 DAG.getConstant(X86::COND_E, MVT::i8),
7079 Op.getValue(1)
7080 };
7081 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007082
Owen Anderson825b72b2009-08-11 20:47:22 +00007083 if (VT == MVT::i8)
7084 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007085 return Op;
7086}
7087
Mon P Wangaf9b9522008-12-18 21:42:19 +00007088SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007089 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007091 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007092
Mon P Wangaf9b9522008-12-18 21:42:19 +00007093 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7094 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7095 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7096 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7097 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7098 //
7099 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7100 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7101 // return AloBlo + AloBhi + AhiBlo;
7102
7103 SDValue A = Op.getOperand(0);
7104 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007105
Dale Johannesene4d209d2009-02-03 20:21:25 +00007106 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007107 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7108 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007109 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007110 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7111 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007112 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007114 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007115 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007117 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007118 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007119 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007120 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007121 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7123 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007124 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7126 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007127 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7128 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007129 return Res;
7130}
7131
7132
Bill Wendling74c37652008-12-09 22:08:41 +00007133SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7134 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7135 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007136 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7137 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007138 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007139 SDValue LHS = N->getOperand(0);
7140 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007141 unsigned BaseOp = 0;
7142 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007143 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007144
7145 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007146 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007147 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007148 // A subtract of one will be selected as a INC. Note that INC doesn't
7149 // set CF, so we can't do this for UADDO.
7150 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7151 if (C->getAPIntValue() == 1) {
7152 BaseOp = X86ISD::INC;
7153 Cond = X86::COND_O;
7154 break;
7155 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007156 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007157 Cond = X86::COND_O;
7158 break;
7159 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007160 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007161 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007162 break;
7163 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007164 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7165 // set CF, so we can't do this for USUBO.
7166 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7167 if (C->getAPIntValue() == 1) {
7168 BaseOp = X86ISD::DEC;
7169 Cond = X86::COND_O;
7170 break;
7171 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007172 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007173 Cond = X86::COND_O;
7174 break;
7175 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007176 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007177 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007178 break;
7179 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007180 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007181 Cond = X86::COND_O;
7182 break;
7183 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007184 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007185 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007186 break;
7187 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007188
Bill Wendling61edeb52008-12-02 01:06:39 +00007189 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007191 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007192
Bill Wendling61edeb52008-12-02 01:06:39 +00007193 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007194 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007195 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007196
Bill Wendling61edeb52008-12-02 01:06:39 +00007197 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7198 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007199}
7200
Dan Gohman475871a2008-07-27 21:46:04 +00007201SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007202 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007203 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007204 unsigned Reg = 0;
7205 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007207 default:
7208 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007209 case MVT::i8: Reg = X86::AL; size = 1; break;
7210 case MVT::i16: Reg = X86::AX; size = 2; break;
7211 case MVT::i32: Reg = X86::EAX; size = 4; break;
7212 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007213 assert(Subtarget->is64Bit() && "Node not type legal!");
7214 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007215 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007216 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007217 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007218 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007219 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007220 Op.getOperand(1),
7221 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007222 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007223 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007225 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007226 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007227 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007228 return cpOut;
7229}
7230
Duncan Sands1607f052008-12-01 11:39:25 +00007231SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007232 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007233 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007235 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007236 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007237 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007238 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7239 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007240 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007241 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7242 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007243 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007245 rdx.getValue(1)
7246 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007247 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007248}
7249
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007250SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7251 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007252 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007253 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007255 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007256 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007257 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007258 Node->getOperand(0),
7259 Node->getOperand(1), negOp,
7260 cast<AtomicSDNode>(Node)->getSrcValue(),
7261 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007262}
7263
Evan Cheng0db9fe62006-04-25 20:13:52 +00007264/// LowerOperation - Provide custom lowering hooks for some operations.
7265///
Dan Gohman475871a2008-07-27 21:46:04 +00007266SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007267 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007268 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007269 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7270 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007271 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007272 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007273 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7274 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7275 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7276 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7277 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7278 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007279 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007280 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007281 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007282 case ISD::SHL_PARTS:
7283 case ISD::SRA_PARTS:
7284 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7285 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007286 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007287 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007288 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289 case ISD::FABS: return LowerFABS(Op, DAG);
7290 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007291 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007292 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007293 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007294 case ISD::SELECT: return LowerSELECT(Op, DAG);
7295 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007296 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007297 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007298 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007299 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007300 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007301 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7302 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007303 case ISD::FRAME_TO_ARGS_OFFSET:
7304 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007305 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007306 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007307 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007308 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007309 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7310 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007311 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007312 case ISD::SADDO:
7313 case ISD::UADDO:
7314 case ISD::SSUBO:
7315 case ISD::USUBO:
7316 case ISD::SMULO:
7317 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007318 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007319 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007320}
7321
Duncan Sands1607f052008-12-01 11:39:25 +00007322void X86TargetLowering::
7323ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7324 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007325 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007326 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007328
7329 SDValue Chain = Node->getOperand(0);
7330 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007331 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007332 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007334 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007335 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007336 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007337 SDValue Result =
7338 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7339 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007340 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007341 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007342 Results.push_back(Result.getValue(2));
7343}
7344
Duncan Sands126d9072008-07-04 11:47:58 +00007345/// ReplaceNodeResults - Replace a node with an illegal result type
7346/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007347void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7348 SmallVectorImpl<SDValue>&Results,
7349 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007350 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007351 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007352 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007353 assert(false && "Do not know how to custom type legalize this operation!");
7354 return;
7355 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007356 std::pair<SDValue,SDValue> Vals =
7357 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007358 SDValue FIST = Vals.first, StackSlot = Vals.second;
7359 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007360 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007361 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007362 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007363 }
7364 return;
7365 }
7366 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007367 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007368 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007369 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007370 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007371 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007373 eax.getValue(2));
7374 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7375 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007376 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007377 Results.push_back(edx.getValue(1));
7378 return;
7379 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007380 case ISD::SDIV:
7381 case ISD::UDIV:
7382 case ISD::SREM:
7383 case ISD::UREM: {
7384 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7385 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7386 return;
7387 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007388 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007389 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007390 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007391 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007392 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7393 DAG.getConstant(0, MVT::i32));
7394 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7395 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007396 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7397 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007398 cpInL.getValue(1));
7399 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007400 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7401 DAG.getConstant(0, MVT::i32));
7402 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7403 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007404 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007405 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007406 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007407 swapInL.getValue(1));
7408 SDValue Ops[] = { swapInH.getValue(0),
7409 N->getOperand(1),
7410 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007413 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007414 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007415 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007417 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007418 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007419 Results.push_back(cpOutH.getValue(1));
7420 return;
7421 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007422 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007423 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7424 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007425 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007426 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7427 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007428 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007429 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7430 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007431 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007432 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7433 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007434 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007435 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7436 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007437 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007438 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7439 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007440 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007441 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7442 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007443 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007444}
7445
Evan Cheng72261582005-12-20 06:22:03 +00007446const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7447 switch (Opcode) {
7448 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007449 case X86ISD::BSF: return "X86ISD::BSF";
7450 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007451 case X86ISD::SHLD: return "X86ISD::SHLD";
7452 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007453 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007454 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007455 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007456 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007457 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007458 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007459 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7460 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7461 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007462 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007463 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007464 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007465 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007466 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007467 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007468 case X86ISD::COMI: return "X86ISD::COMI";
7469 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007470 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007471 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007472 case X86ISD::CMOV: return "X86ISD::CMOV";
7473 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007474 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007475 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7476 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007477 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007478 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007479 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007480 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007481 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007482 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7483 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007484 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007485 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007486 case X86ISD::FMAX: return "X86ISD::FMAX";
7487 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007488 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7489 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007491 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007492 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007493 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007494 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007495 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7496 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007497 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7498 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7499 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7500 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7501 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7502 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007503 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7504 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007505 case X86ISD::VSHL: return "X86ISD::VSHL";
7506 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007507 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7508 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7509 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7510 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7511 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7512 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7513 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7514 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7515 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7516 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007517 case X86ISD::ADD: return "X86ISD::ADD";
7518 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007519 case X86ISD::SMUL: return "X86ISD::SMUL";
7520 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007521 case X86ISD::INC: return "X86ISD::INC";
7522 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007523 case X86ISD::OR: return "X86ISD::OR";
7524 case X86ISD::XOR: return "X86ISD::XOR";
7525 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007526 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007527 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007528 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007529 }
7530}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007531
Chris Lattnerc9addb72007-03-30 23:15:24 +00007532// isLegalAddressingMode - Return true if the addressing mode represented
7533// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007534bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007535 const Type *Ty) const {
7536 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007537 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007538
Chris Lattnerc9addb72007-03-30 23:15:24 +00007539 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007540 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007541 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007542
Chris Lattnerc9addb72007-03-30 23:15:24 +00007543 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007544 unsigned GVFlags =
7545 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007546
Chris Lattnerdfed4132009-07-10 07:38:24 +00007547 // If a reference to this global requires an extra load, we can't fold it.
7548 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007549 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007550
Chris Lattnerdfed4132009-07-10 07:38:24 +00007551 // If BaseGV requires a register for the PIC base, we cannot also have a
7552 // BaseReg specified.
7553 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007554 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007555
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007556 // If lower 4G is not available, then we must use rip-relative addressing.
7557 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7558 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007559 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007560
Chris Lattnerc9addb72007-03-30 23:15:24 +00007561 switch (AM.Scale) {
7562 case 0:
7563 case 1:
7564 case 2:
7565 case 4:
7566 case 8:
7567 // These scales always work.
7568 break;
7569 case 3:
7570 case 5:
7571 case 9:
7572 // These scales are formed with basereg+scalereg. Only accept if there is
7573 // no basereg yet.
7574 if (AM.HasBaseReg)
7575 return false;
7576 break;
7577 default: // Other stuff never works.
7578 return false;
7579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007580
Chris Lattnerc9addb72007-03-30 23:15:24 +00007581 return true;
7582}
7583
7584
Evan Cheng2bd122c2007-10-26 01:56:11 +00007585bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7586 if (!Ty1->isInteger() || !Ty2->isInteger())
7587 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007588 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7589 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007590 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007591 return false;
7592 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007593}
7594
Owen Andersone50ed302009-08-10 22:56:29 +00007595bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007596 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007597 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007598 unsigned NumBits1 = VT1.getSizeInBits();
7599 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007600 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007601 return false;
7602 return Subtarget->is64Bit() || NumBits1 < 64;
7603}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007604
Dan Gohman97121ba2009-04-08 00:15:30 +00007605bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007606 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007607 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007608}
7609
Owen Andersone50ed302009-08-10 22:56:29 +00007610bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007611 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007612 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007613}
7614
Owen Andersone50ed302009-08-10 22:56:29 +00007615bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007616 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007617 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007618}
7619
Evan Cheng60c07e12006-07-05 22:17:51 +00007620/// isShuffleMaskLegal - Targets can use this to indicate that they only
7621/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7622/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7623/// are assumed to be legal.
7624bool
Eric Christopherfd179292009-08-27 18:07:15 +00007625X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007626 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007627 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007628 if (VT.getSizeInBits() == 64)
7629 return false;
7630
Nate Begemana09008b2009-10-19 02:17:23 +00007631 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007632 return (VT.getVectorNumElements() == 2 ||
7633 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7634 isMOVLMask(M, VT) ||
7635 isSHUFPMask(M, VT) ||
7636 isPSHUFDMask(M, VT) ||
7637 isPSHUFHWMask(M, VT) ||
7638 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007639 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007640 isUNPCKLMask(M, VT) ||
7641 isUNPCKHMask(M, VT) ||
7642 isUNPCKL_v_undef_Mask(M, VT) ||
7643 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007644}
7645
Dan Gohman7d8143f2008-04-09 20:09:42 +00007646bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007647X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007648 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007649 unsigned NumElts = VT.getVectorNumElements();
7650 // FIXME: This collection of masks seems suspect.
7651 if (NumElts == 2)
7652 return true;
7653 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7654 return (isMOVLMask(Mask, VT) ||
7655 isCommutedMOVLMask(Mask, VT, true) ||
7656 isSHUFPMask(Mask, VT) ||
7657 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007658 }
7659 return false;
7660}
7661
7662//===----------------------------------------------------------------------===//
7663// X86 Scheduler Hooks
7664//===----------------------------------------------------------------------===//
7665
Mon P Wang63307c32008-05-05 19:05:59 +00007666// private utility function
7667MachineBasicBlock *
7668X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7669 MachineBasicBlock *MBB,
7670 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007671 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007672 unsigned LoadOpc,
7673 unsigned CXchgOpc,
7674 unsigned copyOpc,
7675 unsigned notOpc,
7676 unsigned EAXreg,
7677 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007678 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007679 // For the atomic bitwise operator, we generate
7680 // thisMBB:
7681 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007682 // ld t1 = [bitinstr.addr]
7683 // op t2 = t1, [bitinstr.val]
7684 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007685 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7686 // bz newMBB
7687 // fallthrough -->nextMBB
7688 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7689 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007690 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007691 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007692
Mon P Wang63307c32008-05-05 19:05:59 +00007693 /// First build the CFG
7694 MachineFunction *F = MBB->getParent();
7695 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007696 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7697 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7698 F->insert(MBBIter, newMBB);
7699 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007700
Mon P Wang63307c32008-05-05 19:05:59 +00007701 // Move all successors to thisMBB to nextMBB
7702 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007703
Mon P Wang63307c32008-05-05 19:05:59 +00007704 // Update thisMBB to fall through to newMBB
7705 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007706
Mon P Wang63307c32008-05-05 19:05:59 +00007707 // newMBB jumps to itself and fall through to nextMBB
7708 newMBB->addSuccessor(nextMBB);
7709 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007710
Mon P Wang63307c32008-05-05 19:05:59 +00007711 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007712 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007713 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007714 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007715 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007716 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007717 int numArgs = bInstr->getNumOperands() - 1;
7718 for (int i=0; i < numArgs; ++i)
7719 argOpers[i] = &bInstr->getOperand(i+1);
7720
7721 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007722 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7723 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007724
Dale Johannesen140be2d2008-08-19 18:47:28 +00007725 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007726 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007727 for (int i=0; i <= lastAddrIndx; ++i)
7728 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007729
Dale Johannesen140be2d2008-08-19 18:47:28 +00007730 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007731 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007732 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007733 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007734 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007735 tt = t1;
7736
Dale Johannesen140be2d2008-08-19 18:47:28 +00007737 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007738 assert((argOpers[valArgIndx]->isReg() ||
7739 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007740 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007741 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007742 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007743 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007744 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007745 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007746 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007747
Dale Johannesene4d209d2009-02-03 20:21:25 +00007748 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007749 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007750
Dale Johannesene4d209d2009-02-03 20:21:25 +00007751 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007752 for (int i=0; i <= lastAddrIndx; ++i)
7753 (*MIB).addOperand(*argOpers[i]);
7754 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007755 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007756 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7757 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007758
Dale Johannesene4d209d2009-02-03 20:21:25 +00007759 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007760 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007761
Mon P Wang63307c32008-05-05 19:05:59 +00007762 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007763 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007764
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007765 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007766 return nextMBB;
7767}
7768
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007769// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007770MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007771X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7772 MachineBasicBlock *MBB,
7773 unsigned regOpcL,
7774 unsigned regOpcH,
7775 unsigned immOpcL,
7776 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007777 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007778 // For the atomic bitwise operator, we generate
7779 // thisMBB (instructions are in pairs, except cmpxchg8b)
7780 // ld t1,t2 = [bitinstr.addr]
7781 // newMBB:
7782 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7783 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007784 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007785 // mov ECX, EBX <- t5, t6
7786 // mov EAX, EDX <- t1, t2
7787 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7788 // mov t3, t4 <- EAX, EDX
7789 // bz newMBB
7790 // result in out1, out2
7791 // fallthrough -->nextMBB
7792
7793 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7794 const unsigned LoadOpc = X86::MOV32rm;
7795 const unsigned copyOpc = X86::MOV32rr;
7796 const unsigned NotOpc = X86::NOT32r;
7797 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7798 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7799 MachineFunction::iterator MBBIter = MBB;
7800 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007801
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007802 /// First build the CFG
7803 MachineFunction *F = MBB->getParent();
7804 MachineBasicBlock *thisMBB = MBB;
7805 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7806 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7807 F->insert(MBBIter, newMBB);
7808 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007809
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007810 // Move all successors to thisMBB to nextMBB
7811 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007812
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007813 // Update thisMBB to fall through to newMBB
7814 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007815
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007816 // newMBB jumps to itself and fall through to nextMBB
7817 newMBB->addSuccessor(nextMBB);
7818 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007819
Dale Johannesene4d209d2009-02-03 20:21:25 +00007820 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007821 // Insert instructions into newMBB based on incoming instruction
7822 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007823 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007824 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007825 MachineOperand& dest1Oper = bInstr->getOperand(0);
7826 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007827 MachineOperand* argOpers[2 + X86AddrNumOperands];
7828 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007829 argOpers[i] = &bInstr->getOperand(i+2);
7830
Evan Chengad5b52f2010-01-08 19:14:57 +00007831 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007832 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007833
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007834 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007835 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007836 for (int i=0; i <= lastAddrIndx; ++i)
7837 (*MIB).addOperand(*argOpers[i]);
7838 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007839 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007840 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007841 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007842 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007843 MachineOperand newOp3 = *(argOpers[3]);
7844 if (newOp3.isImm())
7845 newOp3.setImm(newOp3.getImm()+4);
7846 else
7847 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007848 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007849 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007850
7851 // t3/4 are defined later, at the bottom of the loop
7852 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7853 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007854 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007855 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007856 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007857 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7858
Evan Cheng306b4ca2010-01-08 23:41:50 +00007859 // The subsequent operations should be using the destination registers of
7860 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007861 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007862 t1 = F->getRegInfo().createVirtualRegister(RC);
7863 t2 = F->getRegInfo().createVirtualRegister(RC);
7864 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7865 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007866 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007867 t1 = dest1Oper.getReg();
7868 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007869 }
7870
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007871 int valArgIndx = lastAddrIndx + 1;
7872 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007873 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007874 "invalid operand");
7875 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7876 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007877 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007878 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007879 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007880 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007881 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007882 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007883 (*MIB).addOperand(*argOpers[valArgIndx]);
7884 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007885 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007886 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007887 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007888 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007889 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007890 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007891 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007892 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007893 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007894 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007895
Dale Johannesene4d209d2009-02-03 20:21:25 +00007896 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007897 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007898 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007899 MIB.addReg(t2);
7900
Dale Johannesene4d209d2009-02-03 20:21:25 +00007901 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007902 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007903 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007904 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007905
Dale Johannesene4d209d2009-02-03 20:21:25 +00007906 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007907 for (int i=0; i <= lastAddrIndx; ++i)
7908 (*MIB).addOperand(*argOpers[i]);
7909
7910 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007911 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7912 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007913
Dale Johannesene4d209d2009-02-03 20:21:25 +00007914 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007915 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007916 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007917 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007918
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007919 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007921
7922 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7923 return nextMBB;
7924}
7925
7926// private utility function
7927MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007928X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7929 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007930 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007931 // For the atomic min/max operator, we generate
7932 // thisMBB:
7933 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007934 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007935 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007936 // cmp t1, t2
7937 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007938 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007939 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7940 // bz newMBB
7941 // fallthrough -->nextMBB
7942 //
7943 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7944 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007945 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007946 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007947
Mon P Wang63307c32008-05-05 19:05:59 +00007948 /// First build the CFG
7949 MachineFunction *F = MBB->getParent();
7950 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007951 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7952 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7953 F->insert(MBBIter, newMBB);
7954 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007955
Dan Gohmand6708ea2009-08-15 01:38:56 +00007956 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00007957 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007958
Mon P Wang63307c32008-05-05 19:05:59 +00007959 // Update thisMBB to fall through to newMBB
7960 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007961
Mon P Wang63307c32008-05-05 19:05:59 +00007962 // newMBB jumps to newMBB and fall through to nextMBB
7963 newMBB->addSuccessor(nextMBB);
7964 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007965
Dale Johannesene4d209d2009-02-03 20:21:25 +00007966 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007967 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007968 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007969 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007970 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007971 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007972 int numArgs = mInstr->getNumOperands() - 1;
7973 for (int i=0; i < numArgs; ++i)
7974 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007975
Mon P Wang63307c32008-05-05 19:05:59 +00007976 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007977 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7978 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007979
Mon P Wangab3e7472008-05-05 22:56:23 +00007980 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007982 for (int i=0; i <= lastAddrIndx; ++i)
7983 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007984
Mon P Wang63307c32008-05-05 19:05:59 +00007985 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007986 assert((argOpers[valArgIndx]->isReg() ||
7987 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007988 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007989
7990 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007991 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007992 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007993 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007995 (*MIB).addOperand(*argOpers[valArgIndx]);
7996
Dale Johannesene4d209d2009-02-03 20:21:25 +00007997 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007998 MIB.addReg(t1);
7999
Dale Johannesene4d209d2009-02-03 20:21:25 +00008000 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008001 MIB.addReg(t1);
8002 MIB.addReg(t2);
8003
8004 // Generate movc
8005 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008006 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008007 MIB.addReg(t2);
8008 MIB.addReg(t1);
8009
8010 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008011 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008012 for (int i=0; i <= lastAddrIndx; ++i)
8013 (*MIB).addOperand(*argOpers[i]);
8014 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008015 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008016 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8017 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008018
Dale Johannesene4d209d2009-02-03 20:21:25 +00008019 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008020 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008021
Mon P Wang63307c32008-05-05 19:05:59 +00008022 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008023 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008024
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008025 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008026 return nextMBB;
8027}
8028
Eric Christopherf83a5de2009-08-27 18:08:16 +00008029// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8030// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008031MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008032X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008033 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008034
8035 MachineFunction *F = BB->getParent();
8036 DebugLoc dl = MI->getDebugLoc();
8037 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8038
8039 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008040 if (memArg)
8041 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8042 else
8043 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008044
8045 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8046
8047 for (unsigned i = 0; i < numArgs; ++i) {
8048 MachineOperand &Op = MI->getOperand(i+1);
8049
8050 if (!(Op.isReg() && Op.isImplicit()))
8051 MIB.addOperand(Op);
8052 }
8053
8054 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8055 .addReg(X86::XMM0);
8056
8057 F->DeleteMachineInstr(MI);
8058
8059 return BB;
8060}
8061
8062MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008063X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8064 MachineInstr *MI,
8065 MachineBasicBlock *MBB) const {
8066 // Emit code to save XMM registers to the stack. The ABI says that the
8067 // number of registers to save is given in %al, so it's theoretically
8068 // possible to do an indirect jump trick to avoid saving all of them,
8069 // however this code takes a simpler approach and just executes all
8070 // of the stores if %al is non-zero. It's less code, and it's probably
8071 // easier on the hardware branch predictor, and stores aren't all that
8072 // expensive anyway.
8073
8074 // Create the new basic blocks. One block contains all the XMM stores,
8075 // and one block is the final destination regardless of whether any
8076 // stores were performed.
8077 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8078 MachineFunction *F = MBB->getParent();
8079 MachineFunction::iterator MBBIter = MBB;
8080 ++MBBIter;
8081 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8082 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8083 F->insert(MBBIter, XMMSaveMBB);
8084 F->insert(MBBIter, EndMBB);
8085
8086 // Set up the CFG.
8087 // Move any original successors of MBB to the end block.
8088 EndMBB->transferSuccessors(MBB);
8089 // The original block will now fall through to the XMM save block.
8090 MBB->addSuccessor(XMMSaveMBB);
8091 // The XMMSaveMBB will fall through to the end block.
8092 XMMSaveMBB->addSuccessor(EndMBB);
8093
8094 // Now add the instructions.
8095 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8096 DebugLoc DL = MI->getDebugLoc();
8097
8098 unsigned CountReg = MI->getOperand(0).getReg();
8099 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8100 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8101
8102 if (!Subtarget->isTargetWin64()) {
8103 // If %al is 0, branch around the XMM save block.
8104 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8105 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8106 MBB->addSuccessor(EndMBB);
8107 }
8108
8109 // In the XMM save block, save all the XMM argument registers.
8110 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8111 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008112 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008113 F->getMachineMemOperand(
8114 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8115 MachineMemOperand::MOStore, Offset,
8116 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008117 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8118 .addFrameIndex(RegSaveFrameIndex)
8119 .addImm(/*Scale=*/1)
8120 .addReg(/*IndexReg=*/0)
8121 .addImm(/*Disp=*/Offset)
8122 .addReg(/*Segment=*/0)
8123 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008124 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008125 }
8126
8127 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8128
8129 return EndMBB;
8130}
Mon P Wang63307c32008-05-05 19:05:59 +00008131
Evan Cheng60c07e12006-07-05 22:17:51 +00008132MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008133X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008134 MachineBasicBlock *BB,
8135 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008136 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8137 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008138
Chris Lattner52600972009-09-02 05:57:00 +00008139 // To "insert" a SELECT_CC instruction, we actually have to insert the
8140 // diamond control-flow pattern. The incoming instruction knows the
8141 // destination vreg to set, the condition code register to branch on, the
8142 // true/false values to select between, and a branch opcode to use.
8143 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8144 MachineFunction::iterator It = BB;
8145 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008146
Chris Lattner52600972009-09-02 05:57:00 +00008147 // thisMBB:
8148 // ...
8149 // TrueVal = ...
8150 // cmpTY ccX, r1, r2
8151 // bCC copy1MBB
8152 // fallthrough --> copy0MBB
8153 MachineBasicBlock *thisMBB = BB;
8154 MachineFunction *F = BB->getParent();
8155 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8156 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8157 unsigned Opc =
8158 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8159 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8160 F->insert(It, copy0MBB);
8161 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008162 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008163 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008164 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008165 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008166 E = BB->succ_end(); I != E; ++I) {
8167 EM->insert(std::make_pair(*I, sinkMBB));
8168 sinkMBB->addSuccessor(*I);
8169 }
8170 // Next, remove all successors of the current block, and add the true
8171 // and fallthrough blocks as its successors.
8172 while (!BB->succ_empty())
8173 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008174 // Add the true and fallthrough blocks as its successors.
8175 BB->addSuccessor(copy0MBB);
8176 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008177
Chris Lattner52600972009-09-02 05:57:00 +00008178 // copy0MBB:
8179 // %FalseValue = ...
8180 // # fallthrough to sinkMBB
8181 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008182
Chris Lattner52600972009-09-02 05:57:00 +00008183 // Update machine-CFG edges
8184 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008185
Chris Lattner52600972009-09-02 05:57:00 +00008186 // sinkMBB:
8187 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8188 // ...
8189 BB = sinkMBB;
8190 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8191 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8192 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8193
8194 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8195 return BB;
8196}
8197
8198
8199MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008200X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008201 MachineBasicBlock *BB,
8202 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008203 switch (MI->getOpcode()) {
8204 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008205 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008206 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008207 case X86::CMOV_FR32:
8208 case X86::CMOV_FR64:
8209 case X86::CMOV_V4F32:
8210 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008211 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008212 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008213
Dale Johannesen849f2142007-07-03 00:53:03 +00008214 case X86::FP32_TO_INT16_IN_MEM:
8215 case X86::FP32_TO_INT32_IN_MEM:
8216 case X86::FP32_TO_INT64_IN_MEM:
8217 case X86::FP64_TO_INT16_IN_MEM:
8218 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008219 case X86::FP64_TO_INT64_IN_MEM:
8220 case X86::FP80_TO_INT16_IN_MEM:
8221 case X86::FP80_TO_INT32_IN_MEM:
8222 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008223 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8224 DebugLoc DL = MI->getDebugLoc();
8225
Evan Cheng60c07e12006-07-05 22:17:51 +00008226 // Change the floating point control register to use "round towards zero"
8227 // mode when truncating to an integer value.
8228 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008229 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008230 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008231
8232 // Load the old value of the high byte of the control word...
8233 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008234 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008235 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008236 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008237
8238 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008239 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008240 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008241
8242 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008243 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008244
8245 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008246 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008247 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008248
8249 // Get the X86 opcode to use.
8250 unsigned Opc;
8251 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008252 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008253 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8254 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8255 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8256 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8257 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8258 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008259 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8260 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8261 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008262 }
8263
8264 X86AddressMode AM;
8265 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008266 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008267 AM.BaseType = X86AddressMode::RegBase;
8268 AM.Base.Reg = Op.getReg();
8269 } else {
8270 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008271 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008272 }
8273 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008274 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008275 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008276 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008277 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008278 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008279 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008280 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008281 AM.GV = Op.getGlobal();
8282 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008283 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008284 }
Chris Lattner52600972009-09-02 05:57:00 +00008285 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008286 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008287
8288 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008289 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008290
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008291 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008292 return BB;
8293 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008294 // String/text processing lowering.
8295 case X86::PCMPISTRM128REG:
8296 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8297 case X86::PCMPISTRM128MEM:
8298 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8299 case X86::PCMPESTRM128REG:
8300 return EmitPCMP(MI, BB, 5, false /* in mem */);
8301 case X86::PCMPESTRM128MEM:
8302 return EmitPCMP(MI, BB, 5, true /* in mem */);
8303
8304 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008305 case X86::ATOMAND32:
8306 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008307 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008308 X86::LCMPXCHG32, X86::MOV32rr,
8309 X86::NOT32r, X86::EAX,
8310 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008311 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008312 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8313 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008314 X86::LCMPXCHG32, X86::MOV32rr,
8315 X86::NOT32r, X86::EAX,
8316 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008317 case X86::ATOMXOR32:
8318 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008319 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008320 X86::LCMPXCHG32, X86::MOV32rr,
8321 X86::NOT32r, X86::EAX,
8322 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008323 case X86::ATOMNAND32:
8324 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008325 X86::AND32ri, X86::MOV32rm,
8326 X86::LCMPXCHG32, X86::MOV32rr,
8327 X86::NOT32r, X86::EAX,
8328 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008329 case X86::ATOMMIN32:
8330 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8331 case X86::ATOMMAX32:
8332 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8333 case X86::ATOMUMIN32:
8334 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8335 case X86::ATOMUMAX32:
8336 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008337
8338 case X86::ATOMAND16:
8339 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8340 X86::AND16ri, X86::MOV16rm,
8341 X86::LCMPXCHG16, X86::MOV16rr,
8342 X86::NOT16r, X86::AX,
8343 X86::GR16RegisterClass);
8344 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008345 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008346 X86::OR16ri, X86::MOV16rm,
8347 X86::LCMPXCHG16, X86::MOV16rr,
8348 X86::NOT16r, X86::AX,
8349 X86::GR16RegisterClass);
8350 case X86::ATOMXOR16:
8351 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8352 X86::XOR16ri, X86::MOV16rm,
8353 X86::LCMPXCHG16, X86::MOV16rr,
8354 X86::NOT16r, X86::AX,
8355 X86::GR16RegisterClass);
8356 case X86::ATOMNAND16:
8357 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8358 X86::AND16ri, X86::MOV16rm,
8359 X86::LCMPXCHG16, X86::MOV16rr,
8360 X86::NOT16r, X86::AX,
8361 X86::GR16RegisterClass, true);
8362 case X86::ATOMMIN16:
8363 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8364 case X86::ATOMMAX16:
8365 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8366 case X86::ATOMUMIN16:
8367 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8368 case X86::ATOMUMAX16:
8369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8370
8371 case X86::ATOMAND8:
8372 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8373 X86::AND8ri, X86::MOV8rm,
8374 X86::LCMPXCHG8, X86::MOV8rr,
8375 X86::NOT8r, X86::AL,
8376 X86::GR8RegisterClass);
8377 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008378 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008379 X86::OR8ri, X86::MOV8rm,
8380 X86::LCMPXCHG8, X86::MOV8rr,
8381 X86::NOT8r, X86::AL,
8382 X86::GR8RegisterClass);
8383 case X86::ATOMXOR8:
8384 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8385 X86::XOR8ri, X86::MOV8rm,
8386 X86::LCMPXCHG8, X86::MOV8rr,
8387 X86::NOT8r, X86::AL,
8388 X86::GR8RegisterClass);
8389 case X86::ATOMNAND8:
8390 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8391 X86::AND8ri, X86::MOV8rm,
8392 X86::LCMPXCHG8, X86::MOV8rr,
8393 X86::NOT8r, X86::AL,
8394 X86::GR8RegisterClass, true);
8395 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008396 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008397 case X86::ATOMAND64:
8398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008399 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008400 X86::LCMPXCHG64, X86::MOV64rr,
8401 X86::NOT64r, X86::RAX,
8402 X86::GR64RegisterClass);
8403 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008404 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8405 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008406 X86::LCMPXCHG64, X86::MOV64rr,
8407 X86::NOT64r, X86::RAX,
8408 X86::GR64RegisterClass);
8409 case X86::ATOMXOR64:
8410 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008411 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008412 X86::LCMPXCHG64, X86::MOV64rr,
8413 X86::NOT64r, X86::RAX,
8414 X86::GR64RegisterClass);
8415 case X86::ATOMNAND64:
8416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8417 X86::AND64ri32, X86::MOV64rm,
8418 X86::LCMPXCHG64, X86::MOV64rr,
8419 X86::NOT64r, X86::RAX,
8420 X86::GR64RegisterClass, true);
8421 case X86::ATOMMIN64:
8422 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8423 case X86::ATOMMAX64:
8424 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8425 case X86::ATOMUMIN64:
8426 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8427 case X86::ATOMUMAX64:
8428 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008429
8430 // This group does 64-bit operations on a 32-bit host.
8431 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008432 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008433 X86::AND32rr, X86::AND32rr,
8434 X86::AND32ri, X86::AND32ri,
8435 false);
8436 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008437 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008438 X86::OR32rr, X86::OR32rr,
8439 X86::OR32ri, X86::OR32ri,
8440 false);
8441 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008442 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008443 X86::XOR32rr, X86::XOR32rr,
8444 X86::XOR32ri, X86::XOR32ri,
8445 false);
8446 case X86::ATOMNAND6432:
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 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008451 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008452 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008453 X86::ADD32rr, X86::ADC32rr,
8454 X86::ADD32ri, X86::ADC32ri,
8455 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008456 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008457 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008458 X86::SUB32rr, X86::SBB32rr,
8459 X86::SUB32ri, X86::SBB32ri,
8460 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008461 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008462 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008463 X86::MOV32rr, X86::MOV32rr,
8464 X86::MOV32ri, X86::MOV32ri,
8465 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008466 case X86::VASTART_SAVE_XMM_REGS:
8467 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008468 }
8469}
8470
8471//===----------------------------------------------------------------------===//
8472// X86 Optimization Hooks
8473//===----------------------------------------------------------------------===//
8474
Dan Gohman475871a2008-07-27 21:46:04 +00008475void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008476 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008477 APInt &KnownZero,
8478 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008479 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008480 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008481 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008482 assert((Opc >= ISD::BUILTIN_OP_END ||
8483 Opc == ISD::INTRINSIC_WO_CHAIN ||
8484 Opc == ISD::INTRINSIC_W_CHAIN ||
8485 Opc == ISD::INTRINSIC_VOID) &&
8486 "Should use MaskedValueIsZero if you don't know whether Op"
8487 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008488
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008489 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008490 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008491 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008492 case X86ISD::ADD:
8493 case X86ISD::SUB:
8494 case X86ISD::SMUL:
8495 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008496 case X86ISD::INC:
8497 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008498 case X86ISD::OR:
8499 case X86ISD::XOR:
8500 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008501 // These nodes' second result is a boolean.
8502 if (Op.getResNo() == 0)
8503 break;
8504 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008505 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008506 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8507 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008508 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008509 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008510}
Chris Lattner259e97c2006-01-31 19:43:35 +00008511
Evan Cheng206ee9d2006-07-07 08:33:52 +00008512/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008513/// node is a GlobalAddress + offset.
8514bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8515 GlobalValue* &GA, int64_t &Offset) const{
8516 if (N->getOpcode() == X86ISD::Wrapper) {
8517 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008518 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008519 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008520 return true;
8521 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008522 }
Evan Chengad4196b2008-05-12 19:56:52 +00008523 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008524}
8525
Nate Begeman9008ca62009-04-27 18:41:29 +00008526static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008527 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008528 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008529 SelectionDAG &DAG, MachineFrameInfo *MFI,
8530 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008531 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008532 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008533 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008534 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008535 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008536 return false;
8537 continue;
8538 }
8539
Dan Gohman475871a2008-07-27 21:46:04 +00008540 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008541 if (!Elt.getNode() ||
8542 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008543 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008544 if (!LDBase) {
8545 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008546 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008547 LDBase = cast<LoadSDNode>(Elt.getNode());
8548 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008549 continue;
8550 }
8551 if (Elt.getOpcode() == ISD::UNDEF)
8552 continue;
8553
Nate Begemanabc01992009-06-05 21:37:30 +00008554 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008555 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008556 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008557 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008558 }
8559 return true;
8560}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008561
8562/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8563/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8564/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008565/// order. In the case of v2i64, it will see if it can rewrite the
8566/// shuffle to be an appropriate build vector so it can take advantage of
8567// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008568static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008569 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008570 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008571 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008572 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008573 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8574 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008575
Eli Friedman7a5e5552009-06-07 06:52:44 +00008576 if (VT.getSizeInBits() != 128)
8577 return SDValue();
8578
Mon P Wang1e955802009-04-03 02:43:30 +00008579 // Try to combine a vector_shuffle into a 128-bit load.
8580 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008581 LoadSDNode *LD = NULL;
8582 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008583 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008584 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008585 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008586
Eli Friedman7a5e5552009-06-07 06:52:44 +00008587 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008588 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008589 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8590 LD->getSrcValue(), LD->getSrcValueOffset(),
8591 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008592 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008593 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008594 LD->isVolatile(), LD->getAlignment());
8595 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008596 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008597 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8598 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008599 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8600 }
8601 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008602}
Evan Chengd880b972008-05-09 21:53:03 +00008603
Chris Lattner83e6c992006-10-04 06:57:07 +00008604/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008605static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008606 const X86Subtarget *Subtarget) {
8607 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008608 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008609 // Get the LHS/RHS of the select.
8610 SDValue LHS = N->getOperand(1);
8611 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008612
Dan Gohman670e5392009-09-21 18:03:22 +00008613 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8614 // instructions have the peculiarity that if either operand is a NaN,
8615 // they chose what we call the RHS operand (and as such are not symmetric).
8616 // It happens that this matches the semantics of the common C idiom
8617 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008618 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008619 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008620 Cond.getOpcode() == ISD::SETCC) {
8621 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008622
Chris Lattner47b4ce82009-03-11 05:48:52 +00008623 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008624 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008625 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8626 switch (CC) {
8627 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008628 case ISD::SETULT:
8629 // This can be a min if we can prove that at least one of the operands
8630 // is not a nan.
8631 if (!FiniteOnlyFPMath()) {
8632 if (DAG.isKnownNeverNaN(RHS)) {
8633 // Put the potential NaN in the RHS so that SSE will preserve it.
8634 std::swap(LHS, RHS);
8635 } else if (!DAG.isKnownNeverNaN(LHS))
8636 break;
8637 }
8638 Opcode = X86ISD::FMIN;
8639 break;
8640 case ISD::SETOLE:
8641 // This can be a min if we can prove that at least one of the operands
8642 // is not a nan.
8643 if (!FiniteOnlyFPMath()) {
8644 if (DAG.isKnownNeverNaN(LHS)) {
8645 // Put the potential NaN in the RHS so that SSE will preserve it.
8646 std::swap(LHS, RHS);
8647 } else if (!DAG.isKnownNeverNaN(RHS))
8648 break;
8649 }
8650 Opcode = X86ISD::FMIN;
8651 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008652 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008653 // This can be a min, but if either operand is a NaN we need it to
8654 // preserve the original LHS.
8655 std::swap(LHS, RHS);
8656 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008657 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008658 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008659 Opcode = X86ISD::FMIN;
8660 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008661
Dan Gohman670e5392009-09-21 18:03:22 +00008662 case ISD::SETOGE:
8663 // This can be a max if we can prove that at least one of the operands
8664 // is not a nan.
8665 if (!FiniteOnlyFPMath()) {
8666 if (DAG.isKnownNeverNaN(LHS)) {
8667 // Put the potential NaN in the RHS so that SSE will preserve it.
8668 std::swap(LHS, RHS);
8669 } else if (!DAG.isKnownNeverNaN(RHS))
8670 break;
8671 }
8672 Opcode = X86ISD::FMAX;
8673 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008674 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008675 // This can be a max if we can prove that at least one of the operands
8676 // is not a nan.
8677 if (!FiniteOnlyFPMath()) {
8678 if (DAG.isKnownNeverNaN(RHS)) {
8679 // Put the potential NaN in the RHS so that SSE will preserve it.
8680 std::swap(LHS, RHS);
8681 } else if (!DAG.isKnownNeverNaN(LHS))
8682 break;
8683 }
8684 Opcode = X86ISD::FMAX;
8685 break;
8686 case ISD::SETUGE:
8687 // This can be a max, but if either operand is a NaN we need it to
8688 // preserve the original LHS.
8689 std::swap(LHS, RHS);
8690 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008691 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008692 case ISD::SETGE:
8693 Opcode = X86ISD::FMAX;
8694 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008695 }
Dan Gohman670e5392009-09-21 18:03:22 +00008696 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008697 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8698 switch (CC) {
8699 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008700 case ISD::SETOGE:
8701 // This can be a min if we can prove that at least one of the operands
8702 // is not a nan.
8703 if (!FiniteOnlyFPMath()) {
8704 if (DAG.isKnownNeverNaN(RHS)) {
8705 // Put the potential NaN in the RHS so that SSE will preserve it.
8706 std::swap(LHS, RHS);
8707 } else if (!DAG.isKnownNeverNaN(LHS))
8708 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008709 }
Dan Gohman670e5392009-09-21 18:03:22 +00008710 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008711 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008712 case ISD::SETUGT:
8713 // This can be a min if we can prove that at least one of the operands
8714 // is not a nan.
8715 if (!FiniteOnlyFPMath()) {
8716 if (DAG.isKnownNeverNaN(LHS)) {
8717 // Put the potential NaN in the RHS so that SSE will preserve it.
8718 std::swap(LHS, RHS);
8719 } else if (!DAG.isKnownNeverNaN(RHS))
8720 break;
8721 }
8722 Opcode = X86ISD::FMIN;
8723 break;
8724 case ISD::SETUGE:
8725 // This can be a min, but if either operand is a NaN we need it to
8726 // preserve the original LHS.
8727 std::swap(LHS, RHS);
8728 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008729 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008730 case ISD::SETGE:
8731 Opcode = X86ISD::FMIN;
8732 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008733
Dan Gohman670e5392009-09-21 18:03:22 +00008734 case ISD::SETULT:
8735 // This can be a max if we can prove that at least one of the operands
8736 // is not a nan.
8737 if (!FiniteOnlyFPMath()) {
8738 if (DAG.isKnownNeverNaN(LHS)) {
8739 // Put the potential NaN in the RHS so that SSE will preserve it.
8740 std::swap(LHS, RHS);
8741 } else if (!DAG.isKnownNeverNaN(RHS))
8742 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008743 }
Dan Gohman670e5392009-09-21 18:03:22 +00008744 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008745 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008746 case ISD::SETOLE:
8747 // This can be a max if we can prove that at least one of the operands
8748 // is not a nan.
8749 if (!FiniteOnlyFPMath()) {
8750 if (DAG.isKnownNeverNaN(RHS)) {
8751 // Put the potential NaN in the RHS so that SSE will preserve it.
8752 std::swap(LHS, RHS);
8753 } else if (!DAG.isKnownNeverNaN(LHS))
8754 break;
8755 }
8756 Opcode = X86ISD::FMAX;
8757 break;
8758 case ISD::SETULE:
8759 // This can be a max, but if either operand is a NaN we need it to
8760 // preserve the original LHS.
8761 std::swap(LHS, RHS);
8762 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008763 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008764 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008765 Opcode = X86ISD::FMAX;
8766 break;
8767 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008768 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008769
Chris Lattner47b4ce82009-03-11 05:48:52 +00008770 if (Opcode)
8771 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008772 }
Eric Christopherfd179292009-08-27 18:07:15 +00008773
Chris Lattnerd1980a52009-03-12 06:52:53 +00008774 // If this is a select between two integer constants, try to do some
8775 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008776 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8777 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008778 // Don't do this for crazy integer types.
8779 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8780 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008781 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008782 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008783
Chris Lattnercee56e72009-03-13 05:53:31 +00008784 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008785 // Efficiently invertible.
8786 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8787 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8788 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8789 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008790 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008791 }
Eric Christopherfd179292009-08-27 18:07:15 +00008792
Chris Lattnerd1980a52009-03-12 06:52:53 +00008793 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008794 if (FalseC->getAPIntValue() == 0 &&
8795 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008796 if (NeedsCondInvert) // Invert the condition if needed.
8797 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8798 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008799
Chris Lattnerd1980a52009-03-12 06:52:53 +00008800 // Zero extend the condition if needed.
8801 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008802
Chris Lattnercee56e72009-03-13 05:53:31 +00008803 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008804 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008805 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008806 }
Eric Christopherfd179292009-08-27 18:07:15 +00008807
Chris Lattner97a29a52009-03-13 05:22:11 +00008808 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008809 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008810 if (NeedsCondInvert) // Invert the condition if needed.
8811 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8812 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008813
Chris Lattner97a29a52009-03-13 05:22:11 +00008814 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008815 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8816 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008817 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008818 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008819 }
Eric Christopherfd179292009-08-27 18:07:15 +00008820
Chris Lattnercee56e72009-03-13 05:53:31 +00008821 // Optimize cases that will turn into an LEA instruction. This requires
8822 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008823 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008824 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008825 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008826
Chris Lattnercee56e72009-03-13 05:53:31 +00008827 bool isFastMultiplier = false;
8828 if (Diff < 10) {
8829 switch ((unsigned char)Diff) {
8830 default: break;
8831 case 1: // result = add base, cond
8832 case 2: // result = lea base( , cond*2)
8833 case 3: // result = lea base(cond, cond*2)
8834 case 4: // result = lea base( , cond*4)
8835 case 5: // result = lea base(cond, cond*4)
8836 case 8: // result = lea base( , cond*8)
8837 case 9: // result = lea base(cond, cond*8)
8838 isFastMultiplier = true;
8839 break;
8840 }
8841 }
Eric Christopherfd179292009-08-27 18:07:15 +00008842
Chris Lattnercee56e72009-03-13 05:53:31 +00008843 if (isFastMultiplier) {
8844 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8845 if (NeedsCondInvert) // Invert the condition if needed.
8846 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8847 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008848
Chris Lattnercee56e72009-03-13 05:53:31 +00008849 // Zero extend the condition if needed.
8850 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8851 Cond);
8852 // Scale the condition by the difference.
8853 if (Diff != 1)
8854 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8855 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008856
Chris Lattnercee56e72009-03-13 05:53:31 +00008857 // Add the base if non-zero.
8858 if (FalseC->getAPIntValue() != 0)
8859 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8860 SDValue(FalseC, 0));
8861 return Cond;
8862 }
Eric Christopherfd179292009-08-27 18:07:15 +00008863 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008864 }
8865 }
Eric Christopherfd179292009-08-27 18:07:15 +00008866
Dan Gohman475871a2008-07-27 21:46:04 +00008867 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008868}
8869
Chris Lattnerd1980a52009-03-12 06:52:53 +00008870/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8871static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8872 TargetLowering::DAGCombinerInfo &DCI) {
8873 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008874
Chris Lattnerd1980a52009-03-12 06:52:53 +00008875 // If the flag operand isn't dead, don't touch this CMOV.
8876 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8877 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008878
Chris Lattnerd1980a52009-03-12 06:52:53 +00008879 // If this is a select between two integer constants, try to do some
8880 // optimizations. Note that the operands are ordered the opposite of SELECT
8881 // operands.
8882 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8883 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8884 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8885 // larger than FalseC (the false value).
8886 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008887
Chris Lattnerd1980a52009-03-12 06:52:53 +00008888 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8889 CC = X86::GetOppositeBranchCondition(CC);
8890 std::swap(TrueC, FalseC);
8891 }
Eric Christopherfd179292009-08-27 18:07:15 +00008892
Chris Lattnerd1980a52009-03-12 06:52:53 +00008893 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008894 // This is efficient for any integer data type (including i8/i16) and
8895 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008896 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8897 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008898 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8899 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008900
Chris Lattnerd1980a52009-03-12 06:52:53 +00008901 // Zero extend the condition if needed.
8902 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008903
Chris Lattnerd1980a52009-03-12 06:52:53 +00008904 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8905 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008906 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008907 if (N->getNumValues() == 2) // Dead flag value?
8908 return DCI.CombineTo(N, Cond, SDValue());
8909 return Cond;
8910 }
Eric Christopherfd179292009-08-27 18:07:15 +00008911
Chris Lattnercee56e72009-03-13 05:53:31 +00008912 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8913 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008914 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8915 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008916 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8917 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008918
Chris Lattner97a29a52009-03-13 05:22:11 +00008919 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008920 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8921 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008922 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8923 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008924
Chris Lattner97a29a52009-03-13 05:22:11 +00008925 if (N->getNumValues() == 2) // Dead flag value?
8926 return DCI.CombineTo(N, Cond, SDValue());
8927 return Cond;
8928 }
Eric Christopherfd179292009-08-27 18:07:15 +00008929
Chris Lattnercee56e72009-03-13 05:53:31 +00008930 // Optimize cases that will turn into an LEA instruction. This requires
8931 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008932 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008933 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008934 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008935
Chris Lattnercee56e72009-03-13 05:53:31 +00008936 bool isFastMultiplier = false;
8937 if (Diff < 10) {
8938 switch ((unsigned char)Diff) {
8939 default: break;
8940 case 1: // result = add base, cond
8941 case 2: // result = lea base( , cond*2)
8942 case 3: // result = lea base(cond, cond*2)
8943 case 4: // result = lea base( , cond*4)
8944 case 5: // result = lea base(cond, cond*4)
8945 case 8: // result = lea base( , cond*8)
8946 case 9: // result = lea base(cond, cond*8)
8947 isFastMultiplier = true;
8948 break;
8949 }
8950 }
Eric Christopherfd179292009-08-27 18:07:15 +00008951
Chris Lattnercee56e72009-03-13 05:53:31 +00008952 if (isFastMultiplier) {
8953 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8954 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008955 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8956 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00008957 // Zero extend the condition if needed.
8958 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8959 Cond);
8960 // Scale the condition by the difference.
8961 if (Diff != 1)
8962 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8963 DAG.getConstant(Diff, Cond.getValueType()));
8964
8965 // Add the base if non-zero.
8966 if (FalseC->getAPIntValue() != 0)
8967 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8968 SDValue(FalseC, 0));
8969 if (N->getNumValues() == 2) // Dead flag value?
8970 return DCI.CombineTo(N, Cond, SDValue());
8971 return Cond;
8972 }
Eric Christopherfd179292009-08-27 18:07:15 +00008973 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008974 }
8975 }
8976 return SDValue();
8977}
8978
8979
Evan Cheng0b0cd912009-03-28 05:57:29 +00008980/// PerformMulCombine - Optimize a single multiply with constant into two
8981/// in order to implement it with two cheaper instructions, e.g.
8982/// LEA + SHL, LEA + LEA.
8983static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8984 TargetLowering::DAGCombinerInfo &DCI) {
8985 if (DAG.getMachineFunction().
8986 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8987 return SDValue();
8988
8989 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8990 return SDValue();
8991
Owen Andersone50ed302009-08-10 22:56:29 +00008992 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008993 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00008994 return SDValue();
8995
8996 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8997 if (!C)
8998 return SDValue();
8999 uint64_t MulAmt = C->getZExtValue();
9000 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9001 return SDValue();
9002
9003 uint64_t MulAmt1 = 0;
9004 uint64_t MulAmt2 = 0;
9005 if ((MulAmt % 9) == 0) {
9006 MulAmt1 = 9;
9007 MulAmt2 = MulAmt / 9;
9008 } else if ((MulAmt % 5) == 0) {
9009 MulAmt1 = 5;
9010 MulAmt2 = MulAmt / 5;
9011 } else if ((MulAmt % 3) == 0) {
9012 MulAmt1 = 3;
9013 MulAmt2 = MulAmt / 3;
9014 }
9015 if (MulAmt2 &&
9016 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9017 DebugLoc DL = N->getDebugLoc();
9018
9019 if (isPowerOf2_64(MulAmt2) &&
9020 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9021 // If second multiplifer is pow2, issue it first. We want the multiply by
9022 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9023 // is an add.
9024 std::swap(MulAmt1, MulAmt2);
9025
9026 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009027 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009028 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009029 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009030 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009031 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009032 DAG.getConstant(MulAmt1, VT));
9033
Eric Christopherfd179292009-08-27 18:07:15 +00009034 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009035 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009036 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009037 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009038 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009039 DAG.getConstant(MulAmt2, VT));
9040
9041 // Do not add new nodes to DAG combiner worklist.
9042 DCI.CombineTo(N, NewMul, false);
9043 }
9044 return SDValue();
9045}
9046
Evan Chengad9c0a32009-12-15 00:53:42 +00009047static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9048 SDValue N0 = N->getOperand(0);
9049 SDValue N1 = N->getOperand(1);
9050 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9051 EVT VT = N0.getValueType();
9052
9053 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9054 // since the result of setcc_c is all zero's or all ones.
9055 if (N1C && N0.getOpcode() == ISD::AND &&
9056 N0.getOperand(1).getOpcode() == ISD::Constant) {
9057 SDValue N00 = N0.getOperand(0);
9058 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9059 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9060 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9061 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9062 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9063 APInt ShAmt = N1C->getAPIntValue();
9064 Mask = Mask.shl(ShAmt);
9065 if (Mask != 0)
9066 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9067 N00, DAG.getConstant(Mask, VT));
9068 }
9069 }
9070
9071 return SDValue();
9072}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009073
Nate Begeman740ab032009-01-26 00:52:55 +00009074/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9075/// when possible.
9076static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9077 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009078 EVT VT = N->getValueType(0);
9079 if (!VT.isVector() && VT.isInteger() &&
9080 N->getOpcode() == ISD::SHL)
9081 return PerformSHLCombine(N, DAG);
9082
Nate Begeman740ab032009-01-26 00:52:55 +00009083 // On X86 with SSE2 support, we can transform this to a vector shift if
9084 // all elements are shifted by the same amount. We can't do this in legalize
9085 // because the a constant vector is typically transformed to a constant pool
9086 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009087 if (!Subtarget->hasSSE2())
9088 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009089
Owen Anderson825b72b2009-08-11 20:47:22 +00009090 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009091 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009092
Mon P Wang3becd092009-01-28 08:12:05 +00009093 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009094 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009095 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009096 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009097 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9098 unsigned NumElts = VT.getVectorNumElements();
9099 unsigned i = 0;
9100 for (; i != NumElts; ++i) {
9101 SDValue Arg = ShAmtOp.getOperand(i);
9102 if (Arg.getOpcode() == ISD::UNDEF) continue;
9103 BaseShAmt = Arg;
9104 break;
9105 }
9106 for (; i != NumElts; ++i) {
9107 SDValue Arg = ShAmtOp.getOperand(i);
9108 if (Arg.getOpcode() == ISD::UNDEF) continue;
9109 if (Arg != BaseShAmt) {
9110 return SDValue();
9111 }
9112 }
9113 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009114 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009115 SDValue InVec = ShAmtOp.getOperand(0);
9116 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9117 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9118 unsigned i = 0;
9119 for (; i != NumElts; ++i) {
9120 SDValue Arg = InVec.getOperand(i);
9121 if (Arg.getOpcode() == ISD::UNDEF) continue;
9122 BaseShAmt = Arg;
9123 break;
9124 }
9125 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9126 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9127 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9128 if (C->getZExtValue() == SplatIdx)
9129 BaseShAmt = InVec.getOperand(1);
9130 }
9131 }
9132 if (BaseShAmt.getNode() == 0)
9133 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9134 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009135 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009136 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009137
Mon P Wangefa42202009-09-03 19:56:25 +00009138 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009139 if (EltVT.bitsGT(MVT::i32))
9140 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9141 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009142 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009143
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009144 // The shift amount is identical so we can do a vector shift.
9145 SDValue ValOp = N->getOperand(0);
9146 switch (N->getOpcode()) {
9147 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009148 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009149 break;
9150 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009151 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009152 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009153 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009154 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009156 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009158 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009159 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009160 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009162 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009163 break;
9164 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009165 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009166 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009168 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009170 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009171 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009172 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009173 break;
9174 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009175 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009176 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009178 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009179 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009180 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009182 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009184 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009185 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009186 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009187 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009188 }
9189 return SDValue();
9190}
9191
Evan Cheng760d1942010-01-04 21:22:48 +00009192static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9193 const X86Subtarget *Subtarget) {
9194 EVT VT = N->getValueType(0);
9195 if (VT != MVT::i64 || !Subtarget->is64Bit())
9196 return SDValue();
9197
9198 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9199 SDValue N0 = N->getOperand(0);
9200 SDValue N1 = N->getOperand(1);
9201 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9202 std::swap(N0, N1);
9203 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9204 return SDValue();
9205
9206 SDValue ShAmt0 = N0.getOperand(1);
9207 if (ShAmt0.getValueType() != MVT::i8)
9208 return SDValue();
9209 SDValue ShAmt1 = N1.getOperand(1);
9210 if (ShAmt1.getValueType() != MVT::i8)
9211 return SDValue();
9212 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9213 ShAmt0 = ShAmt0.getOperand(0);
9214 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9215 ShAmt1 = ShAmt1.getOperand(0);
9216
9217 DebugLoc DL = N->getDebugLoc();
9218 unsigned Opc = X86ISD::SHLD;
9219 SDValue Op0 = N0.getOperand(0);
9220 SDValue Op1 = N1.getOperand(0);
9221 if (ShAmt0.getOpcode() == ISD::SUB) {
9222 Opc = X86ISD::SHRD;
9223 std::swap(Op0, Op1);
9224 std::swap(ShAmt0, ShAmt1);
9225 }
9226
9227 if (ShAmt1.getOpcode() == ISD::SUB) {
9228 SDValue Sum = ShAmt1.getOperand(0);
9229 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9230 if (SumC->getSExtValue() == 64 &&
9231 ShAmt1.getOperand(1) == ShAmt0)
9232 return DAG.getNode(Opc, DL, VT,
9233 Op0, Op1,
9234 DAG.getNode(ISD::TRUNCATE, DL,
9235 MVT::i8, ShAmt0));
9236 }
9237 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9238 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9239 if (ShAmt0C &&
9240 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9241 return DAG.getNode(Opc, DL, VT,
9242 N0.getOperand(0), N1.getOperand(0),
9243 DAG.getNode(ISD::TRUNCATE, DL,
9244 MVT::i8, ShAmt0));
9245 }
9246
9247 return SDValue();
9248}
9249
Chris Lattner149a4e52008-02-22 02:09:43 +00009250/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009251static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009252 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009253 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9254 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009255 // A preferable solution to the general problem is to figure out the right
9256 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009257
9258 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009259 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009260 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009261 if (VT.getSizeInBits() != 64)
9262 return SDValue();
9263
Devang Patel578efa92009-06-05 21:57:13 +00009264 const Function *F = DAG.getMachineFunction().getFunction();
9265 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009266 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009267 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009268 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009269 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009270 isa<LoadSDNode>(St->getValue()) &&
9271 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9272 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009273 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009274 LoadSDNode *Ld = 0;
9275 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009276 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009277 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009278 // Must be a store of a load. We currently handle two cases: the load
9279 // is a direct child, and it's under an intervening TokenFactor. It is
9280 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009281 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009282 Ld = cast<LoadSDNode>(St->getChain());
9283 else if (St->getValue().hasOneUse() &&
9284 ChainVal->getOpcode() == ISD::TokenFactor) {
9285 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009286 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009287 TokenFactorIndex = i;
9288 Ld = cast<LoadSDNode>(St->getValue());
9289 } else
9290 Ops.push_back(ChainVal->getOperand(i));
9291 }
9292 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009293
Evan Cheng536e6672009-03-12 05:59:15 +00009294 if (!Ld || !ISD::isNormalLoad(Ld))
9295 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009296
Evan Cheng536e6672009-03-12 05:59:15 +00009297 // If this is not the MMX case, i.e. we are just turning i64 load/store
9298 // into f64 load/store, avoid the transformation if there are multiple
9299 // uses of the loaded value.
9300 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9301 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009302
Evan Cheng536e6672009-03-12 05:59:15 +00009303 DebugLoc LdDL = Ld->getDebugLoc();
9304 DebugLoc StDL = N->getDebugLoc();
9305 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9306 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9307 // pair instead.
9308 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009310 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9311 Ld->getBasePtr(), Ld->getSrcValue(),
9312 Ld->getSrcValueOffset(), Ld->isVolatile(),
9313 Ld->getAlignment());
9314 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009315 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009316 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009317 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009318 Ops.size());
9319 }
Evan Cheng536e6672009-03-12 05:59:15 +00009320 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009321 St->getSrcValue(), St->getSrcValueOffset(),
9322 St->isVolatile(), St->getAlignment());
9323 }
Evan Cheng536e6672009-03-12 05:59:15 +00009324
9325 // Otherwise, lower to two pairs of 32-bit loads / stores.
9326 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009327 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9328 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009329
Owen Anderson825b72b2009-08-11 20:47:22 +00009330 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009331 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9332 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009333 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009334 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9335 Ld->isVolatile(),
9336 MinAlign(Ld->getAlignment(), 4));
9337
9338 SDValue NewChain = LoLd.getValue(1);
9339 if (TokenFactorIndex != -1) {
9340 Ops.push_back(LoLd);
9341 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009342 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009343 Ops.size());
9344 }
9345
9346 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9348 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009349
9350 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9351 St->getSrcValue(), St->getSrcValueOffset(),
9352 St->isVolatile(), St->getAlignment());
9353 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9354 St->getSrcValue(),
9355 St->getSrcValueOffset() + 4,
9356 St->isVolatile(),
9357 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009358 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009359 }
Dan Gohman475871a2008-07-27 21:46:04 +00009360 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009361}
9362
Chris Lattner6cf73262008-01-25 06:14:17 +00009363/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9364/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009365static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009366 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9367 // F[X]OR(0.0, x) -> x
9368 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009369 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9370 if (C->getValueAPF().isPosZero())
9371 return N->getOperand(1);
9372 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9373 if (C->getValueAPF().isPosZero())
9374 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009375 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009376}
9377
9378/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009379static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009380 // FAND(0.0, x) -> 0.0
9381 // FAND(x, 0.0) -> 0.0
9382 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9383 if (C->getValueAPF().isPosZero())
9384 return N->getOperand(0);
9385 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9386 if (C->getValueAPF().isPosZero())
9387 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009388 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009389}
9390
Dan Gohmane5af2d32009-01-29 01:59:02 +00009391static SDValue PerformBTCombine(SDNode *N,
9392 SelectionDAG &DAG,
9393 TargetLowering::DAGCombinerInfo &DCI) {
9394 // BT ignores high bits in the bit index operand.
9395 SDValue Op1 = N->getOperand(1);
9396 if (Op1.hasOneUse()) {
9397 unsigned BitWidth = Op1.getValueSizeInBits();
9398 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9399 APInt KnownZero, KnownOne;
9400 TargetLowering::TargetLoweringOpt TLO(DAG);
9401 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9402 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9403 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9404 DCI.CommitTargetLoweringOpt(TLO);
9405 }
9406 return SDValue();
9407}
Chris Lattner83e6c992006-10-04 06:57:07 +00009408
Eli Friedman7a5e5552009-06-07 06:52:44 +00009409static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9410 SDValue Op = N->getOperand(0);
9411 if (Op.getOpcode() == ISD::BIT_CONVERT)
9412 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009413 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009414 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009415 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009416 OpVT.getVectorElementType().getSizeInBits()) {
9417 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9418 }
9419 return SDValue();
9420}
9421
Owen Anderson99177002009-06-29 18:04:45 +00009422// On X86 and X86-64, atomic operations are lowered to locked instructions.
9423// Locked instructions, in turn, have implicit fence semantics (all memory
9424// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009425// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009426// fence-atomic-fence.
9427static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9428 SDValue atomic = N->getOperand(0);
9429 switch (atomic.getOpcode()) {
9430 case ISD::ATOMIC_CMP_SWAP:
9431 case ISD::ATOMIC_SWAP:
9432 case ISD::ATOMIC_LOAD_ADD:
9433 case ISD::ATOMIC_LOAD_SUB:
9434 case ISD::ATOMIC_LOAD_AND:
9435 case ISD::ATOMIC_LOAD_OR:
9436 case ISD::ATOMIC_LOAD_XOR:
9437 case ISD::ATOMIC_LOAD_NAND:
9438 case ISD::ATOMIC_LOAD_MIN:
9439 case ISD::ATOMIC_LOAD_MAX:
9440 case ISD::ATOMIC_LOAD_UMIN:
9441 case ISD::ATOMIC_LOAD_UMAX:
9442 break;
9443 default:
9444 return SDValue();
9445 }
Eric Christopherfd179292009-08-27 18:07:15 +00009446
Owen Anderson99177002009-06-29 18:04:45 +00009447 SDValue fence = atomic.getOperand(0);
9448 if (fence.getOpcode() != ISD::MEMBARRIER)
9449 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009450
Owen Anderson99177002009-06-29 18:04:45 +00009451 switch (atomic.getOpcode()) {
9452 case ISD::ATOMIC_CMP_SWAP:
9453 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9454 atomic.getOperand(1), atomic.getOperand(2),
9455 atomic.getOperand(3));
9456 case ISD::ATOMIC_SWAP:
9457 case ISD::ATOMIC_LOAD_ADD:
9458 case ISD::ATOMIC_LOAD_SUB:
9459 case ISD::ATOMIC_LOAD_AND:
9460 case ISD::ATOMIC_LOAD_OR:
9461 case ISD::ATOMIC_LOAD_XOR:
9462 case ISD::ATOMIC_LOAD_NAND:
9463 case ISD::ATOMIC_LOAD_MIN:
9464 case ISD::ATOMIC_LOAD_MAX:
9465 case ISD::ATOMIC_LOAD_UMIN:
9466 case ISD::ATOMIC_LOAD_UMAX:
9467 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9468 atomic.getOperand(1), atomic.getOperand(2));
9469 default:
9470 return SDValue();
9471 }
9472}
9473
Evan Cheng2e489c42009-12-16 00:53:11 +00009474static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9475 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9476 // (and (i32 x86isd::setcc_carry), 1)
9477 // This eliminates the zext. This transformation is necessary because
9478 // ISD::SETCC is always legalized to i8.
9479 DebugLoc dl = N->getDebugLoc();
9480 SDValue N0 = N->getOperand(0);
9481 EVT VT = N->getValueType(0);
9482 if (N0.getOpcode() == ISD::AND &&
9483 N0.hasOneUse() &&
9484 N0.getOperand(0).hasOneUse()) {
9485 SDValue N00 = N0.getOperand(0);
9486 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9487 return SDValue();
9488 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9489 if (!C || C->getZExtValue() != 1)
9490 return SDValue();
9491 return DAG.getNode(ISD::AND, dl, VT,
9492 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9493 N00.getOperand(0), N00.getOperand(1)),
9494 DAG.getConstant(1, VT));
9495 }
9496
9497 return SDValue();
9498}
9499
Dan Gohman475871a2008-07-27 21:46:04 +00009500SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009501 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009502 SelectionDAG &DAG = DCI.DAG;
9503 switch (N->getOpcode()) {
9504 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009505 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009506 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009507 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009508 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009509 case ISD::SHL:
9510 case ISD::SRA:
9511 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009512 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009513 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009514 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009515 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9516 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009517 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009518 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009519 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009520 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009521 }
9522
Dan Gohman475871a2008-07-27 21:46:04 +00009523 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009524}
9525
Evan Cheng60c07e12006-07-05 22:17:51 +00009526//===----------------------------------------------------------------------===//
9527// X86 Inline Assembly Support
9528//===----------------------------------------------------------------------===//
9529
Chris Lattnerb8105652009-07-20 17:51:36 +00009530static bool LowerToBSwap(CallInst *CI) {
9531 // FIXME: this should verify that we are targetting a 486 or better. If not,
9532 // we will turn this bswap into something that will be lowered to logical ops
9533 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9534 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009535
Chris Lattnerb8105652009-07-20 17:51:36 +00009536 // Verify this is a simple bswap.
9537 if (CI->getNumOperands() != 2 ||
9538 CI->getType() != CI->getOperand(1)->getType() ||
9539 !CI->getType()->isInteger())
9540 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009541
Chris Lattnerb8105652009-07-20 17:51:36 +00009542 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9543 if (!Ty || Ty->getBitWidth() % 16 != 0)
9544 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009545
Chris Lattnerb8105652009-07-20 17:51:36 +00009546 // Okay, we can do this xform, do so now.
9547 const Type *Tys[] = { Ty };
9548 Module *M = CI->getParent()->getParent()->getParent();
9549 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009550
Chris Lattnerb8105652009-07-20 17:51:36 +00009551 Value *Op = CI->getOperand(1);
9552 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009553
Chris Lattnerb8105652009-07-20 17:51:36 +00009554 CI->replaceAllUsesWith(Op);
9555 CI->eraseFromParent();
9556 return true;
9557}
9558
9559bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9560 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9561 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9562
9563 std::string AsmStr = IA->getAsmString();
9564
9565 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009566 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009567 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9568
9569 switch (AsmPieces.size()) {
9570 default: return false;
9571 case 1:
9572 AsmStr = AsmPieces[0];
9573 AsmPieces.clear();
9574 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9575
9576 // bswap $0
9577 if (AsmPieces.size() == 2 &&
9578 (AsmPieces[0] == "bswap" ||
9579 AsmPieces[0] == "bswapq" ||
9580 AsmPieces[0] == "bswapl") &&
9581 (AsmPieces[1] == "$0" ||
9582 AsmPieces[1] == "${0:q}")) {
9583 // No need to check constraints, nothing other than the equivalent of
9584 // "=r,0" would be valid here.
9585 return LowerToBSwap(CI);
9586 }
9587 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009588 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009589 AsmPieces.size() == 3 &&
9590 AsmPieces[0] == "rorw" &&
9591 AsmPieces[1] == "$$8," &&
9592 AsmPieces[2] == "${0:w}" &&
9593 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9594 return LowerToBSwap(CI);
9595 }
9596 break;
9597 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009598 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009599 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009600 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9601 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9602 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009603 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009604 SplitString(AsmPieces[0], Words, " \t");
9605 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9606 Words.clear();
9607 SplitString(AsmPieces[1], Words, " \t");
9608 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9609 Words.clear();
9610 SplitString(AsmPieces[2], Words, " \t,");
9611 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9612 Words[2] == "%edx") {
9613 return LowerToBSwap(CI);
9614 }
9615 }
9616 }
9617 }
9618 break;
9619 }
9620 return false;
9621}
9622
9623
9624
Chris Lattnerf4dff842006-07-11 02:54:03 +00009625/// getConstraintType - Given a constraint letter, return the type of
9626/// constraint it is for this target.
9627X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009628X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9629 if (Constraint.size() == 1) {
9630 switch (Constraint[0]) {
9631 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009632 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009633 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009634 case 'r':
9635 case 'R':
9636 case 'l':
9637 case 'q':
9638 case 'Q':
9639 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009640 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009641 case 'Y':
9642 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009643 case 'e':
9644 case 'Z':
9645 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009646 default:
9647 break;
9648 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009649 }
Chris Lattner4234f572007-03-25 02:14:49 +00009650 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009651}
9652
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009653/// LowerXConstraint - try to replace an X constraint, which matches anything,
9654/// with another that has more specific requirements based on the type of the
9655/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009656const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009657LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009658 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9659 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009660 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009661 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009662 return "Y";
9663 if (Subtarget->hasSSE1())
9664 return "x";
9665 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009666
Chris Lattner5e764232008-04-26 23:02:14 +00009667 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009668}
9669
Chris Lattner48884cd2007-08-25 00:47:38 +00009670/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9671/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009672void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009673 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009674 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009675 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009676 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009677 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009678
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009679 switch (Constraint) {
9680 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009681 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009682 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009683 if (C->getZExtValue() <= 31) {
9684 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009685 break;
9686 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009687 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009688 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009689 case 'J':
9690 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009691 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009692 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9693 break;
9694 }
9695 }
9696 return;
9697 case 'K':
9698 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009699 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009700 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9701 break;
9702 }
9703 }
9704 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009705 case 'N':
9706 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009707 if (C->getZExtValue() <= 255) {
9708 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009709 break;
9710 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009711 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009712 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009713 case 'e': {
9714 // 32-bit signed value
9715 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9716 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009717 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9718 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009719 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009720 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009721 break;
9722 }
9723 // FIXME gcc accepts some relocatable values here too, but only in certain
9724 // memory models; it's complicated.
9725 }
9726 return;
9727 }
9728 case 'Z': {
9729 // 32-bit unsigned 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->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009734 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9735 break;
9736 }
9737 }
9738 // FIXME gcc accepts some relocatable values here too, but only in certain
9739 // memory models; it's complicated.
9740 return;
9741 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009742 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009743 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009744 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009745 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009746 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009747 break;
9748 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009749
Chris Lattnerdc43a882007-05-03 16:52:29 +00009750 // If we are in non-pic codegen mode, we allow the address of a global (with
9751 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009752 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009753 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009754
Chris Lattner49921962009-05-08 18:23:14 +00009755 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9756 while (1) {
9757 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9758 Offset += GA->getOffset();
9759 break;
9760 } else if (Op.getOpcode() == ISD::ADD) {
9761 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9762 Offset += C->getZExtValue();
9763 Op = Op.getOperand(0);
9764 continue;
9765 }
9766 } else if (Op.getOpcode() == ISD::SUB) {
9767 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9768 Offset += -C->getZExtValue();
9769 Op = Op.getOperand(0);
9770 continue;
9771 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009772 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009773
Chris Lattner49921962009-05-08 18:23:14 +00009774 // Otherwise, this isn't something we can handle, reject it.
9775 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009776 }
Eric Christopherfd179292009-08-27 18:07:15 +00009777
Chris Lattner36c25012009-07-10 07:34:39 +00009778 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009779 // If we require an extra load to get this address, as in PIC mode, we
9780 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009781 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9782 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009783 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009784
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009785 if (hasMemory)
9786 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9787 else
9788 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009789 Result = Op;
9790 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009791 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009793
Gabor Greifba36cb52008-08-28 21:40:38 +00009794 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009795 Ops.push_back(Result);
9796 return;
9797 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009798 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9799 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009800}
9801
Chris Lattner259e97c2006-01-31 19:43:35 +00009802std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009803getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009804 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009805 if (Constraint.size() == 1) {
9806 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009807 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009808 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009809 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9810 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009811 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009812 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9813 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9814 X86::R10D,X86::R11D,X86::R12D,
9815 X86::R13D,X86::R14D,X86::R15D,
9816 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009817 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009818 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9819 X86::SI, X86::DI, X86::R8W,X86::R9W,
9820 X86::R10W,X86::R11W,X86::R12W,
9821 X86::R13W,X86::R14W,X86::R15W,
9822 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009823 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009824 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9825 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9826 X86::R10B,X86::R11B,X86::R12B,
9827 X86::R13B,X86::R14B,X86::R15B,
9828 X86::BPL, X86::SPL, 0);
9829
Owen Anderson825b72b2009-08-11 20:47:22 +00009830 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009831 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9832 X86::RSI, X86::RDI, X86::R8, X86::R9,
9833 X86::R10, X86::R11, X86::R12,
9834 X86::R13, X86::R14, X86::R15,
9835 X86::RBP, X86::RSP, 0);
9836
9837 break;
9838 }
Eric Christopherfd179292009-08-27 18:07:15 +00009839 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009840 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009841 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009842 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009843 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009844 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009845 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009846 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009847 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009848 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9849 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009850 }
9851 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009852
Chris Lattner1efa40f2006-02-22 00:56:39 +00009853 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009854}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009855
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009856std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009857X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009858 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009859 // First, see if this is a constraint that directly corresponds to an LLVM
9860 // register class.
9861 if (Constraint.size() == 1) {
9862 // GCC Constraint Letters
9863 switch (Constraint[0]) {
9864 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009865 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009866 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009867 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009868 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009869 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009870 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009871 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009872 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009873 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009874 case 'R': // LEGACY_REGS
9875 if (VT == MVT::i8)
9876 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9877 if (VT == MVT::i16)
9878 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9879 if (VT == MVT::i32 || !Subtarget->is64Bit())
9880 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9881 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009882 case 'f': // FP Stack registers.
9883 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9884 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009885 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009886 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009887 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009888 return std::make_pair(0U, X86::RFP64RegisterClass);
9889 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009890 case 'y': // MMX_REGS if MMX allowed.
9891 if (!Subtarget->hasMMX()) break;
9892 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009893 case 'Y': // SSE_REGS if SSE2 allowed
9894 if (!Subtarget->hasSSE2()) break;
9895 // FALL THROUGH.
9896 case 'x': // SSE_REGS if SSE1 allowed
9897 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009898
Owen Anderson825b72b2009-08-11 20:47:22 +00009899 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009900 default: break;
9901 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009902 case MVT::f32:
9903 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009904 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009905 case MVT::f64:
9906 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009907 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009908 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009909 case MVT::v16i8:
9910 case MVT::v8i16:
9911 case MVT::v4i32:
9912 case MVT::v2i64:
9913 case MVT::v4f32:
9914 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009915 return std::make_pair(0U, X86::VR128RegisterClass);
9916 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009917 break;
9918 }
9919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009920
Chris Lattnerf76d1802006-07-31 23:26:50 +00009921 // Use the default implementation in TargetLowering to convert the register
9922 // constraint into a member of a register class.
9923 std::pair<unsigned, const TargetRegisterClass*> Res;
9924 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009925
9926 // Not found as a standard register?
9927 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009928 // Map st(0) -> st(7) -> ST0
9929 if (Constraint.size() == 7 && Constraint[0] == '{' &&
9930 tolower(Constraint[1]) == 's' &&
9931 tolower(Constraint[2]) == 't' &&
9932 Constraint[3] == '(' &&
9933 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
9934 Constraint[5] == ')' &&
9935 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +00009936
Chris Lattner56d77c72009-09-13 22:41:48 +00009937 Res.first = X86::ST0+Constraint[4]-'0';
9938 Res.second = X86::RFP80RegisterClass;
9939 return Res;
9940 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009941
Chris Lattner56d77c72009-09-13 22:41:48 +00009942 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009943 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +00009944 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009945 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009946 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009947 }
Chris Lattner56d77c72009-09-13 22:41:48 +00009948
9949 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009950 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009951 Res.first = X86::EFLAGS;
9952 Res.second = X86::CCRRegisterClass;
9953 return Res;
9954 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009955
Dale Johannesen330169f2008-11-13 21:52:36 +00009956 // 'A' means EAX + EDX.
9957 if (Constraint == "A") {
9958 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009959 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009960 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +00009961 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009962 return Res;
9963 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009964
Chris Lattnerf76d1802006-07-31 23:26:50 +00009965 // Otherwise, check to see if this is a register class of the wrong value
9966 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9967 // turn into {ax},{dx}.
9968 if (Res.second->hasType(VT))
9969 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009970
Chris Lattnerf76d1802006-07-31 23:26:50 +00009971 // All of the single-register GCC register classes map their values onto
9972 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9973 // really want an 8-bit or 32-bit register, map to the appropriate register
9974 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009975 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009976 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009977 unsigned DestReg = 0;
9978 switch (Res.first) {
9979 default: break;
9980 case X86::AX: DestReg = X86::AL; break;
9981 case X86::DX: DestReg = X86::DL; break;
9982 case X86::CX: DestReg = X86::CL; break;
9983 case X86::BX: DestReg = X86::BL; break;
9984 }
9985 if (DestReg) {
9986 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009987 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009988 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009989 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009990 unsigned DestReg = 0;
9991 switch (Res.first) {
9992 default: break;
9993 case X86::AX: DestReg = X86::EAX; break;
9994 case X86::DX: DestReg = X86::EDX; break;
9995 case X86::CX: DestReg = X86::ECX; break;
9996 case X86::BX: DestReg = X86::EBX; break;
9997 case X86::SI: DestReg = X86::ESI; break;
9998 case X86::DI: DestReg = X86::EDI; break;
9999 case X86::BP: DestReg = X86::EBP; break;
10000 case X86::SP: DestReg = X86::ESP; break;
10001 }
10002 if (DestReg) {
10003 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010004 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010005 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010006 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010007 unsigned DestReg = 0;
10008 switch (Res.first) {
10009 default: break;
10010 case X86::AX: DestReg = X86::RAX; break;
10011 case X86::DX: DestReg = X86::RDX; break;
10012 case X86::CX: DestReg = X86::RCX; break;
10013 case X86::BX: DestReg = X86::RBX; break;
10014 case X86::SI: DestReg = X86::RSI; break;
10015 case X86::DI: DestReg = X86::RDI; break;
10016 case X86::BP: DestReg = X86::RBP; break;
10017 case X86::SP: DestReg = X86::RSP; break;
10018 }
10019 if (DestReg) {
10020 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010021 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010022 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010023 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010024 } else if (Res.second == X86::FR32RegisterClass ||
10025 Res.second == X86::FR64RegisterClass ||
10026 Res.second == X86::VR128RegisterClass) {
10027 // Handle references to XMM physical registers that got mapped into the
10028 // wrong class. This can happen with constraints like {xmm0} where the
10029 // target independent register mapper will just pick the first match it can
10030 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010031 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010032 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010033 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010034 Res.second = X86::FR64RegisterClass;
10035 else if (X86::VR128RegisterClass->hasType(VT))
10036 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010037 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010038
Chris Lattnerf76d1802006-07-31 23:26:50 +000010039 return Res;
10040}
Mon P Wang0c397192008-10-30 08:01:45 +000010041
10042//===----------------------------------------------------------------------===//
10043// X86 Widen vector type
10044//===----------------------------------------------------------------------===//
10045
10046/// getWidenVectorType: given a vector type, returns the type to widen
10047/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010048/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010049/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010050/// scalarizing vs using the wider vector type.
10051
Owen Andersone50ed302009-08-10 22:56:29 +000010052EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010053 assert(VT.isVector());
10054 if (isTypeLegal(VT))
10055 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010056
Mon P Wang0c397192008-10-30 08:01:45 +000010057 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10058 // type based on element type. This would speed up our search (though
10059 // it may not be worth it since the size of the list is relatively
10060 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010061 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010062 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010063
Mon P Wang0c397192008-10-30 08:01:45 +000010064 // On X86, it make sense to widen any vector wider than 1
10065 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010066 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010067
Owen Anderson825b72b2009-08-11 20:47:22 +000010068 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10069 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10070 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010071
10072 if (isTypeLegal(SVT) &&
10073 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010074 SVT.getVectorNumElements() > NElts)
10075 return SVT;
10076 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010077 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010078}