blob: ab51369555c1b135d6e040b8ebd809a4a0b510f8 [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 {
1097 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Chris Lattnere4df7562009-07-09 03:15:51 +00001099 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001100 // This doesn't have DebugLoc associated with it, but is not really the
1101 // same as a Register.
1102 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1103 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001104 return Table;
1105}
1106
Bill Wendlingb4202b82009-07-01 18:50:55 +00001107/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001108unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001109 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001110}
1111
Chris Lattner2b02a442007-02-25 08:29:00 +00001112//===----------------------------------------------------------------------===//
1113// Return Value Calling Convention Implementation
1114//===----------------------------------------------------------------------===//
1115
Chris Lattner59ed56b2007-02-28 04:55:35 +00001116#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001117
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001118bool
1119X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1120 const SmallVectorImpl<EVT> &OutTys,
1121 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1122 SelectionDAG &DAG) {
1123 SmallVector<CCValAssign, 16> RVLocs;
1124 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1125 RVLocs, *DAG.getContext());
1126 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1127}
1128
Dan Gohman98ca4f22009-08-05 01:29:28 +00001129SDValue
1130X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001131 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001132 const SmallVectorImpl<ISD::OutputArg> &Outs,
1133 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001134
Chris Lattner9774c912007-02-27 05:28:59 +00001135 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001136 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1137 RVLocs, *DAG.getContext());
1138 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001139
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001140 // If this is the first return lowered for this function, add the regs to the
1141 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001142 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001143 for (unsigned i = 0; i != RVLocs.size(); ++i)
1144 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001145 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001147
Dan Gohman475871a2008-07-27 21:46:04 +00001148 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001149
Dan Gohman475871a2008-07-27 21:46:04 +00001150 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001151 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1152 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001153 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001154
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001155 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001156 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1157 CCValAssign &VA = RVLocs[i];
1158 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001160
Chris Lattner447ff682008-03-11 03:23:40 +00001161 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1162 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001163 if (VA.getLocReg() == X86::ST0 ||
1164 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001165 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1166 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001167 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001168 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001169 RetOps.push_back(ValToCopy);
1170 // Don't emit a copytoreg.
1171 continue;
1172 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001173
Evan Cheng242b38b2009-02-23 09:03:22 +00001174 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1175 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001176 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001177 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001178 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001179 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001180 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001181 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001182 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001183 }
1184
Dale Johannesendd64c412009-02-04 00:33:20 +00001185 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001186 Flag = Chain.getValue(1);
1187 }
Dan Gohman61a92132008-04-21 23:59:07 +00001188
1189 // The x86-64 ABI for returning structs by value requires that we copy
1190 // the sret argument into %rax for the return. We saved the argument into
1191 // a virtual register in the entry block, so now we copy the value out
1192 // and into %rax.
1193 if (Subtarget->is64Bit() &&
1194 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1195 MachineFunction &MF = DAG.getMachineFunction();
1196 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1197 unsigned Reg = FuncInfo->getSRetReturnReg();
1198 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001200 FuncInfo->setSRetReturnReg(Reg);
1201 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001202 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001203
Dale Johannesendd64c412009-02-04 00:33:20 +00001204 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001205 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001206
1207 // RAX now acts like a return value.
1208 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001209 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001210
Chris Lattner447ff682008-03-11 03:23:40 +00001211 RetOps[0] = Chain; // Update chain.
1212
1213 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001215 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001216
1217 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001219}
1220
Dan Gohman98ca4f22009-08-05 01:29:28 +00001221/// LowerCallResult - Lower the result values of a call into the
1222/// appropriate copies out of appropriate physical registers.
1223///
1224SDValue
1225X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001226 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 const SmallVectorImpl<ISD::InputArg> &Ins,
1228 DebugLoc dl, SelectionDAG &DAG,
1229 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001230
Chris Lattnere32bbf62007-02-28 07:09:55 +00001231 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001232 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001233 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001235 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001236 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001237
Chris Lattner3085e152007-02-25 08:59:22 +00001238 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001239 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001240 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001241 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001242
Torok Edwin3f142c32009-02-01 18:15:56 +00001243 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001244 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001246 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001247 }
1248
Chris Lattner8e6da152008-03-10 21:08:41 +00001249 // If this is a call to a function that returns an fp value on the floating
1250 // point stack, but where we prefer to use the value in xmm registers, copy
1251 // 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 +00001252 if ((VA.getLocReg() == X86::ST0 ||
1253 VA.getLocReg() == X86::ST1) &&
1254 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001257
Evan Cheng79fb3b42009-02-20 20:43:02 +00001258 SDValue Val;
1259 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001260 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1261 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1262 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001264 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001265 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1266 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001267 } else {
1268 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001270 Val = Chain.getValue(0);
1271 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001272 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1273 } else {
1274 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1275 CopyVT, InFlag).getValue(1);
1276 Val = Chain.getValue(0);
1277 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001278 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001279
Dan Gohman37eed792009-02-04 17:28:58 +00001280 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001281 // Round the F80 the right size, which also moves to the appropriate xmm
1282 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001283 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001284 // This truncation won't change the value.
1285 DAG.getIntPtrConstant(1));
1286 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001287
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001289 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001290
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001292}
1293
1294
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001295//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001296// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001297//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001298// StdCall calling convention seems to be standard for many Windows' API
1299// routines and around. It differs from C calling convention just a little:
1300// callee should clean up the stack, not caller. Symbols should be also
1301// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001302// For info on fast calling convention see Fast Calling Convention (tail call)
1303// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001304
Dan Gohman98ca4f22009-08-05 01:29:28 +00001305/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001306/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001307static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1308 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001309 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001310
Dan Gohman98ca4f22009-08-05 01:29:28 +00001311 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001312}
1313
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001314/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001315/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001316static bool
1317ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1318 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001319 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001320
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001322}
1323
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001324/// IsCalleePop - Determines whether the callee is required to pop its
1325/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001326bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001327 if (IsVarArg)
1328 return false;
1329
Dan Gohman095cc292008-09-13 01:54:27 +00001330 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001331 default:
1332 return false;
1333 case CallingConv::X86_StdCall:
1334 return !Subtarget->is64Bit();
1335 case CallingConv::X86_FastCall:
1336 return !Subtarget->is64Bit();
1337 case CallingConv::Fast:
1338 return PerformTailCallOpt;
1339 }
1340}
1341
Dan Gohman095cc292008-09-13 01:54:27 +00001342/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1343/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001344CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001345 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001346 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001347 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001348 else
1349 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001350 }
1351
Gordon Henriksen86737662008-01-05 16:56:59 +00001352 if (CC == CallingConv::X86_FastCall)
1353 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001354 else if (CC == CallingConv::Fast)
1355 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001356 else
1357 return CC_X86_32_C;
1358}
1359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360/// NameDecorationForCallConv - Selects the appropriate decoration to
1361/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001362NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001363X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001365 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001366 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001367 return StdCall;
1368 return None;
1369}
1370
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001371
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001372/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1373/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001374/// the specific parameter attribute. The copy will be passed as a byval
1375/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001376static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001377CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001378 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1379 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001380 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001381 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001382 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001383}
1384
Dan Gohman98ca4f22009-08-05 01:29:28 +00001385SDValue
1386X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001387 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001388 const SmallVectorImpl<ISD::InputArg> &Ins,
1389 DebugLoc dl, SelectionDAG &DAG,
1390 const CCValAssign &VA,
1391 MachineFrameInfo *MFI,
1392 unsigned i) {
1393
Rafael Espindola7effac52007-09-14 15:48:13 +00001394 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1396 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001397 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001398 EVT ValVT;
1399
1400 // If value is passed by pointer we have address passed instead of the value
1401 // itself.
1402 if (VA.getLocInfo() == CCValAssign::Indirect)
1403 ValVT = VA.getLocVT();
1404 else
1405 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001406
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001407 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001408 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001409 // In case of tail call optimization mark all arguments mutable. Since they
1410 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001411 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001412 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001413 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001414 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001415 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001416 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001417 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001418}
1419
Dan Gohman475871a2008-07-27 21:46:04 +00001420SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001422 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001423 bool isVarArg,
1424 const SmallVectorImpl<ISD::InputArg> &Ins,
1425 DebugLoc dl,
1426 SelectionDAG &DAG,
1427 SmallVectorImpl<SDValue> &InVals) {
1428
Evan Cheng1bc78042006-04-26 01:20:17 +00001429 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001431
Gordon Henriksen86737662008-01-05 16:56:59 +00001432 const Function* Fn = MF.getFunction();
1433 if (Fn->hasExternalLinkage() &&
1434 Subtarget->isTargetCygMing() &&
1435 Fn->getName() == "main")
1436 FuncInfo->setForceFramePointer(true);
1437
1438 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001440
Evan Cheng1bc78042006-04-26 01:20:17 +00001441 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001442 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001443 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001446 "Var args not supported with calling convention fastcc");
1447
Chris Lattner638402b2007-02-28 07:00:42 +00001448 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001449 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1451 ArgLocs, *DAG.getContext());
1452 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001453
Chris Lattnerf39f7712007-02-28 05:46:49 +00001454 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001455 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001456 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1457 CCValAssign &VA = ArgLocs[i];
1458 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1459 // places.
1460 assert(VA.getValNo() != LastVal &&
1461 "Don't support value assigned to multiple locs yet");
1462 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001463
Chris Lattnerf39f7712007-02-28 05:46:49 +00001464 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001465 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001466 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001467 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001468 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001469 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001470 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001471 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001472 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001473 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001474 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001475 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001476 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001477 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1478 RC = X86::VR64RegisterClass;
1479 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001480 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001481
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001482 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Chris Lattnerf39f7712007-02-28 05:46:49 +00001485 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1486 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1487 // right size.
1488 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001489 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001490 DAG.getValueType(VA.getValVT()));
1491 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001492 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001493 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001494 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001495 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001496
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001497 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001498 // Handle MMX values passed in XMM regs.
1499 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001500 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1501 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001502 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1503 } else
1504 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001505 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001506 } else {
1507 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001509 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001510
1511 // If value is passed via pointer - do a load.
1512 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001514
Dan Gohman98ca4f22009-08-05 01:29:28 +00001515 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001516 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001517
Dan Gohman61a92132008-04-21 23:59:07 +00001518 // The x86-64 ABI for returning structs by value requires that we copy
1519 // the sret argument into %rax for the return. Save the argument into
1520 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001521 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001522 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1523 unsigned Reg = FuncInfo->getSRetReturnReg();
1524 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001526 FuncInfo->setSRetReturnReg(Reg);
1527 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001528 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001530 }
1531
Chris Lattnerf39f7712007-02-28 05:46:49 +00001532 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001533 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001534 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001535 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001536
Evan Cheng1bc78042006-04-26 01:20:17 +00001537 // If the function takes variable number of arguments, make a frame index for
1538 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001540 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001541 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001542 }
1543 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001544 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1545
1546 // FIXME: We should really autogenerate these arrays
1547 static const unsigned GPR64ArgRegsWin64[] = {
1548 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001549 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001550 static const unsigned XMMArgRegsWin64[] = {
1551 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1552 };
1553 static const unsigned GPR64ArgRegs64Bit[] = {
1554 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1555 };
1556 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1558 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1559 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001560 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1561
1562 if (IsWin64) {
1563 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1564 GPR64ArgRegs = GPR64ArgRegsWin64;
1565 XMMArgRegs = XMMArgRegsWin64;
1566 } else {
1567 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1568 GPR64ArgRegs = GPR64ArgRegs64Bit;
1569 XMMArgRegs = XMMArgRegs64Bit;
1570 }
1571 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1572 TotalNumIntRegs);
1573 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1574 TotalNumXMMRegs);
1575
Devang Patel578efa92009-06-05 21:57:13 +00001576 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001577 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001578 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001579 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001580 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001581 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001582 // Kernel mode asks for SSE to be disabled, so don't push them
1583 // on the stack.
1584 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001585
Gordon Henriksen86737662008-01-05 16:56:59 +00001586 // For X86-64, if there are vararg parameters that are passed via
1587 // registers, then we must store them to their spots on the stack so they
1588 // may be loaded by deferencing the result of va_next.
1589 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001590 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1591 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001592 TotalNumXMMRegs * 16, 16,
1593 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001594
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SmallVector<SDValue, 8> MemOps;
1597 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001598 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001599 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001600 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1601 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001602 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1603 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001604 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001605 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001606 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001607 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001608 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001609 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001610 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001611 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001612
Dan Gohmanface41a2009-08-16 21:24:25 +00001613 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1614 // Now store the XMM (fp + vector) parameter registers.
1615 SmallVector<SDValue, 11> SaveXMMOps;
1616 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001617
Dan Gohmanface41a2009-08-16 21:24:25 +00001618 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1619 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1620 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001621
Dan Gohmanface41a2009-08-16 21:24:25 +00001622 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1623 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001624
Dan Gohmanface41a2009-08-16 21:24:25 +00001625 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1626 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1627 X86::VR128RegisterClass);
1628 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1629 SaveXMMOps.push_back(Val);
1630 }
1631 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1632 MVT::Other,
1633 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001634 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001635
1636 if (!MemOps.empty())
1637 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1638 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001640 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001641
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001643 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001644 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001645 BytesCallerReserves = 0;
1646 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001647 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001648 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001649 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001650 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001651 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001652 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001653
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 if (!Is64Bit) {
1655 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001656 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1658 }
Evan Cheng25caf632006-05-23 21:06:34 +00001659
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001660 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001661
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001663}
1664
Dan Gohman475871a2008-07-27 21:46:04 +00001665SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001666X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1667 SDValue StackPtr, SDValue Arg,
1668 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001669 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001670 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001671 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001672 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001673 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001674 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001675 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001676 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001677 }
Dale Johannesenace16102009-02-03 19:33:06 +00001678 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001679 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001680}
1681
Bill Wendling64e87322009-01-16 19:25:27 +00001682/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001683/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001684SDValue
1685X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001686 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001687 SDValue Chain,
1688 bool IsTailCall,
1689 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001690 int FPDiff,
1691 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001692 if (!IsTailCall || FPDiff==0) return Chain;
1693
1694 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001695 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001696 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001697
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001698 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001699 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001700 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001701}
1702
1703/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1704/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001705static SDValue
1706EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001707 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001708 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001709 // Store the return address to the appropriate stack slot.
1710 if (!FPDiff) return Chain;
1711 // Calculate the new stack slot for the return address.
1712 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001713 int NewReturnAddrFI =
David Greene3f2bf852009-11-12 20:49:22 +00001714 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1715 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001717 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001718 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001719 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001720 return Chain;
1721}
1722
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723SDValue
1724X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001725 CallingConv::ID CallConv, bool isVarArg,
1726 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001727 const SmallVectorImpl<ISD::OutputArg> &Outs,
1728 const SmallVectorImpl<ISD::InputArg> &Ins,
1729 DebugLoc dl, SelectionDAG &DAG,
1730 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001731
Dan Gohman98ca4f22009-08-05 01:29:28 +00001732 MachineFunction &MF = DAG.getMachineFunction();
1733 bool Is64Bit = Subtarget->is64Bit();
1734 bool IsStructRet = CallIsStructReturn(Outs);
1735
1736 assert((!isTailCall ||
1737 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1738 "IsEligibleForTailCallOptimization missed a case!");
1739 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001740 "Var args not supported with calling convention fastcc");
1741
Chris Lattner638402b2007-02-28 07:00:42 +00001742 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001743 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001744 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1745 ArgLocs, *DAG.getContext());
1746 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001747
Chris Lattner423c5f42007-02-28 05:31:48 +00001748 // Get a count of how many bytes are to be pushed on the stack.
1749 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001751 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001752
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001754 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001755 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001756 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1758 FPDiff = NumBytesCallerPushed - NumBytes;
1759
1760 // Set the delta of movement of the returnaddr stackslot.
1761 // But only set if delta is greater than previous delta.
1762 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1763 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1764 }
1765
Chris Lattnere563bbc2008-10-11 22:08:30 +00001766 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001767
Dan Gohman475871a2008-07-27 21:46:04 +00001768 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001769 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001771 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001772
Dan Gohman475871a2008-07-27 21:46:04 +00001773 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1774 SmallVector<SDValue, 8> MemOpChains;
1775 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001776
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001777 // Walk the register/memloc assignments, inserting copies/loads. In the case
1778 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001779 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1780 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001781 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 SDValue Arg = Outs[i].Val;
1783 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001784 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001785
Chris Lattner423c5f42007-02-28 05:31:48 +00001786 // Promote the value if needed.
1787 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001788 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001789 case CCValAssign::Full: break;
1790 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001791 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001792 break;
1793 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001794 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001795 break;
1796 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001797 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1798 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1800 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1801 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001802 } else
1803 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1804 break;
1805 case CCValAssign::BCvt:
1806 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001807 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001808 case CCValAssign::Indirect: {
1809 // Store the argument.
1810 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001811 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001812 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001813 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001814 Arg = SpillSlot;
1815 break;
1816 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001818
Chris Lattner423c5f42007-02-28 05:31:48 +00001819 if (VA.isRegLoc()) {
1820 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1821 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001823 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001824 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001825 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001826
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1828 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001829 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001830 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001832
Evan Cheng32fe1032006-05-25 00:59:30 +00001833 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001834 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001835 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001836
Evan Cheng347d5f72006-04-28 21:29:37 +00001837 // Build a sequence of copy-to-reg nodes chained together with token chain
1838 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001839 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001840 // Tail call byval lowering might overwrite argument registers so in case of
1841 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001842 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001843 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001844 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001845 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001846 InFlag = Chain.getValue(1);
1847 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001848
Eric Christopherfd179292009-08-27 18:07:15 +00001849
Chris Lattner88e1fd52009-07-09 04:24:46 +00001850 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001851 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1852 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001854 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1855 DAG.getNode(X86ISD::GlobalBaseReg,
1856 DebugLoc::getUnknownLoc(),
1857 getPointerTy()),
1858 InFlag);
1859 InFlag = Chain.getValue(1);
1860 } else {
1861 // If we are tail calling and generating PIC/GOT style code load the
1862 // address of the callee into ECX. The value in ecx is used as target of
1863 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1864 // for tail calls on PIC/GOT architectures. Normally we would just put the
1865 // address of GOT into ebx and then call target@PLT. But for tail calls
1866 // ebx would be restored (since ebx is callee saved) before jumping to the
1867 // target@PLT.
1868
1869 // Note: The actual moving to ECX is done further down.
1870 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1871 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1872 !G->getGlobal()->hasProtectedVisibility())
1873 Callee = LowerGlobalAddress(Callee, DAG);
1874 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001875 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001876 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001877 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001878
Gordon Henriksen86737662008-01-05 16:56:59 +00001879 if (Is64Bit && isVarArg) {
1880 // From AMD64 ABI document:
1881 // For calls that may call functions that use varargs or stdargs
1882 // (prototype-less calls or calls to functions containing ellipsis (...) in
1883 // the declaration) %al is used as hidden argument to specify the number
1884 // of SSE registers used. The contents of %al do not need to match exactly
1885 // the number of registers, but must be an ubound on the number of SSE
1886 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001887
1888 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001889 // Count the number of XMM registers allocated.
1890 static const unsigned XMMArgRegs[] = {
1891 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1892 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1893 };
1894 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001895 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001896 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001897
Dale Johannesendd64c412009-02-04 00:33:20 +00001898 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001900 InFlag = Chain.getValue(1);
1901 }
1902
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001903
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001904 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001905 if (isTailCall) {
1906 // Force all the incoming stack arguments to be loaded from the stack
1907 // before any new outgoing arguments are stored to the stack, because the
1908 // outgoing stack slots may alias the incoming argument stack slots, and
1909 // the alias isn't otherwise explicit. This is slightly more conservative
1910 // than necessary, because it means that each store effectively depends
1911 // on every argument instead of just those arguments it would clobber.
1912 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SmallVector<SDValue, 8> MemOpChains2;
1915 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001916 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001917 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001918 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1920 CCValAssign &VA = ArgLocs[i];
1921 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001922 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 SDValue Arg = Outs[i].Val;
1924 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001925 // Create frame index.
1926 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001927 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001928 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001929 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001930
Duncan Sands276dcbd2008-03-21 09:14:45 +00001931 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001932 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001933 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001934 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001936 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001937 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001938
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1940 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001941 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001943 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001944 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001945 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001946 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001947 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 }
1949 }
1950
1951 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001952 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001953 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001954
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001955 // Copy arguments to their registers.
1956 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001957 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001958 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001959 InFlag = Chain.getValue(1);
1960 }
Dan Gohman475871a2008-07-27 21:46:04 +00001961 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001962
Gordon Henriksen86737662008-01-05 16:56:59 +00001963 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001964 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001965 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 }
1967
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00001968 bool WasGlobalOrExternal = false;
1969 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
1970 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
1971 // In the 64-bit large code model, we have to make all calls
1972 // through a register, since the call instruction's 32-bit
1973 // pc-relative offset may not be large enough to hold the whole
1974 // address.
1975 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1976 WasGlobalOrExternal = true;
1977 // If the callee is a GlobalAddress node (quite common, every direct call
1978 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
1979 // it.
1980
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001981 // We should use extra load for direct calls to dllimported functions in
1982 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001983 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001984 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001985 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00001986
Chris Lattner48a7d022009-07-09 05:02:21 +00001987 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1988 // external symbols most go through the PLT in PIC mode. If the symbol
1989 // has hidden or protected visibility, or if it is static or local, then
1990 // we don't need to use the PLT - we can directly call it.
1991 if (Subtarget->isTargetELF() &&
1992 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001993 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001994 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001995 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001996 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1997 Subtarget->getDarwinVers() < 9) {
1998 // PC-relative references to external symbols should go through $stub,
1999 // unless we're building with the leopard linker or later, which
2000 // automatically synthesizes these stubs.
2001 OpFlags = X86II::MO_DARWIN_STUB;
2002 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002003
Chris Lattner74e726e2009-07-09 05:27:35 +00002004 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002005 G->getOffset(), OpFlags);
2006 }
Bill Wendling056292f2008-09-16 21:48:12 +00002007 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002008 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002009 unsigned char OpFlags = 0;
2010
2011 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2012 // symbols should go through the PLT.
2013 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002014 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002015 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002016 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002017 Subtarget->getDarwinVers() < 9) {
2018 // PC-relative references to external symbols should go through $stub,
2019 // unless we're building with the leopard linker or later, which
2020 // automatically synthesizes these stubs.
2021 OpFlags = X86II::MO_DARWIN_STUB;
2022 }
Eric Christopherfd179292009-08-27 18:07:15 +00002023
Chris Lattner48a7d022009-07-09 05:02:21 +00002024 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2025 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002026 }
2027
2028 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002029 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002030
Dale Johannesendd64c412009-02-04 00:33:20 +00002031 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002032 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002033 Callee,InFlag);
2034 Callee = DAG.getRegister(Opc, getPointerTy());
2035 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002036 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Chris Lattnerd96d0722007-02-25 06:40:16 +00002039 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002040 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002041 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002042
Dan Gohman98ca4f22009-08-05 01:29:28 +00002043 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002044 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2045 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002047 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002048
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002049 Ops.push_back(Chain);
2050 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002051
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002053 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002054
Gordon Henriksen86737662008-01-05 16:56:59 +00002055 // Add argument registers to the end of the list so that they are known live
2056 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002057 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2058 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2059 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002060
Evan Cheng586ccac2008-03-18 23:36:35 +00002061 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002062 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002063 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2064
2065 // Add an implicit use of AL for x86 vararg functions.
2066 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002067 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002068
Gabor Greifba36cb52008-08-28 21:40:38 +00002069 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002070 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002071
Dan Gohman98ca4f22009-08-05 01:29:28 +00002072 if (isTailCall) {
2073 // If this is the first return lowered for this function, add the regs
2074 // to the liveout set for the function.
2075 if (MF.getRegInfo().liveout_empty()) {
2076 SmallVector<CCValAssign, 16> RVLocs;
2077 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2078 *DAG.getContext());
2079 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2080 for (unsigned i = 0; i != RVLocs.size(); ++i)
2081 if (RVLocs[i].isRegLoc())
2082 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002084
Dan Gohman98ca4f22009-08-05 01:29:28 +00002085 assert(((Callee.getOpcode() == ISD::Register &&
2086 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002087 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002088 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2089 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002090 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002091
2092 return DAG.getNode(X86ISD::TC_RETURN, dl,
2093 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002094 }
2095
Dale Johannesenace16102009-02-03 19:33:06 +00002096 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002097 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002098
Chris Lattner2d297092006-05-23 18:50:38 +00002099 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002102 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002103 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002104 // If this is is a call to a struct-return function, the callee
2105 // pops the hidden struct pointer, so we have to push it back.
2106 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002107 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002108 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002109 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Gordon Henriksenae636f82008-01-03 16:47:34 +00002111 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002112 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002113 DAG.getIntPtrConstant(NumBytes, true),
2114 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2115 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002116 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002117 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002118
Chris Lattner3085e152007-02-25 08:59:22 +00002119 // Handle result values, copying them out of physregs into vregs that we
2120 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2122 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002123}
2124
Evan Cheng25ab6902006-09-08 06:48:29 +00002125
2126//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002127// Fast Calling Convention (tail call) implementation
2128//===----------------------------------------------------------------------===//
2129
2130// Like std call, callee cleans arguments, convention except that ECX is
2131// reserved for storing the tail called function address. Only 2 registers are
2132// free for argument passing (inreg). Tail call optimization is performed
2133// provided:
2134// * tailcallopt is enabled
2135// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002136// On X86_64 architecture with GOT-style position independent code only local
2137// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002138// To keep the stack aligned according to platform abi the function
2139// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2140// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002141// If a tail called function callee has more arguments than the caller the
2142// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002143// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002144// original REtADDR, but before the saved framepointer or the spilled registers
2145// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2146// stack layout:
2147// arg1
2148// arg2
2149// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002150// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002151// move area ]
2152// (possible EBP)
2153// ESI
2154// EDI
2155// local1 ..
2156
2157/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2158/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002159unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002160 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002161 MachineFunction &MF = DAG.getMachineFunction();
2162 const TargetMachine &TM = MF.getTarget();
2163 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2164 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002165 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002166 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002167 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002168 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2169 // Number smaller than 12 so just add the difference.
2170 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2171 } else {
2172 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002173 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002174 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002175 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002176 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002177}
2178
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2180/// for tail call optimization. Targets which want to do tail call
2181/// optimization should implement this function.
2182bool
2183X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002184 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 bool isVarArg,
2186 const SmallVectorImpl<ISD::InputArg> &Ins,
2187 SelectionDAG& DAG) const {
2188 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002189 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002191}
2192
Dan Gohman3df24e62008-09-03 23:12:08 +00002193FastISel *
2194X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002195 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002196 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002197 DenseMap<const Value *, unsigned> &vm,
2198 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002199 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002200 DenseMap<const AllocaInst *, int> &am
2201#ifndef NDEBUG
2202 , SmallSet<Instruction*, 8> &cil
2203#endif
2204 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002205 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002206#ifndef NDEBUG
2207 , cil
2208#endif
2209 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002210}
2211
2212
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002213//===----------------------------------------------------------------------===//
2214// Other Lowering Hooks
2215//===----------------------------------------------------------------------===//
2216
2217
Dan Gohman475871a2008-07-27 21:46:04 +00002218SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002219 MachineFunction &MF = DAG.getMachineFunction();
2220 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2221 int ReturnAddrIndex = FuncInfo->getRAIndex();
2222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002223 if (ReturnAddrIndex == 0) {
2224 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002225 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002226 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2227 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002228 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002229 }
2230
Evan Cheng25ab6902006-09-08 06:48:29 +00002231 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002232}
2233
2234
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002235bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2236 bool hasSymbolicDisplacement) {
2237 // Offset should fit into 32 bit immediate field.
2238 if (!isInt32(Offset))
2239 return false;
2240
2241 // If we don't have a symbolic displacement - we don't have any extra
2242 // restrictions.
2243 if (!hasSymbolicDisplacement)
2244 return true;
2245
2246 // FIXME: Some tweaks might be needed for medium code model.
2247 if (M != CodeModel::Small && M != CodeModel::Kernel)
2248 return false;
2249
2250 // For small code model we assume that latest object is 16MB before end of 31
2251 // bits boundary. We may also accept pretty large negative constants knowing
2252 // that all objects are in the positive half of address space.
2253 if (M == CodeModel::Small && Offset < 16*1024*1024)
2254 return true;
2255
2256 // For kernel code model we know that all object resist in the negative half
2257 // of 32bits address space. We may not accept negative offsets, since they may
2258 // be just off and we may accept pretty large positive ones.
2259 if (M == CodeModel::Kernel && Offset > 0)
2260 return true;
2261
2262 return false;
2263}
2264
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002265/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2266/// specific condition code, returning the condition code and the LHS/RHS of the
2267/// comparison to make.
2268static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2269 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002270 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002271 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2272 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2273 // X > -1 -> X == 0, jump !sign.
2274 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002275 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002276 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2277 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002278 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002279 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002280 // X < 1 -> X <= 0
2281 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002282 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002283 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002284 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002285
Evan Chengd9558e02006-01-06 00:43:03 +00002286 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002287 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002288 case ISD::SETEQ: return X86::COND_E;
2289 case ISD::SETGT: return X86::COND_G;
2290 case ISD::SETGE: return X86::COND_GE;
2291 case ISD::SETLT: return X86::COND_L;
2292 case ISD::SETLE: return X86::COND_LE;
2293 case ISD::SETNE: return X86::COND_NE;
2294 case ISD::SETULT: return X86::COND_B;
2295 case ISD::SETUGT: return X86::COND_A;
2296 case ISD::SETULE: return X86::COND_BE;
2297 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002298 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002299 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002300
Chris Lattner4c78e022008-12-23 23:42:27 +00002301 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002302
Chris Lattner4c78e022008-12-23 23:42:27 +00002303 // If LHS is a foldable load, but RHS is not, flip the condition.
2304 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2305 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2306 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2307 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002308 }
2309
Chris Lattner4c78e022008-12-23 23:42:27 +00002310 switch (SetCCOpcode) {
2311 default: break;
2312 case ISD::SETOLT:
2313 case ISD::SETOLE:
2314 case ISD::SETUGT:
2315 case ISD::SETUGE:
2316 std::swap(LHS, RHS);
2317 break;
2318 }
2319
2320 // On a floating point condition, the flags are set as follows:
2321 // ZF PF CF op
2322 // 0 | 0 | 0 | X > Y
2323 // 0 | 0 | 1 | X < Y
2324 // 1 | 0 | 0 | X == Y
2325 // 1 | 1 | 1 | unordered
2326 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002327 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002328 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002329 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002330 case ISD::SETOLT: // flipped
2331 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002332 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002333 case ISD::SETOLE: // flipped
2334 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002335 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002336 case ISD::SETUGT: // flipped
2337 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002338 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002339 case ISD::SETUGE: // flipped
2340 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002341 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002342 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002343 case ISD::SETNE: return X86::COND_NE;
2344 case ISD::SETUO: return X86::COND_P;
2345 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002346 case ISD::SETOEQ:
2347 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002348 }
Evan Chengd9558e02006-01-06 00:43:03 +00002349}
2350
Evan Cheng4a460802006-01-11 00:33:36 +00002351/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2352/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002353/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002354static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002355 switch (X86CC) {
2356 default:
2357 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002358 case X86::COND_B:
2359 case X86::COND_BE:
2360 case X86::COND_E:
2361 case X86::COND_P:
2362 case X86::COND_A:
2363 case X86::COND_AE:
2364 case X86::COND_NE:
2365 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002366 return true;
2367 }
2368}
2369
Evan Chengeb2f9692009-10-27 19:56:55 +00002370/// isFPImmLegal - Returns true if the target can instruction select the
2371/// specified FP immediate natively. If false, the legalizer will
2372/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002373bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002374 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2375 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2376 return true;
2377 }
2378 return false;
2379}
2380
Nate Begeman9008ca62009-04-27 18:41:29 +00002381/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2382/// the specified range (L, H].
2383static bool isUndefOrInRange(int Val, int Low, int Hi) {
2384 return (Val < 0) || (Val >= Low && Val < Hi);
2385}
2386
2387/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2388/// specified value.
2389static bool isUndefOrEqual(int Val, int CmpVal) {
2390 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002391 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002392 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002393}
2394
Nate Begeman9008ca62009-04-27 18:41:29 +00002395/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2396/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2397/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002398static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002399 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002400 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002401 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002402 return (Mask[0] < 2 && Mask[1] < 2);
2403 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002404}
2405
Nate Begeman9008ca62009-04-27 18:41:29 +00002406bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002407 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002408 N->getMask(M);
2409 return ::isPSHUFDMask(M, N->getValueType(0));
2410}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002411
Nate Begeman9008ca62009-04-27 18:41:29 +00002412/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2413/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002414static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002415 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002416 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002417
Nate Begeman9008ca62009-04-27 18:41:29 +00002418 // Lower quadword copied in order or undef.
2419 for (int i = 0; i != 4; ++i)
2420 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002421 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002422
Evan Cheng506d3df2006-03-29 23:07:14 +00002423 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002424 for (int i = 4; i != 8; ++i)
2425 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002426 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002427
Evan Cheng506d3df2006-03-29 23:07:14 +00002428 return true;
2429}
2430
Nate Begeman9008ca62009-04-27 18:41:29 +00002431bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002432 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002433 N->getMask(M);
2434 return ::isPSHUFHWMask(M, N->getValueType(0));
2435}
Evan Cheng506d3df2006-03-29 23:07:14 +00002436
Nate Begeman9008ca62009-04-27 18:41:29 +00002437/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2438/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002439static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002440 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002441 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002442
Rafael Espindola15684b22009-04-24 12:40:33 +00002443 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002444 for (int i = 4; i != 8; ++i)
2445 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002446 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002447
Rafael Espindola15684b22009-04-24 12:40:33 +00002448 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002449 for (int i = 0; i != 4; ++i)
2450 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002451 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002452
Rafael Espindola15684b22009-04-24 12:40:33 +00002453 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002454}
2455
Nate Begeman9008ca62009-04-27 18:41:29 +00002456bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002457 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002458 N->getMask(M);
2459 return ::isPSHUFLWMask(M, N->getValueType(0));
2460}
2461
Nate Begemana09008b2009-10-19 02:17:23 +00002462/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2463/// is suitable for input to PALIGNR.
2464static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2465 bool hasSSSE3) {
2466 int i, e = VT.getVectorNumElements();
2467
2468 // Do not handle v2i64 / v2f64 shuffles with palignr.
2469 if (e < 4 || !hasSSSE3)
2470 return false;
2471
2472 for (i = 0; i != e; ++i)
2473 if (Mask[i] >= 0)
2474 break;
2475
2476 // All undef, not a palignr.
2477 if (i == e)
2478 return false;
2479
2480 // Determine if it's ok to perform a palignr with only the LHS, since we
2481 // don't have access to the actual shuffle elements to see if RHS is undef.
2482 bool Unary = Mask[i] < (int)e;
2483 bool NeedsUnary = false;
2484
2485 int s = Mask[i] - i;
2486
2487 // Check the rest of the elements to see if they are consecutive.
2488 for (++i; i != e; ++i) {
2489 int m = Mask[i];
2490 if (m < 0)
2491 continue;
2492
2493 Unary = Unary && (m < (int)e);
2494 NeedsUnary = NeedsUnary || (m < s);
2495
2496 if (NeedsUnary && !Unary)
2497 return false;
2498 if (Unary && m != ((s+i) & (e-1)))
2499 return false;
2500 if (!Unary && m != (s+i))
2501 return false;
2502 }
2503 return true;
2504}
2505
2506bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2507 SmallVector<int, 8> M;
2508 N->getMask(M);
2509 return ::isPALIGNRMask(M, N->getValueType(0), true);
2510}
2511
Evan Cheng14aed5e2006-03-24 01:18:28 +00002512/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2513/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002514static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 int NumElems = VT.getVectorNumElements();
2516 if (NumElems != 2 && NumElems != 4)
2517 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002518
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 int Half = NumElems / 2;
2520 for (int i = 0; i < Half; ++i)
2521 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002522 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002523 for (int i = Half; i < NumElems; ++i)
2524 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002525 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002526
Evan Cheng14aed5e2006-03-24 01:18:28 +00002527 return true;
2528}
2529
Nate Begeman9008ca62009-04-27 18:41:29 +00002530bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2531 SmallVector<int, 8> M;
2532 N->getMask(M);
2533 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002534}
2535
Evan Cheng213d2cf2007-05-17 18:45:50 +00002536/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002537/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2538/// half elements to come from vector 1 (which would equal the dest.) and
2539/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002540static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002541 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002542
2543 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002544 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002545
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 int Half = NumElems / 2;
2547 for (int i = 0; i < Half; ++i)
2548 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002549 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002550 for (int i = Half; i < NumElems; ++i)
2551 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002552 return false;
2553 return true;
2554}
2555
Nate Begeman9008ca62009-04-27 18:41:29 +00002556static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2557 SmallVector<int, 8> M;
2558 N->getMask(M);
2559 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002560}
2561
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002562/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2563/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002564bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2565 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002566 return false;
2567
Evan Cheng2064a2b2006-03-28 06:50:32 +00002568 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2570 isUndefOrEqual(N->getMaskElt(1), 7) &&
2571 isUndefOrEqual(N->getMaskElt(2), 2) &&
2572 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002573}
2574
Nate Begeman0b10b912009-11-07 23:17:15 +00002575/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2576/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2577/// <2, 3, 2, 3>
2578bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2579 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2580
2581 if (NumElems != 4)
2582 return false;
2583
2584 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2585 isUndefOrEqual(N->getMaskElt(1), 3) &&
2586 isUndefOrEqual(N->getMaskElt(2), 2) &&
2587 isUndefOrEqual(N->getMaskElt(3), 3);
2588}
2589
Evan Cheng5ced1d82006-04-06 23:23:56 +00002590/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2591/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002592bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2593 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002594
Evan Cheng5ced1d82006-04-06 23:23:56 +00002595 if (NumElems != 2 && NumElems != 4)
2596 return false;
2597
Evan Chengc5cdff22006-04-07 21:53:05 +00002598 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002600 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002601
Evan Chengc5cdff22006-04-07 21:53:05 +00002602 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002604 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002605
2606 return true;
2607}
2608
Nate Begeman0b10b912009-11-07 23:17:15 +00002609/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2610/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2611bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002613
Evan Cheng5ced1d82006-04-06 23:23:56 +00002614 if (NumElems != 2 && NumElems != 4)
2615 return false;
2616
Evan Chengc5cdff22006-04-07 21:53:05 +00002617 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002618 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002619 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002620
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 for (unsigned i = 0; i < NumElems/2; ++i)
2622 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002623 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002624
2625 return true;
2626}
2627
Evan Cheng0038e592006-03-28 00:39:58 +00002628/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2629/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002630static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002631 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002633 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002634 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002635
Nate Begeman9008ca62009-04-27 18:41:29 +00002636 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2637 int BitI = Mask[i];
2638 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002639 if (!isUndefOrEqual(BitI, j))
2640 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002641 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002642 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002643 return false;
2644 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002645 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002646 return false;
2647 }
Evan Cheng0038e592006-03-28 00:39:58 +00002648 }
Evan Cheng0038e592006-03-28 00:39:58 +00002649 return true;
2650}
2651
Nate Begeman9008ca62009-04-27 18:41:29 +00002652bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2653 SmallVector<int, 8> M;
2654 N->getMask(M);
2655 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002656}
2657
Evan Cheng4fcb9222006-03-28 02:43:26 +00002658/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2659/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002660static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002661 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002663 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002664 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002665
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2667 int BitI = Mask[i];
2668 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002669 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002670 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002671 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002672 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002673 return false;
2674 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002675 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002676 return false;
2677 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002678 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002679 return true;
2680}
2681
Nate Begeman9008ca62009-04-27 18:41:29 +00002682bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2683 SmallVector<int, 8> M;
2684 N->getMask(M);
2685 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002686}
2687
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002688/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2689/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2690/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002691static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002692 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002693 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002694 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002695
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2697 int BitI = Mask[i];
2698 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002699 if (!isUndefOrEqual(BitI, j))
2700 return false;
2701 if (!isUndefOrEqual(BitI1, j))
2702 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002703 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002704 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002705}
2706
Nate Begeman9008ca62009-04-27 18:41:29 +00002707bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2708 SmallVector<int, 8> M;
2709 N->getMask(M);
2710 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2711}
2712
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002713/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2714/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2715/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002716static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002717 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002718 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2719 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002720
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2722 int BitI = Mask[i];
2723 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002724 if (!isUndefOrEqual(BitI, j))
2725 return false;
2726 if (!isUndefOrEqual(BitI1, j))
2727 return false;
2728 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002729 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002730}
2731
Nate Begeman9008ca62009-04-27 18:41:29 +00002732bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2733 SmallVector<int, 8> M;
2734 N->getMask(M);
2735 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2736}
2737
Evan Cheng017dcc62006-04-21 01:05:10 +00002738/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2739/// specifies a shuffle of elements that is suitable for input to MOVSS,
2740/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002741static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002742 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002743 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002744
2745 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002746
Nate Begeman9008ca62009-04-27 18:41:29 +00002747 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002748 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002749
Nate Begeman9008ca62009-04-27 18:41:29 +00002750 for (int i = 1; i < NumElts; ++i)
2751 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002752 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002753
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002754 return true;
2755}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002756
Nate Begeman9008ca62009-04-27 18:41:29 +00002757bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2758 SmallVector<int, 8> M;
2759 N->getMask(M);
2760 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002761}
2762
Evan Cheng017dcc62006-04-21 01:05:10 +00002763/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2764/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002765/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002766static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 bool V2IsSplat = false, bool V2IsUndef = false) {
2768 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002769 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002770 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002771
Nate Begeman9008ca62009-04-27 18:41:29 +00002772 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002773 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002774
Nate Begeman9008ca62009-04-27 18:41:29 +00002775 for (int i = 1; i < NumOps; ++i)
2776 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2777 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2778 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002779 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002780
Evan Cheng39623da2006-04-20 08:58:49 +00002781 return true;
2782}
2783
Nate Begeman9008ca62009-04-27 18:41:29 +00002784static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002785 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002786 SmallVector<int, 8> M;
2787 N->getMask(M);
2788 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002789}
2790
Evan Chengd9539472006-04-14 21:59:03 +00002791/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2792/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002793bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2794 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002795 return false;
2796
2797 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002798 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002799 int Elt = N->getMaskElt(i);
2800 if (Elt >= 0 && Elt != 1)
2801 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002802 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002803
2804 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002805 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 int Elt = N->getMaskElt(i);
2807 if (Elt >= 0 && Elt != 3)
2808 return false;
2809 if (Elt == 3)
2810 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002811 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002812 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002814 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002815}
2816
2817/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2818/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002819bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2820 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002821 return false;
2822
2823 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002824 for (unsigned i = 0; i < 2; ++i)
2825 if (N->getMaskElt(i) > 0)
2826 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002827
2828 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002829 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 int Elt = N->getMaskElt(i);
2831 if (Elt >= 0 && Elt != 2)
2832 return false;
2833 if (Elt == 2)
2834 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002835 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002837 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002838}
2839
Evan Cheng0b457f02008-09-25 20:50:48 +00002840/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2841/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002842bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2843 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002844
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 for (int i = 0; i < e; ++i)
2846 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002847 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002848 for (int i = 0; i < e; ++i)
2849 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002850 return false;
2851 return true;
2852}
2853
Evan Cheng63d33002006-03-22 08:01:21 +00002854/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002855/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002856unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002857 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2858 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2859
Evan Chengb9df0ca2006-03-22 02:53:00 +00002860 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2861 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002862 for (int i = 0; i < NumOperands; ++i) {
2863 int Val = SVOp->getMaskElt(NumOperands-i-1);
2864 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002865 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002866 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002867 if (i != NumOperands - 1)
2868 Mask <<= Shift;
2869 }
Evan Cheng63d33002006-03-22 08:01:21 +00002870 return Mask;
2871}
2872
Evan Cheng506d3df2006-03-29 23:07:14 +00002873/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002874/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002875unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002877 unsigned Mask = 0;
2878 // 8 nodes, but we only care about the last 4.
2879 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 int Val = SVOp->getMaskElt(i);
2881 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002882 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002883 if (i != 4)
2884 Mask <<= 2;
2885 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002886 return Mask;
2887}
2888
2889/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002890/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002891unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002892 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002893 unsigned Mask = 0;
2894 // 8 nodes, but we only care about the first 4.
2895 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002896 int Val = SVOp->getMaskElt(i);
2897 if (Val >= 0)
2898 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002899 if (i != 0)
2900 Mask <<= 2;
2901 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002902 return Mask;
2903}
2904
Nate Begemana09008b2009-10-19 02:17:23 +00002905/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2906/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2907unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2908 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2909 EVT VVT = N->getValueType(0);
2910 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2911 int Val = 0;
2912
2913 unsigned i, e;
2914 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2915 Val = SVOp->getMaskElt(i);
2916 if (Val >= 0)
2917 break;
2918 }
2919 return (Val - i) * EltSize;
2920}
2921
Evan Cheng37b73872009-07-30 08:33:02 +00002922/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2923/// constant +0.0.
2924bool X86::isZeroNode(SDValue Elt) {
2925 return ((isa<ConstantSDNode>(Elt) &&
2926 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2927 (isa<ConstantFPSDNode>(Elt) &&
2928 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2929}
2930
Nate Begeman9008ca62009-04-27 18:41:29 +00002931/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2932/// their permute mask.
2933static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2934 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002935 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002936 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002937 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002938
Nate Begeman5a5ca152009-04-29 05:20:52 +00002939 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 int idx = SVOp->getMaskElt(i);
2941 if (idx < 0)
2942 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002943 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002945 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002947 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2949 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002950}
2951
Evan Cheng779ccea2007-12-07 21:30:01 +00002952/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2953/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00002954static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002955 unsigned NumElems = VT.getVectorNumElements();
2956 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 int idx = Mask[i];
2958 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002959 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002960 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002962 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002963 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002964 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002965}
2966
Evan Cheng533a0aa2006-04-19 20:35:22 +00002967/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2968/// match movhlps. The lower half elements should come from upper half of
2969/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002970/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002971static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2972 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002973 return false;
2974 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002976 return false;
2977 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002979 return false;
2980 return true;
2981}
2982
Evan Cheng5ced1d82006-04-06 23:23:56 +00002983/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002984/// is promoted to a vector. It also returns the LoadSDNode by reference if
2985/// required.
2986static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002987 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2988 return false;
2989 N = N->getOperand(0).getNode();
2990 if (!ISD::isNON_EXTLoad(N))
2991 return false;
2992 if (LD)
2993 *LD = cast<LoadSDNode>(N);
2994 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002995}
2996
Evan Cheng533a0aa2006-04-19 20:35:22 +00002997/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2998/// match movlp{s|d}. The lower half elements should come from lower half of
2999/// V1 (and in order), and the upper half elements should come from the upper
3000/// half of V2 (and in order). And since V1 will become the source of the
3001/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003002static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3003 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003004 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003005 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003006 // Is V2 is a vector load, don't do this transformation. We will try to use
3007 // load folding shufps op.
3008 if (ISD::isNON_EXTLoad(V2))
3009 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003010
Nate Begeman5a5ca152009-04-29 05:20:52 +00003011 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003012
Evan Cheng533a0aa2006-04-19 20:35:22 +00003013 if (NumElems != 2 && NumElems != 4)
3014 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003015 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003017 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003018 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003019 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003020 return false;
3021 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003022}
3023
Evan Cheng39623da2006-04-20 08:58:49 +00003024/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3025/// all the same.
3026static bool isSplatVector(SDNode *N) {
3027 if (N->getOpcode() != ISD::BUILD_VECTOR)
3028 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003029
Dan Gohman475871a2008-07-27 21:46:04 +00003030 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003031 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3032 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003033 return false;
3034 return true;
3035}
3036
Evan Cheng213d2cf2007-05-17 18:45:50 +00003037/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003038/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003039/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003040static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003041 SDValue V1 = N->getOperand(0);
3042 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003043 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3044 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003045 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003046 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003048 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3049 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003050 if (Opc != ISD::BUILD_VECTOR ||
3051 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 return false;
3053 } else if (Idx >= 0) {
3054 unsigned Opc = V1.getOpcode();
3055 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3056 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003057 if (Opc != ISD::BUILD_VECTOR ||
3058 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003059 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003060 }
3061 }
3062 return true;
3063}
3064
3065/// getZeroVector - Returns a vector of specified type with all zero elements.
3066///
Owen Andersone50ed302009-08-10 22:56:29 +00003067static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003068 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003069 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003070
Chris Lattner8a594482007-11-25 00:24:49 +00003071 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3072 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003073 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003074 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3076 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003077 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3079 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003080 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3082 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003083 }
Dale Johannesenace16102009-02-03 19:33:06 +00003084 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003085}
3086
Chris Lattner8a594482007-11-25 00:24:49 +00003087/// getOnesVector - Returns a vector of specified type with all bits set.
3088///
Owen Andersone50ed302009-08-10 22:56:29 +00003089static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003090 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003091
Chris Lattner8a594482007-11-25 00:24:49 +00003092 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3093 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003094 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003095 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003096 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003097 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003098 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003100 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003101}
3102
3103
Evan Cheng39623da2006-04-20 08:58:49 +00003104/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3105/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003106static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003107 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003108 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003109
Evan Cheng39623da2006-04-20 08:58:49 +00003110 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003111 SmallVector<int, 8> MaskVec;
3112 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003113
Nate Begeman5a5ca152009-04-29 05:20:52 +00003114 for (unsigned i = 0; i != NumElems; ++i) {
3115 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 MaskVec[i] = NumElems;
3117 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003118 }
Evan Cheng39623da2006-04-20 08:58:49 +00003119 }
Evan Cheng39623da2006-04-20 08:58:49 +00003120 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003121 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3122 SVOp->getOperand(1), &MaskVec[0]);
3123 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003124}
3125
Evan Cheng017dcc62006-04-21 01:05:10 +00003126/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3127/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003128static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003129 SDValue V2) {
3130 unsigned NumElems = VT.getVectorNumElements();
3131 SmallVector<int, 8> Mask;
3132 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003133 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 Mask.push_back(i);
3135 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003136}
3137
Nate Begeman9008ca62009-04-27 18:41:29 +00003138/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003139static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 SDValue V2) {
3141 unsigned NumElems = VT.getVectorNumElements();
3142 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003143 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 Mask.push_back(i);
3145 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003146 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003148}
3149
Nate Begeman9008ca62009-04-27 18:41:29 +00003150/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003151static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 SDValue V2) {
3153 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003154 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003156 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 Mask.push_back(i + Half);
3158 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003159 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003160 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003161}
3162
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003163/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003164static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 bool HasSSE2) {
3166 if (SV->getValueType(0).getVectorNumElements() <= 4)
3167 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003168
Owen Anderson825b72b2009-08-11 20:47:22 +00003169 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003170 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 DebugLoc dl = SV->getDebugLoc();
3172 SDValue V1 = SV->getOperand(0);
3173 int NumElems = VT.getVectorNumElements();
3174 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003175
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 // unpack elements to the correct location
3177 while (NumElems > 4) {
3178 if (EltNo < NumElems/2) {
3179 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3180 } else {
3181 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3182 EltNo -= NumElems/2;
3183 }
3184 NumElems >>= 1;
3185 }
Eric Christopherfd179292009-08-27 18:07:15 +00003186
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 // Perform the splat.
3188 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003189 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3191 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003192}
3193
Evan Chengba05f722006-04-21 23:03:30 +00003194/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003195/// vector of zero or undef vector. This produces a shuffle where the low
3196/// element of V2 is swizzled into the zero/undef vector, landing at element
3197/// 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 +00003198static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003199 bool isZero, bool HasSSE2,
3200 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003201 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3204 unsigned NumElems = VT.getVectorNumElements();
3205 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003206 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 // If this is the insertion idx, put the low elt of V2 here.
3208 MaskVec.push_back(i == Idx ? NumElems : i);
3209 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003210}
3211
Evan Chengf26ffe92008-05-29 08:22:04 +00003212/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3213/// a shuffle that is zero.
3214static
Nate Begeman9008ca62009-04-27 18:41:29 +00003215unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3216 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003217 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003219 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 int Idx = SVOp->getMaskElt(Index);
3221 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003222 ++NumZeros;
3223 continue;
3224 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003226 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003227 ++NumZeros;
3228 else
3229 break;
3230 }
3231 return NumZeros;
3232}
3233
3234/// isVectorShift - Returns true if the shuffle can be implemented as a
3235/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003236/// FIXME: split into pslldqi, psrldqi, palignr variants.
3237static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003238 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003240
3241 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003243 if (!NumZeros) {
3244 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003246 if (!NumZeros)
3247 return false;
3248 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003249 bool SeenV1 = false;
3250 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 for (int i = NumZeros; i < NumElems; ++i) {
3252 int Val = isLeft ? (i - NumZeros) : i;
3253 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3254 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003255 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003257 SeenV1 = true;
3258 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003260 SeenV2 = true;
3261 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003262 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003263 return false;
3264 }
3265 if (SeenV1 && SeenV2)
3266 return false;
3267
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003269 ShAmt = NumZeros;
3270 return true;
3271}
3272
3273
Evan Chengc78d3b42006-04-24 18:01:45 +00003274/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3275///
Dan Gohman475871a2008-07-27 21:46:04 +00003276static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003277 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003278 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003279 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003280 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003281
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003282 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003283 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003284 bool First = true;
3285 for (unsigned i = 0; i < 16; ++i) {
3286 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3287 if (ThisIsNonZero && First) {
3288 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003290 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003292 First = false;
3293 }
3294
3295 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003297 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3298 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003299 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003301 }
3302 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3304 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3305 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003306 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003308 } else
3309 ThisElt = LastElt;
3310
Gabor Greifba36cb52008-08-28 21:40:38 +00003311 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003313 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003314 }
3315 }
3316
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003318}
3319
Bill Wendlinga348c562007-03-22 18:42:45 +00003320/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003321///
Dan Gohman475871a2008-07-27 21:46:04 +00003322static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003323 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003324 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003325 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003326 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003327
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003328 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003329 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003330 bool First = true;
3331 for (unsigned i = 0; i < 8; ++i) {
3332 bool isNonZero = (NonZeros & (1 << i)) != 0;
3333 if (isNonZero) {
3334 if (First) {
3335 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003337 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003339 First = false;
3340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003341 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003343 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003344 }
3345 }
3346
3347 return V;
3348}
3349
Evan Chengf26ffe92008-05-29 08:22:04 +00003350/// getVShift - Return a vector logical shift node.
3351///
Owen Andersone50ed302009-08-10 22:56:29 +00003352static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003353 unsigned NumBits, SelectionDAG &DAG,
3354 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003355 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003357 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003358 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3359 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3360 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003361 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003362}
3363
Dan Gohman475871a2008-07-27 21:46:04 +00003364SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003365X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3366 SelectionDAG &DAG) {
3367
3368 // Check if the scalar load can be widened into a vector load. And if
3369 // the address is "base + cst" see if the cst can be "absorbed" into
3370 // the shuffle mask.
3371 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3372 SDValue Ptr = LD->getBasePtr();
3373 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3374 return SDValue();
3375 EVT PVT = LD->getValueType(0);
3376 if (PVT != MVT::i32 && PVT != MVT::f32)
3377 return SDValue();
3378
3379 int FI = -1;
3380 int64_t Offset = 0;
3381 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3382 FI = FINode->getIndex();
3383 Offset = 0;
3384 } else if (Ptr.getOpcode() == ISD::ADD &&
3385 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3386 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3387 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3388 Offset = Ptr.getConstantOperandVal(1);
3389 Ptr = Ptr.getOperand(0);
3390 } else {
3391 return SDValue();
3392 }
3393
3394 SDValue Chain = LD->getChain();
3395 // Make sure the stack object alignment is at least 16.
3396 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3397 if (DAG.InferPtrAlignment(Ptr) < 16) {
3398 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003399 // Can't change the alignment. FIXME: It's possible to compute
3400 // the exact stack offset and reference FI + adjust offset instead.
3401 // If someone *really* cares about this. That's the way to implement it.
3402 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003403 } else {
3404 MFI->setObjectAlignment(FI, 16);
3405 }
3406 }
3407
3408 // (Offset % 16) must be multiple of 4. Then address is then
3409 // Ptr + (Offset & ~15).
3410 if (Offset < 0)
3411 return SDValue();
3412 if ((Offset % 16) & 3)
3413 return SDValue();
3414 int64_t StartOffset = Offset & ~15;
3415 if (StartOffset)
3416 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3417 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3418
3419 int EltNo = (Offset - StartOffset) >> 2;
3420 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3421 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3422 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3423 // Canonicalize it to a v4i32 shuffle.
3424 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3425 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3426 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3427 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3428 }
3429
3430 return SDValue();
3431}
3432
3433SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003434X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003435 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003436 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003437 if (ISD::isBuildVectorAllZeros(Op.getNode())
3438 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003439 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3440 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3441 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003442 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003443 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003444
Gabor Greifba36cb52008-08-28 21:40:38 +00003445 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003446 return getOnesVector(Op.getValueType(), DAG, dl);
3447 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003448 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003449
Owen Andersone50ed302009-08-10 22:56:29 +00003450 EVT VT = Op.getValueType();
3451 EVT ExtVT = VT.getVectorElementType();
3452 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003453
3454 unsigned NumElems = Op.getNumOperands();
3455 unsigned NumZero = 0;
3456 unsigned NumNonZero = 0;
3457 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003458 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003460 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003461 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003462 if (Elt.getOpcode() == ISD::UNDEF)
3463 continue;
3464 Values.insert(Elt);
3465 if (Elt.getOpcode() != ISD::Constant &&
3466 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003467 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003468 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003469 NumZero++;
3470 else {
3471 NonZeros |= (1 << i);
3472 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003473 }
3474 }
3475
Dan Gohman7f321562007-06-25 16:23:39 +00003476 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003477 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003478 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003479 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003480
Chris Lattner67f453a2008-03-09 05:42:06 +00003481 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003482 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003483 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003484 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003485
Chris Lattner62098042008-03-09 01:05:04 +00003486 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3487 // the value are obviously zero, truncate the value to i32 and do the
3488 // insertion that way. Only do this if the value is non-constant or if the
3489 // value is a constant being inserted into element 0. It is cheaper to do
3490 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003492 (!IsAllConstants || Idx == 0)) {
3493 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3494 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3496 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003497
Chris Lattner62098042008-03-09 01:05:04 +00003498 // Truncate the value (which may itself be a constant) to i32, and
3499 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003501 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003502 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3503 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003504
Chris Lattner62098042008-03-09 01:05:04 +00003505 // Now we have our 32-bit value zero extended in the low element of
3506 // a vector. If Idx != 0, swizzle it into place.
3507 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003508 SmallVector<int, 4> Mask;
3509 Mask.push_back(Idx);
3510 for (unsigned i = 1; i != VecElts; ++i)
3511 Mask.push_back(i);
3512 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003513 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003514 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003515 }
Dale Johannesenace16102009-02-03 19:33:06 +00003516 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003517 }
3518 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003519
Chris Lattner19f79692008-03-08 22:59:52 +00003520 // If we have a constant or non-constant insertion into the low element of
3521 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3522 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003523 // depending on what the source datatype is.
3524 if (Idx == 0) {
3525 if (NumZero == 0) {
3526 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3528 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003529 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3530 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3531 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3532 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3534 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3535 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003536 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3537 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3538 Subtarget->hasSSE2(), DAG);
3539 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3540 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003541 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003542
3543 // Is it a vector logical left shift?
3544 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003545 X86::isZeroNode(Op.getOperand(0)) &&
3546 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003547 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003548 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003549 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003550 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003551 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003552 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003553
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003554 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003555 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003556
Chris Lattner19f79692008-03-08 22:59:52 +00003557 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3558 // is a non-constant being inserted into an element other than the low one,
3559 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3560 // movd/movss) to move this into the low element, then shuffle it into
3561 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003562 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003563 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003564
Evan Cheng0db9fe62006-04-25 20:13:52 +00003565 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003566 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3567 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003568 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003569 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 MaskVec.push_back(i == Idx ? 0 : 1);
3571 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003572 }
3573 }
3574
Chris Lattner67f453a2008-03-09 05:42:06 +00003575 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003576 if (Values.size() == 1) {
3577 if (EVTBits == 32) {
3578 // Instead of a shuffle like this:
3579 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3580 // Check if it's possible to issue this instead.
3581 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3582 unsigned Idx = CountTrailingZeros_32(NonZeros);
3583 SDValue Item = Op.getOperand(Idx);
3584 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3585 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3586 }
Dan Gohman475871a2008-07-27 21:46:04 +00003587 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003588 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003589
Dan Gohmana3941172007-07-24 22:55:08 +00003590 // A vector full of immediates; various special cases are already
3591 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003592 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003593 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003594
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003595 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003596 if (EVTBits == 64) {
3597 if (NumNonZero == 1) {
3598 // One half is zero or undef.
3599 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003600 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003601 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003602 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3603 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003604 }
Dan Gohman475871a2008-07-27 21:46:04 +00003605 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003606 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003607
3608 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003609 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003610 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003611 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003612 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003613 }
3614
Bill Wendling826f36f2007-03-28 00:57:11 +00003615 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003616 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003617 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003618 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003619 }
3620
3621 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003622 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003623 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003624 if (NumElems == 4 && NumZero > 0) {
3625 for (unsigned i = 0; i < 4; ++i) {
3626 bool isZero = !(NonZeros & (1 << i));
3627 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003628 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003629 else
Dale Johannesenace16102009-02-03 19:33:06 +00003630 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003631 }
3632
3633 for (unsigned i = 0; i < 2; ++i) {
3634 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3635 default: break;
3636 case 0:
3637 V[i] = V[i*2]; // Must be a zero vector.
3638 break;
3639 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003640 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003641 break;
3642 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003643 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003644 break;
3645 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003646 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003647 break;
3648 }
3649 }
3650
Nate Begeman9008ca62009-04-27 18:41:29 +00003651 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003652 bool Reverse = (NonZeros & 0x3) == 2;
3653 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003654 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003655 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3656 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003657 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3658 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003659 }
3660
3661 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003662 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3663 // values to be inserted is equal to the number of elements, in which case
3664 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003665 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003666 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003667 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 getSubtarget()->hasSSE41()) {
3669 V[0] = DAG.getUNDEF(VT);
3670 for (unsigned i = 0; i < NumElems; ++i)
3671 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3672 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3673 Op.getOperand(i), DAG.getIntPtrConstant(i));
3674 return V[0];
3675 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003676 // Expand into a number of unpckl*.
3677 // e.g. for v4f32
3678 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3679 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3680 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003682 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003683 NumElems >>= 1;
3684 while (NumElems != 0) {
3685 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003686 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003687 NumElems >>= 1;
3688 }
3689 return V[0];
3690 }
3691
Dan Gohman475871a2008-07-27 21:46:04 +00003692 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003693}
3694
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003695SDValue
3696X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3697 // We support concatenate two MMX registers and place them in a MMX
3698 // register. This is better than doing a stack convert.
3699 DebugLoc dl = Op.getDebugLoc();
3700 EVT ResVT = Op.getValueType();
3701 assert(Op.getNumOperands() == 2);
3702 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3703 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3704 int Mask[2];
3705 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3706 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3707 InVec = Op.getOperand(1);
3708 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3709 unsigned NumElts = ResVT.getVectorNumElements();
3710 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3711 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3712 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3713 } else {
3714 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3715 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3716 Mask[0] = 0; Mask[1] = 2;
3717 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3718 }
3719 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3720}
3721
Nate Begemanb9a47b82009-02-23 08:49:38 +00003722// v8i16 shuffles - Prefer shuffles in the following order:
3723// 1. [all] pshuflw, pshufhw, optional move
3724// 2. [ssse3] 1 x pshufb
3725// 3. [ssse3] 2 x pshufb + 1 x por
3726// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003727static
Nate Begeman9008ca62009-04-27 18:41:29 +00003728SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3729 SelectionDAG &DAG, X86TargetLowering &TLI) {
3730 SDValue V1 = SVOp->getOperand(0);
3731 SDValue V2 = SVOp->getOperand(1);
3732 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003733 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003734
Nate Begemanb9a47b82009-02-23 08:49:38 +00003735 // Determine if more than 1 of the words in each of the low and high quadwords
3736 // of the result come from the same quadword of one of the two inputs. Undef
3737 // mask values count as coming from any quadword, for better codegen.
3738 SmallVector<unsigned, 4> LoQuad(4);
3739 SmallVector<unsigned, 4> HiQuad(4);
3740 BitVector InputQuads(4);
3741 for (unsigned i = 0; i < 8; ++i) {
3742 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003743 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003744 MaskVals.push_back(EltIdx);
3745 if (EltIdx < 0) {
3746 ++Quad[0];
3747 ++Quad[1];
3748 ++Quad[2];
3749 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003750 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003751 }
3752 ++Quad[EltIdx / 4];
3753 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003754 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003755
Nate Begemanb9a47b82009-02-23 08:49:38 +00003756 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003757 unsigned MaxQuad = 1;
3758 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003759 if (LoQuad[i] > MaxQuad) {
3760 BestLoQuad = i;
3761 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003762 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003763 }
3764
Nate Begemanb9a47b82009-02-23 08:49:38 +00003765 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003766 MaxQuad = 1;
3767 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003768 if (HiQuad[i] > MaxQuad) {
3769 BestHiQuad = i;
3770 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003771 }
3772 }
3773
Nate Begemanb9a47b82009-02-23 08:49:38 +00003774 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003775 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003776 // single pshufb instruction is necessary. If There are more than 2 input
3777 // quads, disable the next transformation since it does not help SSSE3.
3778 bool V1Used = InputQuads[0] || InputQuads[1];
3779 bool V2Used = InputQuads[2] || InputQuads[3];
3780 if (TLI.getSubtarget()->hasSSSE3()) {
3781 if (InputQuads.count() == 2 && V1Used && V2Used) {
3782 BestLoQuad = InputQuads.find_first();
3783 BestHiQuad = InputQuads.find_next(BestLoQuad);
3784 }
3785 if (InputQuads.count() > 2) {
3786 BestLoQuad = -1;
3787 BestHiQuad = -1;
3788 }
3789 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003790
Nate Begemanb9a47b82009-02-23 08:49:38 +00003791 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3792 // the shuffle mask. If a quad is scored as -1, that means that it contains
3793 // words from all 4 input quadwords.
3794 SDValue NewV;
3795 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 SmallVector<int, 8> MaskV;
3797 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3798 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003799 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3801 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3802 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003803
Nate Begemanb9a47b82009-02-23 08:49:38 +00003804 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3805 // source words for the shuffle, to aid later transformations.
3806 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003807 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003808 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003809 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003810 if (idx != (int)i)
3811 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003812 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003813 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003814 AllWordsInNewV = false;
3815 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003816 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003817
Nate Begemanb9a47b82009-02-23 08:49:38 +00003818 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3819 if (AllWordsInNewV) {
3820 for (int i = 0; i != 8; ++i) {
3821 int idx = MaskVals[i];
3822 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003823 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003824 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003825 if ((idx != i) && idx < 4)
3826 pshufhw = false;
3827 if ((idx != i) && idx > 3)
3828 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003829 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003830 V1 = NewV;
3831 V2Used = false;
3832 BestLoQuad = 0;
3833 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003834 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003835
Nate Begemanb9a47b82009-02-23 08:49:38 +00003836 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3837 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003838 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003839 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003840 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003841 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003842 }
Eric Christopherfd179292009-08-27 18:07:15 +00003843
Nate Begemanb9a47b82009-02-23 08:49:38 +00003844 // If we have SSSE3, and all words of the result are from 1 input vector,
3845 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3846 // is present, fall back to case 4.
3847 if (TLI.getSubtarget()->hasSSSE3()) {
3848 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003849
Nate Begemanb9a47b82009-02-23 08:49:38 +00003850 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003851 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003852 // mask, and elements that come from V1 in the V2 mask, so that the two
3853 // results can be OR'd together.
3854 bool TwoInputs = V1Used && V2Used;
3855 for (unsigned i = 0; i != 8; ++i) {
3856 int EltIdx = MaskVals[i] * 2;
3857 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3859 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003860 continue;
3861 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3863 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003864 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003866 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003867 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003869 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003871
Nate Begemanb9a47b82009-02-23 08:49:38 +00003872 // Calculate the shuffle mask for the second input, shuffle it, and
3873 // OR it with the first shuffled input.
3874 pshufbMask.clear();
3875 for (unsigned i = 0; i != 8; ++i) {
3876 int EltIdx = MaskVals[i] * 2;
3877 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3879 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003880 continue;
3881 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3883 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003884 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003886 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003887 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003888 MVT::v16i8, &pshufbMask[0], 16));
3889 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3890 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003891 }
3892
3893 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3894 // and update MaskVals with new element order.
3895 BitVector InOrder(8);
3896 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003897 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003898 for (int i = 0; i != 4; ++i) {
3899 int idx = MaskVals[i];
3900 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003902 InOrder.set(i);
3903 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003905 InOrder.set(i);
3906 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003908 }
3909 }
3910 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003913 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003914 }
Eric Christopherfd179292009-08-27 18:07:15 +00003915
Nate Begemanb9a47b82009-02-23 08:49:38 +00003916 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3917 // and update MaskVals with the new element order.
3918 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003919 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003920 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003921 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003922 for (unsigned i = 4; i != 8; ++i) {
3923 int idx = MaskVals[i];
3924 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003925 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003926 InOrder.set(i);
3927 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003929 InOrder.set(i);
3930 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003932 }
3933 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003936 }
Eric Christopherfd179292009-08-27 18:07:15 +00003937
Nate Begemanb9a47b82009-02-23 08:49:38 +00003938 // In case BestHi & BestLo were both -1, which means each quadword has a word
3939 // from each of the four input quadwords, calculate the InOrder bitvector now
3940 // before falling through to the insert/extract cleanup.
3941 if (BestLoQuad == -1 && BestHiQuad == -1) {
3942 NewV = V1;
3943 for (int i = 0; i != 8; ++i)
3944 if (MaskVals[i] < 0 || MaskVals[i] == i)
3945 InOrder.set(i);
3946 }
Eric Christopherfd179292009-08-27 18:07:15 +00003947
Nate Begemanb9a47b82009-02-23 08:49:38 +00003948 // The other elements are put in the right place using pextrw and pinsrw.
3949 for (unsigned i = 0; i != 8; ++i) {
3950 if (InOrder[i])
3951 continue;
3952 int EltIdx = MaskVals[i];
3953 if (EltIdx < 0)
3954 continue;
3955 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003957 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003959 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003961 DAG.getIntPtrConstant(i));
3962 }
3963 return NewV;
3964}
3965
3966// v16i8 shuffles - Prefer shuffles in the following order:
3967// 1. [ssse3] 1 x pshufb
3968// 2. [ssse3] 2 x pshufb + 1 x por
3969// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3970static
Nate Begeman9008ca62009-04-27 18:41:29 +00003971SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3972 SelectionDAG &DAG, X86TargetLowering &TLI) {
3973 SDValue V1 = SVOp->getOperand(0);
3974 SDValue V2 = SVOp->getOperand(1);
3975 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003976 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00003978
Nate Begemanb9a47b82009-02-23 08:49:38 +00003979 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00003980 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00003981 // present, fall back to case 3.
3982 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3983 bool V1Only = true;
3984 bool V2Only = true;
3985 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003987 if (EltIdx < 0)
3988 continue;
3989 if (EltIdx < 16)
3990 V2Only = false;
3991 else
3992 V1Only = false;
3993 }
Eric Christopherfd179292009-08-27 18:07:15 +00003994
Nate Begemanb9a47b82009-02-23 08:49:38 +00003995 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3996 if (TLI.getSubtarget()->hasSSSE3()) {
3997 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003998
Nate Begemanb9a47b82009-02-23 08:49:38 +00003999 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004000 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004001 //
4002 // Otherwise, we have elements from both input vectors, and must zero out
4003 // elements that come from V2 in the first mask, and V1 in the second mask
4004 // so that we can OR them together.
4005 bool TwoInputs = !(V1Only || V2Only);
4006 for (unsigned i = 0; i != 16; ++i) {
4007 int EltIdx = MaskVals[i];
4008 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004010 continue;
4011 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004012 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004013 }
4014 // If all the elements are from V2, assign it to V1 and return after
4015 // building the first pshufb.
4016 if (V2Only)
4017 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004019 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004021 if (!TwoInputs)
4022 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004023
Nate Begemanb9a47b82009-02-23 08:49:38 +00004024 // Calculate the shuffle mask for the second input, shuffle it, and
4025 // OR it with the first shuffled input.
4026 pshufbMask.clear();
4027 for (unsigned i = 0; i != 16; ++i) {
4028 int EltIdx = MaskVals[i];
4029 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004031 continue;
4032 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004036 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 MVT::v16i8, &pshufbMask[0], 16));
4038 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004039 }
Eric Christopherfd179292009-08-27 18:07:15 +00004040
Nate Begemanb9a47b82009-02-23 08:49:38 +00004041 // No SSSE3 - Calculate in place words and then fix all out of place words
4042 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4043 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4045 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004046 SDValue NewV = V2Only ? V2 : V1;
4047 for (int i = 0; i != 8; ++i) {
4048 int Elt0 = MaskVals[i*2];
4049 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004050
Nate Begemanb9a47b82009-02-23 08:49:38 +00004051 // This word of the result is all undef, skip it.
4052 if (Elt0 < 0 && Elt1 < 0)
4053 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004054
Nate Begemanb9a47b82009-02-23 08:49:38 +00004055 // This word of the result is already in the correct place, skip it.
4056 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4057 continue;
4058 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4059 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004060
Nate Begemanb9a47b82009-02-23 08:49:38 +00004061 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4062 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4063 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004064
4065 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4066 // using a single extract together, load it and store it.
4067 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004069 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004071 DAG.getIntPtrConstant(i));
4072 continue;
4073 }
4074
Nate Begemanb9a47b82009-02-23 08:49:38 +00004075 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004076 // source byte is not also odd, shift the extracted word left 8 bits
4077 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 DAG.getIntPtrConstant(Elt1 / 2));
4081 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004082 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004083 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004084 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4086 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 }
4088 // If Elt0 is defined, extract it from the appropriate source. If the
4089 // source byte is not also even, shift the extracted word right 8 bits. If
4090 // Elt1 was also defined, OR the extracted values together before
4091 // inserting them in the result.
4092 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004094 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4095 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004097 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004098 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004099 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4100 DAG.getConstant(0x00FF, MVT::i16));
4101 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004102 : InsElt0;
4103 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004104 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004105 DAG.getIntPtrConstant(i));
4106 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004108}
4109
Evan Cheng7a831ce2007-12-15 03:00:47 +00004110/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4111/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4112/// done when every pair / quad of shuffle mask elements point to elements in
4113/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004114/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4115static
Nate Begeman9008ca62009-04-27 18:41:29 +00004116SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4117 SelectionDAG &DAG,
4118 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004119 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004120 SDValue V1 = SVOp->getOperand(0);
4121 SDValue V2 = SVOp->getOperand(1);
4122 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004123 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004125 EVT MaskEltVT = MaskVT.getVectorElementType();
4126 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004128 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 case MVT::v4f32: NewVT = MVT::v2f64; break;
4130 case MVT::v4i32: NewVT = MVT::v2i64; break;
4131 case MVT::v8i16: NewVT = MVT::v4i32; break;
4132 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004133 }
4134
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004135 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004136 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004138 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004140 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 int Scale = NumElems / NewWidth;
4142 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004143 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004144 int StartIdx = -1;
4145 for (int j = 0; j < Scale; ++j) {
4146 int EltIdx = SVOp->getMaskElt(i+j);
4147 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004148 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004149 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004150 StartIdx = EltIdx - (EltIdx % Scale);
4151 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004152 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004153 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004154 if (StartIdx == -1)
4155 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004156 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004157 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004158 }
4159
Dale Johannesenace16102009-02-03 19:33:06 +00004160 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4161 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004162 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004163}
4164
Evan Chengd880b972008-05-09 21:53:03 +00004165/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004166///
Owen Andersone50ed302009-08-10 22:56:29 +00004167static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004168 SDValue SrcOp, SelectionDAG &DAG,
4169 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004171 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004172 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004173 LD = dyn_cast<LoadSDNode>(SrcOp);
4174 if (!LD) {
4175 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4176 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004177 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4178 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004179 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4180 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004181 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004182 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004184 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4185 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4186 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4187 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004188 SrcOp.getOperand(0)
4189 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004190 }
4191 }
4192 }
4193
Dale Johannesenace16102009-02-03 19:33:06 +00004194 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4195 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004196 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004197 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004198}
4199
Evan Chengace3c172008-07-22 21:13:36 +00004200/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4201/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004202static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004203LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4204 SDValue V1 = SVOp->getOperand(0);
4205 SDValue V2 = SVOp->getOperand(1);
4206 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004207 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004208
Evan Chengace3c172008-07-22 21:13:36 +00004209 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004210 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 SmallVector<int, 8> Mask1(4U, -1);
4212 SmallVector<int, 8> PermMask;
4213 SVOp->getMask(PermMask);
4214
Evan Chengace3c172008-07-22 21:13:36 +00004215 unsigned NumHi = 0;
4216 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004217 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004218 int Idx = PermMask[i];
4219 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004220 Locs[i] = std::make_pair(-1, -1);
4221 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4223 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004224 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004225 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004226 NumLo++;
4227 } else {
4228 Locs[i] = std::make_pair(1, NumHi);
4229 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004230 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004231 NumHi++;
4232 }
4233 }
4234 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004235
Evan Chengace3c172008-07-22 21:13:36 +00004236 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004237 // If no more than two elements come from either vector. This can be
4238 // implemented with two shuffles. First shuffle gather the elements.
4239 // The second shuffle, which takes the first shuffle as both of its
4240 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004242
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004244
Evan Chengace3c172008-07-22 21:13:36 +00004245 for (unsigned i = 0; i != 4; ++i) {
4246 if (Locs[i].first == -1)
4247 continue;
4248 else {
4249 unsigned Idx = (i < 2) ? 0 : 4;
4250 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004252 }
4253 }
4254
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004256 } else if (NumLo == 3 || NumHi == 3) {
4257 // Otherwise, we must have three elements from one vector, call it X, and
4258 // one element from the other, call it Y. First, use a shufps to build an
4259 // intermediate vector with the one element from Y and the element from X
4260 // that will be in the same half in the final destination (the indexes don't
4261 // matter). Then, use a shufps to build the final vector, taking the half
4262 // containing the element from Y from the intermediate, and the other half
4263 // from X.
4264 if (NumHi == 3) {
4265 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004266 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004267 std::swap(V1, V2);
4268 }
4269
4270 // Find the element from V2.
4271 unsigned HiIndex;
4272 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 int Val = PermMask[HiIndex];
4274 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004275 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004276 if (Val >= 4)
4277 break;
4278 }
4279
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 Mask1[0] = PermMask[HiIndex];
4281 Mask1[1] = -1;
4282 Mask1[2] = PermMask[HiIndex^1];
4283 Mask1[3] = -1;
4284 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004285
4286 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004287 Mask1[0] = PermMask[0];
4288 Mask1[1] = PermMask[1];
4289 Mask1[2] = HiIndex & 1 ? 6 : 4;
4290 Mask1[3] = HiIndex & 1 ? 4 : 6;
4291 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004292 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 Mask1[0] = HiIndex & 1 ? 2 : 0;
4294 Mask1[1] = HiIndex & 1 ? 0 : 2;
4295 Mask1[2] = PermMask[2];
4296 Mask1[3] = PermMask[3];
4297 if (Mask1[2] >= 0)
4298 Mask1[2] += 4;
4299 if (Mask1[3] >= 0)
4300 Mask1[3] += 4;
4301 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004302 }
Evan Chengace3c172008-07-22 21:13:36 +00004303 }
4304
4305 // Break it into (shuffle shuffle_hi, shuffle_lo).
4306 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004307 SmallVector<int,8> LoMask(4U, -1);
4308 SmallVector<int,8> HiMask(4U, -1);
4309
4310 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004311 unsigned MaskIdx = 0;
4312 unsigned LoIdx = 0;
4313 unsigned HiIdx = 2;
4314 for (unsigned i = 0; i != 4; ++i) {
4315 if (i == 2) {
4316 MaskPtr = &HiMask;
4317 MaskIdx = 1;
4318 LoIdx = 0;
4319 HiIdx = 2;
4320 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004321 int Idx = PermMask[i];
4322 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004323 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004325 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004327 LoIdx++;
4328 } else {
4329 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004331 HiIdx++;
4332 }
4333 }
4334
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4336 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4337 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004338 for (unsigned i = 0; i != 4; ++i) {
4339 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004341 } else {
4342 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004344 }
4345 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004347}
4348
Dan Gohman475871a2008-07-27 21:46:04 +00004349SDValue
4350X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004352 SDValue V1 = Op.getOperand(0);
4353 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004354 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004355 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004357 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004358 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4359 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004360 bool V1IsSplat = false;
4361 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004362
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004364 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004365
Nate Begeman9008ca62009-04-27 18:41:29 +00004366 // Promote splats to v4f32.
4367 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004368 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004369 return Op;
4370 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004371 }
4372
Evan Cheng7a831ce2007-12-15 03:00:47 +00004373 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4374 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004377 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004378 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004379 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004380 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004381 // FIXME: Figure out a cleaner way to do this.
4382 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004383 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004384 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004385 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4387 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4388 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004389 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004390 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4392 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004393 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004394 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004395 }
4396 }
Eric Christopherfd179292009-08-27 18:07:15 +00004397
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 if (X86::isPSHUFDMask(SVOp))
4399 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004400
Evan Chengf26ffe92008-05-29 08:22:04 +00004401 // Check if this can be converted into a logical shift.
4402 bool isLeft = false;
4403 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004404 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004405 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004406 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004407 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004408 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004409 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004410 EVT EltVT = VT.getVectorElementType();
4411 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004412 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004413 }
Eric Christopherfd179292009-08-27 18:07:15 +00004414
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004416 if (V1IsUndef)
4417 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004418 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004419 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004420 if (!isMMX)
4421 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004422 }
Eric Christopherfd179292009-08-27 18:07:15 +00004423
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 // FIXME: fold these into legal mask.
4425 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4426 X86::isMOVSLDUPMask(SVOp) ||
4427 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004428 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004430 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 if (ShouldXformToMOVHLPS(SVOp) ||
4433 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4434 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004435
Evan Chengf26ffe92008-05-29 08:22:04 +00004436 if (isShift) {
4437 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004438 EVT EltVT = VT.getVectorElementType();
4439 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004440 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004441 }
Eric Christopherfd179292009-08-27 18:07:15 +00004442
Evan Cheng9eca5e82006-10-25 21:49:50 +00004443 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004444 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4445 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004446 V1IsSplat = isSplatVector(V1.getNode());
4447 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004448
Chris Lattner8a594482007-11-25 00:24:49 +00004449 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004450 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 Op = CommuteVectorShuffle(SVOp, DAG);
4452 SVOp = cast<ShuffleVectorSDNode>(Op);
4453 V1 = SVOp->getOperand(0);
4454 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004455 std::swap(V1IsSplat, V2IsSplat);
4456 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004457 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004458 }
4459
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4461 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004462 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 return V1;
4464 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4465 // the instruction selector will not match, so get a canonical MOVL with
4466 // swapped operands to undo the commute.
4467 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004468 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004469
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4471 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4472 X86::isUNPCKLMask(SVOp) ||
4473 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004474 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004475
Evan Cheng9bbbb982006-10-25 20:48:19 +00004476 if (V2IsSplat) {
4477 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004478 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004479 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 SDValue NewMask = NormalizeMask(SVOp, DAG);
4481 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4482 if (NSVOp != SVOp) {
4483 if (X86::isUNPCKLMask(NSVOp, true)) {
4484 return NewMask;
4485 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4486 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004487 }
4488 }
4489 }
4490
Evan Cheng9eca5e82006-10-25 21:49:50 +00004491 if (Commuted) {
4492 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 // FIXME: this seems wrong.
4494 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4495 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4496 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4497 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4498 X86::isUNPCKLMask(NewSVOp) ||
4499 X86::isUNPCKHMask(NewSVOp))
4500 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004501 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004502
Nate Begemanb9a47b82009-02-23 08:49:38 +00004503 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004504
4505 // Normalize the node to match x86 shuffle ops if needed
4506 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4507 return CommuteVectorShuffle(SVOp, DAG);
4508
4509 // Check for legal shuffle and return?
4510 SmallVector<int, 16> PermMask;
4511 SVOp->getMask(PermMask);
4512 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004513 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004514
Evan Cheng14b32e12007-12-11 01:46:18 +00004515 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004516 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004517 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004518 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004519 return NewOp;
4520 }
4521
Owen Anderson825b72b2009-08-11 20:47:22 +00004522 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004524 if (NewOp.getNode())
4525 return NewOp;
4526 }
Eric Christopherfd179292009-08-27 18:07:15 +00004527
Evan Chengace3c172008-07-22 21:13:36 +00004528 // Handle all 4 wide cases with a number of shuffles except for MMX.
4529 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004531
Dan Gohman475871a2008-07-27 21:46:04 +00004532 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533}
4534
Dan Gohman475871a2008-07-27 21:46:04 +00004535SDValue
4536X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004537 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004538 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004539 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004540 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004541 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004542 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004543 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004544 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004545 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004546 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004547 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4548 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4549 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004550 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4551 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004552 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004553 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004554 Op.getOperand(0)),
4555 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004556 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004557 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004558 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004559 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004560 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004561 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004562 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4563 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004564 // result has a single use which is a store or a bitcast to i32. And in
4565 // the case of a store, it's not worth it if the index is a constant 0,
4566 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004567 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004568 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004569 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004570 if ((User->getOpcode() != ISD::STORE ||
4571 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4572 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004573 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004574 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004575 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004576 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4577 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004578 Op.getOperand(0)),
4579 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4581 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004582 // ExtractPS works with constant index.
4583 if (isa<ConstantSDNode>(Op.getOperand(1)))
4584 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004585 }
Dan Gohman475871a2008-07-27 21:46:04 +00004586 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004587}
4588
4589
Dan Gohman475871a2008-07-27 21:46:04 +00004590SDValue
4591X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004593 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594
Evan Cheng62a3f152008-03-24 21:52:23 +00004595 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004597 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004598 return Res;
4599 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004600
Owen Andersone50ed302009-08-10 22:56:29 +00004601 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004602 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004603 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004604 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004605 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004606 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004607 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4609 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004610 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004612 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004614 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004615 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004617 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004618 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004619 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004620 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004621 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004622 if (Idx == 0)
4623 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004624
Evan Cheng0db9fe62006-04-25 20:13:52 +00004625 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004627 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004628 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004629 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004630 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004631 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004632 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004633 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4634 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4635 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004636 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004637 if (Idx == 0)
4638 return Op;
4639
4640 // UNPCKHPD the element to the lowest double word, then movsd.
4641 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4642 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004643 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004644 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004645 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004647 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004648 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 }
4650
Dan Gohman475871a2008-07-27 21:46:04 +00004651 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652}
4653
Dan Gohman475871a2008-07-27 21:46:04 +00004654SDValue
4655X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004656 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004657 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004658 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004659
Dan Gohman475871a2008-07-27 21:46:04 +00004660 SDValue N0 = Op.getOperand(0);
4661 SDValue N1 = Op.getOperand(1);
4662 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004663
Dan Gohman8a55ce42009-09-23 21:02:20 +00004664 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004665 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004666 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4667 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004668 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4669 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004670 if (N1.getValueType() != MVT::i32)
4671 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4672 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004673 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004674 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004675 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004676 // Bits [7:6] of the constant are the source select. This will always be
4677 // zero here. The DAG Combiner may combine an extract_elt index into these
4678 // bits. For example (insert (extract, 3), 2) could be matched by putting
4679 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004680 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004681 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004682 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004683 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004684 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004685 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004687 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004688 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004689 // PINSR* works with constant index.
4690 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004691 }
Dan Gohman475871a2008-07-27 21:46:04 +00004692 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004693}
4694
Dan Gohman475871a2008-07-27 21:46:04 +00004695SDValue
4696X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004697 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004698 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004699
4700 if (Subtarget->hasSSE41())
4701 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4702
Dan Gohman8a55ce42009-09-23 21:02:20 +00004703 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004704 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004705
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004706 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004707 SDValue N0 = Op.getOperand(0);
4708 SDValue N1 = Op.getOperand(1);
4709 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004710
Dan Gohman8a55ce42009-09-23 21:02:20 +00004711 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004712 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4713 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004714 if (N1.getValueType() != MVT::i32)
4715 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4716 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004717 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004718 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004719 }
Dan Gohman475871a2008-07-27 21:46:04 +00004720 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004721}
4722
Dan Gohman475871a2008-07-27 21:46:04 +00004723SDValue
4724X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004725 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004726 if (Op.getValueType() == MVT::v2f32)
4727 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4728 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4729 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004730 Op.getOperand(0))));
4731
Owen Anderson825b72b2009-08-11 20:47:22 +00004732 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4733 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004734
Owen Anderson825b72b2009-08-11 20:47:22 +00004735 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4736 EVT VT = MVT::v2i32;
4737 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004738 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 case MVT::v16i8:
4740 case MVT::v8i16:
4741 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004742 break;
4743 }
Dale Johannesenace16102009-02-03 19:33:06 +00004744 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4745 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746}
4747
Bill Wendling056292f2008-09-16 21:48:12 +00004748// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4749// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4750// one of the above mentioned nodes. It has to be wrapped because otherwise
4751// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4752// be used to form addressing mode. These wrapped nodes will be selected
4753// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004754SDValue
4755X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004756 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004757
Chris Lattner41621a22009-06-26 19:22:52 +00004758 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4759 // global base reg.
4760 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004761 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004762 CodeModel::Model M = getTargetMachine().getCodeModel();
4763
Chris Lattner4f066492009-07-11 20:29:19 +00004764 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004765 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004766 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004767 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004768 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004769 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004770 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004771
Evan Cheng1606e8e2009-03-13 07:51:59 +00004772 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004773 CP->getAlignment(),
4774 CP->getOffset(), OpFlag);
4775 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004776 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004777 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004778 if (OpFlag) {
4779 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004780 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004781 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004782 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004783 }
4784
4785 return Result;
4786}
4787
Chris Lattner18c59872009-06-27 04:16:01 +00004788SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4789 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004790
Chris Lattner18c59872009-06-27 04:16:01 +00004791 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4792 // global base reg.
4793 unsigned char OpFlag = 0;
4794 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004795 CodeModel::Model M = getTargetMachine().getCodeModel();
4796
Chris Lattner4f066492009-07-11 20:29:19 +00004797 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004798 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004799 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004800 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004801 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004802 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004803 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004804
Chris Lattner18c59872009-06-27 04:16:01 +00004805 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4806 OpFlag);
4807 DebugLoc DL = JT->getDebugLoc();
4808 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004809
Chris Lattner18c59872009-06-27 04:16:01 +00004810 // With PIC, the address is actually $g + Offset.
4811 if (OpFlag) {
4812 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4813 DAG.getNode(X86ISD::GlobalBaseReg,
4814 DebugLoc::getUnknownLoc(), getPointerTy()),
4815 Result);
4816 }
Eric Christopherfd179292009-08-27 18:07:15 +00004817
Chris Lattner18c59872009-06-27 04:16:01 +00004818 return Result;
4819}
4820
4821SDValue
4822X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4823 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004824
Chris Lattner18c59872009-06-27 04:16:01 +00004825 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4826 // global base reg.
4827 unsigned char OpFlag = 0;
4828 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004829 CodeModel::Model M = getTargetMachine().getCodeModel();
4830
Chris Lattner4f066492009-07-11 20:29:19 +00004831 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004832 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004833 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004834 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004835 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004836 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004837 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004838
Chris Lattner18c59872009-06-27 04:16:01 +00004839 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004840
Chris Lattner18c59872009-06-27 04:16:01 +00004841 DebugLoc DL = Op.getDebugLoc();
4842 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004843
4844
Chris Lattner18c59872009-06-27 04:16:01 +00004845 // With PIC, the address is actually $g + Offset.
4846 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004847 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004848 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4849 DAG.getNode(X86ISD::GlobalBaseReg,
4850 DebugLoc::getUnknownLoc(),
4851 getPointerTy()),
4852 Result);
4853 }
Eric Christopherfd179292009-08-27 18:07:15 +00004854
Chris Lattner18c59872009-06-27 04:16:01 +00004855 return Result;
4856}
4857
Dan Gohman475871a2008-07-27 21:46:04 +00004858SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004859X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004860 // Create the TargetBlockAddressAddress node.
4861 unsigned char OpFlags =
4862 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004863 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004864 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4865 DebugLoc dl = Op.getDebugLoc();
4866 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4867 /*isTarget=*/true, OpFlags);
4868
Dan Gohmanf705adb2009-10-30 01:28:02 +00004869 if (Subtarget->isPICStyleRIPRel() &&
4870 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004871 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4872 else
4873 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004874
Dan Gohman29cbade2009-11-20 23:18:13 +00004875 // With PIC, the address is actually $g + Offset.
4876 if (isGlobalRelativeToPICBase(OpFlags)) {
4877 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4878 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4879 Result);
4880 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004881
4882 return Result;
4883}
4884
4885SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004886X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004887 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004888 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004889 // Create the TargetGlobalAddress node, folding in the constant
4890 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004891 unsigned char OpFlags =
4892 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004893 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004894 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004895 if (OpFlags == X86II::MO_NO_FLAG &&
4896 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004897 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004898 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004899 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004900 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004901 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004902 }
Eric Christopherfd179292009-08-27 18:07:15 +00004903
Chris Lattner4f066492009-07-11 20:29:19 +00004904 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004905 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004906 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4907 else
4908 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004909
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004910 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004911 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004912 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4913 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004914 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004915 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004916
Chris Lattner36c25012009-07-10 07:34:39 +00004917 // For globals that require a load from a stub to get the address, emit the
4918 // load.
4919 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004920 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004921 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004922
Dan Gohman6520e202008-10-18 02:06:02 +00004923 // If there was a non-zero offset that we didn't fold, create an explicit
4924 // addition for it.
4925 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004926 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004927 DAG.getConstant(Offset, getPointerTy()));
4928
Evan Cheng0db9fe62006-04-25 20:13:52 +00004929 return Result;
4930}
4931
Evan Chengda43bcf2008-09-24 00:05:32 +00004932SDValue
4933X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4934 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004935 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004936 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004937}
4938
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004939static SDValue
4940GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004941 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004942 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004943 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004944 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004945 DebugLoc dl = GA->getDebugLoc();
4946 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4947 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004948 GA->getOffset(),
4949 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004950 if (InFlag) {
4951 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004952 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004953 } else {
4954 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004955 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004956 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004957
4958 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
4959 MFI->setHasCalls(true);
4960
Rafael Espindola15f1b662009-04-24 12:59:40 +00004961 SDValue Flag = Chain.getValue(1);
4962 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004963}
4964
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004965// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004966static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004967LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004968 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004969 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004970 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4971 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004972 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004973 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004974 PtrVT), InFlag);
4975 InFlag = Chain.getValue(1);
4976
Chris Lattnerb903bed2009-06-26 21:20:29 +00004977 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004978}
4979
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004980// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004981static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004982LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004983 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004984 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4985 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004986}
4987
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004988// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4989// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004990static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004991 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004992 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004993 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004994 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004995 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4996 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004997 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004999
5000 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5001 NULL, 0);
5002
Chris Lattnerb903bed2009-06-26 21:20:29 +00005003 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005004 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5005 // initialexec.
5006 unsigned WrapperKind = X86ISD::Wrapper;
5007 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005008 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005009 } else if (is64Bit) {
5010 assert(model == TLSModel::InitialExec);
5011 OperandFlags = X86II::MO_GOTTPOFF;
5012 WrapperKind = X86ISD::WrapperRIP;
5013 } else {
5014 assert(model == TLSModel::InitialExec);
5015 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005016 }
Eric Christopherfd179292009-08-27 18:07:15 +00005017
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005018 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5019 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005020 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005021 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005022 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005023
Rafael Espindola9a580232009-02-27 13:37:18 +00005024 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005025 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005026 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005027
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005028 // The address of the thread local variable is the add of the thread
5029 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005030 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005031}
5032
Dan Gohman475871a2008-07-27 21:46:04 +00005033SDValue
5034X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005035 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005036 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005037 assert(Subtarget->isTargetELF() &&
5038 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005039 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005040 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005041
Chris Lattnerb903bed2009-06-26 21:20:29 +00005042 // If GV is an alias then use the aliasee for determining
5043 // thread-localness.
5044 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5045 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005046
Chris Lattnerb903bed2009-06-26 21:20:29 +00005047 TLSModel::Model model = getTLSModel(GV,
5048 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005049
Chris Lattnerb903bed2009-06-26 21:20:29 +00005050 switch (model) {
5051 case TLSModel::GeneralDynamic:
5052 case TLSModel::LocalDynamic: // not implemented
5053 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005054 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005055 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005056
Chris Lattnerb903bed2009-06-26 21:20:29 +00005057 case TLSModel::InitialExec:
5058 case TLSModel::LocalExec:
5059 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5060 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005061 }
Eric Christopherfd179292009-08-27 18:07:15 +00005062
Torok Edwinc23197a2009-07-14 16:55:14 +00005063 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005064 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005065}
5066
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005068/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005069/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005070SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005071 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005072 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005073 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005074 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005075 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SDValue ShOpLo = Op.getOperand(0);
5077 SDValue ShOpHi = Op.getOperand(1);
5078 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005079 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005080 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005081 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005082
Dan Gohman475871a2008-07-27 21:46:04 +00005083 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005084 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005085 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5086 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005087 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005088 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5089 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005090 }
Evan Chenge3413162006-01-09 18:33:28 +00005091
Owen Anderson825b72b2009-08-11 20:47:22 +00005092 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5093 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005094 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005095 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005096
Dan Gohman475871a2008-07-27 21:46:04 +00005097 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005098 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005099 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5100 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005101
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005102 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005103 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5104 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005105 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005106 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5107 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005108 }
5109
Dan Gohman475871a2008-07-27 21:46:04 +00005110 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005111 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005112}
Evan Chenga3195e82006-01-12 22:54:21 +00005113
Dan Gohman475871a2008-07-27 21:46:04 +00005114SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005115 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005116
5117 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005118 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005119 return Op;
5120 }
5121 return SDValue();
5122 }
5123
Owen Anderson825b72b2009-08-11 20:47:22 +00005124 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005125 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005126
Eli Friedman36df4992009-05-27 00:47:34 +00005127 // These are really Legal; return the operand so the caller accepts it as
5128 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005129 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005130 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005131 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005132 Subtarget->is64Bit()) {
5133 return Op;
5134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005135
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005136 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005137 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005138 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005139 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005140 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005141 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005142 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005143 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005144 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5145}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005146
Owen Andersone50ed302009-08-10 22:56:29 +00005147SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005148 SDValue StackSlot,
5149 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005150 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005151 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005152 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005153 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005154 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005155 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005156 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005157 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005158 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005159 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005160 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005162 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005163 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005164 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005165
5166 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5167 // shouldn't be necessary except that RFP cannot be live across
5168 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005169 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005170 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005171 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005173 SDValue Ops[] = {
5174 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5175 };
5176 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005177 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005178 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005179 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005180
Evan Cheng0db9fe62006-04-25 20:13:52 +00005181 return Result;
5182}
5183
Bill Wendling8b8a6362009-01-17 03:56:04 +00005184// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5185SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5186 // This algorithm is not obvious. Here it is in C code, more or less:
5187 /*
5188 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5189 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5190 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005191
Bill Wendling8b8a6362009-01-17 03:56:04 +00005192 // Copy ints to xmm registers.
5193 __m128i xh = _mm_cvtsi32_si128( hi );
5194 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005195
Bill Wendling8b8a6362009-01-17 03:56:04 +00005196 // Combine into low half of a single xmm register.
5197 __m128i x = _mm_unpacklo_epi32( xh, xl );
5198 __m128d d;
5199 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005200
Bill Wendling8b8a6362009-01-17 03:56:04 +00005201 // Merge in appropriate exponents to give the integer bits the right
5202 // magnitude.
5203 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005204
Bill Wendling8b8a6362009-01-17 03:56:04 +00005205 // Subtract away the biases to deal with the IEEE-754 double precision
5206 // implicit 1.
5207 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005208
Bill Wendling8b8a6362009-01-17 03:56:04 +00005209 // All conversions up to here are exact. The correctly rounded result is
5210 // calculated using the current rounding mode using the following
5211 // horizontal add.
5212 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5213 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5214 // store doesn't really need to be here (except
5215 // maybe to zero the other double)
5216 return sd;
5217 }
5218 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005219
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005220 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005221 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005222
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005223 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005224 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005225 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5226 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5227 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5228 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005229 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005230 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005231
Bill Wendling8b8a6362009-01-17 03:56:04 +00005232 std::vector<Constant*> CV1;
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, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005235 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005236 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005237 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005238 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005239
Owen Anderson825b72b2009-08-11 20:47:22 +00005240 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5241 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005242 Op.getOperand(0),
5243 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005244 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5245 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005246 Op.getOperand(0),
5247 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005248 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5249 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005250 PseudoSourceValue::getConstantPool(), 0,
5251 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005252 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5253 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5254 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005255 PseudoSourceValue::getConstantPool(), 0,
5256 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005257 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005258
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005259 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005260 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5262 DAG.getUNDEF(MVT::v2f64), ShufMask);
5263 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5264 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005265 DAG.getIntPtrConstant(0));
5266}
5267
Bill Wendling8b8a6362009-01-17 03:56:04 +00005268// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5269SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005270 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005271 // FP constant to bias correct the final result.
5272 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005273 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005274
5275 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005276 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5277 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005278 Op.getOperand(0),
5279 DAG.getIntPtrConstant(0)));
5280
Owen Anderson825b72b2009-08-11 20:47:22 +00005281 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5282 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005283 DAG.getIntPtrConstant(0));
5284
5285 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005286 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5287 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005288 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005289 MVT::v2f64, Load)),
5290 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005291 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005292 MVT::v2f64, Bias)));
5293 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5294 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005295 DAG.getIntPtrConstant(0));
5296
5297 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005299
5300 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005301 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005302
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005304 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005305 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005306 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005307 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005308 }
5309
5310 // Handle final rounding.
5311 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005312}
5313
5314SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005315 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005316 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005317
Evan Chenga06ec9e2009-01-19 08:08:22 +00005318 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5319 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5320 // the optimization here.
5321 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005322 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005323
Owen Andersone50ed302009-08-10 22:56:29 +00005324 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005326 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005327 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005328 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005329
Bill Wendling8b8a6362009-01-17 03:56:04 +00005330 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005332 return LowerUINT_TO_FP_i32(Op, DAG);
5333 }
5334
Owen Anderson825b72b2009-08-11 20:47:22 +00005335 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005336
5337 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005339 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5340 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5341 getPointerTy(), StackSlot, WordOff);
5342 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5343 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005345 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005346 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005347}
5348
Dan Gohman475871a2008-07-27 21:46:04 +00005349std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005350FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005351 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005352
Owen Andersone50ed302009-08-10 22:56:29 +00005353 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005354
5355 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005356 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5357 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005358 }
5359
Owen Anderson825b72b2009-08-11 20:47:22 +00005360 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5361 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005362 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005363
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005364 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005366 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005367 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005368 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005370 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005371 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005372
Evan Cheng87c89352007-10-15 20:11:21 +00005373 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5374 // stack slot.
5375 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005376 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005377 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005378 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005379
Evan Cheng0db9fe62006-04-25 20:13:52 +00005380 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005382 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005383 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5384 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5385 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005387
Dan Gohman475871a2008-07-27 21:46:04 +00005388 SDValue Chain = DAG.getEntryNode();
5389 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005390 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005391 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005392 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005393 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005395 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005396 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5397 };
Dale Johannesenace16102009-02-03 19:33:06 +00005398 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005399 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005400 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5402 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005403
Evan Cheng0db9fe62006-04-25 20:13:52 +00005404 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005405 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005407
Chris Lattner27a6c732007-11-24 07:07:01 +00005408 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409}
5410
Dan Gohman475871a2008-07-27 21:46:04 +00005411SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005412 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 if (Op.getValueType() == MVT::v2i32 &&
5414 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005415 return Op;
5416 }
5417 return SDValue();
5418 }
5419
Eli Friedman948e95a2009-05-23 09:59:16 +00005420 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005421 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005422 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5423 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005424
Chris Lattner27a6c732007-11-24 07:07:01 +00005425 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005426 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005427 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005428}
5429
Eli Friedman948e95a2009-05-23 09:59:16 +00005430SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5431 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5432 SDValue FIST = Vals.first, StackSlot = Vals.second;
5433 assert(FIST.getNode() && "Unexpected failure");
5434
5435 // Load the result.
5436 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5437 FIST, StackSlot, NULL, 0);
5438}
5439
Dan Gohman475871a2008-07-27 21:46:04 +00005440SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005441 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005442 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005443 EVT VT = Op.getValueType();
5444 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005445 if (VT.isVector())
5446 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005448 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005449 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005450 CV.push_back(C);
5451 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005453 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005454 CV.push_back(C);
5455 CV.push_back(C);
5456 CV.push_back(C);
5457 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005458 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005459 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005460 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005461 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005462 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005463 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005464 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005465}
5466
Dan Gohman475871a2008-07-27 21:46:04 +00005467SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005468 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005469 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005470 EVT VT = Op.getValueType();
5471 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005472 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005473 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005474 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005475 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005476 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005477 CV.push_back(C);
5478 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005480 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005481 CV.push_back(C);
5482 CV.push_back(C);
5483 CV.push_back(C);
5484 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005486 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005487 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005488 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005489 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005490 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005491 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005492 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005493 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5494 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005495 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005496 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005497 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005498 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005499 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500}
5501
Dan Gohman475871a2008-07-27 21:46:04 +00005502SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005503 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005504 SDValue Op0 = Op.getOperand(0);
5505 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005506 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005507 EVT VT = Op.getValueType();
5508 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005509
5510 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005511 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005512 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005513 SrcVT = VT;
5514 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005515 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005516 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005517 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005518 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005519 }
5520
5521 // At this point the operands and the result should have the same
5522 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005523
Evan Cheng68c47cb2007-01-05 07:55:56 +00005524 // First get the sign bit of second operand.
5525 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005526 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005527 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5528 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005529 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005530 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5531 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5532 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5533 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005534 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005535 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005536 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005537 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005538 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005539 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005540 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005541
5542 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005543 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005544 // Op0 is MVT::f32, Op1 is MVT::f64.
5545 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5546 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5547 DAG.getConstant(32, MVT::i32));
5548 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5549 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005550 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005551 }
5552
Evan Cheng73d6cf12007-01-05 21:37:56 +00005553 // Clear first operand sign bit.
5554 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005555 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005556 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5557 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005558 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005559 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5560 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5561 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5562 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005563 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005564 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005565 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005566 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005567 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005568 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005569 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005570
5571 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005572 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005573}
5574
Dan Gohman076aee32009-03-04 19:44:21 +00005575/// Emit nodes that will be selected as "test Op0,Op0", or something
5576/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005577SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5578 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005579 DebugLoc dl = Op.getDebugLoc();
5580
Dan Gohman31125812009-03-07 01:58:32 +00005581 // CF and OF aren't always set the way we want. Determine which
5582 // of these we need.
5583 bool NeedCF = false;
5584 bool NeedOF = false;
5585 switch (X86CC) {
5586 case X86::COND_A: case X86::COND_AE:
5587 case X86::COND_B: case X86::COND_BE:
5588 NeedCF = true;
5589 break;
5590 case X86::COND_G: case X86::COND_GE:
5591 case X86::COND_L: case X86::COND_LE:
5592 case X86::COND_O: case X86::COND_NO:
5593 NeedOF = true;
5594 break;
5595 default: break;
5596 }
5597
Dan Gohman076aee32009-03-04 19:44:21 +00005598 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005599 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5600 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5601 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005602 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005603 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005604 switch (Op.getNode()->getOpcode()) {
5605 case ISD::ADD:
5606 // Due to an isel shortcoming, be conservative if this add is likely to
5607 // be selected as part of a load-modify-store instruction. When the root
5608 // node in a match is a store, isel doesn't know how to remap non-chain
5609 // non-flag uses of other nodes in the match, such as the ADD in this
5610 // case. This leads to the ADD being left around and reselected, with
5611 // the result being two adds in the output.
5612 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5613 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5614 if (UI->getOpcode() == ISD::STORE)
5615 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005616 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005617 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5618 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005619 if (C->getAPIntValue() == 1) {
5620 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005621 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005622 break;
5623 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005624 // An add of negative one (subtract of one) will be selected as a DEC.
5625 if (C->getAPIntValue().isAllOnesValue()) {
5626 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005627 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005628 break;
5629 }
5630 }
Dan Gohman076aee32009-03-04 19:44:21 +00005631 // Otherwise use a regular EFLAGS-setting add.
5632 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005633 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005634 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005635 case ISD::AND: {
5636 // If the primary and result isn't used, don't bother using X86ISD::AND,
5637 // because a TEST instruction will be better.
5638 bool NonFlagUse = false;
5639 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005640 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5641 SDNode *User = *UI;
5642 unsigned UOpNo = UI.getOperandNo();
5643 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5644 // Look pass truncate.
5645 UOpNo = User->use_begin().getOperandNo();
5646 User = *User->use_begin();
5647 }
5648 if (User->getOpcode() != ISD::BRCOND &&
5649 User->getOpcode() != ISD::SETCC &&
5650 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005651 NonFlagUse = true;
5652 break;
5653 }
Evan Cheng17751da2010-01-07 00:54:06 +00005654 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005655 if (!NonFlagUse)
5656 break;
5657 }
5658 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005659 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005660 case ISD::OR:
5661 case ISD::XOR:
5662 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005663 // likely to be selected as part of a load-modify-store instruction.
5664 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5665 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5666 if (UI->getOpcode() == ISD::STORE)
5667 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005668 // Otherwise use a regular EFLAGS-setting instruction.
5669 switch (Op.getNode()->getOpcode()) {
5670 case ISD::SUB: Opcode = X86ISD::SUB; break;
5671 case ISD::OR: Opcode = X86ISD::OR; break;
5672 case ISD::XOR: Opcode = X86ISD::XOR; break;
5673 case ISD::AND: Opcode = X86ISD::AND; break;
5674 default: llvm_unreachable("unexpected operator!");
5675 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005676 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005677 break;
5678 case X86ISD::ADD:
5679 case X86ISD::SUB:
5680 case X86ISD::INC:
5681 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005682 case X86ISD::OR:
5683 case X86ISD::XOR:
5684 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005685 return SDValue(Op.getNode(), 1);
5686 default:
5687 default_case:
5688 break;
5689 }
5690 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005692 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005693 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005694 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005695 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005696 DAG.ReplaceAllUsesWith(Op, New);
5697 return SDValue(New.getNode(), 1);
5698 }
5699 }
5700
5701 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005702 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005703 DAG.getConstant(0, Op.getValueType()));
5704}
5705
5706/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5707/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005708SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5709 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005710 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5711 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005712 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005713
5714 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005716}
5717
Evan Chengd40d03e2010-01-06 19:38:29 +00005718/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5719/// if it's possible.
5720static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005721 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005722 SDValue LHS, RHS;
5723 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5724 if (ConstantSDNode *Op010C =
5725 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5726 if (Op010C->getZExtValue() == 1) {
5727 LHS = Op0.getOperand(0);
5728 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005729 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005730 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5731 if (ConstantSDNode *Op000C =
5732 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5733 if (Op000C->getZExtValue() == 1) {
5734 LHS = Op0.getOperand(1);
5735 RHS = Op0.getOperand(0).getOperand(1);
5736 }
5737 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5738 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5739 SDValue AndLHS = Op0.getOperand(0);
5740 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5741 LHS = AndLHS.getOperand(0);
5742 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005743 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005744 }
Evan Cheng0488db92007-09-25 01:57:46 +00005745
Evan Chengd40d03e2010-01-06 19:38:29 +00005746 if (LHS.getNode()) {
5747 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5748 // instruction. Since the shift amount is in-range-or-undefined, we know
5749 // that doing a bittest on the i16 value is ok. We extend to i32 because
5750 // the encoding for the i16 version is larger than the i32 version.
5751 if (LHS.getValueType() == MVT::i8)
5752 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005753
Evan Chengd40d03e2010-01-06 19:38:29 +00005754 // If the operand types disagree, extend the shift amount to match. Since
5755 // BT ignores high bits (like shifts) we can use anyextend.
5756 if (LHS.getValueType() != RHS.getValueType())
5757 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005758
Evan Chengd40d03e2010-01-06 19:38:29 +00005759 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5760 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5761 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5762 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005763 }
5764
Evan Cheng54de3ea2010-01-05 06:52:31 +00005765 return SDValue();
5766}
5767
5768SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5769 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5770 SDValue Op0 = Op.getOperand(0);
5771 SDValue Op1 = Op.getOperand(1);
5772 DebugLoc dl = Op.getDebugLoc();
5773 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5774
5775 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005776 // Lower (X & (1 << N)) == 0 to BT(X, N).
5777 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5778 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5779 if (Op0.getOpcode() == ISD::AND &&
5780 Op0.hasOneUse() &&
5781 Op1.getOpcode() == ISD::Constant &&
5782 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5783 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5784 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5785 if (NewSetCC.getNode())
5786 return NewSetCC;
5787 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005788
Chris Lattnere55484e2008-12-25 05:34:37 +00005789 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5790 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005791 if (X86CC == X86::COND_INVALID)
5792 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005793
Dan Gohman31125812009-03-07 01:58:32 +00005794 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005795
5796 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005797 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005798 return DAG.getNode(ISD::AND, dl, MVT::i8,
5799 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5800 DAG.getConstant(X86CC, MVT::i8), Cond),
5801 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005802
Owen Anderson825b72b2009-08-11 20:47:22 +00005803 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5804 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005805}
5806
Dan Gohman475871a2008-07-27 21:46:04 +00005807SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5808 SDValue Cond;
5809 SDValue Op0 = Op.getOperand(0);
5810 SDValue Op1 = Op.getOperand(1);
5811 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005812 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005813 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5814 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005815 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005816
5817 if (isFP) {
5818 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005819 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005820 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5821 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005822 bool Swap = false;
5823
5824 switch (SetCCOpcode) {
5825 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005826 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005827 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005828 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005829 case ISD::SETGT: Swap = true; // Fallthrough
5830 case ISD::SETLT:
5831 case ISD::SETOLT: SSECC = 1; break;
5832 case ISD::SETOGE:
5833 case ISD::SETGE: Swap = true; // Fallthrough
5834 case ISD::SETLE:
5835 case ISD::SETOLE: SSECC = 2; break;
5836 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005837 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005838 case ISD::SETNE: SSECC = 4; break;
5839 case ISD::SETULE: Swap = true;
5840 case ISD::SETUGE: SSECC = 5; break;
5841 case ISD::SETULT: Swap = true;
5842 case ISD::SETUGT: SSECC = 6; break;
5843 case ISD::SETO: SSECC = 7; break;
5844 }
5845 if (Swap)
5846 std::swap(Op0, Op1);
5847
Nate Begemanfb8ead02008-07-25 19:05:58 +00005848 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005849 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005850 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005851 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005852 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5853 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005854 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005855 }
5856 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005857 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5859 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005860 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005861 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005862 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005863 }
5864 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005865 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005866 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005867
Nate Begeman30a0de92008-07-17 16:51:19 +00005868 // We are handling one of the integer comparisons here. Since SSE only has
5869 // GT and EQ comparisons for integer, swapping operands and multiple
5870 // operations may be required for some comparisons.
5871 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5872 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005873
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005875 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005876 case MVT::v8i8:
5877 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5878 case MVT::v4i16:
5879 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5880 case MVT::v2i32:
5881 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5882 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005883 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005884
Nate Begeman30a0de92008-07-17 16:51:19 +00005885 switch (SetCCOpcode) {
5886 default: break;
5887 case ISD::SETNE: Invert = true;
5888 case ISD::SETEQ: Opc = EQOpc; break;
5889 case ISD::SETLT: Swap = true;
5890 case ISD::SETGT: Opc = GTOpc; break;
5891 case ISD::SETGE: Swap = true;
5892 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5893 case ISD::SETULT: Swap = true;
5894 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5895 case ISD::SETUGE: Swap = true;
5896 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5897 }
5898 if (Swap)
5899 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005900
Nate Begeman30a0de92008-07-17 16:51:19 +00005901 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5902 // bits of the inputs before performing those operations.
5903 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005904 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005905 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5906 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005907 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005908 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5909 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005910 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5911 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005913
Dale Johannesenace16102009-02-03 19:33:06 +00005914 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005915
5916 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005917 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005918 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005919
Nate Begeman30a0de92008-07-17 16:51:19 +00005920 return Result;
5921}
Evan Cheng0488db92007-09-25 01:57:46 +00005922
Evan Cheng370e5342008-12-03 08:38:43 +00005923// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005924static bool isX86LogicalCmp(SDValue Op) {
5925 unsigned Opc = Op.getNode()->getOpcode();
5926 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5927 return true;
5928 if (Op.getResNo() == 1 &&
5929 (Opc == X86ISD::ADD ||
5930 Opc == X86ISD::SUB ||
5931 Opc == X86ISD::SMUL ||
5932 Opc == X86ISD::UMUL ||
5933 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005934 Opc == X86ISD::DEC ||
5935 Opc == X86ISD::OR ||
5936 Opc == X86ISD::XOR ||
5937 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005938 return true;
5939
5940 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005941}
5942
Dan Gohman475871a2008-07-27 21:46:04 +00005943SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005944 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005945 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005946 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005947 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005948
Dan Gohman1a492952009-10-20 16:22:37 +00005949 if (Cond.getOpcode() == ISD::SETCC) {
5950 SDValue NewCond = LowerSETCC(Cond, DAG);
5951 if (NewCond.getNode())
5952 Cond = NewCond;
5953 }
Evan Cheng734503b2006-09-11 02:19:56 +00005954
Evan Chengad9c0a32009-12-15 00:53:42 +00005955 // Look pass (and (setcc_carry (cmp ...)), 1).
5956 if (Cond.getOpcode() == ISD::AND &&
5957 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
5958 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
5959 if (C && C->getAPIntValue() == 1)
5960 Cond = Cond.getOperand(0);
5961 }
5962
Evan Cheng3f41d662007-10-08 22:16:29 +00005963 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5964 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00005965 if (Cond.getOpcode() == X86ISD::SETCC ||
5966 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00005967 CC = Cond.getOperand(0);
5968
Dan Gohman475871a2008-07-27 21:46:04 +00005969 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005970 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00005971 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005972
Evan Cheng3f41d662007-10-08 22:16:29 +00005973 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005974 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005975 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005976 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005977
Chris Lattnerd1980a52009-03-12 06:52:53 +00005978 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5979 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005980 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005981 addTest = false;
5982 }
5983 }
5984
5985 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005986 // Look pass the truncate.
5987 if (Cond.getOpcode() == ISD::TRUNCATE)
5988 Cond = Cond.getOperand(0);
5989
5990 // We know the result of AND is compared against zero. Try to match
5991 // it to BT.
5992 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
5993 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
5994 if (NewSetCC.getNode()) {
5995 CC = NewSetCC.getOperand(0);
5996 Cond = NewSetCC.getOperand(1);
5997 addTest = false;
5998 }
5999 }
6000 }
6001
6002 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006003 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006004 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006005 }
6006
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Evan Cheng0488db92007-09-25 01:57:46 +00006008 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6009 // condition is true.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006010 SDValue Ops[] = { Op.getOperand(2), Op.getOperand(1), CC, Cond };
6011 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006012}
6013
Evan Cheng370e5342008-12-03 08:38:43 +00006014// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6015// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6016// from the AND / OR.
6017static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6018 Opc = Op.getOpcode();
6019 if (Opc != ISD::OR && Opc != ISD::AND)
6020 return false;
6021 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6022 Op.getOperand(0).hasOneUse() &&
6023 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6024 Op.getOperand(1).hasOneUse());
6025}
6026
Evan Cheng961d6d42009-02-02 08:19:07 +00006027// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6028// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006029static bool isXor1OfSetCC(SDValue Op) {
6030 if (Op.getOpcode() != ISD::XOR)
6031 return false;
6032 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6033 if (N1C && N1C->getAPIntValue() == 1) {
6034 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6035 Op.getOperand(0).hasOneUse();
6036 }
6037 return false;
6038}
6039
Dan Gohman475871a2008-07-27 21:46:04 +00006040SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006041 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SDValue Chain = Op.getOperand(0);
6043 SDValue Cond = Op.getOperand(1);
6044 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006045 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006046 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006047
Dan Gohman1a492952009-10-20 16:22:37 +00006048 if (Cond.getOpcode() == ISD::SETCC) {
6049 SDValue NewCond = LowerSETCC(Cond, DAG);
6050 if (NewCond.getNode())
6051 Cond = NewCond;
6052 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006053#if 0
6054 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006055 else if (Cond.getOpcode() == X86ISD::ADD ||
6056 Cond.getOpcode() == X86ISD::SUB ||
6057 Cond.getOpcode() == X86ISD::SMUL ||
6058 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006059 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006060#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006061
Evan Chengad9c0a32009-12-15 00:53:42 +00006062 // Look pass (and (setcc_carry (cmp ...)), 1).
6063 if (Cond.getOpcode() == ISD::AND &&
6064 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6065 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6066 if (C && C->getAPIntValue() == 1)
6067 Cond = Cond.getOperand(0);
6068 }
6069
Evan Cheng3f41d662007-10-08 22:16:29 +00006070 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6071 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006072 if (Cond.getOpcode() == X86ISD::SETCC ||
6073 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006074 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006075
Dan Gohman475871a2008-07-27 21:46:04 +00006076 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006077 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006078 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006079 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006080 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006081 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006082 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006083 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006084 default: break;
6085 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006086 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006087 // These can only come from an arithmetic instruction with overflow,
6088 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006089 Cond = Cond.getNode()->getOperand(1);
6090 addTest = false;
6091 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006092 }
Evan Cheng0488db92007-09-25 01:57:46 +00006093 }
Evan Cheng370e5342008-12-03 08:38:43 +00006094 } else {
6095 unsigned CondOpc;
6096 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6097 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006098 if (CondOpc == ISD::OR) {
6099 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6100 // two branches instead of an explicit OR instruction with a
6101 // separate test.
6102 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006103 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006104 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006105 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006106 Chain, Dest, CC, Cmp);
6107 CC = Cond.getOperand(1).getOperand(0);
6108 Cond = Cmp;
6109 addTest = false;
6110 }
6111 } else { // ISD::AND
6112 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6113 // two branches instead of an explicit AND instruction with a
6114 // separate test. However, we only do this if this block doesn't
6115 // have a fall-through edge, because this requires an explicit
6116 // jmp when the condition is false.
6117 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006118 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006119 Op.getNode()->hasOneUse()) {
6120 X86::CondCode CCode =
6121 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6122 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006123 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006124 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6125 // Look for an unconditional branch following this conditional branch.
6126 // We need this because we need to reverse the successors in order
6127 // to implement FCMP_OEQ.
6128 if (User.getOpcode() == ISD::BR) {
6129 SDValue FalseBB = User.getOperand(1);
6130 SDValue NewBR =
6131 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6132 assert(NewBR == User);
6133 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006134
Dale Johannesene4d209d2009-02-03 20:21:25 +00006135 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006136 Chain, Dest, CC, Cmp);
6137 X86::CondCode CCode =
6138 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6139 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006140 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006141 Cond = Cmp;
6142 addTest = false;
6143 }
6144 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006145 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006146 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6147 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6148 // It should be transformed during dag combiner except when the condition
6149 // is set by a arithmetics with overflow node.
6150 X86::CondCode CCode =
6151 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6152 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006153 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006154 Cond = Cond.getOperand(0).getOperand(1);
6155 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006156 }
Evan Cheng0488db92007-09-25 01:57:46 +00006157 }
6158
6159 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006160 // Look pass the truncate.
6161 if (Cond.getOpcode() == ISD::TRUNCATE)
6162 Cond = Cond.getOperand(0);
6163
6164 // We know the result of AND is compared against zero. Try to match
6165 // it to BT.
6166 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6167 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6168 if (NewSetCC.getNode()) {
6169 CC = NewSetCC.getOperand(0);
6170 Cond = NewSetCC.getOperand(1);
6171 addTest = false;
6172 }
6173 }
6174 }
6175
6176 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006178 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006179 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006180 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006181 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006182}
6183
Anton Korobeynikove060b532007-04-17 19:34:00 +00006184
6185// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6186// Calls to _alloca is needed to probe the stack when allocating more than 4k
6187// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6188// that the guard pages used by the OS virtual memory manager are allocated in
6189// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006190SDValue
6191X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006192 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006193 assert(Subtarget->isTargetCygMing() &&
6194 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006195 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006196
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006197 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006198 SDValue Chain = Op.getOperand(0);
6199 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006200 // FIXME: Ensure alignment here
6201
Dan Gohman475871a2008-07-27 21:46:04 +00006202 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006203
Owen Andersone50ed302009-08-10 22:56:29 +00006204 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006205 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006206
Chris Lattnere563bbc2008-10-11 22:08:30 +00006207 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006208
Dale Johannesendd64c412009-02-04 00:33:20 +00006209 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006210 Flag = Chain.getValue(1);
6211
Owen Anderson825b72b2009-08-11 20:47:22 +00006212 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006213 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006214 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006215 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006216 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006217 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006218 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006219 Flag = Chain.getValue(1);
6220
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006221 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006222 DAG.getIntPtrConstant(0, true),
6223 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006224 Flag);
6225
Dale Johannesendd64c412009-02-04 00:33:20 +00006226 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006227
Dan Gohman475871a2008-07-27 21:46:04 +00006228 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006229 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006230}
6231
Dan Gohman475871a2008-07-27 21:46:04 +00006232SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006233X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006234 SDValue Chain,
6235 SDValue Dst, SDValue Src,
6236 SDValue Size, unsigned Align,
6237 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006238 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006239 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006240
Bill Wendling6f287b22008-09-30 21:22:07 +00006241 // If not DWORD aligned or size is more than the threshold, call the library.
6242 // The libc version is likely to be faster for these cases. It can use the
6243 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006244 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006245 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006246 ConstantSize->getZExtValue() >
6247 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006248 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006249
6250 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006251 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006252
Bill Wendling6158d842008-10-01 00:59:58 +00006253 if (const char *bzeroEntry = V &&
6254 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006255 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006256 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006257 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006258 TargetLowering::ArgListEntry Entry;
6259 Entry.Node = Dst;
6260 Entry.Ty = IntPtrTy;
6261 Args.push_back(Entry);
6262 Entry.Node = Size;
6263 Args.push_back(Entry);
6264 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006265 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6266 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006267 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006268 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6269 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006270 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006271 }
6272
Dan Gohman707e0182008-04-12 04:36:06 +00006273 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006274 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006275 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006276
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006277 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006278 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006279 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006280 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006281 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006282 unsigned BytesLeft = 0;
6283 bool TwoRepStos = false;
6284 if (ValC) {
6285 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006286 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006287
Evan Cheng0db9fe62006-04-25 20:13:52 +00006288 // If the value is a constant, then we can potentially use larger sets.
6289 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006290 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006291 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006292 ValReg = X86::AX;
6293 Val = (Val << 8) | Val;
6294 break;
6295 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006296 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006297 ValReg = X86::EAX;
6298 Val = (Val << 8) | Val;
6299 Val = (Val << 16) | Val;
6300 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006301 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006302 ValReg = X86::RAX;
6303 Val = (Val << 32) | Val;
6304 }
6305 break;
6306 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006307 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006308 ValReg = X86::AL;
6309 Count = DAG.getIntPtrConstant(SizeVal);
6310 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006311 }
6312
Owen Anderson825b72b2009-08-11 20:47:22 +00006313 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006314 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006315 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6316 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006317 }
6318
Dale Johannesen0f502f62009-02-03 22:26:09 +00006319 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006320 InFlag);
6321 InFlag = Chain.getValue(1);
6322 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006323 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006324 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006325 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006326 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006327 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006328
Scott Michelfdc40a02009-02-17 22:15:04 +00006329 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006330 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006331 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006332 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006333 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006334 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006335 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006336 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006337
Owen Anderson825b72b2009-08-11 20:47:22 +00006338 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006339 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6340 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006341
Evan Cheng0db9fe62006-04-25 20:13:52 +00006342 if (TwoRepStos) {
6343 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006344 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006345 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006346 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006347 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6348 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006349 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006350 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006351 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006352 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006353 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6354 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006355 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006356 // Handle the last 1 - 7 bytes.
6357 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006358 EVT AddrVT = Dst.getValueType();
6359 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006360
Dale Johannesen0f502f62009-02-03 22:26:09 +00006361 Chain = DAG.getMemset(Chain, dl,
6362 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006363 DAG.getConstant(Offset, AddrVT)),
6364 Src,
6365 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006366 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006367 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006368
Dan Gohman707e0182008-04-12 04:36:06 +00006369 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006370 return Chain;
6371}
Evan Cheng11e15b32006-04-03 20:53:28 +00006372
Dan Gohman475871a2008-07-27 21:46:04 +00006373SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006374X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006375 SDValue Chain, SDValue Dst, SDValue Src,
6376 SDValue Size, unsigned Align,
6377 bool AlwaysInline,
6378 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006379 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006380 // This requires the copy size to be a constant, preferrably
6381 // within a subtarget-specific limit.
6382 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6383 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006384 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006385 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006386 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006387 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006388
Evan Cheng1887c1c2008-08-21 21:00:15 +00006389 /// If not DWORD aligned, call the library.
6390 if ((Align & 3) != 0)
6391 return SDValue();
6392
6393 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006394 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006395 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006397
Duncan Sands83ec4b62008-06-06 12:08:01 +00006398 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006399 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006400 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006401 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006402
Dan Gohman475871a2008-07-27 21:46:04 +00006403 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006404 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006405 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006406 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006407 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006408 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006409 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006410 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006411 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006412 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006413 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006414 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006415 InFlag = Chain.getValue(1);
6416
Owen Anderson825b72b2009-08-11 20:47:22 +00006417 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006418 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6419 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6420 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006421
Dan Gohman475871a2008-07-27 21:46:04 +00006422 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006423 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006424 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006425 // Handle the last 1 - 7 bytes.
6426 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006427 EVT DstVT = Dst.getValueType();
6428 EVT SrcVT = Src.getValueType();
6429 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006430 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006431 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006432 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006433 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006434 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006435 DAG.getConstant(BytesLeft, SizeVT),
6436 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006437 DstSV, DstSVOff + Offset,
6438 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006439 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006440
Owen Anderson825b72b2009-08-11 20:47:22 +00006441 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006442 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006443}
6444
Dan Gohman475871a2008-07-27 21:46:04 +00006445SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006446 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006447 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006448
Evan Cheng25ab6902006-09-08 06:48:29 +00006449 if (!Subtarget->is64Bit()) {
6450 // vastart just stores the address of the VarArgsFrameIndex slot into the
6451 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006452 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006453 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006454 }
6455
6456 // __va_list_tag:
6457 // gp_offset (0 - 6 * 8)
6458 // fp_offset (48 - 48 + 8 * 16)
6459 // overflow_arg_area (point to parameters coming in memory).
6460 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006461 SmallVector<SDValue, 8> MemOps;
6462 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006463 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006464 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006465 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006466 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006467 MemOps.push_back(Store);
6468
6469 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006470 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006471 FIN, DAG.getIntPtrConstant(4));
6472 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006473 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006474 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006475 MemOps.push_back(Store);
6476
6477 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006478 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006479 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006480 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006481 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006482 MemOps.push_back(Store);
6483
6484 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006485 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006486 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006487 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006488 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006489 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006490 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006491 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006492}
6493
Dan Gohman475871a2008-07-27 21:46:04 +00006494SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006495 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6496 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006497 SDValue Chain = Op.getOperand(0);
6498 SDValue SrcPtr = Op.getOperand(1);
6499 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006500
Torok Edwindac237e2009-07-08 20:53:28 +00006501 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006502 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006503}
6504
Dan Gohman475871a2008-07-27 21:46:04 +00006505SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006506 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006507 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006508 SDValue Chain = Op.getOperand(0);
6509 SDValue DstPtr = Op.getOperand(1);
6510 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006511 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6512 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006513 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006514
Dale Johannesendd64c412009-02-04 00:33:20 +00006515 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006516 DAG.getIntPtrConstant(24), 8, false,
6517 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006518}
6519
Dan Gohman475871a2008-07-27 21:46:04 +00006520SDValue
6521X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006522 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006523 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006524 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006525 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006526 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006527 case Intrinsic::x86_sse_comieq_ss:
6528 case Intrinsic::x86_sse_comilt_ss:
6529 case Intrinsic::x86_sse_comile_ss:
6530 case Intrinsic::x86_sse_comigt_ss:
6531 case Intrinsic::x86_sse_comige_ss:
6532 case Intrinsic::x86_sse_comineq_ss:
6533 case Intrinsic::x86_sse_ucomieq_ss:
6534 case Intrinsic::x86_sse_ucomilt_ss:
6535 case Intrinsic::x86_sse_ucomile_ss:
6536 case Intrinsic::x86_sse_ucomigt_ss:
6537 case Intrinsic::x86_sse_ucomige_ss:
6538 case Intrinsic::x86_sse_ucomineq_ss:
6539 case Intrinsic::x86_sse2_comieq_sd:
6540 case Intrinsic::x86_sse2_comilt_sd:
6541 case Intrinsic::x86_sse2_comile_sd:
6542 case Intrinsic::x86_sse2_comigt_sd:
6543 case Intrinsic::x86_sse2_comige_sd:
6544 case Intrinsic::x86_sse2_comineq_sd:
6545 case Intrinsic::x86_sse2_ucomieq_sd:
6546 case Intrinsic::x86_sse2_ucomilt_sd:
6547 case Intrinsic::x86_sse2_ucomile_sd:
6548 case Intrinsic::x86_sse2_ucomigt_sd:
6549 case Intrinsic::x86_sse2_ucomige_sd:
6550 case Intrinsic::x86_sse2_ucomineq_sd: {
6551 unsigned Opc = 0;
6552 ISD::CondCode CC = ISD::SETCC_INVALID;
6553 switch (IntNo) {
6554 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006555 case Intrinsic::x86_sse_comieq_ss:
6556 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006557 Opc = X86ISD::COMI;
6558 CC = ISD::SETEQ;
6559 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006560 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006561 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006562 Opc = X86ISD::COMI;
6563 CC = ISD::SETLT;
6564 break;
6565 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006566 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006567 Opc = X86ISD::COMI;
6568 CC = ISD::SETLE;
6569 break;
6570 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006571 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006572 Opc = X86ISD::COMI;
6573 CC = ISD::SETGT;
6574 break;
6575 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006576 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577 Opc = X86ISD::COMI;
6578 CC = ISD::SETGE;
6579 break;
6580 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006581 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006582 Opc = X86ISD::COMI;
6583 CC = ISD::SETNE;
6584 break;
6585 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006586 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006587 Opc = X86ISD::UCOMI;
6588 CC = ISD::SETEQ;
6589 break;
6590 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006591 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006592 Opc = X86ISD::UCOMI;
6593 CC = ISD::SETLT;
6594 break;
6595 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006596 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006597 Opc = X86ISD::UCOMI;
6598 CC = ISD::SETLE;
6599 break;
6600 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006601 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 Opc = X86ISD::UCOMI;
6603 CC = ISD::SETGT;
6604 break;
6605 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006606 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006607 Opc = X86ISD::UCOMI;
6608 CC = ISD::SETGE;
6609 break;
6610 case Intrinsic::x86_sse_ucomineq_ss:
6611 case Intrinsic::x86_sse2_ucomineq_sd:
6612 Opc = X86ISD::UCOMI;
6613 CC = ISD::SETNE;
6614 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006615 }
Evan Cheng734503b2006-09-11 02:19:56 +00006616
Dan Gohman475871a2008-07-27 21:46:04 +00006617 SDValue LHS = Op.getOperand(1);
6618 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006619 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006620 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006621 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6622 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6623 DAG.getConstant(X86CC, MVT::i8), Cond);
6624 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006625 }
Eric Christopher71c67532009-07-29 00:28:05 +00006626 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006627 // an integer value, not just an instruction so lower it to the ptest
6628 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006629 case Intrinsic::x86_sse41_ptestz:
6630 case Intrinsic::x86_sse41_ptestc:
6631 case Intrinsic::x86_sse41_ptestnzc:{
6632 unsigned X86CC = 0;
6633 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006634 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006635 case Intrinsic::x86_sse41_ptestz:
6636 // ZF = 1
6637 X86CC = X86::COND_E;
6638 break;
6639 case Intrinsic::x86_sse41_ptestc:
6640 // CF = 1
6641 X86CC = X86::COND_B;
6642 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006643 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006644 // ZF and CF = 0
6645 X86CC = X86::COND_A;
6646 break;
6647 }
Eric Christopherfd179292009-08-27 18:07:15 +00006648
Eric Christopher71c67532009-07-29 00:28:05 +00006649 SDValue LHS = Op.getOperand(1);
6650 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006651 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6652 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6653 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6654 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006655 }
Evan Cheng5759f972008-05-04 09:15:50 +00006656
6657 // Fix vector shift instructions where the last operand is a non-immediate
6658 // i32 value.
6659 case Intrinsic::x86_sse2_pslli_w:
6660 case Intrinsic::x86_sse2_pslli_d:
6661 case Intrinsic::x86_sse2_pslli_q:
6662 case Intrinsic::x86_sse2_psrli_w:
6663 case Intrinsic::x86_sse2_psrli_d:
6664 case Intrinsic::x86_sse2_psrli_q:
6665 case Intrinsic::x86_sse2_psrai_w:
6666 case Intrinsic::x86_sse2_psrai_d:
6667 case Intrinsic::x86_mmx_pslli_w:
6668 case Intrinsic::x86_mmx_pslli_d:
6669 case Intrinsic::x86_mmx_pslli_q:
6670 case Intrinsic::x86_mmx_psrli_w:
6671 case Intrinsic::x86_mmx_psrli_d:
6672 case Intrinsic::x86_mmx_psrli_q:
6673 case Intrinsic::x86_mmx_psrai_w:
6674 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006675 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006676 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006677 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006678
6679 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006680 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006681 switch (IntNo) {
6682 case Intrinsic::x86_sse2_pslli_w:
6683 NewIntNo = Intrinsic::x86_sse2_psll_w;
6684 break;
6685 case Intrinsic::x86_sse2_pslli_d:
6686 NewIntNo = Intrinsic::x86_sse2_psll_d;
6687 break;
6688 case Intrinsic::x86_sse2_pslli_q:
6689 NewIntNo = Intrinsic::x86_sse2_psll_q;
6690 break;
6691 case Intrinsic::x86_sse2_psrli_w:
6692 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6693 break;
6694 case Intrinsic::x86_sse2_psrli_d:
6695 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6696 break;
6697 case Intrinsic::x86_sse2_psrli_q:
6698 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6699 break;
6700 case Intrinsic::x86_sse2_psrai_w:
6701 NewIntNo = Intrinsic::x86_sse2_psra_w;
6702 break;
6703 case Intrinsic::x86_sse2_psrai_d:
6704 NewIntNo = Intrinsic::x86_sse2_psra_d;
6705 break;
6706 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006707 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006708 switch (IntNo) {
6709 case Intrinsic::x86_mmx_pslli_w:
6710 NewIntNo = Intrinsic::x86_mmx_psll_w;
6711 break;
6712 case Intrinsic::x86_mmx_pslli_d:
6713 NewIntNo = Intrinsic::x86_mmx_psll_d;
6714 break;
6715 case Intrinsic::x86_mmx_pslli_q:
6716 NewIntNo = Intrinsic::x86_mmx_psll_q;
6717 break;
6718 case Intrinsic::x86_mmx_psrli_w:
6719 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6720 break;
6721 case Intrinsic::x86_mmx_psrli_d:
6722 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6723 break;
6724 case Intrinsic::x86_mmx_psrli_q:
6725 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6726 break;
6727 case Intrinsic::x86_mmx_psrai_w:
6728 NewIntNo = Intrinsic::x86_mmx_psra_w;
6729 break;
6730 case Intrinsic::x86_mmx_psrai_d:
6731 NewIntNo = Intrinsic::x86_mmx_psra_d;
6732 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006733 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006734 }
6735 break;
6736 }
6737 }
Mon P Wangefa42202009-09-03 19:56:25 +00006738
6739 // The vector shift intrinsics with scalars uses 32b shift amounts but
6740 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6741 // to be zero.
6742 SDValue ShOps[4];
6743 ShOps[0] = ShAmt;
6744 ShOps[1] = DAG.getConstant(0, MVT::i32);
6745 if (ShAmtVT == MVT::v4i32) {
6746 ShOps[2] = DAG.getUNDEF(MVT::i32);
6747 ShOps[3] = DAG.getUNDEF(MVT::i32);
6748 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6749 } else {
6750 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6751 }
6752
Owen Andersone50ed302009-08-10 22:56:29 +00006753 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006754 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006755 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006757 Op.getOperand(1), ShAmt);
6758 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006759 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006760}
Evan Cheng72261582005-12-20 06:22:03 +00006761
Dan Gohman475871a2008-07-27 21:46:04 +00006762SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006763 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006764 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006765
6766 if (Depth > 0) {
6767 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6768 SDValue Offset =
6769 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006770 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006771 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006772 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006773 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006774 NULL, 0);
6775 }
6776
6777 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006778 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006779 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006780 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006781}
6782
Dan Gohman475871a2008-07-27 21:46:04 +00006783SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006784 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6785 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006786 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006787 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006788 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6789 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006790 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006791 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006792 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006793 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006794}
6795
Dan Gohman475871a2008-07-27 21:46:04 +00006796SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006797 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006798 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006799}
6800
Dan Gohman475871a2008-07-27 21:46:04 +00006801SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006802{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006803 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006804 SDValue Chain = Op.getOperand(0);
6805 SDValue Offset = Op.getOperand(1);
6806 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006807 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006808
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006809 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6810 getPointerTy());
6811 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006812
Dale Johannesene4d209d2009-02-03 20:21:25 +00006813 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006814 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006815 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6816 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006817 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006818 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006819
Dale Johannesene4d209d2009-02-03 20:21:25 +00006820 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006821 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006822 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006823}
6824
Dan Gohman475871a2008-07-27 21:46:04 +00006825SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006826 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue Root = Op.getOperand(0);
6828 SDValue Trmp = Op.getOperand(1); // trampoline
6829 SDValue FPtr = Op.getOperand(2); // nested function
6830 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006831 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006832
Dan Gohman69de1932008-02-06 22:27:42 +00006833 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006834
Duncan Sands339e14f2008-01-16 22:55:25 +00006835 const X86InstrInfo *TII =
6836 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6837
Duncan Sandsb116fac2007-07-27 20:02:49 +00006838 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006839 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006840
6841 // Large code-model.
6842
6843 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6844 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6845
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006846 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6847 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006848
6849 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6850
6851 // Load the pointer to the nested function into R11.
6852 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006853 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006854 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006855 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006856
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6858 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006859 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006860
6861 // Load the 'nest' parameter value into R10.
6862 // R10 is specified in X86CallingConv.td
6863 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006864 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6865 DAG.getConstant(10, MVT::i64));
6866 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006867 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006868
Owen Anderson825b72b2009-08-11 20:47:22 +00006869 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6870 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006871 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006872
6873 // Jump to the nested function.
6874 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6876 DAG.getConstant(20, MVT::i64));
6877 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006878 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006879
6880 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006881 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6882 DAG.getConstant(22, MVT::i64));
6883 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006884 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006885
Dan Gohman475871a2008-07-27 21:46:04 +00006886 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006887 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006888 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006889 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006890 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006891 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006892 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006893 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006894
6895 switch (CC) {
6896 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006897 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006898 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006899 case CallingConv::X86_StdCall: {
6900 // Pass 'nest' parameter in ECX.
6901 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006902 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006903
6904 // Check that ECX wasn't needed by an 'inreg' parameter.
6905 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006906 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006907
Chris Lattner58d74912008-03-12 17:45:29 +00006908 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006909 unsigned InRegCount = 0;
6910 unsigned Idx = 1;
6911
6912 for (FunctionType::param_iterator I = FTy->param_begin(),
6913 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006914 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006915 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006916 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006917
6918 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006919 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006920 }
6921 }
6922 break;
6923 }
6924 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006925 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006926 // Pass 'nest' parameter in EAX.
6927 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006928 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006929 break;
6930 }
6931
Dan Gohman475871a2008-07-27 21:46:04 +00006932 SDValue OutChains[4];
6933 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006934
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6936 DAG.getConstant(10, MVT::i32));
6937 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006938
Duncan Sands339e14f2008-01-16 22:55:25 +00006939 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006940 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006941 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006943 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006944
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6946 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006947 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006948
Duncan Sands339e14f2008-01-16 22:55:25 +00006949 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6951 DAG.getConstant(5, MVT::i32));
6952 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006953 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006954
Owen Anderson825b72b2009-08-11 20:47:22 +00006955 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6956 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006957 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006958
Dan Gohman475871a2008-07-27 21:46:04 +00006959 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006961 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006962 }
6963}
6964
Dan Gohman475871a2008-07-27 21:46:04 +00006965SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006966 /*
6967 The rounding mode is in bits 11:10 of FPSR, and has the following
6968 settings:
6969 00 Round to nearest
6970 01 Round to -inf
6971 10 Round to +inf
6972 11 Round to 0
6973
6974 FLT_ROUNDS, on the other hand, expects the following:
6975 -1 Undefined
6976 0 Round to 0
6977 1 Round to nearest
6978 2 Round to +inf
6979 3 Round to -inf
6980
6981 To perform the conversion, we do:
6982 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6983 */
6984
6985 MachineFunction &MF = DAG.getMachineFunction();
6986 const TargetMachine &TM = MF.getTarget();
6987 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6988 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00006989 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006990 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006991
6992 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00006993 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006994 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006995
Owen Anderson825b72b2009-08-11 20:47:22 +00006996 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006997 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006998
6999 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007000 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007001
7002 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007003 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007004 DAG.getNode(ISD::SRL, dl, MVT::i16,
7005 DAG.getNode(ISD::AND, dl, MVT::i16,
7006 CWD, DAG.getConstant(0x800, MVT::i16)),
7007 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007008 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007009 DAG.getNode(ISD::SRL, dl, MVT::i16,
7010 DAG.getNode(ISD::AND, dl, MVT::i16,
7011 CWD, DAG.getConstant(0x400, MVT::i16)),
7012 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007013
Dan Gohman475871a2008-07-27 21:46:04 +00007014 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007015 DAG.getNode(ISD::AND, dl, MVT::i16,
7016 DAG.getNode(ISD::ADD, dl, MVT::i16,
7017 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7018 DAG.getConstant(1, MVT::i16)),
7019 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007020
7021
Duncan Sands83ec4b62008-06-06 12:08:01 +00007022 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007023 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007024}
7025
Dan Gohman475871a2008-07-27 21:46:04 +00007026SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007027 EVT VT = Op.getValueType();
7028 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007029 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007030 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007031
7032 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007034 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007035 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007036 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007037 }
Evan Cheng18efe262007-12-14 02:13:44 +00007038
Evan Cheng152804e2007-12-14 08:30:15 +00007039 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007040 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007041 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007042
7043 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007044 SDValue Ops[] = {
7045 Op,
7046 DAG.getConstant(NumBits+NumBits-1, OpVT),
7047 DAG.getConstant(X86::COND_E, MVT::i8),
7048 Op.getValue(1)
7049 };
7050 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007051
7052 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007053 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007054
Owen Anderson825b72b2009-08-11 20:47:22 +00007055 if (VT == MVT::i8)
7056 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007057 return Op;
7058}
7059
Dan Gohman475871a2008-07-27 21:46:04 +00007060SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007061 EVT VT = Op.getValueType();
7062 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007063 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007064 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007065
7066 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007067 if (VT == MVT::i8) {
7068 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007069 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007070 }
Evan Cheng152804e2007-12-14 08:30:15 +00007071
7072 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007074 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007075
7076 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007077 SDValue Ops[] = {
7078 Op,
7079 DAG.getConstant(NumBits, OpVT),
7080 DAG.getConstant(X86::COND_E, MVT::i8),
7081 Op.getValue(1)
7082 };
7083 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007084
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 if (VT == MVT::i8)
7086 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007087 return Op;
7088}
7089
Mon P Wangaf9b9522008-12-18 21:42:19 +00007090SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007091 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007093 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007094
Mon P Wangaf9b9522008-12-18 21:42:19 +00007095 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7096 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7097 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7098 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7099 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7100 //
7101 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7102 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7103 // return AloBlo + AloBhi + AhiBlo;
7104
7105 SDValue A = Op.getOperand(0);
7106 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007107
Dale Johannesene4d209d2009-02-03 20:21:25 +00007108 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7110 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007111 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007112 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7113 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007114 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007116 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007117 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007119 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007120 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007121 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007122 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007123 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7125 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007126 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007127 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7128 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007129 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7130 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007131 return Res;
7132}
7133
7134
Bill Wendling74c37652008-12-09 22:08:41 +00007135SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7136 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7137 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007138 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7139 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007140 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007141 SDValue LHS = N->getOperand(0);
7142 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007143 unsigned BaseOp = 0;
7144 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007145 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007146
7147 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007148 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007149 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007150 // A subtract of one will be selected as a INC. Note that INC doesn't
7151 // set CF, so we can't do this for UADDO.
7152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7153 if (C->getAPIntValue() == 1) {
7154 BaseOp = X86ISD::INC;
7155 Cond = X86::COND_O;
7156 break;
7157 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007158 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007159 Cond = X86::COND_O;
7160 break;
7161 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007162 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007163 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007164 break;
7165 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007166 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7167 // set CF, so we can't do this for USUBO.
7168 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7169 if (C->getAPIntValue() == 1) {
7170 BaseOp = X86ISD::DEC;
7171 Cond = X86::COND_O;
7172 break;
7173 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007174 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007175 Cond = X86::COND_O;
7176 break;
7177 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007178 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007179 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007180 break;
7181 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007182 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007183 Cond = X86::COND_O;
7184 break;
7185 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007186 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007187 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007188 break;
7189 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007190
Bill Wendling61edeb52008-12-02 01:06:39 +00007191 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007192 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007193 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007194
Bill Wendling61edeb52008-12-02 01:06:39 +00007195 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007196 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007198
Bill Wendling61edeb52008-12-02 01:06:39 +00007199 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7200 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007201}
7202
Dan Gohman475871a2008-07-27 21:46:04 +00007203SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007204 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007205 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007206 unsigned Reg = 0;
7207 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007208 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007209 default:
7210 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007211 case MVT::i8: Reg = X86::AL; size = 1; break;
7212 case MVT::i16: Reg = X86::AX; size = 2; break;
7213 case MVT::i32: Reg = X86::EAX; size = 4; break;
7214 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007215 assert(Subtarget->is64Bit() && "Node not type legal!");
7216 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007217 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007218 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007219 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007220 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007221 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007222 Op.getOperand(1),
7223 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007225 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007226 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007227 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007228 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007229 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007230 return cpOut;
7231}
7232
Duncan Sands1607f052008-12-01 11:39:25 +00007233SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007234 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007235 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007236 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007237 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007238 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007239 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007240 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7241 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007242 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007243 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7244 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007245 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007246 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007247 rdx.getValue(1)
7248 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007249 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007250}
7251
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007252SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7253 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007255 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007256 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007257 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007258 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007259 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007260 Node->getOperand(0),
7261 Node->getOperand(1), negOp,
7262 cast<AtomicSDNode>(Node)->getSrcValue(),
7263 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007264}
7265
Evan Cheng0db9fe62006-04-25 20:13:52 +00007266/// LowerOperation - Provide custom lowering hooks for some operations.
7267///
Dan Gohman475871a2008-07-27 21:46:04 +00007268SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007269 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007270 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007271 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7272 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007273 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007274 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007275 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7276 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7277 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7278 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7279 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7280 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007281 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007282 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007283 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007284 case ISD::SHL_PARTS:
7285 case ISD::SRA_PARTS:
7286 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7287 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007288 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007289 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007290 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007291 case ISD::FABS: return LowerFABS(Op, DAG);
7292 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007293 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007294 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007295 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007296 case ISD::SELECT: return LowerSELECT(Op, DAG);
7297 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007298 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007299 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007300 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007301 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007302 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007303 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7304 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007305 case ISD::FRAME_TO_ARGS_OFFSET:
7306 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007307 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007308 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007309 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007310 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007311 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7312 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007313 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007314 case ISD::SADDO:
7315 case ISD::UADDO:
7316 case ISD::SSUBO:
7317 case ISD::USUBO:
7318 case ISD::SMULO:
7319 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007320 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007321 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007322}
7323
Duncan Sands1607f052008-12-01 11:39:25 +00007324void X86TargetLowering::
7325ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7326 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007327 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007328 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007330
7331 SDValue Chain = Node->getOperand(0);
7332 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007334 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007335 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007336 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007337 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007338 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007339 SDValue Result =
7340 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7341 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007342 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007343 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007344 Results.push_back(Result.getValue(2));
7345}
7346
Duncan Sands126d9072008-07-04 11:47:58 +00007347/// ReplaceNodeResults - Replace a node with an illegal result type
7348/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007349void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7350 SmallVectorImpl<SDValue>&Results,
7351 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007352 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007353 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007354 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007355 assert(false && "Do not know how to custom type legalize this operation!");
7356 return;
7357 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007358 std::pair<SDValue,SDValue> Vals =
7359 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007360 SDValue FIST = Vals.first, StackSlot = Vals.second;
7361 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007362 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007363 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007364 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007365 }
7366 return;
7367 }
7368 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007369 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007370 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007371 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007373 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007374 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007375 eax.getValue(2));
7376 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7377 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007378 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007379 Results.push_back(edx.getValue(1));
7380 return;
7381 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007382 case ISD::SDIV:
7383 case ISD::UDIV:
7384 case ISD::SREM:
7385 case ISD::UREM: {
7386 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7387 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7388 return;
7389 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007390 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007391 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007392 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007393 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007394 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7395 DAG.getConstant(0, MVT::i32));
7396 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7397 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007398 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7399 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007400 cpInL.getValue(1));
7401 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007402 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7403 DAG.getConstant(0, MVT::i32));
7404 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7405 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007406 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007407 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007408 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007409 swapInL.getValue(1));
7410 SDValue Ops[] = { swapInH.getValue(0),
7411 N->getOperand(1),
7412 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007414 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007415 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007417 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007418 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007419 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007420 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007421 Results.push_back(cpOutH.getValue(1));
7422 return;
7423 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007424 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007425 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7426 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007427 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007428 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7429 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007430 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007431 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7432 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007433 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007434 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7435 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007436 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007437 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7438 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007439 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007440 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7441 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007442 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007443 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7444 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007445 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007446}
7447
Evan Cheng72261582005-12-20 06:22:03 +00007448const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7449 switch (Opcode) {
7450 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007451 case X86ISD::BSF: return "X86ISD::BSF";
7452 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007453 case X86ISD::SHLD: return "X86ISD::SHLD";
7454 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007455 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007456 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007457 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007458 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007459 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007460 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007461 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7462 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7463 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007464 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007465 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007466 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007467 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007468 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007469 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007470 case X86ISD::COMI: return "X86ISD::COMI";
7471 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007472 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007473 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007474 case X86ISD::CMOV: return "X86ISD::CMOV";
7475 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007476 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007477 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7478 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007479 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007480 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007481 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007482 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007483 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007484 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7485 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007486 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007487 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007488 case X86ISD::FMAX: return "X86ISD::FMAX";
7489 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007490 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7491 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007492 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007493 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007494 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007495 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007496 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007497 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7498 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007499 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7500 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7501 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7502 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7503 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7504 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007505 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7506 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007507 case X86ISD::VSHL: return "X86ISD::VSHL";
7508 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007509 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7510 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7511 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7512 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7513 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7514 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7515 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7516 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7517 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7518 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007519 case X86ISD::ADD: return "X86ISD::ADD";
7520 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007521 case X86ISD::SMUL: return "X86ISD::SMUL";
7522 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007523 case X86ISD::INC: return "X86ISD::INC";
7524 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007525 case X86ISD::OR: return "X86ISD::OR";
7526 case X86ISD::XOR: return "X86ISD::XOR";
7527 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007528 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007529 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007530 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007531 }
7532}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007533
Chris Lattnerc9addb72007-03-30 23:15:24 +00007534// isLegalAddressingMode - Return true if the addressing mode represented
7535// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007536bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007537 const Type *Ty) const {
7538 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007539 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007540
Chris Lattnerc9addb72007-03-30 23:15:24 +00007541 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007542 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007543 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007544
Chris Lattnerc9addb72007-03-30 23:15:24 +00007545 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007546 unsigned GVFlags =
7547 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007548
Chris Lattnerdfed4132009-07-10 07:38:24 +00007549 // If a reference to this global requires an extra load, we can't fold it.
7550 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007551 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007552
Chris Lattnerdfed4132009-07-10 07:38:24 +00007553 // If BaseGV requires a register for the PIC base, we cannot also have a
7554 // BaseReg specified.
7555 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007556 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007557
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007558 // If lower 4G is not available, then we must use rip-relative addressing.
7559 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7560 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007561 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007562
Chris Lattnerc9addb72007-03-30 23:15:24 +00007563 switch (AM.Scale) {
7564 case 0:
7565 case 1:
7566 case 2:
7567 case 4:
7568 case 8:
7569 // These scales always work.
7570 break;
7571 case 3:
7572 case 5:
7573 case 9:
7574 // These scales are formed with basereg+scalereg. Only accept if there is
7575 // no basereg yet.
7576 if (AM.HasBaseReg)
7577 return false;
7578 break;
7579 default: // Other stuff never works.
7580 return false;
7581 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007582
Chris Lattnerc9addb72007-03-30 23:15:24 +00007583 return true;
7584}
7585
7586
Evan Cheng2bd122c2007-10-26 01:56:11 +00007587bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7588 if (!Ty1->isInteger() || !Ty2->isInteger())
7589 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007590 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7591 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007592 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007593 return false;
7594 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007595}
7596
Owen Andersone50ed302009-08-10 22:56:29 +00007597bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007598 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007599 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007600 unsigned NumBits1 = VT1.getSizeInBits();
7601 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007602 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007603 return false;
7604 return Subtarget->is64Bit() || NumBits1 < 64;
7605}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007606
Dan Gohman97121ba2009-04-08 00:15:30 +00007607bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007608 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007609 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007610}
7611
Owen Andersone50ed302009-08-10 22:56:29 +00007612bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007613 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007615}
7616
Owen Andersone50ed302009-08-10 22:56:29 +00007617bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007618 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007619 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007620}
7621
Evan Cheng60c07e12006-07-05 22:17:51 +00007622/// isShuffleMaskLegal - Targets can use this to indicate that they only
7623/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7624/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7625/// are assumed to be legal.
7626bool
Eric Christopherfd179292009-08-27 18:07:15 +00007627X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007628 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007629 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007630 if (VT.getSizeInBits() == 64)
7631 return false;
7632
Nate Begemana09008b2009-10-19 02:17:23 +00007633 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007634 return (VT.getVectorNumElements() == 2 ||
7635 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7636 isMOVLMask(M, VT) ||
7637 isSHUFPMask(M, VT) ||
7638 isPSHUFDMask(M, VT) ||
7639 isPSHUFHWMask(M, VT) ||
7640 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007641 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007642 isUNPCKLMask(M, VT) ||
7643 isUNPCKHMask(M, VT) ||
7644 isUNPCKL_v_undef_Mask(M, VT) ||
7645 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007646}
7647
Dan Gohman7d8143f2008-04-09 20:09:42 +00007648bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007649X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007650 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007651 unsigned NumElts = VT.getVectorNumElements();
7652 // FIXME: This collection of masks seems suspect.
7653 if (NumElts == 2)
7654 return true;
7655 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7656 return (isMOVLMask(Mask, VT) ||
7657 isCommutedMOVLMask(Mask, VT, true) ||
7658 isSHUFPMask(Mask, VT) ||
7659 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007660 }
7661 return false;
7662}
7663
7664//===----------------------------------------------------------------------===//
7665// X86 Scheduler Hooks
7666//===----------------------------------------------------------------------===//
7667
Mon P Wang63307c32008-05-05 19:05:59 +00007668// private utility function
7669MachineBasicBlock *
7670X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7671 MachineBasicBlock *MBB,
7672 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007673 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007674 unsigned LoadOpc,
7675 unsigned CXchgOpc,
7676 unsigned copyOpc,
7677 unsigned notOpc,
7678 unsigned EAXreg,
7679 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007680 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007681 // For the atomic bitwise operator, we generate
7682 // thisMBB:
7683 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007684 // ld t1 = [bitinstr.addr]
7685 // op t2 = t1, [bitinstr.val]
7686 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007687 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7688 // bz newMBB
7689 // fallthrough -->nextMBB
7690 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7691 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007692 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007693 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007694
Mon P Wang63307c32008-05-05 19:05:59 +00007695 /// First build the CFG
7696 MachineFunction *F = MBB->getParent();
7697 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007698 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7699 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7700 F->insert(MBBIter, newMBB);
7701 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007702
Mon P Wang63307c32008-05-05 19:05:59 +00007703 // Move all successors to thisMBB to nextMBB
7704 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007705
Mon P Wang63307c32008-05-05 19:05:59 +00007706 // Update thisMBB to fall through to newMBB
7707 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007708
Mon P Wang63307c32008-05-05 19:05:59 +00007709 // newMBB jumps to itself and fall through to nextMBB
7710 newMBB->addSuccessor(nextMBB);
7711 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007712
Mon P Wang63307c32008-05-05 19:05:59 +00007713 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007714 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007715 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007716 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007717 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007718 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007719 int numArgs = bInstr->getNumOperands() - 1;
7720 for (int i=0; i < numArgs; ++i)
7721 argOpers[i] = &bInstr->getOperand(i+1);
7722
7723 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007724 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7725 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007726
Dale Johannesen140be2d2008-08-19 18:47:28 +00007727 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007728 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007729 for (int i=0; i <= lastAddrIndx; ++i)
7730 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007731
Dale Johannesen140be2d2008-08-19 18:47:28 +00007732 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007733 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007734 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007735 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007736 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007737 tt = t1;
7738
Dale Johannesen140be2d2008-08-19 18:47:28 +00007739 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007740 assert((argOpers[valArgIndx]->isReg() ||
7741 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007742 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007743 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007744 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007745 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007746 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007747 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007748 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007749
Dale Johannesene4d209d2009-02-03 20:21:25 +00007750 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007751 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007752
Dale Johannesene4d209d2009-02-03 20:21:25 +00007753 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007754 for (int i=0; i <= lastAddrIndx; ++i)
7755 (*MIB).addOperand(*argOpers[i]);
7756 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007757 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007758 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7759 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007760
Dale Johannesene4d209d2009-02-03 20:21:25 +00007761 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007762 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007763
Mon P Wang63307c32008-05-05 19:05:59 +00007764 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007765 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007766
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007767 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007768 return nextMBB;
7769}
7770
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007771// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007772MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007773X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7774 MachineBasicBlock *MBB,
7775 unsigned regOpcL,
7776 unsigned regOpcH,
7777 unsigned immOpcL,
7778 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007779 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007780 // For the atomic bitwise operator, we generate
7781 // thisMBB (instructions are in pairs, except cmpxchg8b)
7782 // ld t1,t2 = [bitinstr.addr]
7783 // newMBB:
7784 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7785 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007786 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007787 // mov ECX, EBX <- t5, t6
7788 // mov EAX, EDX <- t1, t2
7789 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7790 // mov t3, t4 <- EAX, EDX
7791 // bz newMBB
7792 // result in out1, out2
7793 // fallthrough -->nextMBB
7794
7795 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7796 const unsigned LoadOpc = X86::MOV32rm;
7797 const unsigned copyOpc = X86::MOV32rr;
7798 const unsigned NotOpc = X86::NOT32r;
7799 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7800 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7801 MachineFunction::iterator MBBIter = MBB;
7802 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007803
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007804 /// First build the CFG
7805 MachineFunction *F = MBB->getParent();
7806 MachineBasicBlock *thisMBB = MBB;
7807 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7808 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7809 F->insert(MBBIter, newMBB);
7810 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007811
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007812 // Move all successors to thisMBB to nextMBB
7813 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007814
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007815 // Update thisMBB to fall through to newMBB
7816 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007817
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007818 // newMBB jumps to itself and fall through to nextMBB
7819 newMBB->addSuccessor(nextMBB);
7820 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007821
Dale Johannesene4d209d2009-02-03 20:21:25 +00007822 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007823 // Insert instructions into newMBB based on incoming instruction
7824 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007825 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007826 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007827 MachineOperand& dest1Oper = bInstr->getOperand(0);
7828 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007829 MachineOperand* argOpers[2 + X86AddrNumOperands];
7830 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007831 argOpers[i] = &bInstr->getOperand(i+2);
7832
Evan Chengad5b52f2010-01-08 19:14:57 +00007833 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007834 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007835
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007836 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007837 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007838 for (int i=0; i <= lastAddrIndx; ++i)
7839 (*MIB).addOperand(*argOpers[i]);
7840 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007841 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007842 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007843 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007844 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007845 MachineOperand newOp3 = *(argOpers[3]);
7846 if (newOp3.isImm())
7847 newOp3.setImm(newOp3.getImm()+4);
7848 else
7849 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007850 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007851 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007852
7853 // t3/4 are defined later, at the bottom of the loop
7854 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7855 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007856 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007857 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007858 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007859 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7860
Evan Cheng306b4ca2010-01-08 23:41:50 +00007861 // The subsequent operations should be using the destination registers of
7862 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007863 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007864 t1 = F->getRegInfo().createVirtualRegister(RC);
7865 t2 = F->getRegInfo().createVirtualRegister(RC);
7866 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7867 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007868 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007869 t1 = dest1Oper.getReg();
7870 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007871 }
7872
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007873 int valArgIndx = lastAddrIndx + 1;
7874 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007875 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007876 "invalid operand");
7877 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7878 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007879 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007880 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007881 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007882 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007883 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007884 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007885 (*MIB).addOperand(*argOpers[valArgIndx]);
7886 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007887 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007888 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007889 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007890 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007891 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007892 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007893 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007894 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007895 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007896 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007897
Dale Johannesene4d209d2009-02-03 20:21:25 +00007898 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007899 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007900 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007901 MIB.addReg(t2);
7902
Dale Johannesene4d209d2009-02-03 20:21:25 +00007903 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007904 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007905 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007906 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007907
Dale Johannesene4d209d2009-02-03 20:21:25 +00007908 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007909 for (int i=0; i <= lastAddrIndx; ++i)
7910 (*MIB).addOperand(*argOpers[i]);
7911
7912 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007913 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7914 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007915
Dale Johannesene4d209d2009-02-03 20:21:25 +00007916 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007917 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007918 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007919 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007920
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007921 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007922 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007923
7924 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7925 return nextMBB;
7926}
7927
7928// private utility function
7929MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007930X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7931 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007932 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007933 // For the atomic min/max operator, we generate
7934 // thisMBB:
7935 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007936 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007937 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007938 // cmp t1, t2
7939 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007940 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007941 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7942 // bz newMBB
7943 // fallthrough -->nextMBB
7944 //
7945 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7946 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007947 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007948 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007949
Mon P Wang63307c32008-05-05 19:05:59 +00007950 /// First build the CFG
7951 MachineFunction *F = MBB->getParent();
7952 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007953 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7954 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7955 F->insert(MBBIter, newMBB);
7956 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007957
Dan Gohmand6708ea2009-08-15 01:38:56 +00007958 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00007959 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Mon P Wang63307c32008-05-05 19:05:59 +00007961 // Update thisMBB to fall through to newMBB
7962 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007963
Mon P Wang63307c32008-05-05 19:05:59 +00007964 // newMBB jumps to newMBB and fall through to nextMBB
7965 newMBB->addSuccessor(nextMBB);
7966 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007967
Dale Johannesene4d209d2009-02-03 20:21:25 +00007968 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007969 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007970 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007971 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007972 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007973 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007974 int numArgs = mInstr->getNumOperands() - 1;
7975 for (int i=0; i < numArgs; ++i)
7976 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007977
Mon P Wang63307c32008-05-05 19:05:59 +00007978 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007979 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7980 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007981
Mon P Wangab3e7472008-05-05 22:56:23 +00007982 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007983 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007984 for (int i=0; i <= lastAddrIndx; ++i)
7985 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007986
Mon P Wang63307c32008-05-05 19:05:59 +00007987 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007988 assert((argOpers[valArgIndx]->isReg() ||
7989 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007990 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007991
7992 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007993 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007995 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007997 (*MIB).addOperand(*argOpers[valArgIndx]);
7998
Dale Johannesene4d209d2009-02-03 20:21:25 +00007999 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008000 MIB.addReg(t1);
8001
Dale Johannesene4d209d2009-02-03 20:21:25 +00008002 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008003 MIB.addReg(t1);
8004 MIB.addReg(t2);
8005
8006 // Generate movc
8007 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008008 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008009 MIB.addReg(t2);
8010 MIB.addReg(t1);
8011
8012 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008013 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008014 for (int i=0; i <= lastAddrIndx; ++i)
8015 (*MIB).addOperand(*argOpers[i]);
8016 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008017 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008018 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8019 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008020
Dale Johannesene4d209d2009-02-03 20:21:25 +00008021 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008022 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008023
Mon P Wang63307c32008-05-05 19:05:59 +00008024 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008025 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008026
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008027 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008028 return nextMBB;
8029}
8030
Eric Christopherf83a5de2009-08-27 18:08:16 +00008031// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8032// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008033MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008034X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008035 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008036
8037 MachineFunction *F = BB->getParent();
8038 DebugLoc dl = MI->getDebugLoc();
8039 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8040
8041 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008042 if (memArg)
8043 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8044 else
8045 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008046
8047 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8048
8049 for (unsigned i = 0; i < numArgs; ++i) {
8050 MachineOperand &Op = MI->getOperand(i+1);
8051
8052 if (!(Op.isReg() && Op.isImplicit()))
8053 MIB.addOperand(Op);
8054 }
8055
8056 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8057 .addReg(X86::XMM0);
8058
8059 F->DeleteMachineInstr(MI);
8060
8061 return BB;
8062}
8063
8064MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008065X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8066 MachineInstr *MI,
8067 MachineBasicBlock *MBB) const {
8068 // Emit code to save XMM registers to the stack. The ABI says that the
8069 // number of registers to save is given in %al, so it's theoretically
8070 // possible to do an indirect jump trick to avoid saving all of them,
8071 // however this code takes a simpler approach and just executes all
8072 // of the stores if %al is non-zero. It's less code, and it's probably
8073 // easier on the hardware branch predictor, and stores aren't all that
8074 // expensive anyway.
8075
8076 // Create the new basic blocks. One block contains all the XMM stores,
8077 // and one block is the final destination regardless of whether any
8078 // stores were performed.
8079 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8080 MachineFunction *F = MBB->getParent();
8081 MachineFunction::iterator MBBIter = MBB;
8082 ++MBBIter;
8083 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8084 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8085 F->insert(MBBIter, XMMSaveMBB);
8086 F->insert(MBBIter, EndMBB);
8087
8088 // Set up the CFG.
8089 // Move any original successors of MBB to the end block.
8090 EndMBB->transferSuccessors(MBB);
8091 // The original block will now fall through to the XMM save block.
8092 MBB->addSuccessor(XMMSaveMBB);
8093 // The XMMSaveMBB will fall through to the end block.
8094 XMMSaveMBB->addSuccessor(EndMBB);
8095
8096 // Now add the instructions.
8097 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8098 DebugLoc DL = MI->getDebugLoc();
8099
8100 unsigned CountReg = MI->getOperand(0).getReg();
8101 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8102 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8103
8104 if (!Subtarget->isTargetWin64()) {
8105 // If %al is 0, branch around the XMM save block.
8106 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8107 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8108 MBB->addSuccessor(EndMBB);
8109 }
8110
8111 // In the XMM save block, save all the XMM argument registers.
8112 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8113 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008114 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008115 F->getMachineMemOperand(
8116 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8117 MachineMemOperand::MOStore, Offset,
8118 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008119 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8120 .addFrameIndex(RegSaveFrameIndex)
8121 .addImm(/*Scale=*/1)
8122 .addReg(/*IndexReg=*/0)
8123 .addImm(/*Disp=*/Offset)
8124 .addReg(/*Segment=*/0)
8125 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008126 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008127 }
8128
8129 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8130
8131 return EndMBB;
8132}
Mon P Wang63307c32008-05-05 19:05:59 +00008133
Evan Cheng60c07e12006-07-05 22:17:51 +00008134MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008135X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008136 MachineBasicBlock *BB,
8137 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008138 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8139 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008140
Chris Lattner52600972009-09-02 05:57:00 +00008141 // To "insert" a SELECT_CC instruction, we actually have to insert the
8142 // diamond control-flow pattern. The incoming instruction knows the
8143 // destination vreg to set, the condition code register to branch on, the
8144 // true/false values to select between, and a branch opcode to use.
8145 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8146 MachineFunction::iterator It = BB;
8147 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008148
Chris Lattner52600972009-09-02 05:57:00 +00008149 // thisMBB:
8150 // ...
8151 // TrueVal = ...
8152 // cmpTY ccX, r1, r2
8153 // bCC copy1MBB
8154 // fallthrough --> copy0MBB
8155 MachineBasicBlock *thisMBB = BB;
8156 MachineFunction *F = BB->getParent();
8157 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8158 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8159 unsigned Opc =
8160 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8161 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8162 F->insert(It, copy0MBB);
8163 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008164 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008165 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008166 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008167 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008168 E = BB->succ_end(); I != E; ++I) {
8169 EM->insert(std::make_pair(*I, sinkMBB));
8170 sinkMBB->addSuccessor(*I);
8171 }
8172 // Next, remove all successors of the current block, and add the true
8173 // and fallthrough blocks as its successors.
8174 while (!BB->succ_empty())
8175 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008176 // Add the true and fallthrough blocks as its successors.
8177 BB->addSuccessor(copy0MBB);
8178 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008179
Chris Lattner52600972009-09-02 05:57:00 +00008180 // copy0MBB:
8181 // %FalseValue = ...
8182 // # fallthrough to sinkMBB
8183 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008184
Chris Lattner52600972009-09-02 05:57:00 +00008185 // Update machine-CFG edges
8186 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008187
Chris Lattner52600972009-09-02 05:57:00 +00008188 // sinkMBB:
8189 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8190 // ...
8191 BB = sinkMBB;
8192 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8193 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8194 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8195
8196 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8197 return BB;
8198}
8199
8200
8201MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008202X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008203 MachineBasicBlock *BB,
8204 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008205 switch (MI->getOpcode()) {
8206 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008207 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008208 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008209 case X86::CMOV_FR32:
8210 case X86::CMOV_FR64:
8211 case X86::CMOV_V4F32:
8212 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008213 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008214 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008215
Dale Johannesen849f2142007-07-03 00:53:03 +00008216 case X86::FP32_TO_INT16_IN_MEM:
8217 case X86::FP32_TO_INT32_IN_MEM:
8218 case X86::FP32_TO_INT64_IN_MEM:
8219 case X86::FP64_TO_INT16_IN_MEM:
8220 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008221 case X86::FP64_TO_INT64_IN_MEM:
8222 case X86::FP80_TO_INT16_IN_MEM:
8223 case X86::FP80_TO_INT32_IN_MEM:
8224 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008225 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8226 DebugLoc DL = MI->getDebugLoc();
8227
Evan Cheng60c07e12006-07-05 22:17:51 +00008228 // Change the floating point control register to use "round towards zero"
8229 // mode when truncating to an integer value.
8230 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008231 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008232 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008233
8234 // Load the old value of the high byte of the control word...
8235 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008236 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008237 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008238 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008239
8240 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008241 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008242 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008243
8244 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008245 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008246
8247 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008248 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008249 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008250
8251 // Get the X86 opcode to use.
8252 unsigned Opc;
8253 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008254 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008255 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8256 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8257 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8258 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8259 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8260 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008261 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8262 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8263 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008264 }
8265
8266 X86AddressMode AM;
8267 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008268 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008269 AM.BaseType = X86AddressMode::RegBase;
8270 AM.Base.Reg = Op.getReg();
8271 } else {
8272 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008273 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008274 }
8275 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008276 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008277 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008278 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008279 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008280 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008281 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008282 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008283 AM.GV = Op.getGlobal();
8284 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008285 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008286 }
Chris Lattner52600972009-09-02 05:57:00 +00008287 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008288 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008289
8290 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008291 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008292
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008293 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008294 return BB;
8295 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008296 // String/text processing lowering.
8297 case X86::PCMPISTRM128REG:
8298 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8299 case X86::PCMPISTRM128MEM:
8300 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8301 case X86::PCMPESTRM128REG:
8302 return EmitPCMP(MI, BB, 5, false /* in mem */);
8303 case X86::PCMPESTRM128MEM:
8304 return EmitPCMP(MI, BB, 5, true /* in mem */);
8305
8306 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008307 case X86::ATOMAND32:
8308 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008309 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008310 X86::LCMPXCHG32, X86::MOV32rr,
8311 X86::NOT32r, X86::EAX,
8312 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008313 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008314 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8315 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008316 X86::LCMPXCHG32, X86::MOV32rr,
8317 X86::NOT32r, X86::EAX,
8318 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008319 case X86::ATOMXOR32:
8320 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008321 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008322 X86::LCMPXCHG32, X86::MOV32rr,
8323 X86::NOT32r, X86::EAX,
8324 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008325 case X86::ATOMNAND32:
8326 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008327 X86::AND32ri, X86::MOV32rm,
8328 X86::LCMPXCHG32, X86::MOV32rr,
8329 X86::NOT32r, X86::EAX,
8330 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008331 case X86::ATOMMIN32:
8332 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8333 case X86::ATOMMAX32:
8334 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8335 case X86::ATOMUMIN32:
8336 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8337 case X86::ATOMUMAX32:
8338 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008339
8340 case X86::ATOMAND16:
8341 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8342 X86::AND16ri, X86::MOV16rm,
8343 X86::LCMPXCHG16, X86::MOV16rr,
8344 X86::NOT16r, X86::AX,
8345 X86::GR16RegisterClass);
8346 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008347 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008348 X86::OR16ri, X86::MOV16rm,
8349 X86::LCMPXCHG16, X86::MOV16rr,
8350 X86::NOT16r, X86::AX,
8351 X86::GR16RegisterClass);
8352 case X86::ATOMXOR16:
8353 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8354 X86::XOR16ri, X86::MOV16rm,
8355 X86::LCMPXCHG16, X86::MOV16rr,
8356 X86::NOT16r, X86::AX,
8357 X86::GR16RegisterClass);
8358 case X86::ATOMNAND16:
8359 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8360 X86::AND16ri, X86::MOV16rm,
8361 X86::LCMPXCHG16, X86::MOV16rr,
8362 X86::NOT16r, X86::AX,
8363 X86::GR16RegisterClass, true);
8364 case X86::ATOMMIN16:
8365 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8366 case X86::ATOMMAX16:
8367 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8368 case X86::ATOMUMIN16:
8369 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8370 case X86::ATOMUMAX16:
8371 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8372
8373 case X86::ATOMAND8:
8374 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8375 X86::AND8ri, X86::MOV8rm,
8376 X86::LCMPXCHG8, X86::MOV8rr,
8377 X86::NOT8r, X86::AL,
8378 X86::GR8RegisterClass);
8379 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008380 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008381 X86::OR8ri, X86::MOV8rm,
8382 X86::LCMPXCHG8, X86::MOV8rr,
8383 X86::NOT8r, X86::AL,
8384 X86::GR8RegisterClass);
8385 case X86::ATOMXOR8:
8386 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8387 X86::XOR8ri, X86::MOV8rm,
8388 X86::LCMPXCHG8, X86::MOV8rr,
8389 X86::NOT8r, X86::AL,
8390 X86::GR8RegisterClass);
8391 case X86::ATOMNAND8:
8392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8393 X86::AND8ri, X86::MOV8rm,
8394 X86::LCMPXCHG8, X86::MOV8rr,
8395 X86::NOT8r, X86::AL,
8396 X86::GR8RegisterClass, true);
8397 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008398 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008399 case X86::ATOMAND64:
8400 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008401 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008402 X86::LCMPXCHG64, X86::MOV64rr,
8403 X86::NOT64r, X86::RAX,
8404 X86::GR64RegisterClass);
8405 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008406 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8407 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008408 X86::LCMPXCHG64, X86::MOV64rr,
8409 X86::NOT64r, X86::RAX,
8410 X86::GR64RegisterClass);
8411 case X86::ATOMXOR64:
8412 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008413 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008414 X86::LCMPXCHG64, X86::MOV64rr,
8415 X86::NOT64r, X86::RAX,
8416 X86::GR64RegisterClass);
8417 case X86::ATOMNAND64:
8418 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8419 X86::AND64ri32, X86::MOV64rm,
8420 X86::LCMPXCHG64, X86::MOV64rr,
8421 X86::NOT64r, X86::RAX,
8422 X86::GR64RegisterClass, true);
8423 case X86::ATOMMIN64:
8424 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8425 case X86::ATOMMAX64:
8426 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8427 case X86::ATOMUMIN64:
8428 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8429 case X86::ATOMUMAX64:
8430 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008431
8432 // This group does 64-bit operations on a 32-bit host.
8433 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008434 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008435 X86::AND32rr, X86::AND32rr,
8436 X86::AND32ri, X86::AND32ri,
8437 false);
8438 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008439 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008440 X86::OR32rr, X86::OR32rr,
8441 X86::OR32ri, X86::OR32ri,
8442 false);
8443 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008444 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008445 X86::XOR32rr, X86::XOR32rr,
8446 X86::XOR32ri, X86::XOR32ri,
8447 false);
8448 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008449 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008450 X86::AND32rr, X86::AND32rr,
8451 X86::AND32ri, X86::AND32ri,
8452 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008453 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008454 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008455 X86::ADD32rr, X86::ADC32rr,
8456 X86::ADD32ri, X86::ADC32ri,
8457 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008458 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008459 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008460 X86::SUB32rr, X86::SBB32rr,
8461 X86::SUB32ri, X86::SBB32ri,
8462 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008463 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008464 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008465 X86::MOV32rr, X86::MOV32rr,
8466 X86::MOV32ri, X86::MOV32ri,
8467 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008468 case X86::VASTART_SAVE_XMM_REGS:
8469 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008470 }
8471}
8472
8473//===----------------------------------------------------------------------===//
8474// X86 Optimization Hooks
8475//===----------------------------------------------------------------------===//
8476
Dan Gohman475871a2008-07-27 21:46:04 +00008477void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008478 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008479 APInt &KnownZero,
8480 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008481 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008482 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008483 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008484 assert((Opc >= ISD::BUILTIN_OP_END ||
8485 Opc == ISD::INTRINSIC_WO_CHAIN ||
8486 Opc == ISD::INTRINSIC_W_CHAIN ||
8487 Opc == ISD::INTRINSIC_VOID) &&
8488 "Should use MaskedValueIsZero if you don't know whether Op"
8489 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008490
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008491 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008492 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008493 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008494 case X86ISD::ADD:
8495 case X86ISD::SUB:
8496 case X86ISD::SMUL:
8497 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008498 case X86ISD::INC:
8499 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008500 case X86ISD::OR:
8501 case X86ISD::XOR:
8502 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008503 // These nodes' second result is a boolean.
8504 if (Op.getResNo() == 0)
8505 break;
8506 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008507 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008508 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8509 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008510 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008511 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008512}
Chris Lattner259e97c2006-01-31 19:43:35 +00008513
Evan Cheng206ee9d2006-07-07 08:33:52 +00008514/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008515/// node is a GlobalAddress + offset.
8516bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8517 GlobalValue* &GA, int64_t &Offset) const{
8518 if (N->getOpcode() == X86ISD::Wrapper) {
8519 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008520 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008521 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008522 return true;
8523 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008524 }
Evan Chengad4196b2008-05-12 19:56:52 +00008525 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008526}
8527
Nate Begeman9008ca62009-04-27 18:41:29 +00008528static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008529 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008530 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008531 SelectionDAG &DAG, MachineFrameInfo *MFI,
8532 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008533 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008534 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008535 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008536 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008537 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008538 return false;
8539 continue;
8540 }
8541
Dan Gohman475871a2008-07-27 21:46:04 +00008542 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008543 if (!Elt.getNode() ||
8544 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008545 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008546 if (!LDBase) {
8547 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008548 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008549 LDBase = cast<LoadSDNode>(Elt.getNode());
8550 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008551 continue;
8552 }
8553 if (Elt.getOpcode() == ISD::UNDEF)
8554 continue;
8555
Nate Begemanabc01992009-06-05 21:37:30 +00008556 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008557 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008558 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008559 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008560 }
8561 return true;
8562}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008563
8564/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8565/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8566/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008567/// order. In the case of v2i64, it will see if it can rewrite the
8568/// shuffle to be an appropriate build vector so it can take advantage of
8569// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008570static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008571 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008572 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008573 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008574 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008575 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8576 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008577
Eli Friedman7a5e5552009-06-07 06:52:44 +00008578 if (VT.getSizeInBits() != 128)
8579 return SDValue();
8580
Mon P Wang1e955802009-04-03 02:43:30 +00008581 // Try to combine a vector_shuffle into a 128-bit load.
8582 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008583 LoadSDNode *LD = NULL;
8584 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008585 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008586 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008587 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008588
Eli Friedman7a5e5552009-06-07 06:52:44 +00008589 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008590 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008591 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8592 LD->getSrcValue(), LD->getSrcValueOffset(),
8593 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008594 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008595 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008596 LD->isVolatile(), LD->getAlignment());
8597 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008598 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008599 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8600 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008601 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8602 }
8603 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008604}
Evan Chengd880b972008-05-09 21:53:03 +00008605
Chris Lattner83e6c992006-10-04 06:57:07 +00008606/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008607static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008608 const X86Subtarget *Subtarget) {
8609 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008610 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008611 // Get the LHS/RHS of the select.
8612 SDValue LHS = N->getOperand(1);
8613 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008614
Dan Gohman670e5392009-09-21 18:03:22 +00008615 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8616 // instructions have the peculiarity that if either operand is a NaN,
8617 // they chose what we call the RHS operand (and as such are not symmetric).
8618 // It happens that this matches the semantics of the common C idiom
8619 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008620 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008622 Cond.getOpcode() == ISD::SETCC) {
8623 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008624
Chris Lattner47b4ce82009-03-11 05:48:52 +00008625 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008626 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008627 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8628 switch (CC) {
8629 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008630 case ISD::SETULT:
8631 // This can be a min if we can prove that at least one of the operands
8632 // is not a nan.
8633 if (!FiniteOnlyFPMath()) {
8634 if (DAG.isKnownNeverNaN(RHS)) {
8635 // Put the potential NaN in the RHS so that SSE will preserve it.
8636 std::swap(LHS, RHS);
8637 } else if (!DAG.isKnownNeverNaN(LHS))
8638 break;
8639 }
8640 Opcode = X86ISD::FMIN;
8641 break;
8642 case ISD::SETOLE:
8643 // This can be a min if we can prove that at least one of the operands
8644 // is not a nan.
8645 if (!FiniteOnlyFPMath()) {
8646 if (DAG.isKnownNeverNaN(LHS)) {
8647 // Put the potential NaN in the RHS so that SSE will preserve it.
8648 std::swap(LHS, RHS);
8649 } else if (!DAG.isKnownNeverNaN(RHS))
8650 break;
8651 }
8652 Opcode = X86ISD::FMIN;
8653 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008654 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008655 // This can be a min, but if either operand is a NaN we need it to
8656 // preserve the original LHS.
8657 std::swap(LHS, RHS);
8658 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008659 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008660 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008661 Opcode = X86ISD::FMIN;
8662 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008663
Dan Gohman670e5392009-09-21 18:03:22 +00008664 case ISD::SETOGE:
8665 // This can be a max if we can prove that at least one of the operands
8666 // is not a nan.
8667 if (!FiniteOnlyFPMath()) {
8668 if (DAG.isKnownNeverNaN(LHS)) {
8669 // Put the potential NaN in the RHS so that SSE will preserve it.
8670 std::swap(LHS, RHS);
8671 } else if (!DAG.isKnownNeverNaN(RHS))
8672 break;
8673 }
8674 Opcode = X86ISD::FMAX;
8675 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008676 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008677 // This can be a max if we can prove that at least one of the operands
8678 // is not a nan.
8679 if (!FiniteOnlyFPMath()) {
8680 if (DAG.isKnownNeverNaN(RHS)) {
8681 // Put the potential NaN in the RHS so that SSE will preserve it.
8682 std::swap(LHS, RHS);
8683 } else if (!DAG.isKnownNeverNaN(LHS))
8684 break;
8685 }
8686 Opcode = X86ISD::FMAX;
8687 break;
8688 case ISD::SETUGE:
8689 // This can be a max, but if either operand is a NaN we need it to
8690 // preserve the original LHS.
8691 std::swap(LHS, RHS);
8692 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008693 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008694 case ISD::SETGE:
8695 Opcode = X86ISD::FMAX;
8696 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008697 }
Dan Gohman670e5392009-09-21 18:03:22 +00008698 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008699 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8700 switch (CC) {
8701 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008702 case ISD::SETOGE:
8703 // This can be a min if we can prove that at least one of the operands
8704 // is not a nan.
8705 if (!FiniteOnlyFPMath()) {
8706 if (DAG.isKnownNeverNaN(RHS)) {
8707 // Put the potential NaN in the RHS so that SSE will preserve it.
8708 std::swap(LHS, RHS);
8709 } else if (!DAG.isKnownNeverNaN(LHS))
8710 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008711 }
Dan Gohman670e5392009-09-21 18:03:22 +00008712 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008713 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008714 case ISD::SETUGT:
8715 // This can be a min if we can prove that at least one of the operands
8716 // is not a nan.
8717 if (!FiniteOnlyFPMath()) {
8718 if (DAG.isKnownNeverNaN(LHS)) {
8719 // Put the potential NaN in the RHS so that SSE will preserve it.
8720 std::swap(LHS, RHS);
8721 } else if (!DAG.isKnownNeverNaN(RHS))
8722 break;
8723 }
8724 Opcode = X86ISD::FMIN;
8725 break;
8726 case ISD::SETUGE:
8727 // This can be a min, but if either operand is a NaN we need it to
8728 // preserve the original LHS.
8729 std::swap(LHS, RHS);
8730 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008731 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008732 case ISD::SETGE:
8733 Opcode = X86ISD::FMIN;
8734 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008735
Dan Gohman670e5392009-09-21 18:03:22 +00008736 case ISD::SETULT:
8737 // This can be a max if we can prove that at least one of the operands
8738 // is not a nan.
8739 if (!FiniteOnlyFPMath()) {
8740 if (DAG.isKnownNeverNaN(LHS)) {
8741 // Put the potential NaN in the RHS so that SSE will preserve it.
8742 std::swap(LHS, RHS);
8743 } else if (!DAG.isKnownNeverNaN(RHS))
8744 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008745 }
Dan Gohman670e5392009-09-21 18:03:22 +00008746 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008747 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008748 case ISD::SETOLE:
8749 // This can be a max if we can prove that at least one of the operands
8750 // is not a nan.
8751 if (!FiniteOnlyFPMath()) {
8752 if (DAG.isKnownNeverNaN(RHS)) {
8753 // Put the potential NaN in the RHS so that SSE will preserve it.
8754 std::swap(LHS, RHS);
8755 } else if (!DAG.isKnownNeverNaN(LHS))
8756 break;
8757 }
8758 Opcode = X86ISD::FMAX;
8759 break;
8760 case ISD::SETULE:
8761 // This can be a max, but if either operand is a NaN we need it to
8762 // preserve the original LHS.
8763 std::swap(LHS, RHS);
8764 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008765 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008766 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008767 Opcode = X86ISD::FMAX;
8768 break;
8769 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008770 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008771
Chris Lattner47b4ce82009-03-11 05:48:52 +00008772 if (Opcode)
8773 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008774 }
Eric Christopherfd179292009-08-27 18:07:15 +00008775
Chris Lattnerd1980a52009-03-12 06:52:53 +00008776 // If this is a select between two integer constants, try to do some
8777 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008778 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8779 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008780 // Don't do this for crazy integer types.
8781 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8782 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008783 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008784 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008785
Chris Lattnercee56e72009-03-13 05:53:31 +00008786 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008787 // Efficiently invertible.
8788 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8789 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8790 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8791 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008792 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008793 }
Eric Christopherfd179292009-08-27 18:07:15 +00008794
Chris Lattnerd1980a52009-03-12 06:52:53 +00008795 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008796 if (FalseC->getAPIntValue() == 0 &&
8797 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008798 if (NeedsCondInvert) // Invert the condition if needed.
8799 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8800 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008801
Chris Lattnerd1980a52009-03-12 06:52:53 +00008802 // Zero extend the condition if needed.
8803 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008804
Chris Lattnercee56e72009-03-13 05:53:31 +00008805 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008806 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008807 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008808 }
Eric Christopherfd179292009-08-27 18:07:15 +00008809
Chris Lattner97a29a52009-03-13 05:22:11 +00008810 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008811 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008812 if (NeedsCondInvert) // Invert the condition if needed.
8813 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8814 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008815
Chris Lattner97a29a52009-03-13 05:22:11 +00008816 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008817 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8818 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008819 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008820 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008821 }
Eric Christopherfd179292009-08-27 18:07:15 +00008822
Chris Lattnercee56e72009-03-13 05:53:31 +00008823 // Optimize cases that will turn into an LEA instruction. This requires
8824 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008825 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008826 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008827 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008828
Chris Lattnercee56e72009-03-13 05:53:31 +00008829 bool isFastMultiplier = false;
8830 if (Diff < 10) {
8831 switch ((unsigned char)Diff) {
8832 default: break;
8833 case 1: // result = add base, cond
8834 case 2: // result = lea base( , cond*2)
8835 case 3: // result = lea base(cond, cond*2)
8836 case 4: // result = lea base( , cond*4)
8837 case 5: // result = lea base(cond, cond*4)
8838 case 8: // result = lea base( , cond*8)
8839 case 9: // result = lea base(cond, cond*8)
8840 isFastMultiplier = true;
8841 break;
8842 }
8843 }
Eric Christopherfd179292009-08-27 18:07:15 +00008844
Chris Lattnercee56e72009-03-13 05:53:31 +00008845 if (isFastMultiplier) {
8846 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8847 if (NeedsCondInvert) // Invert the condition if needed.
8848 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8849 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008850
Chris Lattnercee56e72009-03-13 05:53:31 +00008851 // Zero extend the condition if needed.
8852 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8853 Cond);
8854 // Scale the condition by the difference.
8855 if (Diff != 1)
8856 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8857 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008858
Chris Lattnercee56e72009-03-13 05:53:31 +00008859 // Add the base if non-zero.
8860 if (FalseC->getAPIntValue() != 0)
8861 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8862 SDValue(FalseC, 0));
8863 return Cond;
8864 }
Eric Christopherfd179292009-08-27 18:07:15 +00008865 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008866 }
8867 }
Eric Christopherfd179292009-08-27 18:07:15 +00008868
Dan Gohman475871a2008-07-27 21:46:04 +00008869 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008870}
8871
Chris Lattnerd1980a52009-03-12 06:52:53 +00008872/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8873static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8874 TargetLowering::DAGCombinerInfo &DCI) {
8875 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008876
Chris Lattnerd1980a52009-03-12 06:52:53 +00008877 // If the flag operand isn't dead, don't touch this CMOV.
8878 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8879 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008880
Chris Lattnerd1980a52009-03-12 06:52:53 +00008881 // If this is a select between two integer constants, try to do some
8882 // optimizations. Note that the operands are ordered the opposite of SELECT
8883 // operands.
8884 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8885 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8886 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8887 // larger than FalseC (the false value).
8888 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008889
Chris Lattnerd1980a52009-03-12 06:52:53 +00008890 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8891 CC = X86::GetOppositeBranchCondition(CC);
8892 std::swap(TrueC, FalseC);
8893 }
Eric Christopherfd179292009-08-27 18:07:15 +00008894
Chris Lattnerd1980a52009-03-12 06:52:53 +00008895 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008896 // This is efficient for any integer data type (including i8/i16) and
8897 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008898 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8899 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8901 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008902
Chris Lattnerd1980a52009-03-12 06:52:53 +00008903 // Zero extend the condition if needed.
8904 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008905
Chris Lattnerd1980a52009-03-12 06:52:53 +00008906 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8907 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008908 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008909 if (N->getNumValues() == 2) // Dead flag value?
8910 return DCI.CombineTo(N, Cond, SDValue());
8911 return Cond;
8912 }
Eric Christopherfd179292009-08-27 18:07:15 +00008913
Chris Lattnercee56e72009-03-13 05:53:31 +00008914 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8915 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008916 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8917 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008918 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8919 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008920
Chris Lattner97a29a52009-03-13 05:22:11 +00008921 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008922 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8923 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008924 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8925 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008926
Chris Lattner97a29a52009-03-13 05:22:11 +00008927 if (N->getNumValues() == 2) // Dead flag value?
8928 return DCI.CombineTo(N, Cond, SDValue());
8929 return Cond;
8930 }
Eric Christopherfd179292009-08-27 18:07:15 +00008931
Chris Lattnercee56e72009-03-13 05:53:31 +00008932 // Optimize cases that will turn into an LEA instruction. This requires
8933 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008934 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008935 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008936 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008937
Chris Lattnercee56e72009-03-13 05:53:31 +00008938 bool isFastMultiplier = false;
8939 if (Diff < 10) {
8940 switch ((unsigned char)Diff) {
8941 default: break;
8942 case 1: // result = add base, cond
8943 case 2: // result = lea base( , cond*2)
8944 case 3: // result = lea base(cond, cond*2)
8945 case 4: // result = lea base( , cond*4)
8946 case 5: // result = lea base(cond, cond*4)
8947 case 8: // result = lea base( , cond*8)
8948 case 9: // result = lea base(cond, cond*8)
8949 isFastMultiplier = true;
8950 break;
8951 }
8952 }
Eric Christopherfd179292009-08-27 18:07:15 +00008953
Chris Lattnercee56e72009-03-13 05:53:31 +00008954 if (isFastMultiplier) {
8955 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8956 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008957 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8958 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00008959 // Zero extend the condition if needed.
8960 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8961 Cond);
8962 // Scale the condition by the difference.
8963 if (Diff != 1)
8964 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8965 DAG.getConstant(Diff, Cond.getValueType()));
8966
8967 // Add the base if non-zero.
8968 if (FalseC->getAPIntValue() != 0)
8969 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8970 SDValue(FalseC, 0));
8971 if (N->getNumValues() == 2) // Dead flag value?
8972 return DCI.CombineTo(N, Cond, SDValue());
8973 return Cond;
8974 }
Eric Christopherfd179292009-08-27 18:07:15 +00008975 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008976 }
8977 }
8978 return SDValue();
8979}
8980
8981
Evan Cheng0b0cd912009-03-28 05:57:29 +00008982/// PerformMulCombine - Optimize a single multiply with constant into two
8983/// in order to implement it with two cheaper instructions, e.g.
8984/// LEA + SHL, LEA + LEA.
8985static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8986 TargetLowering::DAGCombinerInfo &DCI) {
8987 if (DAG.getMachineFunction().
8988 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8989 return SDValue();
8990
8991 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8992 return SDValue();
8993
Owen Andersone50ed302009-08-10 22:56:29 +00008994 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008995 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00008996 return SDValue();
8997
8998 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8999 if (!C)
9000 return SDValue();
9001 uint64_t MulAmt = C->getZExtValue();
9002 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9003 return SDValue();
9004
9005 uint64_t MulAmt1 = 0;
9006 uint64_t MulAmt2 = 0;
9007 if ((MulAmt % 9) == 0) {
9008 MulAmt1 = 9;
9009 MulAmt2 = MulAmt / 9;
9010 } else if ((MulAmt % 5) == 0) {
9011 MulAmt1 = 5;
9012 MulAmt2 = MulAmt / 5;
9013 } else if ((MulAmt % 3) == 0) {
9014 MulAmt1 = 3;
9015 MulAmt2 = MulAmt / 3;
9016 }
9017 if (MulAmt2 &&
9018 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9019 DebugLoc DL = N->getDebugLoc();
9020
9021 if (isPowerOf2_64(MulAmt2) &&
9022 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9023 // If second multiplifer is pow2, issue it first. We want the multiply by
9024 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9025 // is an add.
9026 std::swap(MulAmt1, MulAmt2);
9027
9028 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009029 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009030 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009031 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009032 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009033 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009034 DAG.getConstant(MulAmt1, VT));
9035
Eric Christopherfd179292009-08-27 18:07:15 +00009036 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009037 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009038 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009039 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009040 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009041 DAG.getConstant(MulAmt2, VT));
9042
9043 // Do not add new nodes to DAG combiner worklist.
9044 DCI.CombineTo(N, NewMul, false);
9045 }
9046 return SDValue();
9047}
9048
Evan Chengad9c0a32009-12-15 00:53:42 +00009049static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9050 SDValue N0 = N->getOperand(0);
9051 SDValue N1 = N->getOperand(1);
9052 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9053 EVT VT = N0.getValueType();
9054
9055 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9056 // since the result of setcc_c is all zero's or all ones.
9057 if (N1C && N0.getOpcode() == ISD::AND &&
9058 N0.getOperand(1).getOpcode() == ISD::Constant) {
9059 SDValue N00 = N0.getOperand(0);
9060 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9061 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9062 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9063 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9064 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9065 APInt ShAmt = N1C->getAPIntValue();
9066 Mask = Mask.shl(ShAmt);
9067 if (Mask != 0)
9068 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9069 N00, DAG.getConstant(Mask, VT));
9070 }
9071 }
9072
9073 return SDValue();
9074}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009075
Nate Begeman740ab032009-01-26 00:52:55 +00009076/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9077/// when possible.
9078static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9079 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009080 EVT VT = N->getValueType(0);
9081 if (!VT.isVector() && VT.isInteger() &&
9082 N->getOpcode() == ISD::SHL)
9083 return PerformSHLCombine(N, DAG);
9084
Nate Begeman740ab032009-01-26 00:52:55 +00009085 // On X86 with SSE2 support, we can transform this to a vector shift if
9086 // all elements are shifted by the same amount. We can't do this in legalize
9087 // because the a constant vector is typically transformed to a constant pool
9088 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009089 if (!Subtarget->hasSSE2())
9090 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009091
Owen Anderson825b72b2009-08-11 20:47:22 +00009092 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009093 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009094
Mon P Wang3becd092009-01-28 08:12:05 +00009095 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009096 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009097 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009098 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009099 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9100 unsigned NumElts = VT.getVectorNumElements();
9101 unsigned i = 0;
9102 for (; i != NumElts; ++i) {
9103 SDValue Arg = ShAmtOp.getOperand(i);
9104 if (Arg.getOpcode() == ISD::UNDEF) continue;
9105 BaseShAmt = Arg;
9106 break;
9107 }
9108 for (; i != NumElts; ++i) {
9109 SDValue Arg = ShAmtOp.getOperand(i);
9110 if (Arg.getOpcode() == ISD::UNDEF) continue;
9111 if (Arg != BaseShAmt) {
9112 return SDValue();
9113 }
9114 }
9115 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009116 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009117 SDValue InVec = ShAmtOp.getOperand(0);
9118 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9119 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9120 unsigned i = 0;
9121 for (; i != NumElts; ++i) {
9122 SDValue Arg = InVec.getOperand(i);
9123 if (Arg.getOpcode() == ISD::UNDEF) continue;
9124 BaseShAmt = Arg;
9125 break;
9126 }
9127 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9128 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9129 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9130 if (C->getZExtValue() == SplatIdx)
9131 BaseShAmt = InVec.getOperand(1);
9132 }
9133 }
9134 if (BaseShAmt.getNode() == 0)
9135 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9136 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009137 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009138 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009139
Mon P Wangefa42202009-09-03 19:56:25 +00009140 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009141 if (EltVT.bitsGT(MVT::i32))
9142 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9143 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009144 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009145
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009146 // The shift amount is identical so we can do a vector shift.
9147 SDValue ValOp = N->getOperand(0);
9148 switch (N->getOpcode()) {
9149 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009150 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009151 break;
9152 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009153 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009154 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009156 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009157 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009158 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009159 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009160 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009162 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009163 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009164 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009165 break;
9166 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009167 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009168 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009170 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009171 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009172 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009173 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009174 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009175 break;
9176 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009177 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009178 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009179 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009180 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009181 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009182 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009183 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009184 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009185 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009186 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009187 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009188 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009189 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009190 }
9191 return SDValue();
9192}
9193
Evan Cheng760d1942010-01-04 21:22:48 +00009194static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9195 const X86Subtarget *Subtarget) {
9196 EVT VT = N->getValueType(0);
9197 if (VT != MVT::i64 || !Subtarget->is64Bit())
9198 return SDValue();
9199
9200 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9201 SDValue N0 = N->getOperand(0);
9202 SDValue N1 = N->getOperand(1);
9203 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9204 std::swap(N0, N1);
9205 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9206 return SDValue();
9207
9208 SDValue ShAmt0 = N0.getOperand(1);
9209 if (ShAmt0.getValueType() != MVT::i8)
9210 return SDValue();
9211 SDValue ShAmt1 = N1.getOperand(1);
9212 if (ShAmt1.getValueType() != MVT::i8)
9213 return SDValue();
9214 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9215 ShAmt0 = ShAmt0.getOperand(0);
9216 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9217 ShAmt1 = ShAmt1.getOperand(0);
9218
9219 DebugLoc DL = N->getDebugLoc();
9220 unsigned Opc = X86ISD::SHLD;
9221 SDValue Op0 = N0.getOperand(0);
9222 SDValue Op1 = N1.getOperand(0);
9223 if (ShAmt0.getOpcode() == ISD::SUB) {
9224 Opc = X86ISD::SHRD;
9225 std::swap(Op0, Op1);
9226 std::swap(ShAmt0, ShAmt1);
9227 }
9228
9229 if (ShAmt1.getOpcode() == ISD::SUB) {
9230 SDValue Sum = ShAmt1.getOperand(0);
9231 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9232 if (SumC->getSExtValue() == 64 &&
9233 ShAmt1.getOperand(1) == ShAmt0)
9234 return DAG.getNode(Opc, DL, VT,
9235 Op0, Op1,
9236 DAG.getNode(ISD::TRUNCATE, DL,
9237 MVT::i8, ShAmt0));
9238 }
9239 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9240 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9241 if (ShAmt0C &&
9242 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9243 return DAG.getNode(Opc, DL, VT,
9244 N0.getOperand(0), N1.getOperand(0),
9245 DAG.getNode(ISD::TRUNCATE, DL,
9246 MVT::i8, ShAmt0));
9247 }
9248
9249 return SDValue();
9250}
9251
Chris Lattner149a4e52008-02-22 02:09:43 +00009252/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009253static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009254 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009255 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9256 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009257 // A preferable solution to the general problem is to figure out the right
9258 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009259
9260 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009261 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009262 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009263 if (VT.getSizeInBits() != 64)
9264 return SDValue();
9265
Devang Patel578efa92009-06-05 21:57:13 +00009266 const Function *F = DAG.getMachineFunction().getFunction();
9267 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009268 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009269 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009270 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009271 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009272 isa<LoadSDNode>(St->getValue()) &&
9273 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9274 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009275 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009276 LoadSDNode *Ld = 0;
9277 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009278 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009279 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009280 // Must be a store of a load. We currently handle two cases: the load
9281 // is a direct child, and it's under an intervening TokenFactor. It is
9282 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009283 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009284 Ld = cast<LoadSDNode>(St->getChain());
9285 else if (St->getValue().hasOneUse() &&
9286 ChainVal->getOpcode() == ISD::TokenFactor) {
9287 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009288 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009289 TokenFactorIndex = i;
9290 Ld = cast<LoadSDNode>(St->getValue());
9291 } else
9292 Ops.push_back(ChainVal->getOperand(i));
9293 }
9294 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009295
Evan Cheng536e6672009-03-12 05:59:15 +00009296 if (!Ld || !ISD::isNormalLoad(Ld))
9297 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009298
Evan Cheng536e6672009-03-12 05:59:15 +00009299 // If this is not the MMX case, i.e. we are just turning i64 load/store
9300 // into f64 load/store, avoid the transformation if there are multiple
9301 // uses of the loaded value.
9302 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9303 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009304
Evan Cheng536e6672009-03-12 05:59:15 +00009305 DebugLoc LdDL = Ld->getDebugLoc();
9306 DebugLoc StDL = N->getDebugLoc();
9307 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9308 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9309 // pair instead.
9310 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009311 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009312 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9313 Ld->getBasePtr(), Ld->getSrcValue(),
9314 Ld->getSrcValueOffset(), Ld->isVolatile(),
9315 Ld->getAlignment());
9316 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009317 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009318 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009319 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009320 Ops.size());
9321 }
Evan Cheng536e6672009-03-12 05:59:15 +00009322 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009323 St->getSrcValue(), St->getSrcValueOffset(),
9324 St->isVolatile(), St->getAlignment());
9325 }
Evan Cheng536e6672009-03-12 05:59:15 +00009326
9327 // Otherwise, lower to two pairs of 32-bit loads / stores.
9328 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009329 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9330 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009331
Owen Anderson825b72b2009-08-11 20:47:22 +00009332 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009333 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9334 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009335 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009336 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9337 Ld->isVolatile(),
9338 MinAlign(Ld->getAlignment(), 4));
9339
9340 SDValue NewChain = LoLd.getValue(1);
9341 if (TokenFactorIndex != -1) {
9342 Ops.push_back(LoLd);
9343 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009344 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009345 Ops.size());
9346 }
9347
9348 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009349 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9350 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009351
9352 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9353 St->getSrcValue(), St->getSrcValueOffset(),
9354 St->isVolatile(), St->getAlignment());
9355 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9356 St->getSrcValue(),
9357 St->getSrcValueOffset() + 4,
9358 St->isVolatile(),
9359 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009360 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009361 }
Dan Gohman475871a2008-07-27 21:46:04 +00009362 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009363}
9364
Chris Lattner6cf73262008-01-25 06:14:17 +00009365/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9366/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009367static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009368 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9369 // F[X]OR(0.0, x) -> x
9370 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009371 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9372 if (C->getValueAPF().isPosZero())
9373 return N->getOperand(1);
9374 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9375 if (C->getValueAPF().isPosZero())
9376 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009377 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009378}
9379
9380/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009381static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009382 // FAND(0.0, x) -> 0.0
9383 // FAND(x, 0.0) -> 0.0
9384 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9385 if (C->getValueAPF().isPosZero())
9386 return N->getOperand(0);
9387 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9388 if (C->getValueAPF().isPosZero())
9389 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009390 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009391}
9392
Dan Gohmane5af2d32009-01-29 01:59:02 +00009393static SDValue PerformBTCombine(SDNode *N,
9394 SelectionDAG &DAG,
9395 TargetLowering::DAGCombinerInfo &DCI) {
9396 // BT ignores high bits in the bit index operand.
9397 SDValue Op1 = N->getOperand(1);
9398 if (Op1.hasOneUse()) {
9399 unsigned BitWidth = Op1.getValueSizeInBits();
9400 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9401 APInt KnownZero, KnownOne;
9402 TargetLowering::TargetLoweringOpt TLO(DAG);
9403 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9404 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9405 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9406 DCI.CommitTargetLoweringOpt(TLO);
9407 }
9408 return SDValue();
9409}
Chris Lattner83e6c992006-10-04 06:57:07 +00009410
Eli Friedman7a5e5552009-06-07 06:52:44 +00009411static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9412 SDValue Op = N->getOperand(0);
9413 if (Op.getOpcode() == ISD::BIT_CONVERT)
9414 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009415 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009416 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009417 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009418 OpVT.getVectorElementType().getSizeInBits()) {
9419 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9420 }
9421 return SDValue();
9422}
9423
Owen Anderson99177002009-06-29 18:04:45 +00009424// On X86 and X86-64, atomic operations are lowered to locked instructions.
9425// Locked instructions, in turn, have implicit fence semantics (all memory
9426// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009427// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009428// fence-atomic-fence.
9429static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9430 SDValue atomic = N->getOperand(0);
9431 switch (atomic.getOpcode()) {
9432 case ISD::ATOMIC_CMP_SWAP:
9433 case ISD::ATOMIC_SWAP:
9434 case ISD::ATOMIC_LOAD_ADD:
9435 case ISD::ATOMIC_LOAD_SUB:
9436 case ISD::ATOMIC_LOAD_AND:
9437 case ISD::ATOMIC_LOAD_OR:
9438 case ISD::ATOMIC_LOAD_XOR:
9439 case ISD::ATOMIC_LOAD_NAND:
9440 case ISD::ATOMIC_LOAD_MIN:
9441 case ISD::ATOMIC_LOAD_MAX:
9442 case ISD::ATOMIC_LOAD_UMIN:
9443 case ISD::ATOMIC_LOAD_UMAX:
9444 break;
9445 default:
9446 return SDValue();
9447 }
Eric Christopherfd179292009-08-27 18:07:15 +00009448
Owen Anderson99177002009-06-29 18:04:45 +00009449 SDValue fence = atomic.getOperand(0);
9450 if (fence.getOpcode() != ISD::MEMBARRIER)
9451 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009452
Owen Anderson99177002009-06-29 18:04:45 +00009453 switch (atomic.getOpcode()) {
9454 case ISD::ATOMIC_CMP_SWAP:
9455 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9456 atomic.getOperand(1), atomic.getOperand(2),
9457 atomic.getOperand(3));
9458 case ISD::ATOMIC_SWAP:
9459 case ISD::ATOMIC_LOAD_ADD:
9460 case ISD::ATOMIC_LOAD_SUB:
9461 case ISD::ATOMIC_LOAD_AND:
9462 case ISD::ATOMIC_LOAD_OR:
9463 case ISD::ATOMIC_LOAD_XOR:
9464 case ISD::ATOMIC_LOAD_NAND:
9465 case ISD::ATOMIC_LOAD_MIN:
9466 case ISD::ATOMIC_LOAD_MAX:
9467 case ISD::ATOMIC_LOAD_UMIN:
9468 case ISD::ATOMIC_LOAD_UMAX:
9469 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9470 atomic.getOperand(1), atomic.getOperand(2));
9471 default:
9472 return SDValue();
9473 }
9474}
9475
Evan Cheng2e489c42009-12-16 00:53:11 +00009476static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9477 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9478 // (and (i32 x86isd::setcc_carry), 1)
9479 // This eliminates the zext. This transformation is necessary because
9480 // ISD::SETCC is always legalized to i8.
9481 DebugLoc dl = N->getDebugLoc();
9482 SDValue N0 = N->getOperand(0);
9483 EVT VT = N->getValueType(0);
9484 if (N0.getOpcode() == ISD::AND &&
9485 N0.hasOneUse() &&
9486 N0.getOperand(0).hasOneUse()) {
9487 SDValue N00 = N0.getOperand(0);
9488 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9489 return SDValue();
9490 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9491 if (!C || C->getZExtValue() != 1)
9492 return SDValue();
9493 return DAG.getNode(ISD::AND, dl, VT,
9494 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9495 N00.getOperand(0), N00.getOperand(1)),
9496 DAG.getConstant(1, VT));
9497 }
9498
9499 return SDValue();
9500}
9501
Dan Gohman475871a2008-07-27 21:46:04 +00009502SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009503 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009504 SelectionDAG &DAG = DCI.DAG;
9505 switch (N->getOpcode()) {
9506 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009507 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009508 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009509 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009510 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009511 case ISD::SHL:
9512 case ISD::SRA:
9513 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009514 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009515 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009516 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009517 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9518 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009519 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009520 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009521 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009522 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009523 }
9524
Dan Gohman475871a2008-07-27 21:46:04 +00009525 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009526}
9527
Evan Cheng60c07e12006-07-05 22:17:51 +00009528//===----------------------------------------------------------------------===//
9529// X86 Inline Assembly Support
9530//===----------------------------------------------------------------------===//
9531
Chris Lattnerb8105652009-07-20 17:51:36 +00009532static bool LowerToBSwap(CallInst *CI) {
9533 // FIXME: this should verify that we are targetting a 486 or better. If not,
9534 // we will turn this bswap into something that will be lowered to logical ops
9535 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9536 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009537
Chris Lattnerb8105652009-07-20 17:51:36 +00009538 // Verify this is a simple bswap.
9539 if (CI->getNumOperands() != 2 ||
9540 CI->getType() != CI->getOperand(1)->getType() ||
9541 !CI->getType()->isInteger())
9542 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009543
Chris Lattnerb8105652009-07-20 17:51:36 +00009544 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9545 if (!Ty || Ty->getBitWidth() % 16 != 0)
9546 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009547
Chris Lattnerb8105652009-07-20 17:51:36 +00009548 // Okay, we can do this xform, do so now.
9549 const Type *Tys[] = { Ty };
9550 Module *M = CI->getParent()->getParent()->getParent();
9551 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009552
Chris Lattnerb8105652009-07-20 17:51:36 +00009553 Value *Op = CI->getOperand(1);
9554 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009555
Chris Lattnerb8105652009-07-20 17:51:36 +00009556 CI->replaceAllUsesWith(Op);
9557 CI->eraseFromParent();
9558 return true;
9559}
9560
9561bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9562 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9563 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9564
9565 std::string AsmStr = IA->getAsmString();
9566
9567 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009568 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009569 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9570
9571 switch (AsmPieces.size()) {
9572 default: return false;
9573 case 1:
9574 AsmStr = AsmPieces[0];
9575 AsmPieces.clear();
9576 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9577
9578 // bswap $0
9579 if (AsmPieces.size() == 2 &&
9580 (AsmPieces[0] == "bswap" ||
9581 AsmPieces[0] == "bswapq" ||
9582 AsmPieces[0] == "bswapl") &&
9583 (AsmPieces[1] == "$0" ||
9584 AsmPieces[1] == "${0:q}")) {
9585 // No need to check constraints, nothing other than the equivalent of
9586 // "=r,0" would be valid here.
9587 return LowerToBSwap(CI);
9588 }
9589 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009590 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009591 AsmPieces.size() == 3 &&
9592 AsmPieces[0] == "rorw" &&
9593 AsmPieces[1] == "$$8," &&
9594 AsmPieces[2] == "${0:w}" &&
9595 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9596 return LowerToBSwap(CI);
9597 }
9598 break;
9599 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009600 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009601 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009602 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9603 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9604 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009605 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009606 SplitString(AsmPieces[0], Words, " \t");
9607 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9608 Words.clear();
9609 SplitString(AsmPieces[1], Words, " \t");
9610 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9611 Words.clear();
9612 SplitString(AsmPieces[2], Words, " \t,");
9613 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9614 Words[2] == "%edx") {
9615 return LowerToBSwap(CI);
9616 }
9617 }
9618 }
9619 }
9620 break;
9621 }
9622 return false;
9623}
9624
9625
9626
Chris Lattnerf4dff842006-07-11 02:54:03 +00009627/// getConstraintType - Given a constraint letter, return the type of
9628/// constraint it is for this target.
9629X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009630X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9631 if (Constraint.size() == 1) {
9632 switch (Constraint[0]) {
9633 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009634 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009635 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009636 case 'r':
9637 case 'R':
9638 case 'l':
9639 case 'q':
9640 case 'Q':
9641 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009642 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009643 case 'Y':
9644 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009645 case 'e':
9646 case 'Z':
9647 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009648 default:
9649 break;
9650 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009651 }
Chris Lattner4234f572007-03-25 02:14:49 +00009652 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009653}
9654
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009655/// LowerXConstraint - try to replace an X constraint, which matches anything,
9656/// with another that has more specific requirements based on the type of the
9657/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009658const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009659LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009660 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9661 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009662 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009663 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009664 return "Y";
9665 if (Subtarget->hasSSE1())
9666 return "x";
9667 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009668
Chris Lattner5e764232008-04-26 23:02:14 +00009669 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009670}
9671
Chris Lattner48884cd2007-08-25 00:47:38 +00009672/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9673/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009674void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009675 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009676 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009677 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009678 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009679 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009680
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009681 switch (Constraint) {
9682 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009683 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009684 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009685 if (C->getZExtValue() <= 31) {
9686 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009687 break;
9688 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009689 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009690 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009691 case 'J':
9692 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009693 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009694 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9695 break;
9696 }
9697 }
9698 return;
9699 case 'K':
9700 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009701 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009702 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9703 break;
9704 }
9705 }
9706 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009707 case 'N':
9708 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009709 if (C->getZExtValue() <= 255) {
9710 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009711 break;
9712 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009713 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009714 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009715 case 'e': {
9716 // 32-bit signed value
9717 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9718 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009719 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9720 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009721 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009722 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009723 break;
9724 }
9725 // FIXME gcc accepts some relocatable values here too, but only in certain
9726 // memory models; it's complicated.
9727 }
9728 return;
9729 }
9730 case 'Z': {
9731 // 32-bit unsigned value
9732 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9733 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009734 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9735 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009736 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9737 break;
9738 }
9739 }
9740 // FIXME gcc accepts some relocatable values here too, but only in certain
9741 // memory models; it's complicated.
9742 return;
9743 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009744 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009745 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009746 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009747 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009748 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009749 break;
9750 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009751
Chris Lattnerdc43a882007-05-03 16:52:29 +00009752 // If we are in non-pic codegen mode, we allow the address of a global (with
9753 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009754 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009755 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Chris Lattner49921962009-05-08 18:23:14 +00009757 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9758 while (1) {
9759 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9760 Offset += GA->getOffset();
9761 break;
9762 } else if (Op.getOpcode() == ISD::ADD) {
9763 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9764 Offset += C->getZExtValue();
9765 Op = Op.getOperand(0);
9766 continue;
9767 }
9768 } else if (Op.getOpcode() == ISD::SUB) {
9769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9770 Offset += -C->getZExtValue();
9771 Op = Op.getOperand(0);
9772 continue;
9773 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009774 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009775
Chris Lattner49921962009-05-08 18:23:14 +00009776 // Otherwise, this isn't something we can handle, reject it.
9777 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009778 }
Eric Christopherfd179292009-08-27 18:07:15 +00009779
Chris Lattner36c25012009-07-10 07:34:39 +00009780 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009781 // If we require an extra load to get this address, as in PIC mode, we
9782 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009783 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9784 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009785 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009786
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009787 if (hasMemory)
9788 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9789 else
9790 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009791 Result = Op;
9792 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009793 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009794 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009795
Gabor Greifba36cb52008-08-28 21:40:38 +00009796 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009797 Ops.push_back(Result);
9798 return;
9799 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009800 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9801 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009802}
9803
Chris Lattner259e97c2006-01-31 19:43:35 +00009804std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009805getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009806 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009807 if (Constraint.size() == 1) {
9808 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009809 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009810 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009811 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9812 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009813 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009814 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9815 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9816 X86::R10D,X86::R11D,X86::R12D,
9817 X86::R13D,X86::R14D,X86::R15D,
9818 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009819 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009820 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9821 X86::SI, X86::DI, X86::R8W,X86::R9W,
9822 X86::R10W,X86::R11W,X86::R12W,
9823 X86::R13W,X86::R14W,X86::R15W,
9824 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009825 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009826 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9827 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9828 X86::R10B,X86::R11B,X86::R12B,
9829 X86::R13B,X86::R14B,X86::R15B,
9830 X86::BPL, X86::SPL, 0);
9831
Owen Anderson825b72b2009-08-11 20:47:22 +00009832 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009833 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9834 X86::RSI, X86::RDI, X86::R8, X86::R9,
9835 X86::R10, X86::R11, X86::R12,
9836 X86::R13, X86::R14, X86::R15,
9837 X86::RBP, X86::RSP, 0);
9838
9839 break;
9840 }
Eric Christopherfd179292009-08-27 18:07:15 +00009841 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009842 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009843 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009844 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009845 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009846 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009847 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009848 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009849 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009850 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9851 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009852 }
9853 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009854
Chris Lattner1efa40f2006-02-22 00:56:39 +00009855 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009856}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009857
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009858std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009859X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009860 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009861 // First, see if this is a constraint that directly corresponds to an LLVM
9862 // register class.
9863 if (Constraint.size() == 1) {
9864 // GCC Constraint Letters
9865 switch (Constraint[0]) {
9866 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009867 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009868 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009869 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009870 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009871 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009872 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009873 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009874 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009875 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009876 case 'R': // LEGACY_REGS
9877 if (VT == MVT::i8)
9878 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9879 if (VT == MVT::i16)
9880 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9881 if (VT == MVT::i32 || !Subtarget->is64Bit())
9882 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9883 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009884 case 'f': // FP Stack registers.
9885 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9886 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009887 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009888 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009889 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009890 return std::make_pair(0U, X86::RFP64RegisterClass);
9891 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009892 case 'y': // MMX_REGS if MMX allowed.
9893 if (!Subtarget->hasMMX()) break;
9894 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009895 case 'Y': // SSE_REGS if SSE2 allowed
9896 if (!Subtarget->hasSSE2()) break;
9897 // FALL THROUGH.
9898 case 'x': // SSE_REGS if SSE1 allowed
9899 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009900
Owen Anderson825b72b2009-08-11 20:47:22 +00009901 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009902 default: break;
9903 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009904 case MVT::f32:
9905 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009906 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009907 case MVT::f64:
9908 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009909 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009910 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009911 case MVT::v16i8:
9912 case MVT::v8i16:
9913 case MVT::v4i32:
9914 case MVT::v2i64:
9915 case MVT::v4f32:
9916 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009917 return std::make_pair(0U, X86::VR128RegisterClass);
9918 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009919 break;
9920 }
9921 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009922
Chris Lattnerf76d1802006-07-31 23:26:50 +00009923 // Use the default implementation in TargetLowering to convert the register
9924 // constraint into a member of a register class.
9925 std::pair<unsigned, const TargetRegisterClass*> Res;
9926 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009927
9928 // Not found as a standard register?
9929 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009930 // Map st(0) -> st(7) -> ST0
9931 if (Constraint.size() == 7 && Constraint[0] == '{' &&
9932 tolower(Constraint[1]) == 's' &&
9933 tolower(Constraint[2]) == 't' &&
9934 Constraint[3] == '(' &&
9935 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
9936 Constraint[5] == ')' &&
9937 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +00009938
Chris Lattner56d77c72009-09-13 22:41:48 +00009939 Res.first = X86::ST0+Constraint[4]-'0';
9940 Res.second = X86::RFP80RegisterClass;
9941 return Res;
9942 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009943
Chris Lattner56d77c72009-09-13 22:41:48 +00009944 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009945 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +00009946 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009947 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009948 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009949 }
Chris Lattner56d77c72009-09-13 22:41:48 +00009950
9951 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009952 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009953 Res.first = X86::EFLAGS;
9954 Res.second = X86::CCRRegisterClass;
9955 return Res;
9956 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009957
Dale Johannesen330169f2008-11-13 21:52:36 +00009958 // 'A' means EAX + EDX.
9959 if (Constraint == "A") {
9960 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009961 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +00009962 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +00009963 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009964 return Res;
9965 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009966
Chris Lattnerf76d1802006-07-31 23:26:50 +00009967 // Otherwise, check to see if this is a register class of the wrong value
9968 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9969 // turn into {ax},{dx}.
9970 if (Res.second->hasType(VT))
9971 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009972
Chris Lattnerf76d1802006-07-31 23:26:50 +00009973 // All of the single-register GCC register classes map their values onto
9974 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9975 // really want an 8-bit or 32-bit register, map to the appropriate register
9976 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009977 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009978 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009979 unsigned DestReg = 0;
9980 switch (Res.first) {
9981 default: break;
9982 case X86::AX: DestReg = X86::AL; break;
9983 case X86::DX: DestReg = X86::DL; break;
9984 case X86::CX: DestReg = X86::CL; break;
9985 case X86::BX: DestReg = X86::BL; break;
9986 }
9987 if (DestReg) {
9988 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009989 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009990 }
Owen Anderson825b72b2009-08-11 20:47:22 +00009991 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +00009992 unsigned DestReg = 0;
9993 switch (Res.first) {
9994 default: break;
9995 case X86::AX: DestReg = X86::EAX; break;
9996 case X86::DX: DestReg = X86::EDX; break;
9997 case X86::CX: DestReg = X86::ECX; break;
9998 case X86::BX: DestReg = X86::EBX; break;
9999 case X86::SI: DestReg = X86::ESI; break;
10000 case X86::DI: DestReg = X86::EDI; break;
10001 case X86::BP: DestReg = X86::EBP; break;
10002 case X86::SP: DestReg = X86::ESP; break;
10003 }
10004 if (DestReg) {
10005 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010006 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010007 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010008 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010009 unsigned DestReg = 0;
10010 switch (Res.first) {
10011 default: break;
10012 case X86::AX: DestReg = X86::RAX; break;
10013 case X86::DX: DestReg = X86::RDX; break;
10014 case X86::CX: DestReg = X86::RCX; break;
10015 case X86::BX: DestReg = X86::RBX; break;
10016 case X86::SI: DestReg = X86::RSI; break;
10017 case X86::DI: DestReg = X86::RDI; break;
10018 case X86::BP: DestReg = X86::RBP; break;
10019 case X86::SP: DestReg = X86::RSP; break;
10020 }
10021 if (DestReg) {
10022 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010023 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010024 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010025 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010026 } else if (Res.second == X86::FR32RegisterClass ||
10027 Res.second == X86::FR64RegisterClass ||
10028 Res.second == X86::VR128RegisterClass) {
10029 // Handle references to XMM physical registers that got mapped into the
10030 // wrong class. This can happen with constraints like {xmm0} where the
10031 // target independent register mapper will just pick the first match it can
10032 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010033 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010034 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010035 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010036 Res.second = X86::FR64RegisterClass;
10037 else if (X86::VR128RegisterClass->hasType(VT))
10038 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010039 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010040
Chris Lattnerf76d1802006-07-31 23:26:50 +000010041 return Res;
10042}
Mon P Wang0c397192008-10-30 08:01:45 +000010043
10044//===----------------------------------------------------------------------===//
10045// X86 Widen vector type
10046//===----------------------------------------------------------------------===//
10047
10048/// getWidenVectorType: given a vector type, returns the type to widen
10049/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010050/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010051/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010052/// scalarizing vs using the wider vector type.
10053
Owen Andersone50ed302009-08-10 22:56:29 +000010054EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010055 assert(VT.isVector());
10056 if (isTypeLegal(VT))
10057 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010058
Mon P Wang0c397192008-10-30 08:01:45 +000010059 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10060 // type based on element type. This would speed up our search (though
10061 // it may not be worth it since the size of the list is relatively
10062 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010063 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010064 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010065
Mon P Wang0c397192008-10-30 08:01:45 +000010066 // On X86, it make sense to widen any vector wider than 1
10067 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010068 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010069
Owen Anderson825b72b2009-08-11 20:47:22 +000010070 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10071 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10072 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010073
10074 if (isTypeLegal(SVT) &&
10075 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010076 SVT.getVectorNumElements() > NElts)
10077 return SVT;
10078 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010079 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010080}