blob: 36145af37eb30a22919dddbec583265dc80553dc [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000037#include "llvm/MC/MCContext.h"
38#include "llvm/MC/MCExpr.h"
39#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/ADT/BitVector.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"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000044#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/Support/Debug.h"
46#include "llvm/Support/ErrorHandling.h"
47#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000048#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049using namespace llvm;
50
Mon P Wang3c81d352008-11-23 04:37:22 +000051static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000052DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000053
Dan Gohman2f67df72009-09-03 17:18:51 +000054// Disable16Bit - 16-bit operations typically have a larger encoding than
55// corresponding 32-bit instructions, and 16-bit code is slow on some
56// processors. This is an experimental flag to disable 16-bit operations
57// (which forces them to be Legalized to 32-bit operations).
58static cl::opt<bool>
59Disable16Bit("disable-16bit", cl::Hidden,
60 cl::desc("Disable use of 16-bit instructions"));
61
Evan Cheng10e86422008-04-25 19:11:04 +000062// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000063static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000064 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000065
Chris Lattnerf0144122009-07-28 03:13:23 +000066static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
67 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
68 default: llvm_unreachable("unknown subtarget type");
69 case X86Subtarget::isDarwin:
Chris Lattner8c6ed052009-09-16 01:46:41 +000070 if (TM.getSubtarget<X86Subtarget>().is64Bit())
71 return new X8664_MachoTargetObjectFile();
Chris Lattner228252f2009-09-18 20:22:52 +000072 return new X8632_MachoTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +000073 case X86Subtarget::isELF:
74 return new TargetLoweringObjectFileELF();
75 case X86Subtarget::isMingw:
76 case X86Subtarget::isCygwin:
77 case X86Subtarget::isWindows:
78 return new TargetLoweringObjectFileCOFF();
79 }
Eric Christopherfd179292009-08-27 18:07:15 +000080
Chris Lattnerf0144122009-07-28 03:13:23 +000081}
82
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000083X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000084 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000085 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000086 X86ScalarSSEf64 = Subtarget->hasSSE2();
87 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000088 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093 // Set up the TargetLowering object.
94
95 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000096 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000097 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000098 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000099 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000100
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000101 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000102 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000103 setUseUnderscoreSetJmp(false);
104 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000105 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000106 // MS runtime is weird: it exports _setjmp, but longjmp!
107 setUseUnderscoreSetJmp(true);
108 setUseUnderscoreLongJmp(false);
109 } else {
110 setUseUnderscoreSetJmp(true);
111 setUseUnderscoreLongJmp(true);
112 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000113
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000114 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000116 if (!Disable16Bit)
117 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000119 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000123
Scott Michelfdc40a02009-02-17 22:15:04 +0000124 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000126 if (!Disable16Bit)
127 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000129 if (!Disable16Bit)
130 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
132 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000133
134 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
136 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
137 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
138 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
139 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
140 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000141
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
143 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000144 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
145 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000147
Evan Cheng25ab6902006-09-08 06:48:29 +0000148 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000151 } else if (!UseSoftFloat) {
152 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000153 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000155 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000156 // We have an algorithm for SSE2, and we turn this into a 64-bit
157 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000159 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000160
161 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
162 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
164 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165
Devang Patel6a784892009-06-05 18:48:29 +0000166 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000167 // SSE has no i16 to fp conversion, only i32
168 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000170 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000172 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
174 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000175 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000176 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
178 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000179 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
Dale Johannesen73328d12007-09-19 23:55:34 +0000181 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
182 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
184 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000185
Evan Cheng02568ff2006-01-30 22:13:22 +0000186 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
187 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000188 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
189 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000190
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000191 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000193 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000195 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
197 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000198 }
199
200 // Handle FP_TO_UINT by promoting the destination to a larger signed
201 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
203 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
204 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
208 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000209 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000210 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 // Expand FP_TO_UINT into a select.
212 // FIXME: We would like to use a Custom expander here eventually to do
213 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000216 // With SSE3 we can use fisttpll to convert to a signed i64; without
217 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220
Chris Lattner399610a2006-12-05 18:22:22 +0000221 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000222 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
224 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000225 }
Chris Lattner21f66852005-12-23 05:15:23 +0000226
Dan Gohmanb00ee212008-02-18 19:34:53 +0000227 // Scalar integer divide and remainder are lowered to use operations that
228 // produce two results, to match the available instructions. This exposes
229 // the two-result form to trivial CSE, which is able to combine x/y and x%y
230 // into a single instruction.
231 //
232 // Scalar integer multiply-high is also lowered to use two-result
233 // operations, to match the available instructions. However, plain multiply
234 // (low) operations are left as Legal, as there are single-result
235 // instructions for this in x86. Using the two-result multiply instructions
236 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000237 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
238 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
239 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
240 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
241 setOperationAction(ISD::SREM , MVT::i8 , Expand);
242 setOperationAction(ISD::UREM , MVT::i8 , Expand);
243 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
244 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
245 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
246 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
247 setOperationAction(ISD::SREM , MVT::i16 , Expand);
248 setOperationAction(ISD::UREM , MVT::i16 , Expand);
249 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
250 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
251 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
252 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
253 setOperationAction(ISD::SREM , MVT::i32 , Expand);
254 setOperationAction(ISD::UREM , MVT::i32 , Expand);
255 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
256 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
257 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
258 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
259 setOperationAction(ISD::SREM , MVT::i64 , Expand);
260 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000261
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
263 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
264 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
265 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
271 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
272 setOperationAction(ISD::FREM , MVT::f32 , Expand);
273 setOperationAction(ISD::FREM , MVT::f64 , Expand);
274 setOperationAction(ISD::FREM , MVT::f80 , Expand);
275 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
278 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
279 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
280 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000281 if (Disable16Bit) {
282 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
283 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
284 } else {
285 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
287 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000288 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
289 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
293 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
294 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000295 }
296
Owen Anderson825b72b2009-08-11 20:47:22 +0000297 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
298 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000299
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000301 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000302 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000303 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000304 if (Disable16Bit)
305 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
306 else
307 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000308 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
309 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
310 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
311 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
312 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000313 if (Disable16Bit)
314 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
315 else
316 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
323 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000326
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000332 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
334 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000335 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
338 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
339 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
340 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000343 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Evan Chengd2cde682008-03-10 19:38:10 +0000353 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000355
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000356 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000358
Mon P Wang63307c32008-05-05 19:05:59 +0000359 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
361 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
362 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
363 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000364
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
366 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000369
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000370 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
372 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000378 }
379
Evan Cheng3c992d22006-03-07 02:02:57 +0000380 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000381 if (!Subtarget->isTargetDarwin() &&
382 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000383 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000385 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000386
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
388 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
389 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
390 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000391 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000392 setExceptionPointerRegister(X86::RAX);
393 setExceptionSelectorRegister(X86::RDX);
394 } else {
395 setExceptionPointerRegister(X86::EAX);
396 setExceptionSelectorRegister(X86::EDX);
397 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
399 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000400
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000402
Owen Anderson825b72b2009-08-11 20:47:22 +0000403 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000404
Nate Begemanacc398c2006-01-25 18:21:52 +0000405 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::VASTART , MVT::Other, Custom);
407 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000408 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VAARG , MVT::Other, Custom);
410 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::VAARG , MVT::Other, Expand);
413 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000414 }
Evan Chengae642192007-03-02 23:16:35 +0000415
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
417 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000418 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000420 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000421 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000422 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000424
Evan Chengc7ce29b2009-02-13 22:36:38 +0000425 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000426 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000427 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
429 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000430
Evan Cheng223547a2006-01-31 22:28:30 +0000431 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::FABS , MVT::f64, Custom);
433 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000434
435 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::FNEG , MVT::f64, Custom);
437 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000438
Evan Cheng68c47cb2007-01-05 07:55:56 +0000439 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442
Evan Chengd25e9e82006-02-02 00:28:23 +0000443 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 setOperationAction(ISD::FSIN , MVT::f64, Expand);
445 setOperationAction(ISD::FCOS , MVT::f64, Expand);
446 setOperationAction(ISD::FSIN , MVT::f32, Expand);
447 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000448
Chris Lattnera54aa942006-01-29 06:26:08 +0000449 // Expand FP immediates into loads from the stack, except for the special
450 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451 addLegalFPImmediate(APFloat(+0.0)); // xorpd
452 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000453 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454 // Use SSE for f32, x87 for f64.
455 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000456 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
457 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
459 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466
467 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000470
471 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::FSIN , MVT::f32, Expand);
473 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474
Nate Begemane1795842008-02-14 08:57:00 +0000475 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000476 addLegalFPImmediate(APFloat(+0.0f)); // xorps
477 addLegalFPImmediate(APFloat(+0.0)); // FLD0
478 addLegalFPImmediate(APFloat(+1.0)); // FLD1
479 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
480 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
481
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000482 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000483 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
484 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000486 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000487 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000488 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000489 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
490 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000491
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
493 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
494 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
495 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000496
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000497 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000498 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
499 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000501 addLegalFPImmediate(APFloat(+0.0)); // FLD0
502 addLegalFPImmediate(APFloat(+1.0)); // FLD1
503 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
504 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000505 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
506 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
507 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
508 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000509 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000510
Dale Johannesen59a58732007-08-05 18:49:15 +0000511 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000512 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000513 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
514 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
515 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000516 {
517 bool ignored;
518 APFloat TmpFlt(+0.0);
519 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
520 &ignored);
521 addLegalFPImmediate(TmpFlt); // FLD0
522 TmpFlt.changeSign();
523 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
524 APFloat TmpFlt2(+1.0);
525 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
526 &ignored);
527 addLegalFPImmediate(TmpFlt2); // FLD1
528 TmpFlt2.changeSign();
529 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
530 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000531
Evan Chengc7ce29b2009-02-13 22:36:38 +0000532 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000533 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
534 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000535 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000536 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000537
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000538 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
540 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
541 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000542
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::FLOG, MVT::f80, Expand);
544 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
545 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
546 setOperationAction(ISD::FEXP, MVT::f80, Expand);
547 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000548
Mon P Wangf007a8b2008-11-06 05:31:54 +0000549 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000550 // (for widening) or expand (for scalarization). Then we will selectively
551 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000552 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
553 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
554 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
569 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
570 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000602 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000603 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
607 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
608 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
609 setTruncStoreAction((MVT::SimpleValueType)VT,
610 (MVT::SimpleValueType)InnerVT, Expand);
611 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
612 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
613 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000614 }
615
Evan Chengc7ce29b2009-02-13 22:36:38 +0000616 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
617 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000618 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
620 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
621 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
622 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
623 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
626 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
627 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
628 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
631 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
632 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
633 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
636 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000637
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::AND, MVT::v8i8, Promote);
639 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
640 setOperationAction(ISD::AND, MVT::v4i16, Promote);
641 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
642 setOperationAction(ISD::AND, MVT::v2i32, Promote);
643 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
644 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::OR, MVT::v8i8, Promote);
647 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
648 setOperationAction(ISD::OR, MVT::v4i16, Promote);
649 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
650 setOperationAction(ISD::OR, MVT::v2i32, Promote);
651 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
652 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
655 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
656 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
657 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
658 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
659 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
660 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
663 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
664 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
665 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
666 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
667 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
668 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
669 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
670 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
673 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
674 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
675 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
676 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000677
Owen Anderson825b72b2009-08-11 20:47:22 +0000678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
679 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
680 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
681 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000682
Owen Anderson825b72b2009-08-11 20:47:22 +0000683 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
684 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
685 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
686 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000687
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000689
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
691 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
692 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
693 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
694 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000697 }
698
Evan Cheng92722532009-03-26 23:06:32 +0000699 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
704 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
705 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
706 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
707 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
708 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
709 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
710 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
711 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
712 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
713 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000714 }
715
Evan Cheng92722532009-03-26 23:06:32 +0000716 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000718
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000719 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
720 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
722 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
723 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
724 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
727 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
728 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
729 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
730 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
731 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
732 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
733 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
734 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
735 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
736 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
738 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
739 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
740 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
741 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
744 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
745 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000753
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
756 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
757 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
759
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
762 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000763 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000765 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000766 // Do not attempt to custom lower non-128-bit vectors
767 if (!VT.is128BitVector())
768 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::BUILD_VECTOR,
770 VT.getSimpleVT().SimpleTy, Custom);
771 setOperationAction(ISD::VECTOR_SHUFFLE,
772 VT.getSimpleVT().SimpleTy, Custom);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
774 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000775 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
778 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
779 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
780 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
781 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
782 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000783
Nate Begemancdd1eec2008-02-12 22:51:28 +0000784 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
786 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000787 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000788
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000789 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
791 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000792 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000793
794 // Do not attempt to promote non-128-bit vectors
795 if (!VT.is128BitVector()) {
796 continue;
797 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000798 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000800 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000801 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000802 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000804 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000806 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000808 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000809
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000811
Evan Cheng2c3ae372006-04-12 21:21:57 +0000812 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
814 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
815 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
816 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
819 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000820 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
822 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000823 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000824 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000825
Nate Begeman14d12ca2008-02-11 04:19:36 +0000826 if (Subtarget->hasSSE41()) {
827 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000829
830 // i8 and i16 vectors are custom , because the source register and source
831 // source memory operand types are not the same width. f32 vectors are
832 // custom since the immediate controlling the insert encodes additional
833 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
836 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
837 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000838
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
840 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
841 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000843
844 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
846 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000847 }
848 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000849
Nate Begeman30a0de92008-07-17 16:51:19 +0000850 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000852 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000853
David Greene9b9838d2009-06-29 16:47:10 +0000854 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
856 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
857 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
858 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000859
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
861 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
862 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
863 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
864 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
865 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
866 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
867 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
868 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
869 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
870 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
871 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
872 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
873 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
874 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000875
876 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
878 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
879 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
880 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
881 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
882 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
883 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
884 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
885 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
886 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
887 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
888 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
889 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
890 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000891
Owen Anderson825b72b2009-08-11 20:47:22 +0000892 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
893 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
894 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
895 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000896
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
898 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
899 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
900 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
901 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000902
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
904 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
905 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
906 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
907 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
908 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000909
910#if 0
911 // Not sure we want to do this since there are no 256-bit integer
912 // operations in AVX
913
914 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
915 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000916 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
917 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000918
919 // Do not attempt to custom lower non-power-of-2 vectors
920 if (!isPowerOf2_32(VT.getVectorNumElements()))
921 continue;
922
923 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
924 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
925 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
926 }
927
928 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
930 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000931 }
David Greene9b9838d2009-06-29 16:47:10 +0000932#endif
933
934#if 0
935 // Not sure we want to do this since there are no 256-bit integer
936 // operations in AVX
937
938 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
939 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
941 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000942
943 if (!VT.is256BitVector()) {
944 continue;
945 }
946 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000948 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000950 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000952 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000953 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000954 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000956 }
957
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000959#endif
960 }
961
Evan Cheng6be2c582006-04-05 23:38:46 +0000962 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000964
Bill Wendling74c37652008-12-09 22:08:41 +0000965 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setOperationAction(ISD::SADDO, MVT::i32, Custom);
967 setOperationAction(ISD::SADDO, MVT::i64, Custom);
968 setOperationAction(ISD::UADDO, MVT::i32, Custom);
969 setOperationAction(ISD::UADDO, MVT::i64, Custom);
970 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
971 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
972 setOperationAction(ISD::USUBO, MVT::i32, Custom);
973 setOperationAction(ISD::USUBO, MVT::i64, Custom);
974 setOperationAction(ISD::SMULO, MVT::i32, Custom);
975 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000976
Evan Chengd54f2d52009-03-31 19:38:51 +0000977 if (!Subtarget->is64Bit()) {
978 // These libcalls are not available in 32-bit.
979 setLibcallName(RTLIB::SHL_I128, 0);
980 setLibcallName(RTLIB::SRL_I128, 0);
981 setLibcallName(RTLIB::SRA_I128, 0);
982 }
983
Evan Cheng206ee9d2006-07-07 08:33:52 +0000984 // We have target-specific dag combine patterns for the following nodes:
985 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000986 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000987 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000988 setTargetDAGCombine(ISD::SHL);
989 setTargetDAGCombine(ISD::SRA);
990 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000991 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000992 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000993 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000994 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000995 if (Subtarget->is64Bit())
996 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000997
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000998 computeRegisterProperties();
999
Mon P Wangcd6e7252009-11-30 02:42:02 +00001000 // Divide and reminder operations have no vector equivalent and can
1001 // trap. Do a custom widening for these operations in which we never
1002 // generate more divides/remainder than the original vector width.
1003 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1004 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1005 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1006 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1007 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1008 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1009 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1010 }
1011 }
1012
Evan Cheng87ed7162006-02-14 08:25:08 +00001013 // FIXME: These should be based on subtarget info. Plus, the values should
1014 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001015 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1016 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1017 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001018 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001019 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001020}
1021
Scott Michel5b8f82e2008-03-10 15:42:14 +00001022
Owen Anderson825b72b2009-08-11 20:47:22 +00001023MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1024 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001025}
1026
1027
Evan Cheng29286502008-01-23 23:17:41 +00001028/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1029/// the desired ByVal argument alignment.
1030static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1031 if (MaxAlign == 16)
1032 return;
1033 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1034 if (VTy->getBitWidth() == 128)
1035 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001036 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1037 unsigned EltAlign = 0;
1038 getMaxByValAlign(ATy->getElementType(), EltAlign);
1039 if (EltAlign > MaxAlign)
1040 MaxAlign = EltAlign;
1041 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1042 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1043 unsigned EltAlign = 0;
1044 getMaxByValAlign(STy->getElementType(i), EltAlign);
1045 if (EltAlign > MaxAlign)
1046 MaxAlign = EltAlign;
1047 if (MaxAlign == 16)
1048 break;
1049 }
1050 }
1051 return;
1052}
1053
1054/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1055/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001056/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1057/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001058unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001059 if (Subtarget->is64Bit()) {
1060 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001061 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001062 if (TyAlign > 8)
1063 return TyAlign;
1064 return 8;
1065 }
1066
Evan Cheng29286502008-01-23 23:17:41 +00001067 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001068 if (Subtarget->hasSSE1())
1069 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001070 return Align;
1071}
Chris Lattner2b02a442007-02-25 08:29:00 +00001072
Evan Chengf0df0312008-05-15 08:39:06 +00001073/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001074/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001075/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001076/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001077EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001078X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001079 bool isSrcConst, bool isSrcStr,
1080 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001081 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1082 // linux. This is because the stack realignment code can't handle certain
1083 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001084 const Function *F = DAG.getMachineFunction().getFunction();
1085 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1086 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001088 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001089 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001090 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001091 }
Evan Chengf0df0312008-05-15 08:39:06 +00001092 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 return MVT::i64;
1094 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001095}
1096
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001097/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1098/// current function. The returned value is a member of the
1099/// MachineJumpTableInfo::JTEntryKind enum.
1100unsigned X86TargetLowering::getJumpTableEncoding() const {
1101 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1102 // symbol.
1103 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1104 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001105 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001106
1107 // Otherwise, use the normal jump table encoding heuristics.
1108 return TargetLowering::getJumpTableEncoding();
1109}
1110
Chris Lattner589c6f62010-01-26 06:28:43 +00001111/// getPICBaseSymbol - Return the X86-32 PIC base.
1112MCSymbol *
1113X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1114 MCContext &Ctx) const {
1115 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
1116 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1117 Twine(MF->getFunctionNumber())+"$pb");
1118}
1119
1120
Chris Lattnerc64daab2010-01-26 05:02:42 +00001121const MCExpr *
1122X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1123 const MachineBasicBlock *MBB,
1124 unsigned uid,MCContext &Ctx) const{
1125 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1126 Subtarget->isPICStyleGOT());
1127 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1128 // entries.
1129
1130 // FIXME: @GOTOFF should be a property of MCSymbolRefExpr not in the MCSymbol.
1131 std::string Name = MBB->getSymbol(Ctx)->getName() + "@GOTOFF";
1132 return MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(StringRef(Name)), Ctx);
1133}
1134
Evan Chengcc415862007-11-09 01:32:10 +00001135/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1136/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001137SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001138 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001139 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001140 // This doesn't have DebugLoc associated with it, but is not really the
1141 // same as a Register.
1142 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1143 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001144 return Table;
1145}
1146
Chris Lattner589c6f62010-01-26 06:28:43 +00001147/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1148/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1149/// MCExpr.
1150const MCExpr *X86TargetLowering::
1151getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1152 MCContext &Ctx) const {
1153 // X86-64 uses RIP relative addressing based on the jump table label.
1154 if (Subtarget->isPICStyleRIPRel())
1155 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1156
1157 // Otherwise, the reference is relative to the PIC base.
1158 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1159}
1160
Bill Wendlingb4202b82009-07-01 18:50:55 +00001161/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001162unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001163 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001164}
1165
Chris Lattner2b02a442007-02-25 08:29:00 +00001166//===----------------------------------------------------------------------===//
1167// Return Value Calling Convention Implementation
1168//===----------------------------------------------------------------------===//
1169
Chris Lattner59ed56b2007-02-28 04:55:35 +00001170#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001171
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001172bool
1173X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1174 const SmallVectorImpl<EVT> &OutTys,
1175 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1176 SelectionDAG &DAG) {
1177 SmallVector<CCValAssign, 16> RVLocs;
1178 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1179 RVLocs, *DAG.getContext());
1180 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1181}
1182
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183SDValue
1184X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001185 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001186 const SmallVectorImpl<ISD::OutputArg> &Outs,
1187 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001188
Chris Lattner9774c912007-02-27 05:28:59 +00001189 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001190 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1191 RVLocs, *DAG.getContext());
1192 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001193
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001194 // If this is the first return lowered for this function, add the regs to the
1195 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001196 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001197 for (unsigned i = 0; i != RVLocs.size(); ++i)
1198 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001199 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001201
Dan Gohman475871a2008-07-27 21:46:04 +00001202 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001203
Dan Gohman475871a2008-07-27 21:46:04 +00001204 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001205 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1206 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001207 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001208
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001209 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001210 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1211 CCValAssign &VA = RVLocs[i];
1212 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001213 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001214
Chris Lattner447ff682008-03-11 03:23:40 +00001215 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1216 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001217 if (VA.getLocReg() == X86::ST0 ||
1218 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001219 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1220 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001221 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001223 RetOps.push_back(ValToCopy);
1224 // Don't emit a copytoreg.
1225 continue;
1226 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001227
Evan Cheng242b38b2009-02-23 09:03:22 +00001228 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1229 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001230 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001231 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001232 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001233 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001234 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001236 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001237 }
1238
Dale Johannesendd64c412009-02-04 00:33:20 +00001239 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001240 Flag = Chain.getValue(1);
1241 }
Dan Gohman61a92132008-04-21 23:59:07 +00001242
1243 // The x86-64 ABI for returning structs by value requires that we copy
1244 // the sret argument into %rax for the return. We saved the argument into
1245 // a virtual register in the entry block, so now we copy the value out
1246 // and into %rax.
1247 if (Subtarget->is64Bit() &&
1248 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1249 MachineFunction &MF = DAG.getMachineFunction();
1250 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1251 unsigned Reg = FuncInfo->getSRetReturnReg();
1252 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001254 FuncInfo->setSRetReturnReg(Reg);
1255 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001256 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001257
Dale Johannesendd64c412009-02-04 00:33:20 +00001258 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001259 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001260
1261 // RAX now acts like a return value.
1262 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001263 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001264
Chris Lattner447ff682008-03-11 03:23:40 +00001265 RetOps[0] = Chain; // Update chain.
1266
1267 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001268 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001269 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
1271 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001272 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001273}
1274
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275/// LowerCallResult - Lower the result values of a call into the
1276/// appropriate copies out of appropriate physical registers.
1277///
1278SDValue
1279X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001280 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001281 const SmallVectorImpl<ISD::InputArg> &Ins,
1282 DebugLoc dl, SelectionDAG &DAG,
1283 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001284
Chris Lattnere32bbf62007-02-28 07:09:55 +00001285 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001286 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001287 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001288 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001289 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Chris Lattner3085e152007-02-25 08:59:22 +00001292 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001293 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001294 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001295 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001296
Torok Edwin3f142c32009-02-01 18:15:56 +00001297 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001298 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001299 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001300 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001301 }
1302
Chris Lattner8e6da152008-03-10 21:08:41 +00001303 // If this is a call to a function that returns an fp value on the floating
1304 // point stack, but where we prefer to use the value in xmm registers, copy
1305 // 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 +00001306 if ((VA.getLocReg() == X86::ST0 ||
1307 VA.getLocReg() == X86::ST1) &&
1308 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Evan Cheng79fb3b42009-02-20 20:43:02 +00001312 SDValue Val;
1313 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001314 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1315 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1316 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001318 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1320 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001321 } else {
1322 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001324 Val = Chain.getValue(0);
1325 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001326 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1327 } else {
1328 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1329 CopyVT, InFlag).getValue(1);
1330 Val = Chain.getValue(0);
1331 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001332 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001333
Dan Gohman37eed792009-02-04 17:28:58 +00001334 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001335 // Round the F80 the right size, which also moves to the appropriate xmm
1336 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001337 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001338 // This truncation won't change the value.
1339 DAG.getIntPtrConstant(1));
1340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001341
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001343 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001344
Dan Gohman98ca4f22009-08-05 01:29:28 +00001345 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001346}
1347
1348
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001349//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001350// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001351//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001352// StdCall calling convention seems to be standard for many Windows' API
1353// routines and around. It differs from C calling convention just a little:
1354// callee should clean up the stack, not caller. Symbols should be also
1355// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001356// For info on fast calling convention see Fast Calling Convention (tail call)
1357// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001358
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001360/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1362 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001363 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001364
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001366}
1367
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001368/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001369/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370static bool
1371ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1372 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001373 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001374
Dan Gohman98ca4f22009-08-05 01:29:28 +00001375 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001376}
1377
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001378/// IsCalleePop - Determines whether the callee is required to pop its
1379/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001380bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001381 if (IsVarArg)
1382 return false;
1383
Dan Gohman095cc292008-09-13 01:54:27 +00001384 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001385 default:
1386 return false;
1387 case CallingConv::X86_StdCall:
1388 return !Subtarget->is64Bit();
1389 case CallingConv::X86_FastCall:
1390 return !Subtarget->is64Bit();
1391 case CallingConv::Fast:
1392 return PerformTailCallOpt;
1393 }
1394}
1395
Dan Gohman095cc292008-09-13 01:54:27 +00001396/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1397/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001398CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001399 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001400 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001401 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001402 else
1403 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001404 }
1405
Gordon Henriksen86737662008-01-05 16:56:59 +00001406 if (CC == CallingConv::X86_FastCall)
1407 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001408 else if (CC == CallingConv::Fast)
1409 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001410 else
1411 return CC_X86_32_C;
1412}
1413
Dan Gohman98ca4f22009-08-05 01:29:28 +00001414/// NameDecorationForCallConv - Selects the appropriate decoration to
1415/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001416NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001417X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001418 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001419 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001420 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001421 return StdCall;
1422 return None;
1423}
1424
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001425
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001426/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1427/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001428/// the specific parameter attribute. The copy will be passed as a byval
1429/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001430static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001431CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001432 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1433 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001435 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001436 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001437}
1438
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439SDValue
1440X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001441 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001442 const SmallVectorImpl<ISD::InputArg> &Ins,
1443 DebugLoc dl, SelectionDAG &DAG,
1444 const CCValAssign &VA,
1445 MachineFrameInfo *MFI,
1446 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001447 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Chengddc419c2010-01-26 19:04:47 +00001449 bool AlwaysUseMutable = X86::IsEligibleForTailCallOpt(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001450 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001451 EVT ValVT;
1452
1453 // If value is passed by pointer we have address passed instead of the value
1454 // itself.
1455 if (VA.getLocInfo() == CCValAssign::Indirect)
1456 ValVT = VA.getLocVT();
1457 else
1458 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001459
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001460 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001461 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001462 // In case of tail call optimization mark all arguments mutable. Since they
1463 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001464 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001465 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001466 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001467 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001468 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001469 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001470 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001471}
1472
Dan Gohman475871a2008-07-27 21:46:04 +00001473SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001474X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001475 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476 bool isVarArg,
1477 const SmallVectorImpl<ISD::InputArg> &Ins,
1478 DebugLoc dl,
1479 SelectionDAG &DAG,
1480 SmallVectorImpl<SDValue> &InVals) {
1481
Evan Cheng1bc78042006-04-26 01:20:17 +00001482 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001483 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 const Function* Fn = MF.getFunction();
1486 if (Fn->hasExternalLinkage() &&
1487 Subtarget->isTargetCygMing() &&
1488 Fn->getName() == "main")
1489 FuncInfo->setForceFramePointer(true);
1490
1491 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001492 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Evan Cheng1bc78042006-04-26 01:20:17 +00001494 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001495 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001496 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001497
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001499 "Var args not supported with calling convention fastcc");
1500
Chris Lattner638402b2007-02-28 07:00:42 +00001501 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001502 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001503 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1504 ArgLocs, *DAG.getContext());
1505 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001506
Chris Lattnerf39f7712007-02-28 05:46:49 +00001507 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001508 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001509 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1510 CCValAssign &VA = ArgLocs[i];
1511 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1512 // places.
1513 assert(VA.getValNo() != LastVal &&
1514 "Don't support value assigned to multiple locs yet");
1515 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001516
Chris Lattnerf39f7712007-02-28 05:46:49 +00001517 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001518 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001519 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001520 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001521 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001522 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001523 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001525 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001528 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001529 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001530 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1531 RC = X86::VR64RegisterClass;
1532 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001533 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001534
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001535 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001536 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001537
Chris Lattnerf39f7712007-02-28 05:46:49 +00001538 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1539 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1540 // right size.
1541 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001542 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001543 DAG.getValueType(VA.getValVT()));
1544 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001545 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001546 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001547 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001548 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001551 // Handle MMX values passed in XMM regs.
1552 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1554 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001555 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1556 } else
1557 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001558 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 } else {
1560 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001561 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001562 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001563
1564 // If value is passed via pointer - do a load.
1565 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001567
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001569 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001570
Dan Gohman61a92132008-04-21 23:59:07 +00001571 // The x86-64 ABI for returning structs by value requires that we copy
1572 // the sret argument into %rax for the return. Save the argument into
1573 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001574 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001575 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1576 unsigned Reg = FuncInfo->getSRetReturnReg();
1577 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001578 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001579 FuncInfo->setSRetReturnReg(Reg);
1580 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001581 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001583 }
1584
Chris Lattnerf39f7712007-02-28 05:46:49 +00001585 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001586 // align stack specially for tail calls
Evan Chengddc419c2010-01-26 19:04:47 +00001587 if (X86::IsEligibleForTailCallOpt(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001588 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001589
Evan Cheng1bc78042006-04-26 01:20:17 +00001590 // If the function takes variable number of arguments, make a frame index for
1591 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001592 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001593 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001594 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001595 }
1596 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001597 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1598
1599 // FIXME: We should really autogenerate these arrays
1600 static const unsigned GPR64ArgRegsWin64[] = {
1601 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001602 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001603 static const unsigned XMMArgRegsWin64[] = {
1604 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1605 };
1606 static const unsigned GPR64ArgRegs64Bit[] = {
1607 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1608 };
1609 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001610 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1611 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1612 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001613 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1614
1615 if (IsWin64) {
1616 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1617 GPR64ArgRegs = GPR64ArgRegsWin64;
1618 XMMArgRegs = XMMArgRegsWin64;
1619 } else {
1620 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1621 GPR64ArgRegs = GPR64ArgRegs64Bit;
1622 XMMArgRegs = XMMArgRegs64Bit;
1623 }
1624 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1625 TotalNumIntRegs);
1626 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1627 TotalNumXMMRegs);
1628
Devang Patel578efa92009-06-05 21:57:13 +00001629 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001630 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001631 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001632 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001633 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001634 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001635 // Kernel mode asks for SSE to be disabled, so don't push them
1636 // on the stack.
1637 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001638
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 // For X86-64, if there are vararg parameters that are passed via
1640 // registers, then we must store them to their spots on the stack so they
1641 // may be loaded by deferencing the result of va_next.
1642 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001643 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1644 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001645 TotalNumXMMRegs * 16, 16,
1646 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001647
Gordon Henriksen86737662008-01-05 16:56:59 +00001648 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SmallVector<SDValue, 8> MemOps;
1650 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001651 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001652 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001653 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1654 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001655 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1656 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001657 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001658 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001659 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001660 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001661 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001663 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001665
Dan Gohmanface41a2009-08-16 21:24:25 +00001666 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1667 // Now store the XMM (fp + vector) parameter registers.
1668 SmallVector<SDValue, 11> SaveXMMOps;
1669 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001670
Dan Gohmanface41a2009-08-16 21:24:25 +00001671 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1672 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1673 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001674
Dan Gohmanface41a2009-08-16 21:24:25 +00001675 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1676 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001677
Dan Gohmanface41a2009-08-16 21:24:25 +00001678 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1679 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1680 X86::VR128RegisterClass);
1681 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1682 SaveXMMOps.push_back(Val);
1683 }
1684 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1685 MVT::Other,
1686 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001687 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001688
1689 if (!MemOps.empty())
1690 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1691 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001693 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001694
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001698 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001699 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001700 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001701 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001702 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001703 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001704
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 if (!Is64Bit) {
1706 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1709 }
Evan Cheng25caf632006-05-23 21:06:34 +00001710
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001711 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001712
Dan Gohman98ca4f22009-08-05 01:29:28 +00001713 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001714}
1715
Dan Gohman475871a2008-07-27 21:46:04 +00001716SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1718 SDValue StackPtr, SDValue Arg,
1719 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001720 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001722 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001723 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001724 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001725 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001726 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001727 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001728 }
Dale Johannesenace16102009-02-03 19:33:06 +00001729 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001730 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001731}
1732
Bill Wendling64e87322009-01-16 19:25:27 +00001733/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001734/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001735SDValue
1736X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001737 SDValue &OutRetAddr, SDValue Chain,
1738 bool IsTailCall, bool Is64Bit,
1739 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001740 if (!IsTailCall || FPDiff==0) return Chain;
1741
1742 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001743 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001744 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001745
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001746 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001747 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001748 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001749}
1750
1751/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1752/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001753static SDValue
1754EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001755 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001756 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001757 // Store the return address to the appropriate stack slot.
1758 if (!FPDiff) return Chain;
1759 // Calculate the new stack slot for the return address.
1760 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001761 int NewReturnAddrFI =
Evan Chengddc419c2010-01-26 19:04:47 +00001762 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, true,false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001763 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001764 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001765 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001766 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001767 return Chain;
1768}
1769
Dan Gohman98ca4f22009-08-05 01:29:28 +00001770SDValue
1771X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001772 CallingConv::ID CallConv, bool isVarArg,
1773 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 const SmallVectorImpl<ISD::OutputArg> &Outs,
1775 const SmallVectorImpl<ISD::InputArg> &Ins,
1776 DebugLoc dl, SelectionDAG &DAG,
1777 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778 MachineFunction &MF = DAG.getMachineFunction();
1779 bool Is64Bit = Subtarget->is64Bit();
1780 bool IsStructRet = CallIsStructReturn(Outs);
1781
Evan Chengddc419c2010-01-26 19:04:47 +00001782 assert((!isTailCall || X86::IsEligibleForTailCallOpt(CallConv)) &&
1783 "Call is not eligible for tail call optimization!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001785 "Var args not supported with calling convention fastcc");
1786
Chris Lattner638402b2007-02-28 07:00:42 +00001787 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001788 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001789 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1790 ArgLocs, *DAG.getContext());
1791 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001792
Chris Lattner423c5f42007-02-28 05:31:48 +00001793 // Get a count of how many bytes are to be pushed on the stack.
1794 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengddc419c2010-01-26 19:04:47 +00001795 if (X86::IsEligibleForTailCallOpt(CallConv))
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001796 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001797
Gordon Henriksen86737662008-01-05 16:56:59 +00001798 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001800 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001801 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001802 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1803 FPDiff = NumBytesCallerPushed - NumBytes;
1804
1805 // Set the delta of movement of the returnaddr stackslot.
1806 // But only set if delta is greater than previous delta.
1807 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1808 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1809 }
1810
Chris Lattnere563bbc2008-10-11 22:08:30 +00001811 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001812
Dan Gohman475871a2008-07-27 21:46:04 +00001813 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001814 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001816 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001817
Dan Gohman475871a2008-07-27 21:46:04 +00001818 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1819 SmallVector<SDValue, 8> MemOpChains;
1820 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001821
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001822 // Walk the register/memloc assignments, inserting copies/loads. In the case
1823 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001824 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1825 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001826 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001827 SDValue Arg = Outs[i].Val;
1828 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001829 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001830
Chris Lattner423c5f42007-02-28 05:31:48 +00001831 // Promote the value if needed.
1832 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001833 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001834 case CCValAssign::Full: break;
1835 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001836 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001837 break;
1838 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001839 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001840 break;
1841 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001842 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1843 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001844 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1845 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1846 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001847 } else
1848 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1849 break;
1850 case CCValAssign::BCvt:
1851 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001852 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001853 case CCValAssign::Indirect: {
1854 // Store the argument.
1855 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001856 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001857 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001858 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001859 Arg = SpillSlot;
1860 break;
1861 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001862 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001863
Chris Lattner423c5f42007-02-28 05:31:48 +00001864 if (VA.isRegLoc()) {
1865 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1866 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001867 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001868 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001869 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001870 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001871
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1873 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001874 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001875 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001876 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001877
Evan Cheng32fe1032006-05-25 00:59:30 +00001878 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001879 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001880 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001881
Evan Cheng347d5f72006-04-28 21:29:37 +00001882 // Build a sequence of copy-to-reg nodes chained together with token chain
1883 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001884 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001885 // Tail call byval lowering might overwrite argument registers so in case of
1886 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001887 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001888 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001889 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001890 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001891 InFlag = Chain.getValue(1);
1892 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001893
Eric Christopherfd179292009-08-27 18:07:15 +00001894
Chris Lattner88e1fd52009-07-09 04:24:46 +00001895 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001896 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1897 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001898 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001899 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1900 DAG.getNode(X86ISD::GlobalBaseReg,
1901 DebugLoc::getUnknownLoc(),
1902 getPointerTy()),
1903 InFlag);
1904 InFlag = Chain.getValue(1);
1905 } else {
1906 // If we are tail calling and generating PIC/GOT style code load the
1907 // address of the callee into ECX. The value in ecx is used as target of
1908 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1909 // for tail calls on PIC/GOT architectures. Normally we would just put the
1910 // address of GOT into ebx and then call target@PLT. But for tail calls
1911 // ebx would be restored (since ebx is callee saved) before jumping to the
1912 // target@PLT.
1913
1914 // Note: The actual moving to ECX is done further down.
1915 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1916 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1917 !G->getGlobal()->hasProtectedVisibility())
1918 Callee = LowerGlobalAddress(Callee, DAG);
1919 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001920 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001921 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001922 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001923
Gordon Henriksen86737662008-01-05 16:56:59 +00001924 if (Is64Bit && isVarArg) {
1925 // From AMD64 ABI document:
1926 // For calls that may call functions that use varargs or stdargs
1927 // (prototype-less calls or calls to functions containing ellipsis (...) in
1928 // the declaration) %al is used as hidden argument to specify the number
1929 // of SSE registers used. The contents of %al do not need to match exactly
1930 // the number of registers, but must be an ubound on the number of SSE
1931 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001932
1933 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 // Count the number of XMM registers allocated.
1935 static const unsigned XMMArgRegs[] = {
1936 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1937 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1938 };
1939 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001940 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001941 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001942
Dale Johannesendd64c412009-02-04 00:33:20 +00001943 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001944 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 InFlag = Chain.getValue(1);
1946 }
1947
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001948
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001949 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001950 if (isTailCall) {
1951 // Force all the incoming stack arguments to be loaded from the stack
1952 // before any new outgoing arguments are stored to the stack, because the
1953 // outgoing stack slots may alias the incoming argument stack slots, and
1954 // the alias isn't otherwise explicit. This is slightly more conservative
1955 // than necessary, because it means that each store effectively depends
1956 // on every argument instead of just those arguments it would clobber.
1957 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1958
Dan Gohman475871a2008-07-27 21:46:04 +00001959 SmallVector<SDValue, 8> MemOpChains2;
1960 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001961 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001962 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001964 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1965 CCValAssign &VA = ArgLocs[i];
1966 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001967 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001968 SDValue Arg = Outs[i].Val;
1969 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001970 // Create frame index.
1971 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001972 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001973 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001974 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001975
Duncan Sands276dcbd2008-03-21 09:14:45 +00001976 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001977 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001978 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001979 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001980 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001981 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001982 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001983
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1985 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001986 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001988 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001989 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001990 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001991 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001992 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 }
1994 }
1995
1996 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001997 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001998 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001999
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002000 // Copy arguments to their registers.
2001 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002002 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002003 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002004 InFlag = Chain.getValue(1);
2005 }
Dan Gohman475871a2008-07-27 21:46:04 +00002006 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002007
Gordon Henriksen86737662008-01-05 16:56:59 +00002008 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002009 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002010 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002011 }
2012
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002013 bool WasGlobalOrExternal = false;
2014 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2015 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2016 // In the 64-bit large code model, we have to make all calls
2017 // through a register, since the call instruction's 32-bit
2018 // pc-relative offset may not be large enough to hold the whole
2019 // address.
2020 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2021 WasGlobalOrExternal = true;
2022 // If the callee is a GlobalAddress node (quite common, every direct call
2023 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2024 // it.
2025
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002026 // We should use extra load for direct calls to dllimported functions in
2027 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002028 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002029 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002030 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002031
Chris Lattner48a7d022009-07-09 05:02:21 +00002032 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2033 // external symbols most go through the PLT in PIC mode. If the symbol
2034 // has hidden or protected visibility, or if it is static or local, then
2035 // we don't need to use the PLT - we can directly call it.
2036 if (Subtarget->isTargetELF() &&
2037 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002038 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002039 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002040 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002041 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2042 Subtarget->getDarwinVers() < 9) {
2043 // PC-relative references to external symbols should go through $stub,
2044 // unless we're building with the leopard linker or later, which
2045 // automatically synthesizes these stubs.
2046 OpFlags = X86II::MO_DARWIN_STUB;
2047 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002048
Chris Lattner74e726e2009-07-09 05:27:35 +00002049 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002050 G->getOffset(), OpFlags);
2051 }
Bill Wendling056292f2008-09-16 21:48:12 +00002052 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002053 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002054 unsigned char OpFlags = 0;
2055
2056 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2057 // symbols should go through the PLT.
2058 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002059 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002060 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002061 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002062 Subtarget->getDarwinVers() < 9) {
2063 // PC-relative references to external symbols should go through $stub,
2064 // unless we're building with the leopard linker or later, which
2065 // automatically synthesizes these stubs.
2066 OpFlags = X86II::MO_DARWIN_STUB;
2067 }
Eric Christopherfd179292009-08-27 18:07:15 +00002068
Chris Lattner48a7d022009-07-09 05:02:21 +00002069 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2070 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002071 }
2072
2073 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002074 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002075
Dale Johannesendd64c412009-02-04 00:33:20 +00002076 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002077 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 Callee,InFlag);
2079 Callee = DAG.getRegister(Opc, getPointerTy());
2080 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002081 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002082 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002083
Chris Lattnerd96d0722007-02-25 06:40:16 +00002084 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002085 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002087
Dan Gohman98ca4f22009-08-05 01:29:28 +00002088 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002089 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2090 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002091 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002093
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002094 Ops.push_back(Chain);
2095 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002096
Dan Gohman98ca4f22009-08-05 01:29:28 +00002097 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002099
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 // Add argument registers to the end of the list so that they are known live
2101 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002102 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2103 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2104 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002105
Evan Cheng586ccac2008-03-18 23:36:35 +00002106 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002108 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2109
2110 // Add an implicit use of AL for x86 vararg functions.
2111 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002112 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002113
Gabor Greifba36cb52008-08-28 21:40:38 +00002114 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002115 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002116
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 if (isTailCall) {
2118 // If this is the first return lowered for this function, add the regs
2119 // to the liveout set for the function.
2120 if (MF.getRegInfo().liveout_empty()) {
2121 SmallVector<CCValAssign, 16> RVLocs;
2122 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2123 *DAG.getContext());
2124 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2125 for (unsigned i = 0; i != RVLocs.size(); ++i)
2126 if (RVLocs[i].isRegLoc())
2127 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002129
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 assert(((Callee.getOpcode() == ISD::Register &&
2131 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002132 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2134 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002135 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002136
2137 return DAG.getNode(X86ISD::TC_RETURN, dl,
2138 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002139 }
2140
Dale Johannesenace16102009-02-03 19:33:06 +00002141 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002142 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002143
Chris Lattner2d297092006-05-23 18:50:38 +00002144 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002145 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002147 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002149 // If this is is a call to a struct-return function, the callee
2150 // pops the hidden struct pointer, so we have to push it back.
2151 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002152 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002153 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002154 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002155
Gordon Henriksenae636f82008-01-03 16:47:34 +00002156 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002157 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002158 DAG.getIntPtrConstant(NumBytes, true),
2159 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2160 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002161 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002162 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002163
Chris Lattner3085e152007-02-25 08:59:22 +00002164 // Handle result values, copying them out of physregs into vregs that we
2165 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002166 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2167 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002168}
2169
Evan Cheng25ab6902006-09-08 06:48:29 +00002170
2171//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002172// Fast Calling Convention (tail call) implementation
2173//===----------------------------------------------------------------------===//
2174
2175// Like std call, callee cleans arguments, convention except that ECX is
2176// reserved for storing the tail called function address. Only 2 registers are
2177// free for argument passing (inreg). Tail call optimization is performed
2178// provided:
2179// * tailcallopt is enabled
2180// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002181// On X86_64 architecture with GOT-style position independent code only local
2182// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002183// To keep the stack aligned according to platform abi the function
2184// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2185// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002186// If a tail called function callee has more arguments than the caller the
2187// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002188// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002189// original REtADDR, but before the saved framepointer or the spilled registers
2190// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2191// stack layout:
2192// arg1
2193// arg2
2194// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002195// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002196// move area ]
2197// (possible EBP)
2198// ESI
2199// EDI
2200// local1 ..
2201
2202/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2203/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002204unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002205 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002206 MachineFunction &MF = DAG.getMachineFunction();
2207 const TargetMachine &TM = MF.getTarget();
2208 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2209 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002210 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002211 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002212 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002213 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2214 // Number smaller than 12 so just add the difference.
2215 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2216 } else {
2217 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002218 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002219 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002220 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002221 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002222}
2223
Dan Gohman98ca4f22009-08-05 01:29:28 +00002224/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2225/// for tail call optimization. Targets which want to do tail call
2226/// optimization should implement this function.
2227bool
2228X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002229 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002230 bool isVarArg,
2231 const SmallVectorImpl<ISD::InputArg> &Ins,
2232 SelectionDAG& DAG) const {
Evan Chengddc419c2010-01-26 19:04:47 +00002233 return X86::IsEligibleForTailCallOpt(CalleeCC) &&
2234 DAG.getMachineFunction().getFunction()->getCallingConv() == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002235}
2236
Dan Gohman3df24e62008-09-03 23:12:08 +00002237FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002238X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2239 DwarfWriter *dw,
2240 DenseMap<const Value *, unsigned> &vm,
2241 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2242 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002243#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002244 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002245#endif
2246 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002247 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002248#ifndef NDEBUG
2249 , cil
2250#endif
2251 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002252}
2253
2254
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002255//===----------------------------------------------------------------------===//
2256// Other Lowering Hooks
2257//===----------------------------------------------------------------------===//
2258
2259
Dan Gohman475871a2008-07-27 21:46:04 +00002260SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002261 MachineFunction &MF = DAG.getMachineFunction();
2262 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2263 int ReturnAddrIndex = FuncInfo->getRAIndex();
2264
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002265 if (ReturnAddrIndex == 0) {
2266 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002267 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002268 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2269 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002270 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002271 }
2272
Evan Cheng25ab6902006-09-08 06:48:29 +00002273 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002274}
2275
2276
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002277bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2278 bool hasSymbolicDisplacement) {
2279 // Offset should fit into 32 bit immediate field.
2280 if (!isInt32(Offset))
2281 return false;
2282
2283 // If we don't have a symbolic displacement - we don't have any extra
2284 // restrictions.
2285 if (!hasSymbolicDisplacement)
2286 return true;
2287
2288 // FIXME: Some tweaks might be needed for medium code model.
2289 if (M != CodeModel::Small && M != CodeModel::Kernel)
2290 return false;
2291
2292 // For small code model we assume that latest object is 16MB before end of 31
2293 // bits boundary. We may also accept pretty large negative constants knowing
2294 // that all objects are in the positive half of address space.
2295 if (M == CodeModel::Small && Offset < 16*1024*1024)
2296 return true;
2297
2298 // For kernel code model we know that all object resist in the negative half
2299 // of 32bits address space. We may not accept negative offsets, since they may
2300 // be just off and we may accept pretty large positive ones.
2301 if (M == CodeModel::Kernel && Offset > 0)
2302 return true;
2303
2304 return false;
2305}
2306
Evan Chengddc419c2010-01-26 19:04:47 +00002307bool X86::IsEligibleForTailCallOpt(CallingConv::ID CC) {
2308 return PerformTailCallOpt && CC == CallingConv::Fast;
2309}
2310
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002311/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2312/// specific condition code, returning the condition code and the LHS/RHS of the
2313/// comparison to make.
2314static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2315 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002316 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002317 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2318 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2319 // X > -1 -> X == 0, jump !sign.
2320 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002321 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002322 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2323 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002324 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002325 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002326 // X < 1 -> X <= 0
2327 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002328 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002329 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002330 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002331
Evan Chengd9558e02006-01-06 00:43:03 +00002332 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002333 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002334 case ISD::SETEQ: return X86::COND_E;
2335 case ISD::SETGT: return X86::COND_G;
2336 case ISD::SETGE: return X86::COND_GE;
2337 case ISD::SETLT: return X86::COND_L;
2338 case ISD::SETLE: return X86::COND_LE;
2339 case ISD::SETNE: return X86::COND_NE;
2340 case ISD::SETULT: return X86::COND_B;
2341 case ISD::SETUGT: return X86::COND_A;
2342 case ISD::SETULE: return X86::COND_BE;
2343 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002344 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002346
Chris Lattner4c78e022008-12-23 23:42:27 +00002347 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002348
Chris Lattner4c78e022008-12-23 23:42:27 +00002349 // If LHS is a foldable load, but RHS is not, flip the condition.
2350 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2351 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2352 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2353 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002354 }
2355
Chris Lattner4c78e022008-12-23 23:42:27 +00002356 switch (SetCCOpcode) {
2357 default: break;
2358 case ISD::SETOLT:
2359 case ISD::SETOLE:
2360 case ISD::SETUGT:
2361 case ISD::SETUGE:
2362 std::swap(LHS, RHS);
2363 break;
2364 }
2365
2366 // On a floating point condition, the flags are set as follows:
2367 // ZF PF CF op
2368 // 0 | 0 | 0 | X > Y
2369 // 0 | 0 | 1 | X < Y
2370 // 1 | 0 | 0 | X == Y
2371 // 1 | 1 | 1 | unordered
2372 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002373 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002374 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002375 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002376 case ISD::SETOLT: // flipped
2377 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002378 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002379 case ISD::SETOLE: // flipped
2380 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002381 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002382 case ISD::SETUGT: // flipped
2383 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002384 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002385 case ISD::SETUGE: // flipped
2386 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002387 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002388 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002389 case ISD::SETNE: return X86::COND_NE;
2390 case ISD::SETUO: return X86::COND_P;
2391 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002392 case ISD::SETOEQ:
2393 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002394 }
Evan Chengd9558e02006-01-06 00:43:03 +00002395}
2396
Evan Cheng4a460802006-01-11 00:33:36 +00002397/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2398/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002399/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002400static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002401 switch (X86CC) {
2402 default:
2403 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002404 case X86::COND_B:
2405 case X86::COND_BE:
2406 case X86::COND_E:
2407 case X86::COND_P:
2408 case X86::COND_A:
2409 case X86::COND_AE:
2410 case X86::COND_NE:
2411 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002412 return true;
2413 }
2414}
2415
Evan Chengeb2f9692009-10-27 19:56:55 +00002416/// isFPImmLegal - Returns true if the target can instruction select the
2417/// specified FP immediate natively. If false, the legalizer will
2418/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002419bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002420 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2421 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2422 return true;
2423 }
2424 return false;
2425}
2426
Nate Begeman9008ca62009-04-27 18:41:29 +00002427/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2428/// the specified range (L, H].
2429static bool isUndefOrInRange(int Val, int Low, int Hi) {
2430 return (Val < 0) || (Val >= Low && Val < Hi);
2431}
2432
2433/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2434/// specified value.
2435static bool isUndefOrEqual(int Val, int CmpVal) {
2436 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002437 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002438 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002439}
2440
Nate Begeman9008ca62009-04-27 18:41:29 +00002441/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2442/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2443/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002444static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002445 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002446 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002447 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002448 return (Mask[0] < 2 && Mask[1] < 2);
2449 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002450}
2451
Nate Begeman9008ca62009-04-27 18:41:29 +00002452bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002453 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002454 N->getMask(M);
2455 return ::isPSHUFDMask(M, N->getValueType(0));
2456}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002457
Nate Begeman9008ca62009-04-27 18:41:29 +00002458/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2459/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002460static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002461 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002462 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002463
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 // Lower quadword copied in order or undef.
2465 for (int i = 0; i != 4; ++i)
2466 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002467 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002468
Evan Cheng506d3df2006-03-29 23:07:14 +00002469 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002470 for (int i = 4; i != 8; ++i)
2471 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002472 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002473
Evan Cheng506d3df2006-03-29 23:07:14 +00002474 return true;
2475}
2476
Nate Begeman9008ca62009-04-27 18:41:29 +00002477bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002478 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002479 N->getMask(M);
2480 return ::isPSHUFHWMask(M, N->getValueType(0));
2481}
Evan Cheng506d3df2006-03-29 23:07:14 +00002482
Nate Begeman9008ca62009-04-27 18:41:29 +00002483/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2484/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002485static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002486 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002487 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002488
Rafael Espindola15684b22009-04-24 12:40:33 +00002489 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002490 for (int i = 4; i != 8; ++i)
2491 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002492 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002493
Rafael Espindola15684b22009-04-24 12:40:33 +00002494 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002495 for (int i = 0; i != 4; ++i)
2496 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002497 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002498
Rafael Espindola15684b22009-04-24 12:40:33 +00002499 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002500}
2501
Nate Begeman9008ca62009-04-27 18:41:29 +00002502bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002503 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002504 N->getMask(M);
2505 return ::isPSHUFLWMask(M, N->getValueType(0));
2506}
2507
Nate Begemana09008b2009-10-19 02:17:23 +00002508/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2509/// is suitable for input to PALIGNR.
2510static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2511 bool hasSSSE3) {
2512 int i, e = VT.getVectorNumElements();
2513
2514 // Do not handle v2i64 / v2f64 shuffles with palignr.
2515 if (e < 4 || !hasSSSE3)
2516 return false;
2517
2518 for (i = 0; i != e; ++i)
2519 if (Mask[i] >= 0)
2520 break;
2521
2522 // All undef, not a palignr.
2523 if (i == e)
2524 return false;
2525
2526 // Determine if it's ok to perform a palignr with only the LHS, since we
2527 // don't have access to the actual shuffle elements to see if RHS is undef.
2528 bool Unary = Mask[i] < (int)e;
2529 bool NeedsUnary = false;
2530
2531 int s = Mask[i] - i;
2532
2533 // Check the rest of the elements to see if they are consecutive.
2534 for (++i; i != e; ++i) {
2535 int m = Mask[i];
2536 if (m < 0)
2537 continue;
2538
2539 Unary = Unary && (m < (int)e);
2540 NeedsUnary = NeedsUnary || (m < s);
2541
2542 if (NeedsUnary && !Unary)
2543 return false;
2544 if (Unary && m != ((s+i) & (e-1)))
2545 return false;
2546 if (!Unary && m != (s+i))
2547 return false;
2548 }
2549 return true;
2550}
2551
2552bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2553 SmallVector<int, 8> M;
2554 N->getMask(M);
2555 return ::isPALIGNRMask(M, N->getValueType(0), true);
2556}
2557
Evan Cheng14aed5e2006-03-24 01:18:28 +00002558/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2559/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002560static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002561 int NumElems = VT.getVectorNumElements();
2562 if (NumElems != 2 && NumElems != 4)
2563 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002564
Nate Begeman9008ca62009-04-27 18:41:29 +00002565 int Half = NumElems / 2;
2566 for (int i = 0; i < Half; ++i)
2567 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002568 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 for (int i = Half; i < NumElems; ++i)
2570 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002571 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002572
Evan Cheng14aed5e2006-03-24 01:18:28 +00002573 return true;
2574}
2575
Nate Begeman9008ca62009-04-27 18:41:29 +00002576bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2577 SmallVector<int, 8> M;
2578 N->getMask(M);
2579 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002580}
2581
Evan Cheng213d2cf2007-05-17 18:45:50 +00002582/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002583/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2584/// half elements to come from vector 1 (which would equal the dest.) and
2585/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002586static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002587 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002588
2589 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002590 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002591
Nate Begeman9008ca62009-04-27 18:41:29 +00002592 int Half = NumElems / 2;
2593 for (int i = 0; i < Half; ++i)
2594 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002595 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 for (int i = Half; i < NumElems; ++i)
2597 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002598 return false;
2599 return true;
2600}
2601
Nate Begeman9008ca62009-04-27 18:41:29 +00002602static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2603 SmallVector<int, 8> M;
2604 N->getMask(M);
2605 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002606}
2607
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002608/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2609/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002610bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2611 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002612 return false;
2613
Evan Cheng2064a2b2006-03-28 06:50:32 +00002614 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2616 isUndefOrEqual(N->getMaskElt(1), 7) &&
2617 isUndefOrEqual(N->getMaskElt(2), 2) &&
2618 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002619}
2620
Nate Begeman0b10b912009-11-07 23:17:15 +00002621/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2622/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2623/// <2, 3, 2, 3>
2624bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2625 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2626
2627 if (NumElems != 4)
2628 return false;
2629
2630 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2631 isUndefOrEqual(N->getMaskElt(1), 3) &&
2632 isUndefOrEqual(N->getMaskElt(2), 2) &&
2633 isUndefOrEqual(N->getMaskElt(3), 3);
2634}
2635
Evan Cheng5ced1d82006-04-06 23:23:56 +00002636/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2637/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002638bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2639 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002640
Evan Cheng5ced1d82006-04-06 23:23:56 +00002641 if (NumElems != 2 && NumElems != 4)
2642 return false;
2643
Evan Chengc5cdff22006-04-07 21:53:05 +00002644 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002645 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002646 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002647
Evan Chengc5cdff22006-04-07 21:53:05 +00002648 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002649 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002650 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002651
2652 return true;
2653}
2654
Nate Begeman0b10b912009-11-07 23:17:15 +00002655/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2656/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2657bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002659
Evan Cheng5ced1d82006-04-06 23:23:56 +00002660 if (NumElems != 2 && NumElems != 4)
2661 return false;
2662
Evan Chengc5cdff22006-04-07 21:53:05 +00002663 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002665 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002666
Nate Begeman9008ca62009-04-27 18:41:29 +00002667 for (unsigned i = 0; i < NumElems/2; ++i)
2668 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002669 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002670
2671 return true;
2672}
2673
Evan Cheng0038e592006-03-28 00:39:58 +00002674/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2675/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002676static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002677 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002678 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002679 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002680 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002681
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2683 int BitI = Mask[i];
2684 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002685 if (!isUndefOrEqual(BitI, j))
2686 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002687 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002688 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002689 return false;
2690 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002691 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002692 return false;
2693 }
Evan Cheng0038e592006-03-28 00:39:58 +00002694 }
Evan Cheng0038e592006-03-28 00:39:58 +00002695 return true;
2696}
2697
Nate Begeman9008ca62009-04-27 18:41:29 +00002698bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2699 SmallVector<int, 8> M;
2700 N->getMask(M);
2701 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002702}
2703
Evan Cheng4fcb9222006-03-28 02:43:26 +00002704/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2705/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002706static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002707 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002708 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002709 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002710 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002711
Nate Begeman9008ca62009-04-27 18:41:29 +00002712 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2713 int BitI = Mask[i];
2714 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002715 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002716 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002717 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002718 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002719 return false;
2720 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002721 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002722 return false;
2723 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002724 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002725 return true;
2726}
2727
Nate Begeman9008ca62009-04-27 18:41:29 +00002728bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2729 SmallVector<int, 8> M;
2730 N->getMask(M);
2731 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002732}
2733
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002734/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2735/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2736/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002737static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002738 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002739 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002740 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002741
Nate Begeman9008ca62009-04-27 18:41:29 +00002742 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2743 int BitI = Mask[i];
2744 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002745 if (!isUndefOrEqual(BitI, j))
2746 return false;
2747 if (!isUndefOrEqual(BitI1, j))
2748 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002749 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002750 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002751}
2752
Nate Begeman9008ca62009-04-27 18:41:29 +00002753bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2754 SmallVector<int, 8> M;
2755 N->getMask(M);
2756 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2757}
2758
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002759/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2760/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2761/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002762static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002764 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2765 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002766
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2768 int BitI = Mask[i];
2769 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002770 if (!isUndefOrEqual(BitI, j))
2771 return false;
2772 if (!isUndefOrEqual(BitI1, j))
2773 return false;
2774 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002775 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002776}
2777
Nate Begeman9008ca62009-04-27 18:41:29 +00002778bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2779 SmallVector<int, 8> M;
2780 N->getMask(M);
2781 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2782}
2783
Evan Cheng017dcc62006-04-21 01:05:10 +00002784/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2785/// specifies a shuffle of elements that is suitable for input to MOVSS,
2786/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002787static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002788 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002789 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002790
2791 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002792
Nate Begeman9008ca62009-04-27 18:41:29 +00002793 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002794 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002795
Nate Begeman9008ca62009-04-27 18:41:29 +00002796 for (int i = 1; i < NumElts; ++i)
2797 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002798 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002799
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002800 return true;
2801}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002802
Nate Begeman9008ca62009-04-27 18:41:29 +00002803bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2804 SmallVector<int, 8> M;
2805 N->getMask(M);
2806 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002807}
2808
Evan Cheng017dcc62006-04-21 01:05:10 +00002809/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2810/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002811/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002812static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 bool V2IsSplat = false, bool V2IsUndef = false) {
2814 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002815 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002816 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002817
Nate Begeman9008ca62009-04-27 18:41:29 +00002818 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002819 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002820
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 for (int i = 1; i < NumOps; ++i)
2822 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2823 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2824 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002825 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002826
Evan Cheng39623da2006-04-20 08:58:49 +00002827 return true;
2828}
2829
Nate Begeman9008ca62009-04-27 18:41:29 +00002830static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002831 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002832 SmallVector<int, 8> M;
2833 N->getMask(M);
2834 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002835}
2836
Evan Chengd9539472006-04-14 21:59:03 +00002837/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2838/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002839bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2840 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002841 return false;
2842
2843 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002844 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002845 int Elt = N->getMaskElt(i);
2846 if (Elt >= 0 && Elt != 1)
2847 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002848 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002849
2850 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002851 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002852 int Elt = N->getMaskElt(i);
2853 if (Elt >= 0 && Elt != 3)
2854 return false;
2855 if (Elt == 3)
2856 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002857 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002858 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002860 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002861}
2862
2863/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2864/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002865bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2866 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002867 return false;
2868
2869 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 for (unsigned i = 0; i < 2; ++i)
2871 if (N->getMaskElt(i) > 0)
2872 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002873
2874 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002875 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 int Elt = N->getMaskElt(i);
2877 if (Elt >= 0 && Elt != 2)
2878 return false;
2879 if (Elt == 2)
2880 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002881 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002882 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002883 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002884}
2885
Evan Cheng0b457f02008-09-25 20:50:48 +00002886/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2887/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002888bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2889 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002890
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 for (int i = 0; i < e; ++i)
2892 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002893 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002894 for (int i = 0; i < e; ++i)
2895 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002896 return false;
2897 return true;
2898}
2899
Evan Cheng63d33002006-03-22 08:01:21 +00002900/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002901/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002902unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002903 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2904 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2905
Evan Chengb9df0ca2006-03-22 02:53:00 +00002906 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2907 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002908 for (int i = 0; i < NumOperands; ++i) {
2909 int Val = SVOp->getMaskElt(NumOperands-i-1);
2910 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002911 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002912 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002913 if (i != NumOperands - 1)
2914 Mask <<= Shift;
2915 }
Evan Cheng63d33002006-03-22 08:01:21 +00002916 return Mask;
2917}
2918
Evan Cheng506d3df2006-03-29 23:07:14 +00002919/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002920/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002921unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002923 unsigned Mask = 0;
2924 // 8 nodes, but we only care about the last 4.
2925 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 int Val = SVOp->getMaskElt(i);
2927 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002928 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002929 if (i != 4)
2930 Mask <<= 2;
2931 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002932 return Mask;
2933}
2934
2935/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002936/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002937unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002939 unsigned Mask = 0;
2940 // 8 nodes, but we only care about the first 4.
2941 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002942 int Val = SVOp->getMaskElt(i);
2943 if (Val >= 0)
2944 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002945 if (i != 0)
2946 Mask <<= 2;
2947 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002948 return Mask;
2949}
2950
Nate Begemana09008b2009-10-19 02:17:23 +00002951/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2952/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2953unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2954 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2955 EVT VVT = N->getValueType(0);
2956 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2957 int Val = 0;
2958
2959 unsigned i, e;
2960 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2961 Val = SVOp->getMaskElt(i);
2962 if (Val >= 0)
2963 break;
2964 }
2965 return (Val - i) * EltSize;
2966}
2967
Evan Cheng37b73872009-07-30 08:33:02 +00002968/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2969/// constant +0.0.
2970bool X86::isZeroNode(SDValue Elt) {
2971 return ((isa<ConstantSDNode>(Elt) &&
2972 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2973 (isa<ConstantFPSDNode>(Elt) &&
2974 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2975}
2976
Nate Begeman9008ca62009-04-27 18:41:29 +00002977/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2978/// their permute mask.
2979static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2980 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002981 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002982 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002983 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002984
Nate Begeman5a5ca152009-04-29 05:20:52 +00002985 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002986 int idx = SVOp->getMaskElt(i);
2987 if (idx < 0)
2988 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002989 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002991 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002992 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002993 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2995 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002996}
2997
Evan Cheng779ccea2007-12-07 21:30:01 +00002998/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2999/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003000static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003001 unsigned NumElems = VT.getVectorNumElements();
3002 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 int idx = Mask[i];
3004 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003005 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003006 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003008 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003009 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003010 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003011}
3012
Evan Cheng533a0aa2006-04-19 20:35:22 +00003013/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3014/// match movhlps. The lower half elements should come from upper half of
3015/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003016/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003017static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3018 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003019 return false;
3020 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003022 return false;
3023 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003025 return false;
3026 return true;
3027}
3028
Evan Cheng5ced1d82006-04-06 23:23:56 +00003029/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003030/// is promoted to a vector. It also returns the LoadSDNode by reference if
3031/// required.
3032static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003033 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3034 return false;
3035 N = N->getOperand(0).getNode();
3036 if (!ISD::isNON_EXTLoad(N))
3037 return false;
3038 if (LD)
3039 *LD = cast<LoadSDNode>(N);
3040 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003041}
3042
Evan Cheng533a0aa2006-04-19 20:35:22 +00003043/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3044/// match movlp{s|d}. The lower half elements should come from lower half of
3045/// V1 (and in order), and the upper half elements should come from the upper
3046/// half of V2 (and in order). And since V1 will become the source of the
3047/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003048static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3049 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003050 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003051 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003052 // Is V2 is a vector load, don't do this transformation. We will try to use
3053 // load folding shufps op.
3054 if (ISD::isNON_EXTLoad(V2))
3055 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003056
Nate Begeman5a5ca152009-04-29 05:20:52 +00003057 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003058
Evan Cheng533a0aa2006-04-19 20:35:22 +00003059 if (NumElems != 2 && NumElems != 4)
3060 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003061 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003063 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003064 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003066 return false;
3067 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003068}
3069
Evan Cheng39623da2006-04-20 08:58:49 +00003070/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3071/// all the same.
3072static bool isSplatVector(SDNode *N) {
3073 if (N->getOpcode() != ISD::BUILD_VECTOR)
3074 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003075
Dan Gohman475871a2008-07-27 21:46:04 +00003076 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003077 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3078 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003079 return false;
3080 return true;
3081}
3082
Evan Cheng213d2cf2007-05-17 18:45:50 +00003083/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003084/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003085/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003086static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003087 SDValue V1 = N->getOperand(0);
3088 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003089 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3090 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003092 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003094 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3095 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003096 if (Opc != ISD::BUILD_VECTOR ||
3097 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 return false;
3099 } else if (Idx >= 0) {
3100 unsigned Opc = V1.getOpcode();
3101 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3102 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003103 if (Opc != ISD::BUILD_VECTOR ||
3104 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003105 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003106 }
3107 }
3108 return true;
3109}
3110
3111/// getZeroVector - Returns a vector of specified type with all zero elements.
3112///
Owen Andersone50ed302009-08-10 22:56:29 +00003113static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003114 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003115 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003116
Chris Lattner8a594482007-11-25 00:24:49 +00003117 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3118 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003119 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003120 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3122 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003123 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3125 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003126 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3128 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003129 }
Dale Johannesenace16102009-02-03 19:33:06 +00003130 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003131}
3132
Chris Lattner8a594482007-11-25 00:24:49 +00003133/// getOnesVector - Returns a vector of specified type with all bits set.
3134///
Owen Andersone50ed302009-08-10 22:56:29 +00003135static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003136 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003137
Chris Lattner8a594482007-11-25 00:24:49 +00003138 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3139 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003140 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003141 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003142 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003143 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003144 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003146 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003147}
3148
3149
Evan Cheng39623da2006-04-20 08:58:49 +00003150/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3151/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003152static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003153 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003154 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003155
Evan Cheng39623da2006-04-20 08:58:49 +00003156 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 SmallVector<int, 8> MaskVec;
3158 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Nate Begeman5a5ca152009-04-29 05:20:52 +00003160 for (unsigned i = 0; i != NumElems; ++i) {
3161 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 MaskVec[i] = NumElems;
3163 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003164 }
Evan Cheng39623da2006-04-20 08:58:49 +00003165 }
Evan Cheng39623da2006-04-20 08:58:49 +00003166 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3168 SVOp->getOperand(1), &MaskVec[0]);
3169 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003170}
3171
Evan Cheng017dcc62006-04-21 01:05:10 +00003172/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3173/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003174static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 SDValue V2) {
3176 unsigned NumElems = VT.getVectorNumElements();
3177 SmallVector<int, 8> Mask;
3178 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003179 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 Mask.push_back(i);
3181 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003182}
3183
Nate Begeman9008ca62009-04-27 18:41:29 +00003184/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003185static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 SDValue V2) {
3187 unsigned NumElems = VT.getVectorNumElements();
3188 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003189 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 Mask.push_back(i);
3191 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003192 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003194}
3195
Nate Begeman9008ca62009-04-27 18:41:29 +00003196/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003197static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 SDValue V2) {
3199 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003200 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003202 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 Mask.push_back(i + Half);
3204 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003205 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003207}
3208
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003209/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003210static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 bool HasSSE2) {
3212 if (SV->getValueType(0).getVectorNumElements() <= 4)
3213 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003214
Owen Anderson825b72b2009-08-11 20:47:22 +00003215 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003216 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 DebugLoc dl = SV->getDebugLoc();
3218 SDValue V1 = SV->getOperand(0);
3219 int NumElems = VT.getVectorNumElements();
3220 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003221
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 // unpack elements to the correct location
3223 while (NumElems > 4) {
3224 if (EltNo < NumElems/2) {
3225 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3226 } else {
3227 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3228 EltNo -= NumElems/2;
3229 }
3230 NumElems >>= 1;
3231 }
Eric Christopherfd179292009-08-27 18:07:15 +00003232
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 // Perform the splat.
3234 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003235 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3237 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003238}
3239
Evan Chengba05f722006-04-21 23:03:30 +00003240/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003241/// vector of zero or undef vector. This produces a shuffle where the low
3242/// element of V2 is swizzled into the zero/undef vector, landing at element
3243/// 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 +00003244static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003245 bool isZero, bool HasSSE2,
3246 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003247 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003248 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3250 unsigned NumElems = VT.getVectorNumElements();
3251 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003252 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 // If this is the insertion idx, put the low elt of V2 here.
3254 MaskVec.push_back(i == Idx ? NumElems : i);
3255 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003256}
3257
Evan Chengf26ffe92008-05-29 08:22:04 +00003258/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3259/// a shuffle that is zero.
3260static
Nate Begeman9008ca62009-04-27 18:41:29 +00003261unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3262 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003263 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003265 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003266 int Idx = SVOp->getMaskElt(Index);
3267 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003268 ++NumZeros;
3269 continue;
3270 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003271 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003272 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003273 ++NumZeros;
3274 else
3275 break;
3276 }
3277 return NumZeros;
3278}
3279
3280/// isVectorShift - Returns true if the shuffle can be implemented as a
3281/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003282/// FIXME: split into pslldqi, psrldqi, palignr variants.
3283static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003284 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003285 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003286
3287 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003289 if (!NumZeros) {
3290 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003291 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003292 if (!NumZeros)
3293 return false;
3294 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003295 bool SeenV1 = false;
3296 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003297 for (int i = NumZeros; i < NumElems; ++i) {
3298 int Val = isLeft ? (i - NumZeros) : i;
3299 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3300 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003301 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003302 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003303 SeenV1 = true;
3304 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003306 SeenV2 = true;
3307 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003308 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003309 return false;
3310 }
3311 if (SeenV1 && SeenV2)
3312 return false;
3313
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003315 ShAmt = NumZeros;
3316 return true;
3317}
3318
3319
Evan Chengc78d3b42006-04-24 18:01:45 +00003320/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3321///
Dan Gohman475871a2008-07-27 21:46:04 +00003322static SDValue LowerBuildVectorv16i8(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 > 8)
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 < 16; ++i) {
3332 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3333 if (ThisIsNonZero && First) {
3334 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003336 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003338 First = false;
3339 }
3340
3341 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003342 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003343 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3344 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003345 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003347 }
3348 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3350 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3351 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003352 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003354 } else
3355 ThisElt = LastElt;
3356
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003359 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003360 }
3361 }
3362
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003364}
3365
Bill Wendlinga348c562007-03-22 18:42:45 +00003366/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003367///
Dan Gohman475871a2008-07-27 21:46:04 +00003368static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003369 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003370 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003371 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003372 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003373
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003374 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003375 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003376 bool First = true;
3377 for (unsigned i = 0; i < 8; ++i) {
3378 bool isNonZero = (NonZeros & (1 << i)) != 0;
3379 if (isNonZero) {
3380 if (First) {
3381 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003383 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003385 First = false;
3386 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003387 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003389 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003390 }
3391 }
3392
3393 return V;
3394}
3395
Evan Chengf26ffe92008-05-29 08:22:04 +00003396/// getVShift - Return a vector logical shift node.
3397///
Owen Andersone50ed302009-08-10 22:56:29 +00003398static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 unsigned NumBits, SelectionDAG &DAG,
3400 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003401 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003403 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003404 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3405 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3406 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003407 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003408}
3409
Dan Gohman475871a2008-07-27 21:46:04 +00003410SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003411X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3412 SelectionDAG &DAG) {
3413
3414 // Check if the scalar load can be widened into a vector load. And if
3415 // the address is "base + cst" see if the cst can be "absorbed" into
3416 // the shuffle mask.
3417 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3418 SDValue Ptr = LD->getBasePtr();
3419 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3420 return SDValue();
3421 EVT PVT = LD->getValueType(0);
3422 if (PVT != MVT::i32 && PVT != MVT::f32)
3423 return SDValue();
3424
3425 int FI = -1;
3426 int64_t Offset = 0;
3427 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3428 FI = FINode->getIndex();
3429 Offset = 0;
3430 } else if (Ptr.getOpcode() == ISD::ADD &&
3431 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3432 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3433 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3434 Offset = Ptr.getConstantOperandVal(1);
3435 Ptr = Ptr.getOperand(0);
3436 } else {
3437 return SDValue();
3438 }
3439
3440 SDValue Chain = LD->getChain();
3441 // Make sure the stack object alignment is at least 16.
3442 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3443 if (DAG.InferPtrAlignment(Ptr) < 16) {
3444 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003445 // Can't change the alignment. FIXME: It's possible to compute
3446 // the exact stack offset and reference FI + adjust offset instead.
3447 // If someone *really* cares about this. That's the way to implement it.
3448 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003449 } else {
3450 MFI->setObjectAlignment(FI, 16);
3451 }
3452 }
3453
3454 // (Offset % 16) must be multiple of 4. Then address is then
3455 // Ptr + (Offset & ~15).
3456 if (Offset < 0)
3457 return SDValue();
3458 if ((Offset % 16) & 3)
3459 return SDValue();
3460 int64_t StartOffset = Offset & ~15;
3461 if (StartOffset)
3462 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3463 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3464
3465 int EltNo = (Offset - StartOffset) >> 2;
3466 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3467 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3468 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3469 // Canonicalize it to a v4i32 shuffle.
3470 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3471 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3472 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3473 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3474 }
3475
3476 return SDValue();
3477}
3478
3479SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003480X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003481 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003482 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003483 if (ISD::isBuildVectorAllZeros(Op.getNode())
3484 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003485 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3486 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3487 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003489 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003490
Gabor Greifba36cb52008-08-28 21:40:38 +00003491 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003492 return getOnesVector(Op.getValueType(), DAG, dl);
3493 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003494 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003495
Owen Andersone50ed302009-08-10 22:56:29 +00003496 EVT VT = Op.getValueType();
3497 EVT ExtVT = VT.getVectorElementType();
3498 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003499
3500 unsigned NumElems = Op.getNumOperands();
3501 unsigned NumZero = 0;
3502 unsigned NumNonZero = 0;
3503 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003504 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003505 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003506 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003507 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003508 if (Elt.getOpcode() == ISD::UNDEF)
3509 continue;
3510 Values.insert(Elt);
3511 if (Elt.getOpcode() != ISD::Constant &&
3512 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003513 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003514 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003515 NumZero++;
3516 else {
3517 NonZeros |= (1 << i);
3518 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003519 }
3520 }
3521
Dan Gohman7f321562007-06-25 16:23:39 +00003522 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003523 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003524 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003525 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003526
Chris Lattner67f453a2008-03-09 05:42:06 +00003527 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003528 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003529 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003531
Chris Lattner62098042008-03-09 01:05:04 +00003532 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3533 // the value are obviously zero, truncate the value to i32 and do the
3534 // insertion that way. Only do this if the value is non-constant or if the
3535 // value is a constant being inserted into element 0. It is cheaper to do
3536 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003538 (!IsAllConstants || Idx == 0)) {
3539 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3540 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3542 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003543
Chris Lattner62098042008-03-09 01:05:04 +00003544 // Truncate the value (which may itself be a constant) to i32, and
3545 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003547 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003548 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3549 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003550
Chris Lattner62098042008-03-09 01:05:04 +00003551 // Now we have our 32-bit value zero extended in the low element of
3552 // a vector. If Idx != 0, swizzle it into place.
3553 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003554 SmallVector<int, 4> Mask;
3555 Mask.push_back(Idx);
3556 for (unsigned i = 1; i != VecElts; ++i)
3557 Mask.push_back(i);
3558 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003559 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003560 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003561 }
Dale Johannesenace16102009-02-03 19:33:06 +00003562 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003563 }
3564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003565
Chris Lattner19f79692008-03-08 22:59:52 +00003566 // If we have a constant or non-constant insertion into the low element of
3567 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3568 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003569 // depending on what the source datatype is.
3570 if (Idx == 0) {
3571 if (NumZero == 0) {
3572 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3574 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003575 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3576 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3577 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3578 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3580 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3581 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003582 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3583 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3584 Subtarget->hasSSE2(), DAG);
3585 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3586 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003587 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003588
3589 // Is it a vector logical left shift?
3590 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003591 X86::isZeroNode(Op.getOperand(0)) &&
3592 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003593 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003594 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003595 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003596 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003597 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003599
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003600 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003601 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003602
Chris Lattner19f79692008-03-08 22:59:52 +00003603 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3604 // is a non-constant being inserted into an element other than the low one,
3605 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3606 // movd/movss) to move this into the low element, then shuffle it into
3607 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003608 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003609 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003610
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003612 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3613 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003615 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003616 MaskVec.push_back(i == Idx ? 0 : 1);
3617 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003618 }
3619 }
3620
Chris Lattner67f453a2008-03-09 05:42:06 +00003621 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003622 if (Values.size() == 1) {
3623 if (EVTBits == 32) {
3624 // Instead of a shuffle like this:
3625 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3626 // Check if it's possible to issue this instead.
3627 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3628 unsigned Idx = CountTrailingZeros_32(NonZeros);
3629 SDValue Item = Op.getOperand(Idx);
3630 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3631 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3632 }
Dan Gohman475871a2008-07-27 21:46:04 +00003633 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003635
Dan Gohmana3941172007-07-24 22:55:08 +00003636 // A vector full of immediates; various special cases are already
3637 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003638 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003639 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003640
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003641 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003642 if (EVTBits == 64) {
3643 if (NumNonZero == 1) {
3644 // One half is zero or undef.
3645 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003646 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003647 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003648 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3649 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003650 }
Dan Gohman475871a2008-07-27 21:46:04 +00003651 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003652 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003653
3654 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003655 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003656 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003657 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003658 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003659 }
3660
Bill Wendling826f36f2007-03-28 00:57:11 +00003661 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003662 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003663 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003664 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003665 }
3666
3667 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003668 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003669 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003670 if (NumElems == 4 && NumZero > 0) {
3671 for (unsigned i = 0; i < 4; ++i) {
3672 bool isZero = !(NonZeros & (1 << i));
3673 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003674 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003675 else
Dale Johannesenace16102009-02-03 19:33:06 +00003676 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003677 }
3678
3679 for (unsigned i = 0; i < 2; ++i) {
3680 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3681 default: break;
3682 case 0:
3683 V[i] = V[i*2]; // Must be a zero vector.
3684 break;
3685 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003686 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003687 break;
3688 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003689 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003690 break;
3691 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003693 break;
3694 }
3695 }
3696
Nate Begeman9008ca62009-04-27 18:41:29 +00003697 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003698 bool Reverse = (NonZeros & 0x3) == 2;
3699 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003700 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003701 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3702 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003703 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3704 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003705 }
3706
3707 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003708 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3709 // values to be inserted is equal to the number of elements, in which case
3710 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003711 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003713 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003714 getSubtarget()->hasSSE41()) {
3715 V[0] = DAG.getUNDEF(VT);
3716 for (unsigned i = 0; i < NumElems; ++i)
3717 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3718 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3719 Op.getOperand(i), DAG.getIntPtrConstant(i));
3720 return V[0];
3721 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003722 // Expand into a number of unpckl*.
3723 // e.g. for v4f32
3724 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3725 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3726 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003727 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003728 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 NumElems >>= 1;
3730 while (NumElems != 0) {
3731 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003732 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733 NumElems >>= 1;
3734 }
3735 return V[0];
3736 }
3737
Dan Gohman475871a2008-07-27 21:46:04 +00003738 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003739}
3740
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003741SDValue
3742X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3743 // We support concatenate two MMX registers and place them in a MMX
3744 // register. This is better than doing a stack convert.
3745 DebugLoc dl = Op.getDebugLoc();
3746 EVT ResVT = Op.getValueType();
3747 assert(Op.getNumOperands() == 2);
3748 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3749 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3750 int Mask[2];
3751 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3752 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3753 InVec = Op.getOperand(1);
3754 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3755 unsigned NumElts = ResVT.getVectorNumElements();
3756 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3757 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3758 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3759 } else {
3760 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3761 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3762 Mask[0] = 0; Mask[1] = 2;
3763 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3764 }
3765 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3766}
3767
Nate Begemanb9a47b82009-02-23 08:49:38 +00003768// v8i16 shuffles - Prefer shuffles in the following order:
3769// 1. [all] pshuflw, pshufhw, optional move
3770// 2. [ssse3] 1 x pshufb
3771// 3. [ssse3] 2 x pshufb + 1 x por
3772// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003773static
Nate Begeman9008ca62009-04-27 18:41:29 +00003774SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3775 SelectionDAG &DAG, X86TargetLowering &TLI) {
3776 SDValue V1 = SVOp->getOperand(0);
3777 SDValue V2 = SVOp->getOperand(1);
3778 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003779 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003780
Nate Begemanb9a47b82009-02-23 08:49:38 +00003781 // Determine if more than 1 of the words in each of the low and high quadwords
3782 // of the result come from the same quadword of one of the two inputs. Undef
3783 // mask values count as coming from any quadword, for better codegen.
3784 SmallVector<unsigned, 4> LoQuad(4);
3785 SmallVector<unsigned, 4> HiQuad(4);
3786 BitVector InputQuads(4);
3787 for (unsigned i = 0; i < 8; ++i) {
3788 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003789 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003790 MaskVals.push_back(EltIdx);
3791 if (EltIdx < 0) {
3792 ++Quad[0];
3793 ++Quad[1];
3794 ++Quad[2];
3795 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003796 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003797 }
3798 ++Quad[EltIdx / 4];
3799 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003800 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003801
Nate Begemanb9a47b82009-02-23 08:49:38 +00003802 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003803 unsigned MaxQuad = 1;
3804 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003805 if (LoQuad[i] > MaxQuad) {
3806 BestLoQuad = i;
3807 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003808 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003809 }
3810
Nate Begemanb9a47b82009-02-23 08:49:38 +00003811 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003812 MaxQuad = 1;
3813 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003814 if (HiQuad[i] > MaxQuad) {
3815 BestHiQuad = i;
3816 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003817 }
3818 }
3819
Nate Begemanb9a47b82009-02-23 08:49:38 +00003820 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003821 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003822 // single pshufb instruction is necessary. If There are more than 2 input
3823 // quads, disable the next transformation since it does not help SSSE3.
3824 bool V1Used = InputQuads[0] || InputQuads[1];
3825 bool V2Used = InputQuads[2] || InputQuads[3];
3826 if (TLI.getSubtarget()->hasSSSE3()) {
3827 if (InputQuads.count() == 2 && V1Used && V2Used) {
3828 BestLoQuad = InputQuads.find_first();
3829 BestHiQuad = InputQuads.find_next(BestLoQuad);
3830 }
3831 if (InputQuads.count() > 2) {
3832 BestLoQuad = -1;
3833 BestHiQuad = -1;
3834 }
3835 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003836
Nate Begemanb9a47b82009-02-23 08:49:38 +00003837 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3838 // the shuffle mask. If a quad is scored as -1, that means that it contains
3839 // words from all 4 input quadwords.
3840 SDValue NewV;
3841 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003842 SmallVector<int, 8> MaskV;
3843 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3844 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003845 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3847 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3848 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003849
Nate Begemanb9a47b82009-02-23 08:49:38 +00003850 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3851 // source words for the shuffle, to aid later transformations.
3852 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003853 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003854 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003855 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003856 if (idx != (int)i)
3857 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003858 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003859 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003860 AllWordsInNewV = false;
3861 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003862 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003863
Nate Begemanb9a47b82009-02-23 08:49:38 +00003864 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3865 if (AllWordsInNewV) {
3866 for (int i = 0; i != 8; ++i) {
3867 int idx = MaskVals[i];
3868 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003869 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003870 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003871 if ((idx != i) && idx < 4)
3872 pshufhw = false;
3873 if ((idx != i) && idx > 3)
3874 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003875 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003876 V1 = NewV;
3877 V2Used = false;
3878 BestLoQuad = 0;
3879 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003880 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003881
Nate Begemanb9a47b82009-02-23 08:49:38 +00003882 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3883 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003884 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003885 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003887 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003888 }
Eric Christopherfd179292009-08-27 18:07:15 +00003889
Nate Begemanb9a47b82009-02-23 08:49:38 +00003890 // If we have SSSE3, and all words of the result are from 1 input vector,
3891 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3892 // is present, fall back to case 4.
3893 if (TLI.getSubtarget()->hasSSSE3()) {
3894 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003895
Nate Begemanb9a47b82009-02-23 08:49:38 +00003896 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003897 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003898 // mask, and elements that come from V1 in the V2 mask, so that the two
3899 // results can be OR'd together.
3900 bool TwoInputs = V1Used && V2Used;
3901 for (unsigned i = 0; i != 8; ++i) {
3902 int EltIdx = MaskVals[i] * 2;
3903 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003904 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3905 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003906 continue;
3907 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3909 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003910 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003912 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003913 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003915 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003916 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003917
Nate Begemanb9a47b82009-02-23 08:49:38 +00003918 // Calculate the shuffle mask for the second input, shuffle it, and
3919 // OR it with the first shuffled input.
3920 pshufbMask.clear();
3921 for (unsigned i = 0; i != 8; ++i) {
3922 int EltIdx = MaskVals[i] * 2;
3923 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3925 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003926 continue;
3927 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3929 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003930 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003931 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003932 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003933 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 MVT::v16i8, &pshufbMask[0], 16));
3935 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3936 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003937 }
3938
3939 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3940 // and update MaskVals with new element order.
3941 BitVector InOrder(8);
3942 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003944 for (int i = 0; i != 4; ++i) {
3945 int idx = MaskVals[i];
3946 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003948 InOrder.set(i);
3949 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003951 InOrder.set(i);
3952 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003953 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003954 }
3955 }
3956 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003960 }
Eric Christopherfd179292009-08-27 18:07:15 +00003961
Nate Begemanb9a47b82009-02-23 08:49:38 +00003962 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3963 // and update MaskVals with the new element order.
3964 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003966 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003967 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003968 for (unsigned i = 4; i != 8; ++i) {
3969 int idx = MaskVals[i];
3970 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003972 InOrder.set(i);
3973 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003975 InOrder.set(i);
3976 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003977 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003978 }
3979 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003980 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003982 }
Eric Christopherfd179292009-08-27 18:07:15 +00003983
Nate Begemanb9a47b82009-02-23 08:49:38 +00003984 // In case BestHi & BestLo were both -1, which means each quadword has a word
3985 // from each of the four input quadwords, calculate the InOrder bitvector now
3986 // before falling through to the insert/extract cleanup.
3987 if (BestLoQuad == -1 && BestHiQuad == -1) {
3988 NewV = V1;
3989 for (int i = 0; i != 8; ++i)
3990 if (MaskVals[i] < 0 || MaskVals[i] == i)
3991 InOrder.set(i);
3992 }
Eric Christopherfd179292009-08-27 18:07:15 +00003993
Nate Begemanb9a47b82009-02-23 08:49:38 +00003994 // The other elements are put in the right place using pextrw and pinsrw.
3995 for (unsigned i = 0; i != 8; ++i) {
3996 if (InOrder[i])
3997 continue;
3998 int EltIdx = MaskVals[i];
3999 if (EltIdx < 0)
4000 continue;
4001 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004002 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004003 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004004 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004005 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004006 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004007 DAG.getIntPtrConstant(i));
4008 }
4009 return NewV;
4010}
4011
4012// v16i8 shuffles - Prefer shuffles in the following order:
4013// 1. [ssse3] 1 x pshufb
4014// 2. [ssse3] 2 x pshufb + 1 x por
4015// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4016static
Nate Begeman9008ca62009-04-27 18:41:29 +00004017SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4018 SelectionDAG &DAG, X86TargetLowering &TLI) {
4019 SDValue V1 = SVOp->getOperand(0);
4020 SDValue V2 = SVOp->getOperand(1);
4021 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004022 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004023 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004024
Nate Begemanb9a47b82009-02-23 08:49:38 +00004025 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004026 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004027 // present, fall back to case 3.
4028 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4029 bool V1Only = true;
4030 bool V2Only = true;
4031 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004033 if (EltIdx < 0)
4034 continue;
4035 if (EltIdx < 16)
4036 V2Only = false;
4037 else
4038 V1Only = false;
4039 }
Eric Christopherfd179292009-08-27 18:07:15 +00004040
Nate Begemanb9a47b82009-02-23 08:49:38 +00004041 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4042 if (TLI.getSubtarget()->hasSSSE3()) {
4043 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004044
Nate Begemanb9a47b82009-02-23 08:49:38 +00004045 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004046 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004047 //
4048 // Otherwise, we have elements from both input vectors, and must zero out
4049 // elements that come from V2 in the first mask, and V1 in the second mask
4050 // so that we can OR them together.
4051 bool TwoInputs = !(V1Only || V2Only);
4052 for (unsigned i = 0; i != 16; ++i) {
4053 int EltIdx = MaskVals[i];
4054 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004055 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004056 continue;
4057 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004059 }
4060 // If all the elements are from V2, assign it to V1 and return after
4061 // building the first pshufb.
4062 if (V2Only)
4063 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004064 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004065 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004067 if (!TwoInputs)
4068 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004069
Nate Begemanb9a47b82009-02-23 08:49:38 +00004070 // Calculate the shuffle mask for the second input, shuffle it, and
4071 // OR it with the first shuffled input.
4072 pshufbMask.clear();
4073 for (unsigned i = 0; i != 16; ++i) {
4074 int EltIdx = MaskVals[i];
4075 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004077 continue;
4078 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004081 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004082 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 MVT::v16i8, &pshufbMask[0], 16));
4084 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 }
Eric Christopherfd179292009-08-27 18:07:15 +00004086
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 // No SSSE3 - Calculate in place words and then fix all out of place words
4088 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4089 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4091 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 SDValue NewV = V2Only ? V2 : V1;
4093 for (int i = 0; i != 8; ++i) {
4094 int Elt0 = MaskVals[i*2];
4095 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004096
Nate Begemanb9a47b82009-02-23 08:49:38 +00004097 // This word of the result is all undef, skip it.
4098 if (Elt0 < 0 && Elt1 < 0)
4099 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004100
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 // This word of the result is already in the correct place, skip it.
4102 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4103 continue;
4104 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4105 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004106
Nate Begemanb9a47b82009-02-23 08:49:38 +00004107 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4108 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4109 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004110
4111 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4112 // using a single extract together, load it and store it.
4113 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004114 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004115 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004117 DAG.getIntPtrConstant(i));
4118 continue;
4119 }
4120
Nate Begemanb9a47b82009-02-23 08:49:38 +00004121 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004122 // source byte is not also odd, shift the extracted word left 8 bits
4123 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004124 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004126 DAG.getIntPtrConstant(Elt1 / 2));
4127 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004129 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004130 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4132 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004133 }
4134 // If Elt0 is defined, extract it from the appropriate source. If the
4135 // source byte is not also even, shift the extracted word right 8 bits. If
4136 // Elt1 was also defined, OR the extracted values together before
4137 // inserting them in the result.
4138 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4141 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004143 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004144 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4146 DAG.getConstant(0x00FF, MVT::i16));
4147 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 : InsElt0;
4149 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004151 DAG.getIntPtrConstant(i));
4152 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004154}
4155
Evan Cheng7a831ce2007-12-15 03:00:47 +00004156/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4157/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4158/// done when every pair / quad of shuffle mask elements point to elements in
4159/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004160/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4161static
Nate Begeman9008ca62009-04-27 18:41:29 +00004162SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4163 SelectionDAG &DAG,
4164 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004165 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 SDValue V1 = SVOp->getOperand(0);
4167 SDValue V2 = SVOp->getOperand(1);
4168 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004169 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004171 EVT MaskEltVT = MaskVT.getVectorElementType();
4172 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004174 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 case MVT::v4f32: NewVT = MVT::v2f64; break;
4176 case MVT::v4i32: NewVT = MVT::v2i64; break;
4177 case MVT::v8i16: NewVT = MVT::v4i32; break;
4178 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004179 }
4180
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004181 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004182 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004184 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004186 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 int Scale = NumElems / NewWidth;
4188 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004189 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 int StartIdx = -1;
4191 for (int j = 0; j < Scale; ++j) {
4192 int EltIdx = SVOp->getMaskElt(i+j);
4193 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004194 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004196 StartIdx = EltIdx - (EltIdx % Scale);
4197 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004198 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004199 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004200 if (StartIdx == -1)
4201 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004202 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004203 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004204 }
4205
Dale Johannesenace16102009-02-03 19:33:06 +00004206 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4207 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004208 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004209}
4210
Evan Chengd880b972008-05-09 21:53:03 +00004211/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004212///
Owen Andersone50ed302009-08-10 22:56:29 +00004213static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004214 SDValue SrcOp, SelectionDAG &DAG,
4215 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004217 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004218 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004219 LD = dyn_cast<LoadSDNode>(SrcOp);
4220 if (!LD) {
4221 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4222 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004223 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4224 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004225 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4226 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004227 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004228 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004229 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004230 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4231 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4232 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4233 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004234 SrcOp.getOperand(0)
4235 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004236 }
4237 }
4238 }
4239
Dale Johannesenace16102009-02-03 19:33:06 +00004240 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4241 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004242 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004243 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004244}
4245
Evan Chengace3c172008-07-22 21:13:36 +00004246/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4247/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004248static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004249LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4250 SDValue V1 = SVOp->getOperand(0);
4251 SDValue V2 = SVOp->getOperand(1);
4252 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004253 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004254
Evan Chengace3c172008-07-22 21:13:36 +00004255 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004256 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004257 SmallVector<int, 8> Mask1(4U, -1);
4258 SmallVector<int, 8> PermMask;
4259 SVOp->getMask(PermMask);
4260
Evan Chengace3c172008-07-22 21:13:36 +00004261 unsigned NumHi = 0;
4262 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004263 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 int Idx = PermMask[i];
4265 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004266 Locs[i] = std::make_pair(-1, -1);
4267 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4269 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004270 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004272 NumLo++;
4273 } else {
4274 Locs[i] = std::make_pair(1, NumHi);
4275 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004277 NumHi++;
4278 }
4279 }
4280 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004281
Evan Chengace3c172008-07-22 21:13:36 +00004282 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004283 // If no more than two elements come from either vector. This can be
4284 // implemented with two shuffles. First shuffle gather the elements.
4285 // The second shuffle, which takes the first shuffle as both of its
4286 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004287 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004288
Nate Begeman9008ca62009-04-27 18:41:29 +00004289 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004290
Evan Chengace3c172008-07-22 21:13:36 +00004291 for (unsigned i = 0; i != 4; ++i) {
4292 if (Locs[i].first == -1)
4293 continue;
4294 else {
4295 unsigned Idx = (i < 2) ? 0 : 4;
4296 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004297 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004298 }
4299 }
4300
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004302 } else if (NumLo == 3 || NumHi == 3) {
4303 // Otherwise, we must have three elements from one vector, call it X, and
4304 // one element from the other, call it Y. First, use a shufps to build an
4305 // intermediate vector with the one element from Y and the element from X
4306 // that will be in the same half in the final destination (the indexes don't
4307 // matter). Then, use a shufps to build the final vector, taking the half
4308 // containing the element from Y from the intermediate, and the other half
4309 // from X.
4310 if (NumHi == 3) {
4311 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004312 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004313 std::swap(V1, V2);
4314 }
4315
4316 // Find the element from V2.
4317 unsigned HiIndex;
4318 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004319 int Val = PermMask[HiIndex];
4320 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004321 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004322 if (Val >= 4)
4323 break;
4324 }
4325
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 Mask1[0] = PermMask[HiIndex];
4327 Mask1[1] = -1;
4328 Mask1[2] = PermMask[HiIndex^1];
4329 Mask1[3] = -1;
4330 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004331
4332 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 Mask1[0] = PermMask[0];
4334 Mask1[1] = PermMask[1];
4335 Mask1[2] = HiIndex & 1 ? 6 : 4;
4336 Mask1[3] = HiIndex & 1 ? 4 : 6;
4337 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004338 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004339 Mask1[0] = HiIndex & 1 ? 2 : 0;
4340 Mask1[1] = HiIndex & 1 ? 0 : 2;
4341 Mask1[2] = PermMask[2];
4342 Mask1[3] = PermMask[3];
4343 if (Mask1[2] >= 0)
4344 Mask1[2] += 4;
4345 if (Mask1[3] >= 0)
4346 Mask1[3] += 4;
4347 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004348 }
Evan Chengace3c172008-07-22 21:13:36 +00004349 }
4350
4351 // Break it into (shuffle shuffle_hi, shuffle_lo).
4352 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004353 SmallVector<int,8> LoMask(4U, -1);
4354 SmallVector<int,8> HiMask(4U, -1);
4355
4356 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004357 unsigned MaskIdx = 0;
4358 unsigned LoIdx = 0;
4359 unsigned HiIdx = 2;
4360 for (unsigned i = 0; i != 4; ++i) {
4361 if (i == 2) {
4362 MaskPtr = &HiMask;
4363 MaskIdx = 1;
4364 LoIdx = 0;
4365 HiIdx = 2;
4366 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 int Idx = PermMask[i];
4368 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004369 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004371 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004373 LoIdx++;
4374 } else {
4375 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004377 HiIdx++;
4378 }
4379 }
4380
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4382 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4383 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004384 for (unsigned i = 0; i != 4; ++i) {
4385 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004387 } else {
4388 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004389 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004390 }
4391 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004392 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004393}
4394
Dan Gohman475871a2008-07-27 21:46:04 +00004395SDValue
4396X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004397 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004398 SDValue V1 = Op.getOperand(0);
4399 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004400 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004401 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004402 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004403 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4405 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004406 bool V1IsSplat = false;
4407 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004410 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004411
Nate Begeman9008ca62009-04-27 18:41:29 +00004412 // Promote splats to v4f32.
4413 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004414 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 return Op;
4416 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004417 }
4418
Evan Cheng7a831ce2007-12-15 03:00:47 +00004419 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4420 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004421 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004423 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004424 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004425 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004426 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004427 // FIXME: Figure out a cleaner way to do this.
4428 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004429 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004431 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4433 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4434 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004435 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004436 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4438 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004439 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004441 }
4442 }
Eric Christopherfd179292009-08-27 18:07:15 +00004443
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 if (X86::isPSHUFDMask(SVOp))
4445 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004446
Evan Chengf26ffe92008-05-29 08:22:04 +00004447 // Check if this can be converted into a logical shift.
4448 bool isLeft = false;
4449 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004450 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004452 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004453 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004454 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004455 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004456 EVT EltVT = VT.getVectorElementType();
4457 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004458 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004459 }
Eric Christopherfd179292009-08-27 18:07:15 +00004460
Nate Begeman9008ca62009-04-27 18:41:29 +00004461 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004462 if (V1IsUndef)
4463 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004464 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004465 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004466 if (!isMMX)
4467 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004468 }
Eric Christopherfd179292009-08-27 18:07:15 +00004469
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 // FIXME: fold these into legal mask.
4471 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4472 X86::isMOVSLDUPMask(SVOp) ||
4473 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004474 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004476 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 if (ShouldXformToMOVHLPS(SVOp) ||
4479 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4480 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481
Evan Chengf26ffe92008-05-29 08:22:04 +00004482 if (isShift) {
4483 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004484 EVT EltVT = VT.getVectorElementType();
4485 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004486 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004487 }
Eric Christopherfd179292009-08-27 18:07:15 +00004488
Evan Cheng9eca5e82006-10-25 21:49:50 +00004489 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004490 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4491 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004492 V1IsSplat = isSplatVector(V1.getNode());
4493 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004494
Chris Lattner8a594482007-11-25 00:24:49 +00004495 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004496 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004497 Op = CommuteVectorShuffle(SVOp, DAG);
4498 SVOp = cast<ShuffleVectorSDNode>(Op);
4499 V1 = SVOp->getOperand(0);
4500 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004501 std::swap(V1IsSplat, V2IsSplat);
4502 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004503 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004504 }
4505
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4507 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004508 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004509 return V1;
4510 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4511 // the instruction selector will not match, so get a canonical MOVL with
4512 // swapped operands to undo the commute.
4513 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004514 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004515
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4517 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4518 X86::isUNPCKLMask(SVOp) ||
4519 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004520 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004521
Evan Cheng9bbbb982006-10-25 20:48:19 +00004522 if (V2IsSplat) {
4523 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004524 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004525 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 SDValue NewMask = NormalizeMask(SVOp, DAG);
4527 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4528 if (NSVOp != SVOp) {
4529 if (X86::isUNPCKLMask(NSVOp, true)) {
4530 return NewMask;
4531 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4532 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533 }
4534 }
4535 }
4536
Evan Cheng9eca5e82006-10-25 21:49:50 +00004537 if (Commuted) {
4538 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 // FIXME: this seems wrong.
4540 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4541 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4542 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4543 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4544 X86::isUNPCKLMask(NewSVOp) ||
4545 X86::isUNPCKHMask(NewSVOp))
4546 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004547 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004548
Nate Begemanb9a47b82009-02-23 08:49:38 +00004549 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004550
4551 // Normalize the node to match x86 shuffle ops if needed
4552 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4553 return CommuteVectorShuffle(SVOp, DAG);
4554
4555 // Check for legal shuffle and return?
4556 SmallVector<int, 16> PermMask;
4557 SVOp->getMask(PermMask);
4558 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004559 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004560
Evan Cheng14b32e12007-12-11 01:46:18 +00004561 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004562 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004563 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004564 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004565 return NewOp;
4566 }
4567
Owen Anderson825b72b2009-08-11 20:47:22 +00004568 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004570 if (NewOp.getNode())
4571 return NewOp;
4572 }
Eric Christopherfd179292009-08-27 18:07:15 +00004573
Evan Chengace3c172008-07-22 21:13:36 +00004574 // Handle all 4 wide cases with a number of shuffles except for MMX.
4575 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577
Dan Gohman475871a2008-07-27 21:46:04 +00004578 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579}
4580
Dan Gohman475871a2008-07-27 21:46:04 +00004581SDValue
4582X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004583 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004584 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004585 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004586 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004587 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004588 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004589 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004590 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004591 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004592 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004593 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4594 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4595 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4597 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004598 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004600 Op.getOperand(0)),
4601 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004602 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004603 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004604 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004605 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004606 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004607 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004608 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4609 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004610 // result has a single use which is a store or a bitcast to i32. And in
4611 // the case of a store, it's not worth it if the index is a constant 0,
4612 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004613 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004614 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004615 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004616 if ((User->getOpcode() != ISD::STORE ||
4617 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4618 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004619 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004620 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004621 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004622 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4623 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004624 Op.getOperand(0)),
4625 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004626 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4627 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004628 // ExtractPS works with constant index.
4629 if (isa<ConstantSDNode>(Op.getOperand(1)))
4630 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004631 }
Dan Gohman475871a2008-07-27 21:46:04 +00004632 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004633}
4634
4635
Dan Gohman475871a2008-07-27 21:46:04 +00004636SDValue
4637X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004638 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004639 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004640
Evan Cheng62a3f152008-03-24 21:52:23 +00004641 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004643 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004644 return Res;
4645 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004646
Owen Andersone50ed302009-08-10 22:56:29 +00004647 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004648 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004651 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004652 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004653 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004654 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4655 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004656 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004657 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004658 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004659 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004660 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004661 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004662 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004663 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004664 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004665 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004666 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004667 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004668 if (Idx == 0)
4669 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004670
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004672 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004673 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004674 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004676 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004677 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004678 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004679 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4680 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4681 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004682 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 if (Idx == 0)
4684 return Op;
4685
4686 // UNPCKHPD the element to the lowest double word, then movsd.
4687 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4688 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004690 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004691 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004692 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004693 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004694 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004695 }
4696
Dan Gohman475871a2008-07-27 21:46:04 +00004697 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004698}
4699
Dan Gohman475871a2008-07-27 21:46:04 +00004700SDValue
4701X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004702 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004703 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004704 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004705
Dan Gohman475871a2008-07-27 21:46:04 +00004706 SDValue N0 = Op.getOperand(0);
4707 SDValue N1 = Op.getOperand(1);
4708 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004709
Dan Gohman8a55ce42009-09-23 21:02:20 +00004710 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004711 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004712 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4713 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004714 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4715 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 if (N1.getValueType() != MVT::i32)
4717 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4718 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004719 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004720 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004721 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004722 // Bits [7:6] of the constant are the source select. This will always be
4723 // zero here. The DAG Combiner may combine an extract_elt index into these
4724 // bits. For example (insert (extract, 3), 2) could be matched by putting
4725 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004726 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004727 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004728 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004729 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004730 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004731 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004732 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004733 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004734 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004735 // PINSR* works with constant index.
4736 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004737 }
Dan Gohman475871a2008-07-27 21:46:04 +00004738 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004739}
4740
Dan Gohman475871a2008-07-27 21:46:04 +00004741SDValue
4742X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004743 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004744 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004745
4746 if (Subtarget->hasSSE41())
4747 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4748
Dan Gohman8a55ce42009-09-23 21:02:20 +00004749 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004750 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004751
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004752 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004753 SDValue N0 = Op.getOperand(0);
4754 SDValue N1 = Op.getOperand(1);
4755 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004756
Dan Gohman8a55ce42009-09-23 21:02:20 +00004757 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004758 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4759 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004760 if (N1.getValueType() != MVT::i32)
4761 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4762 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004763 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004764 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004765 }
Dan Gohman475871a2008-07-27 21:46:04 +00004766 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004767}
4768
Dan Gohman475871a2008-07-27 21:46:04 +00004769SDValue
4770X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004771 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 if (Op.getValueType() == MVT::v2f32)
4773 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4774 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4775 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004776 Op.getOperand(0))));
4777
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4779 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004780
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4782 EVT VT = MVT::v2i32;
4783 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004784 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 case MVT::v16i8:
4786 case MVT::v8i16:
4787 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004788 break;
4789 }
Dale Johannesenace16102009-02-03 19:33:06 +00004790 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4791 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004792}
4793
Bill Wendling056292f2008-09-16 21:48:12 +00004794// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4795// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4796// one of the above mentioned nodes. It has to be wrapped because otherwise
4797// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4798// be used to form addressing mode. These wrapped nodes will be selected
4799// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004800SDValue
4801X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004803
Chris Lattner41621a22009-06-26 19:22:52 +00004804 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4805 // global base reg.
4806 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004807 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004808 CodeModel::Model M = getTargetMachine().getCodeModel();
4809
Chris Lattner4f066492009-07-11 20:29:19 +00004810 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004811 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004812 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004813 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004814 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004815 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004816 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004817
Evan Cheng1606e8e2009-03-13 07:51:59 +00004818 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004819 CP->getAlignment(),
4820 CP->getOffset(), OpFlag);
4821 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004822 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004823 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004824 if (OpFlag) {
4825 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004826 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004827 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004828 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829 }
4830
4831 return Result;
4832}
4833
Chris Lattner18c59872009-06-27 04:16:01 +00004834SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4835 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004836
Chris Lattner18c59872009-06-27 04:16:01 +00004837 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4838 // global base reg.
4839 unsigned char OpFlag = 0;
4840 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004841 CodeModel::Model M = getTargetMachine().getCodeModel();
4842
Chris Lattner4f066492009-07-11 20:29:19 +00004843 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004844 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004845 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004846 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004847 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004848 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004849 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004850
Chris Lattner18c59872009-06-27 04:16:01 +00004851 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4852 OpFlag);
4853 DebugLoc DL = JT->getDebugLoc();
4854 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004855
Chris Lattner18c59872009-06-27 04:16:01 +00004856 // With PIC, the address is actually $g + Offset.
4857 if (OpFlag) {
4858 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4859 DAG.getNode(X86ISD::GlobalBaseReg,
4860 DebugLoc::getUnknownLoc(), getPointerTy()),
4861 Result);
4862 }
Eric Christopherfd179292009-08-27 18:07:15 +00004863
Chris Lattner18c59872009-06-27 04:16:01 +00004864 return Result;
4865}
4866
4867SDValue
4868X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4869 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004870
Chris Lattner18c59872009-06-27 04:16:01 +00004871 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4872 // global base reg.
4873 unsigned char OpFlag = 0;
4874 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004875 CodeModel::Model M = getTargetMachine().getCodeModel();
4876
Chris Lattner4f066492009-07-11 20:29:19 +00004877 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004878 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004879 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004880 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004881 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004882 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004883 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004884
Chris Lattner18c59872009-06-27 04:16:01 +00004885 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004886
Chris Lattner18c59872009-06-27 04:16:01 +00004887 DebugLoc DL = Op.getDebugLoc();
4888 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004889
4890
Chris Lattner18c59872009-06-27 04:16:01 +00004891 // With PIC, the address is actually $g + Offset.
4892 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004893 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004894 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4895 DAG.getNode(X86ISD::GlobalBaseReg,
4896 DebugLoc::getUnknownLoc(),
4897 getPointerTy()),
4898 Result);
4899 }
Eric Christopherfd179292009-08-27 18:07:15 +00004900
Chris Lattner18c59872009-06-27 04:16:01 +00004901 return Result;
4902}
4903
Dan Gohman475871a2008-07-27 21:46:04 +00004904SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004905X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004906 // Create the TargetBlockAddressAddress node.
4907 unsigned char OpFlags =
4908 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004909 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004910 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4911 DebugLoc dl = Op.getDebugLoc();
4912 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4913 /*isTarget=*/true, OpFlags);
4914
Dan Gohmanf705adb2009-10-30 01:28:02 +00004915 if (Subtarget->isPICStyleRIPRel() &&
4916 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004917 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4918 else
4919 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004920
Dan Gohman29cbade2009-11-20 23:18:13 +00004921 // With PIC, the address is actually $g + Offset.
4922 if (isGlobalRelativeToPICBase(OpFlags)) {
4923 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4924 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4925 Result);
4926 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004927
4928 return Result;
4929}
4930
4931SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004932X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004933 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004934 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004935 // Create the TargetGlobalAddress node, folding in the constant
4936 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004937 unsigned char OpFlags =
4938 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004939 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004940 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004941 if (OpFlags == X86II::MO_NO_FLAG &&
4942 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004943 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004944 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004945 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004946 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004947 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004948 }
Eric Christopherfd179292009-08-27 18:07:15 +00004949
Chris Lattner4f066492009-07-11 20:29:19 +00004950 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004951 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004952 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4953 else
4954 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004955
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004956 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004957 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004958 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4959 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004960 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004961 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004962
Chris Lattner36c25012009-07-10 07:34:39 +00004963 // For globals that require a load from a stub to get the address, emit the
4964 // load.
4965 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004966 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004967 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004968
Dan Gohman6520e202008-10-18 02:06:02 +00004969 // If there was a non-zero offset that we didn't fold, create an explicit
4970 // addition for it.
4971 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004972 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004973 DAG.getConstant(Offset, getPointerTy()));
4974
Evan Cheng0db9fe62006-04-25 20:13:52 +00004975 return Result;
4976}
4977
Evan Chengda43bcf2008-09-24 00:05:32 +00004978SDValue
4979X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4980 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004981 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004982 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004983}
4984
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004985static SDValue
4986GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004987 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004988 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004989 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004991 DebugLoc dl = GA->getDebugLoc();
4992 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4993 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004994 GA->getOffset(),
4995 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004996 if (InFlag) {
4997 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004998 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004999 } else {
5000 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005001 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005002 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005003
5004 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5005 MFI->setHasCalls(true);
5006
Rafael Espindola15f1b662009-04-24 12:59:40 +00005007 SDValue Flag = Chain.getValue(1);
5008 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005009}
5010
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005011// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005012static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005013LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005014 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005015 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005016 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5017 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005018 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005019 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005020 PtrVT), InFlag);
5021 InFlag = Chain.getValue(1);
5022
Chris Lattnerb903bed2009-06-26 21:20:29 +00005023 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005024}
5025
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005026// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005027static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005028LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005029 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005030 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5031 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005032}
5033
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005034// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5035// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005036static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005037 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005038 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005039 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005040 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005041 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5042 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005043 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005044 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005045
5046 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5047 NULL, 0);
5048
Chris Lattnerb903bed2009-06-26 21:20:29 +00005049 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005050 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5051 // initialexec.
5052 unsigned WrapperKind = X86ISD::Wrapper;
5053 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005054 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005055 } else if (is64Bit) {
5056 assert(model == TLSModel::InitialExec);
5057 OperandFlags = X86II::MO_GOTTPOFF;
5058 WrapperKind = X86ISD::WrapperRIP;
5059 } else {
5060 assert(model == TLSModel::InitialExec);
5061 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005062 }
Eric Christopherfd179292009-08-27 18:07:15 +00005063
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005064 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5065 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005066 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005067 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005068 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005069
Rafael Espindola9a580232009-02-27 13:37:18 +00005070 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005071 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005072 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005073
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005074 // The address of the thread local variable is the add of the thread
5075 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005076 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005077}
5078
Dan Gohman475871a2008-07-27 21:46:04 +00005079SDValue
5080X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005081 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005082 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005083 assert(Subtarget->isTargetELF() &&
5084 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005085 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005086 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005087
Chris Lattnerb903bed2009-06-26 21:20:29 +00005088 // If GV is an alias then use the aliasee for determining
5089 // thread-localness.
5090 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5091 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005092
Chris Lattnerb903bed2009-06-26 21:20:29 +00005093 TLSModel::Model model = getTLSModel(GV,
5094 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005095
Chris Lattnerb903bed2009-06-26 21:20:29 +00005096 switch (model) {
5097 case TLSModel::GeneralDynamic:
5098 case TLSModel::LocalDynamic: // not implemented
5099 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005100 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005101 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005102
Chris Lattnerb903bed2009-06-26 21:20:29 +00005103 case TLSModel::InitialExec:
5104 case TLSModel::LocalExec:
5105 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5106 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005107 }
Eric Christopherfd179292009-08-27 18:07:15 +00005108
Torok Edwinc23197a2009-07-14 16:55:14 +00005109 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005110 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005111}
5112
Evan Cheng0db9fe62006-04-25 20:13:52 +00005113
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005114/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005115/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005116SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005117 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005118 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005119 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005120 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005121 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005122 SDValue ShOpLo = Op.getOperand(0);
5123 SDValue ShOpHi = Op.getOperand(1);
5124 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005125 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005126 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005127 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005128
Dan Gohman475871a2008-07-27 21:46:04 +00005129 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005130 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005131 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5132 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005133 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005134 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5135 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005136 }
Evan Chenge3413162006-01-09 18:33:28 +00005137
Owen Anderson825b72b2009-08-11 20:47:22 +00005138 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5139 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005140 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005141 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005142
Dan Gohman475871a2008-07-27 21:46:04 +00005143 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005144 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005145 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5146 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005147
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005148 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005149 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5150 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005151 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005152 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5153 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005154 }
5155
Dan Gohman475871a2008-07-27 21:46:04 +00005156 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005157 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005158}
Evan Chenga3195e82006-01-12 22:54:21 +00005159
Dan Gohman475871a2008-07-27 21:46:04 +00005160SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005161 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005162
5163 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005164 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005165 return Op;
5166 }
5167 return SDValue();
5168 }
5169
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005171 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005172
Eli Friedman36df4992009-05-27 00:47:34 +00005173 // These are really Legal; return the operand so the caller accepts it as
5174 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005175 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005176 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005178 Subtarget->is64Bit()) {
5179 return Op;
5180 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005181
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005182 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005183 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005184 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005185 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005186 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005187 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005188 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005189 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005190 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5191}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192
Owen Andersone50ed302009-08-10 22:56:29 +00005193SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005194 SDValue StackSlot,
5195 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005196 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005197 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005198 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005199 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005200 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005201 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005202 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005203 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005204 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005205 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005206 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005207
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005208 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005209 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005210 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211
5212 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5213 // shouldn't be necessary except that RFP cannot be live across
5214 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005215 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005216 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005217 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005218 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005219 SDValue Ops[] = {
5220 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5221 };
5222 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005223 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005224 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005225 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005226
Evan Cheng0db9fe62006-04-25 20:13:52 +00005227 return Result;
5228}
5229
Bill Wendling8b8a6362009-01-17 03:56:04 +00005230// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5231SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5232 // This algorithm is not obvious. Here it is in C code, more or less:
5233 /*
5234 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5235 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5236 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005237
Bill Wendling8b8a6362009-01-17 03:56:04 +00005238 // Copy ints to xmm registers.
5239 __m128i xh = _mm_cvtsi32_si128( hi );
5240 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005241
Bill Wendling8b8a6362009-01-17 03:56:04 +00005242 // Combine into low half of a single xmm register.
5243 __m128i x = _mm_unpacklo_epi32( xh, xl );
5244 __m128d d;
5245 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005246
Bill Wendling8b8a6362009-01-17 03:56:04 +00005247 // Merge in appropriate exponents to give the integer bits the right
5248 // magnitude.
5249 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005250
Bill Wendling8b8a6362009-01-17 03:56:04 +00005251 // Subtract away the biases to deal with the IEEE-754 double precision
5252 // implicit 1.
5253 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005254
Bill Wendling8b8a6362009-01-17 03:56:04 +00005255 // All conversions up to here are exact. The correctly rounded result is
5256 // calculated using the current rounding mode using the following
5257 // horizontal add.
5258 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5259 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5260 // store doesn't really need to be here (except
5261 // maybe to zero the other double)
5262 return sd;
5263 }
5264 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005265
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005266 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005267 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005268
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005269 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005270 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005271 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5272 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5273 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5274 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005275 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005276 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005277
Bill Wendling8b8a6362009-01-17 03:56:04 +00005278 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005279 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005280 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005281 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005282 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005283 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005284 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005285
Owen Anderson825b72b2009-08-11 20:47:22 +00005286 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5287 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005288 Op.getOperand(0),
5289 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005290 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5291 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005292 Op.getOperand(0),
5293 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005294 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5295 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005296 PseudoSourceValue::getConstantPool(), 0,
5297 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5299 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5300 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005301 PseudoSourceValue::getConstantPool(), 0,
5302 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005304
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005305 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005306 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005307 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5308 DAG.getUNDEF(MVT::v2f64), ShufMask);
5309 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5310 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005311 DAG.getIntPtrConstant(0));
5312}
5313
Bill Wendling8b8a6362009-01-17 03:56:04 +00005314// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5315SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005316 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005317 // FP constant to bias correct the final result.
5318 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005320
5321 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005322 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5323 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005324 Op.getOperand(0),
5325 DAG.getIntPtrConstant(0)));
5326
Owen Anderson825b72b2009-08-11 20:47:22 +00005327 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5328 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005329 DAG.getIntPtrConstant(0));
5330
5331 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005332 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5333 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005334 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005335 MVT::v2f64, Load)),
5336 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005337 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 MVT::v2f64, Bias)));
5339 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5340 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005341 DAG.getIntPtrConstant(0));
5342
5343 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005345
5346 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005347 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005348
Owen Anderson825b72b2009-08-11 20:47:22 +00005349 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005350 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005351 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005352 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005353 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005354 }
5355
5356 // Handle final rounding.
5357 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005358}
5359
5360SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005361 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005362 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005363
Evan Chenga06ec9e2009-01-19 08:08:22 +00005364 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5365 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5366 // the optimization here.
5367 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005368 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005369
Owen Andersone50ed302009-08-10 22:56:29 +00005370 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005371 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005372 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005373 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005374 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005375
Bill Wendling8b8a6362009-01-17 03:56:04 +00005376 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005377 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005378 return LowerUINT_TO_FP_i32(Op, DAG);
5379 }
5380
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005382
5383 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005384 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005385 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5386 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5387 getPointerTy(), StackSlot, WordOff);
5388 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5389 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005390 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005391 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005392 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005393}
5394
Dan Gohman475871a2008-07-27 21:46:04 +00005395std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005396FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005397 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005398
Owen Andersone50ed302009-08-10 22:56:29 +00005399 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005400
5401 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005402 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5403 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005404 }
5405
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5407 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005408 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005410 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005412 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005413 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005414 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005416 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005417 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005418
Evan Cheng87c89352007-10-15 20:11:21 +00005419 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5420 // stack slot.
5421 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005422 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005423 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005424 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005425
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005427 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005428 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005429 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5430 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5431 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005432 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005433
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue Chain = DAG.getEntryNode();
5435 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005436 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005437 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005438 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005439 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005440 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005441 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005442 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5443 };
Dale Johannesenace16102009-02-03 19:33:06 +00005444 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005446 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005447 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5448 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005449
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005451 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005453
Chris Lattner27a6c732007-11-24 07:07:01 +00005454 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005455}
5456
Dan Gohman475871a2008-07-27 21:46:04 +00005457SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005458 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005459 if (Op.getValueType() == MVT::v2i32 &&
5460 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005461 return Op;
5462 }
5463 return SDValue();
5464 }
5465
Eli Friedman948e95a2009-05-23 09:59:16 +00005466 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005467 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005468 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5469 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005470
Chris Lattner27a6c732007-11-24 07:07:01 +00005471 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005472 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005473 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005474}
5475
Eli Friedman948e95a2009-05-23 09:59:16 +00005476SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5477 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5478 SDValue FIST = Vals.first, StackSlot = Vals.second;
5479 assert(FIST.getNode() && "Unexpected failure");
5480
5481 // Load the result.
5482 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5483 FIST, StackSlot, NULL, 0);
5484}
5485
Dan Gohman475871a2008-07-27 21:46:04 +00005486SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005487 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005488 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005489 EVT VT = Op.getValueType();
5490 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005491 if (VT.isVector())
5492 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005493 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005494 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005495 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005496 CV.push_back(C);
5497 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005498 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005499 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005500 CV.push_back(C);
5501 CV.push_back(C);
5502 CV.push_back(C);
5503 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005505 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005506 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005507 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005508 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005509 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005510 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005511}
5512
Dan Gohman475871a2008-07-27 21:46:04 +00005513SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005514 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005515 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005516 EVT VT = Op.getValueType();
5517 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005518 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005519 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005520 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005521 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005522 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005523 CV.push_back(C);
5524 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005525 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005526 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005527 CV.push_back(C);
5528 CV.push_back(C);
5529 CV.push_back(C);
5530 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005531 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005532 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005533 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005534 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005535 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005536 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005537 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005538 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005539 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5540 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005541 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005543 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005544 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005545 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005546}
5547
Dan Gohman475871a2008-07-27 21:46:04 +00005548SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005549 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005550 SDValue Op0 = Op.getOperand(0);
5551 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005552 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005553 EVT VT = Op.getValueType();
5554 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005555
5556 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005557 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005558 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005559 SrcVT = VT;
5560 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005561 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005562 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005563 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005564 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005565 }
5566
5567 // At this point the operands and the result should have the same
5568 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005569
Evan Cheng68c47cb2007-01-05 07:55:56 +00005570 // First get the sign bit of second operand.
5571 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005573 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5574 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005575 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005576 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5577 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5578 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5579 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005580 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005581 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005582 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005583 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005584 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005585 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005586 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005587
5588 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005589 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 // Op0 is MVT::f32, Op1 is MVT::f64.
5591 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5592 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5593 DAG.getConstant(32, MVT::i32));
5594 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5595 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005596 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005597 }
5598
Evan Cheng73d6cf12007-01-05 21:37:56 +00005599 // Clear first operand sign bit.
5600 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005602 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5603 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005604 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005605 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5606 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5607 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5608 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005609 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005610 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005611 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005612 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005613 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005614 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005615 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005616
5617 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005618 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005619}
5620
Dan Gohman076aee32009-03-04 19:44:21 +00005621/// Emit nodes that will be selected as "test Op0,Op0", or something
5622/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005623SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5624 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005625 DebugLoc dl = Op.getDebugLoc();
5626
Dan Gohman31125812009-03-07 01:58:32 +00005627 // CF and OF aren't always set the way we want. Determine which
5628 // of these we need.
5629 bool NeedCF = false;
5630 bool NeedOF = false;
5631 switch (X86CC) {
5632 case X86::COND_A: case X86::COND_AE:
5633 case X86::COND_B: case X86::COND_BE:
5634 NeedCF = true;
5635 break;
5636 case X86::COND_G: case X86::COND_GE:
5637 case X86::COND_L: case X86::COND_LE:
5638 case X86::COND_O: case X86::COND_NO:
5639 NeedOF = true;
5640 break;
5641 default: break;
5642 }
5643
Dan Gohman076aee32009-03-04 19:44:21 +00005644 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005645 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5646 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5647 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005648 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005649 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005650 switch (Op.getNode()->getOpcode()) {
5651 case ISD::ADD:
5652 // Due to an isel shortcoming, be conservative if this add is likely to
5653 // be selected as part of a load-modify-store instruction. When the root
5654 // node in a match is a store, isel doesn't know how to remap non-chain
5655 // non-flag uses of other nodes in the match, such as the ADD in this
5656 // case. This leads to the ADD being left around and reselected, with
5657 // the result being two adds in the output.
5658 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5659 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5660 if (UI->getOpcode() == ISD::STORE)
5661 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005662 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005663 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5664 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005665 if (C->getAPIntValue() == 1) {
5666 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005667 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005668 break;
5669 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005670 // An add of negative one (subtract of one) will be selected as a DEC.
5671 if (C->getAPIntValue().isAllOnesValue()) {
5672 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005673 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005674 break;
5675 }
5676 }
Dan Gohman076aee32009-03-04 19:44:21 +00005677 // Otherwise use a regular EFLAGS-setting add.
5678 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005679 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005680 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005681 case ISD::AND: {
5682 // If the primary and result isn't used, don't bother using X86ISD::AND,
5683 // because a TEST instruction will be better.
5684 bool NonFlagUse = false;
5685 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005686 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5687 SDNode *User = *UI;
5688 unsigned UOpNo = UI.getOperandNo();
5689 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5690 // Look pass truncate.
5691 UOpNo = User->use_begin().getOperandNo();
5692 User = *User->use_begin();
5693 }
5694 if (User->getOpcode() != ISD::BRCOND &&
5695 User->getOpcode() != ISD::SETCC &&
5696 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005697 NonFlagUse = true;
5698 break;
5699 }
Evan Cheng17751da2010-01-07 00:54:06 +00005700 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005701 if (!NonFlagUse)
5702 break;
5703 }
5704 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005705 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005706 case ISD::OR:
5707 case ISD::XOR:
5708 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005709 // likely to be selected as part of a load-modify-store instruction.
5710 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5711 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5712 if (UI->getOpcode() == ISD::STORE)
5713 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005714 // Otherwise use a regular EFLAGS-setting instruction.
5715 switch (Op.getNode()->getOpcode()) {
5716 case ISD::SUB: Opcode = X86ISD::SUB; break;
5717 case ISD::OR: Opcode = X86ISD::OR; break;
5718 case ISD::XOR: Opcode = X86ISD::XOR; break;
5719 case ISD::AND: Opcode = X86ISD::AND; break;
5720 default: llvm_unreachable("unexpected operator!");
5721 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005722 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005723 break;
5724 case X86ISD::ADD:
5725 case X86ISD::SUB:
5726 case X86ISD::INC:
5727 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005728 case X86ISD::OR:
5729 case X86ISD::XOR:
5730 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005731 return SDValue(Op.getNode(), 1);
5732 default:
5733 default_case:
5734 break;
5735 }
5736 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005737 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005738 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005739 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005740 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005741 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005742 DAG.ReplaceAllUsesWith(Op, New);
5743 return SDValue(New.getNode(), 1);
5744 }
5745 }
5746
5747 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005749 DAG.getConstant(0, Op.getValueType()));
5750}
5751
5752/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5753/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005754SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5755 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005756 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5757 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005758 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005759
5760 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005762}
5763
Evan Chengd40d03e2010-01-06 19:38:29 +00005764/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5765/// if it's possible.
5766static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005767 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005768 SDValue LHS, RHS;
5769 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5770 if (ConstantSDNode *Op010C =
5771 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5772 if (Op010C->getZExtValue() == 1) {
5773 LHS = Op0.getOperand(0);
5774 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005775 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005776 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5777 if (ConstantSDNode *Op000C =
5778 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5779 if (Op000C->getZExtValue() == 1) {
5780 LHS = Op0.getOperand(1);
5781 RHS = Op0.getOperand(0).getOperand(1);
5782 }
5783 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5784 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5785 SDValue AndLHS = Op0.getOperand(0);
5786 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5787 LHS = AndLHS.getOperand(0);
5788 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005789 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005790 }
Evan Cheng0488db92007-09-25 01:57:46 +00005791
Evan Chengd40d03e2010-01-06 19:38:29 +00005792 if (LHS.getNode()) {
5793 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5794 // instruction. Since the shift amount is in-range-or-undefined, we know
5795 // that doing a bittest on the i16 value is ok. We extend to i32 because
5796 // the encoding for the i16 version is larger than the i32 version.
5797 if (LHS.getValueType() == MVT::i8)
5798 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005799
Evan Chengd40d03e2010-01-06 19:38:29 +00005800 // If the operand types disagree, extend the shift amount to match. Since
5801 // BT ignores high bits (like shifts) we can use anyextend.
5802 if (LHS.getValueType() != RHS.getValueType())
5803 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005804
Evan Chengd40d03e2010-01-06 19:38:29 +00005805 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5806 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5807 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5808 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005809 }
5810
Evan Cheng54de3ea2010-01-05 06:52:31 +00005811 return SDValue();
5812}
5813
5814SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5815 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5816 SDValue Op0 = Op.getOperand(0);
5817 SDValue Op1 = Op.getOperand(1);
5818 DebugLoc dl = Op.getDebugLoc();
5819 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5820
5821 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005822 // Lower (X & (1 << N)) == 0 to BT(X, N).
5823 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5824 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5825 if (Op0.getOpcode() == ISD::AND &&
5826 Op0.hasOneUse() &&
5827 Op1.getOpcode() == ISD::Constant &&
5828 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5829 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5830 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5831 if (NewSetCC.getNode())
5832 return NewSetCC;
5833 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005834
Chris Lattnere55484e2008-12-25 05:34:37 +00005835 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5836 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005837 if (X86CC == X86::COND_INVALID)
5838 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005839
Dan Gohman31125812009-03-07 01:58:32 +00005840 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005841
5842 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005843 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005844 return DAG.getNode(ISD::AND, dl, MVT::i8,
5845 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5846 DAG.getConstant(X86CC, MVT::i8), Cond),
5847 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005848
Owen Anderson825b72b2009-08-11 20:47:22 +00005849 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5850 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005851}
5852
Dan Gohman475871a2008-07-27 21:46:04 +00005853SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5854 SDValue Cond;
5855 SDValue Op0 = Op.getOperand(0);
5856 SDValue Op1 = Op.getOperand(1);
5857 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005858 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005859 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5860 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005861 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005862
5863 if (isFP) {
5864 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005865 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005866 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5867 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005868 bool Swap = false;
5869
5870 switch (SetCCOpcode) {
5871 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005872 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005873 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005874 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005875 case ISD::SETGT: Swap = true; // Fallthrough
5876 case ISD::SETLT:
5877 case ISD::SETOLT: SSECC = 1; break;
5878 case ISD::SETOGE:
5879 case ISD::SETGE: Swap = true; // Fallthrough
5880 case ISD::SETLE:
5881 case ISD::SETOLE: SSECC = 2; break;
5882 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005883 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005884 case ISD::SETNE: SSECC = 4; break;
5885 case ISD::SETULE: Swap = true;
5886 case ISD::SETUGE: SSECC = 5; break;
5887 case ISD::SETULT: Swap = true;
5888 case ISD::SETUGT: SSECC = 6; break;
5889 case ISD::SETO: SSECC = 7; break;
5890 }
5891 if (Swap)
5892 std::swap(Op0, Op1);
5893
Nate Begemanfb8ead02008-07-25 19:05:58 +00005894 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005895 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005896 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005897 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005898 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5899 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005900 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005901 }
5902 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005903 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5905 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005906 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005907 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005908 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005909 }
5910 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005913
Nate Begeman30a0de92008-07-17 16:51:19 +00005914 // We are handling one of the integer comparisons here. Since SSE only has
5915 // GT and EQ comparisons for integer, swapping operands and multiple
5916 // operations may be required for some comparisons.
5917 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5918 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005919
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005921 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005922 case MVT::v8i8:
5923 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5924 case MVT::v4i16:
5925 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5926 case MVT::v2i32:
5927 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5928 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005930
Nate Begeman30a0de92008-07-17 16:51:19 +00005931 switch (SetCCOpcode) {
5932 default: break;
5933 case ISD::SETNE: Invert = true;
5934 case ISD::SETEQ: Opc = EQOpc; break;
5935 case ISD::SETLT: Swap = true;
5936 case ISD::SETGT: Opc = GTOpc; break;
5937 case ISD::SETGE: Swap = true;
5938 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5939 case ISD::SETULT: Swap = true;
5940 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5941 case ISD::SETUGE: Swap = true;
5942 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5943 }
5944 if (Swap)
5945 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005946
Nate Begeman30a0de92008-07-17 16:51:19 +00005947 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5948 // bits of the inputs before performing those operations.
5949 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005950 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005951 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5952 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005953 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005954 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5955 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005956 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5957 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005959
Dale Johannesenace16102009-02-03 19:33:06 +00005960 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005961
5962 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005963 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005964 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005965
Nate Begeman30a0de92008-07-17 16:51:19 +00005966 return Result;
5967}
Evan Cheng0488db92007-09-25 01:57:46 +00005968
Evan Cheng370e5342008-12-03 08:38:43 +00005969// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005970static bool isX86LogicalCmp(SDValue Op) {
5971 unsigned Opc = Op.getNode()->getOpcode();
5972 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5973 return true;
5974 if (Op.getResNo() == 1 &&
5975 (Opc == X86ISD::ADD ||
5976 Opc == X86ISD::SUB ||
5977 Opc == X86ISD::SMUL ||
5978 Opc == X86ISD::UMUL ||
5979 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005980 Opc == X86ISD::DEC ||
5981 Opc == X86ISD::OR ||
5982 Opc == X86ISD::XOR ||
5983 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005984 return true;
5985
5986 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005987}
5988
Dan Gohman475871a2008-07-27 21:46:04 +00005989SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005990 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005991 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005992 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005993 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005994
Dan Gohman1a492952009-10-20 16:22:37 +00005995 if (Cond.getOpcode() == ISD::SETCC) {
5996 SDValue NewCond = LowerSETCC(Cond, DAG);
5997 if (NewCond.getNode())
5998 Cond = NewCond;
5999 }
Evan Cheng734503b2006-09-11 02:19:56 +00006000
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006001 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6002 SDValue Op1 = Op.getOperand(1);
6003 SDValue Op2 = Op.getOperand(2);
6004 if (Cond.getOpcode() == X86ISD::SETCC &&
6005 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6006 SDValue Cmp = Cond.getOperand(1);
6007 if (Cmp.getOpcode() == X86ISD::CMP) {
6008 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6009 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6010 ConstantSDNode *RHSC =
6011 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6012 if (N1C && N1C->isAllOnesValue() &&
6013 N2C && N2C->isNullValue() &&
6014 RHSC && RHSC->isNullValue()) {
6015 SDValue CmpOp0 = Cmp.getOperand(0);
6016 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
6017 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6018 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6019 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6020 }
6021 }
6022 }
6023
Evan Chengad9c0a32009-12-15 00:53:42 +00006024 // Look pass (and (setcc_carry (cmp ...)), 1).
6025 if (Cond.getOpcode() == ISD::AND &&
6026 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6027 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6028 if (C && C->getAPIntValue() == 1)
6029 Cond = Cond.getOperand(0);
6030 }
6031
Evan Cheng3f41d662007-10-08 22:16:29 +00006032 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6033 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006034 if (Cond.getOpcode() == X86ISD::SETCC ||
6035 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006036 CC = Cond.getOperand(0);
6037
Dan Gohman475871a2008-07-27 21:46:04 +00006038 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006039 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006040 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006041
Evan Cheng3f41d662007-10-08 22:16:29 +00006042 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006043 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006044 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006045 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006046
Chris Lattnerd1980a52009-03-12 06:52:53 +00006047 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6048 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006049 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006050 addTest = false;
6051 }
6052 }
6053
6054 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006055 // Look pass the truncate.
6056 if (Cond.getOpcode() == ISD::TRUNCATE)
6057 Cond = Cond.getOperand(0);
6058
6059 // We know the result of AND is compared against zero. Try to match
6060 // it to BT.
6061 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6062 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6063 if (NewSetCC.getNode()) {
6064 CC = NewSetCC.getOperand(0);
6065 Cond = NewSetCC.getOperand(1);
6066 addTest = false;
6067 }
6068 }
6069 }
6070
6071 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006072 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006073 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006074 }
6075
Evan Cheng0488db92007-09-25 01:57:46 +00006076 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6077 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006078 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6079 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006080 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006081}
6082
Evan Cheng370e5342008-12-03 08:38:43 +00006083// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6084// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6085// from the AND / OR.
6086static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6087 Opc = Op.getOpcode();
6088 if (Opc != ISD::OR && Opc != ISD::AND)
6089 return false;
6090 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6091 Op.getOperand(0).hasOneUse() &&
6092 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6093 Op.getOperand(1).hasOneUse());
6094}
6095
Evan Cheng961d6d42009-02-02 08:19:07 +00006096// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6097// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006098static bool isXor1OfSetCC(SDValue Op) {
6099 if (Op.getOpcode() != ISD::XOR)
6100 return false;
6101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6102 if (N1C && N1C->getAPIntValue() == 1) {
6103 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6104 Op.getOperand(0).hasOneUse();
6105 }
6106 return false;
6107}
6108
Dan Gohman475871a2008-07-27 21:46:04 +00006109SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006110 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006111 SDValue Chain = Op.getOperand(0);
6112 SDValue Cond = Op.getOperand(1);
6113 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006114 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006115 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006116
Dan Gohman1a492952009-10-20 16:22:37 +00006117 if (Cond.getOpcode() == ISD::SETCC) {
6118 SDValue NewCond = LowerSETCC(Cond, DAG);
6119 if (NewCond.getNode())
6120 Cond = NewCond;
6121 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006122#if 0
6123 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006124 else if (Cond.getOpcode() == X86ISD::ADD ||
6125 Cond.getOpcode() == X86ISD::SUB ||
6126 Cond.getOpcode() == X86ISD::SMUL ||
6127 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006128 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006129#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006130
Evan Chengad9c0a32009-12-15 00:53:42 +00006131 // Look pass (and (setcc_carry (cmp ...)), 1).
6132 if (Cond.getOpcode() == ISD::AND &&
6133 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6134 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6135 if (C && C->getAPIntValue() == 1)
6136 Cond = Cond.getOperand(0);
6137 }
6138
Evan Cheng3f41d662007-10-08 22:16:29 +00006139 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6140 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006141 if (Cond.getOpcode() == X86ISD::SETCC ||
6142 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006143 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006144
Dan Gohman475871a2008-07-27 21:46:04 +00006145 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006146 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006147 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006148 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006149 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006150 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006151 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006152 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006153 default: break;
6154 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006155 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006156 // These can only come from an arithmetic instruction with overflow,
6157 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006158 Cond = Cond.getNode()->getOperand(1);
6159 addTest = false;
6160 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006161 }
Evan Cheng0488db92007-09-25 01:57:46 +00006162 }
Evan Cheng370e5342008-12-03 08:38:43 +00006163 } else {
6164 unsigned CondOpc;
6165 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6166 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006167 if (CondOpc == ISD::OR) {
6168 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6169 // two branches instead of an explicit OR instruction with a
6170 // separate test.
6171 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006172 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006173 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006174 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006175 Chain, Dest, CC, Cmp);
6176 CC = Cond.getOperand(1).getOperand(0);
6177 Cond = Cmp;
6178 addTest = false;
6179 }
6180 } else { // ISD::AND
6181 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6182 // two branches instead of an explicit AND instruction with a
6183 // separate test. However, we only do this if this block doesn't
6184 // have a fall-through edge, because this requires an explicit
6185 // jmp when the condition is false.
6186 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006187 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006188 Op.getNode()->hasOneUse()) {
6189 X86::CondCode CCode =
6190 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6191 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006192 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006193 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6194 // Look for an unconditional branch following this conditional branch.
6195 // We need this because we need to reverse the successors in order
6196 // to implement FCMP_OEQ.
6197 if (User.getOpcode() == ISD::BR) {
6198 SDValue FalseBB = User.getOperand(1);
6199 SDValue NewBR =
6200 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6201 assert(NewBR == User);
6202 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006203
Dale Johannesene4d209d2009-02-03 20:21:25 +00006204 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006205 Chain, Dest, CC, Cmp);
6206 X86::CondCode CCode =
6207 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6208 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006209 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006210 Cond = Cmp;
6211 addTest = false;
6212 }
6213 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006214 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006215 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6216 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6217 // It should be transformed during dag combiner except when the condition
6218 // is set by a arithmetics with overflow node.
6219 X86::CondCode CCode =
6220 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6221 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006222 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006223 Cond = Cond.getOperand(0).getOperand(1);
6224 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006225 }
Evan Cheng0488db92007-09-25 01:57:46 +00006226 }
6227
6228 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006229 // Look pass the truncate.
6230 if (Cond.getOpcode() == ISD::TRUNCATE)
6231 Cond = Cond.getOperand(0);
6232
6233 // We know the result of AND is compared against zero. Try to match
6234 // it to BT.
6235 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6236 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6237 if (NewSetCC.getNode()) {
6238 CC = NewSetCC.getOperand(0);
6239 Cond = NewSetCC.getOperand(1);
6240 addTest = false;
6241 }
6242 }
6243 }
6244
6245 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006247 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006248 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006249 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006250 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006251}
6252
Anton Korobeynikove060b532007-04-17 19:34:00 +00006253
6254// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6255// Calls to _alloca is needed to probe the stack when allocating more than 4k
6256// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6257// that the guard pages used by the OS virtual memory manager are allocated in
6258// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006259SDValue
6260X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006261 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006262 assert(Subtarget->isTargetCygMing() &&
6263 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006264 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006265
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006266 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006267 SDValue Chain = Op.getOperand(0);
6268 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006269 // FIXME: Ensure alignment here
6270
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006272
Owen Andersone50ed302009-08-10 22:56:29 +00006273 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006274 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006275
Chris Lattnere563bbc2008-10-11 22:08:30 +00006276 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006277
Dale Johannesendd64c412009-02-04 00:33:20 +00006278 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006279 Flag = Chain.getValue(1);
6280
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006282 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006283 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006284 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006285 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006286 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006287 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006288 Flag = Chain.getValue(1);
6289
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006290 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006291 DAG.getIntPtrConstant(0, true),
6292 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006293 Flag);
6294
Dale Johannesendd64c412009-02-04 00:33:20 +00006295 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006296
Dan Gohman475871a2008-07-27 21:46:04 +00006297 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006298 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006299}
6300
Dan Gohman475871a2008-07-27 21:46:04 +00006301SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006302X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006303 SDValue Chain,
6304 SDValue Dst, SDValue Src,
6305 SDValue Size, unsigned Align,
6306 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006307 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006308 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006309
Bill Wendling6f287b22008-09-30 21:22:07 +00006310 // If not DWORD aligned or size is more than the threshold, call the library.
6311 // The libc version is likely to be faster for these cases. It can use the
6312 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006313 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006314 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006315 ConstantSize->getZExtValue() >
6316 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006317 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006318
6319 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006320 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006321
Bill Wendling6158d842008-10-01 00:59:58 +00006322 if (const char *bzeroEntry = V &&
6323 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006324 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006325 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006326 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006327 TargetLowering::ArgListEntry Entry;
6328 Entry.Node = Dst;
6329 Entry.Ty = IntPtrTy;
6330 Args.push_back(Entry);
6331 Entry.Node = Size;
6332 Args.push_back(Entry);
6333 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006334 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6335 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006336 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006337 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6338 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006339 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006340 }
6341
Dan Gohman707e0182008-04-12 04:36:06 +00006342 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006343 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006344 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006345
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006346 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006347 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006348 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006349 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006350 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006351 unsigned BytesLeft = 0;
6352 bool TwoRepStos = false;
6353 if (ValC) {
6354 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006355 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006356
Evan Cheng0db9fe62006-04-25 20:13:52 +00006357 // If the value is a constant, then we can potentially use larger sets.
6358 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006359 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006360 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006361 ValReg = X86::AX;
6362 Val = (Val << 8) | Val;
6363 break;
6364 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006365 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006366 ValReg = X86::EAX;
6367 Val = (Val << 8) | Val;
6368 Val = (Val << 16) | Val;
6369 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006370 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006371 ValReg = X86::RAX;
6372 Val = (Val << 32) | Val;
6373 }
6374 break;
6375 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006376 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006377 ValReg = X86::AL;
6378 Count = DAG.getIntPtrConstant(SizeVal);
6379 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006380 }
6381
Owen Anderson825b72b2009-08-11 20:47:22 +00006382 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006383 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006384 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6385 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006386 }
6387
Dale Johannesen0f502f62009-02-03 22:26:09 +00006388 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006389 InFlag);
6390 InFlag = Chain.getValue(1);
6391 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006393 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006394 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006395 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006396 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006397
Scott Michelfdc40a02009-02-17 22:15:04 +00006398 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006399 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006400 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006401 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006402 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006403 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006404 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006405 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006406
Owen Anderson825b72b2009-08-11 20:47:22 +00006407 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006408 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6409 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006410
Evan Cheng0db9fe62006-04-25 20:13:52 +00006411 if (TwoRepStos) {
6412 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006413 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006414 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006415 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006416 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6417 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006418 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006419 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006420 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006421 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006422 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6423 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006424 } else 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 AddrVT = Dst.getValueType();
6428 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006429
Dale Johannesen0f502f62009-02-03 22:26:09 +00006430 Chain = DAG.getMemset(Chain, dl,
6431 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006432 DAG.getConstant(Offset, AddrVT)),
6433 Src,
6434 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006435 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006436 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006437
Dan Gohman707e0182008-04-12 04:36:06 +00006438 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006439 return Chain;
6440}
Evan Cheng11e15b32006-04-03 20:53:28 +00006441
Dan Gohman475871a2008-07-27 21:46:04 +00006442SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006443X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006444 SDValue Chain, SDValue Dst, SDValue Src,
6445 SDValue Size, unsigned Align,
6446 bool AlwaysInline,
6447 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006448 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006449 // This requires the copy size to be a constant, preferrably
6450 // within a subtarget-specific limit.
6451 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6452 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006453 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006454 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006455 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006456 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006457
Evan Cheng1887c1c2008-08-21 21:00:15 +00006458 /// If not DWORD aligned, call the library.
6459 if ((Align & 3) != 0)
6460 return SDValue();
6461
6462 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006464 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006465 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006466
Duncan Sands83ec4b62008-06-06 12:08:01 +00006467 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006468 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006469 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006470 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006471
Dan Gohman475871a2008-07-27 21:46:04 +00006472 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006473 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006474 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006475 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006476 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006477 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006478 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006479 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006480 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006481 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006482 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006483 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484 InFlag = Chain.getValue(1);
6485
Owen Anderson825b72b2009-08-11 20:47:22 +00006486 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006487 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6488 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6489 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006490
Dan Gohman475871a2008-07-27 21:46:04 +00006491 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006492 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006493 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006494 // Handle the last 1 - 7 bytes.
6495 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006496 EVT DstVT = Dst.getValueType();
6497 EVT SrcVT = Src.getValueType();
6498 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006499 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006500 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006501 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006502 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006503 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006504 DAG.getConstant(BytesLeft, SizeVT),
6505 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006506 DstSV, DstSVOff + Offset,
6507 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006508 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006509
Owen Anderson825b72b2009-08-11 20:47:22 +00006510 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006511 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006512}
6513
Dan Gohman475871a2008-07-27 21:46:04 +00006514SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006515 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006516 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006517
Evan Cheng25ab6902006-09-08 06:48:29 +00006518 if (!Subtarget->is64Bit()) {
6519 // vastart just stores the address of the VarArgsFrameIndex slot into the
6520 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006521 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006522 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006523 }
6524
6525 // __va_list_tag:
6526 // gp_offset (0 - 6 * 8)
6527 // fp_offset (48 - 48 + 8 * 16)
6528 // overflow_arg_area (point to parameters coming in memory).
6529 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006530 SmallVector<SDValue, 8> MemOps;
6531 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006532 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006533 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006534 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006535 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006536 MemOps.push_back(Store);
6537
6538 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006539 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006540 FIN, DAG.getIntPtrConstant(4));
6541 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006542 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006543 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006544 MemOps.push_back(Store);
6545
6546 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006547 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006548 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006549 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006550 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006551 MemOps.push_back(Store);
6552
6553 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006554 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006555 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006556 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006557 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006558 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006559 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006560 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006561}
6562
Dan Gohman475871a2008-07-27 21:46:04 +00006563SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006564 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6565 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006566 SDValue Chain = Op.getOperand(0);
6567 SDValue SrcPtr = Op.getOperand(1);
6568 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006569
Torok Edwindac237e2009-07-08 20:53:28 +00006570 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006571 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006572}
6573
Dan Gohman475871a2008-07-27 21:46:04 +00006574SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006575 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006576 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006577 SDValue Chain = Op.getOperand(0);
6578 SDValue DstPtr = Op.getOperand(1);
6579 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006580 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6581 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006582 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006583
Dale Johannesendd64c412009-02-04 00:33:20 +00006584 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006585 DAG.getIntPtrConstant(24), 8, false,
6586 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006587}
6588
Dan Gohman475871a2008-07-27 21:46:04 +00006589SDValue
6590X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006591 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006592 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006594 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006595 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006596 case Intrinsic::x86_sse_comieq_ss:
6597 case Intrinsic::x86_sse_comilt_ss:
6598 case Intrinsic::x86_sse_comile_ss:
6599 case Intrinsic::x86_sse_comigt_ss:
6600 case Intrinsic::x86_sse_comige_ss:
6601 case Intrinsic::x86_sse_comineq_ss:
6602 case Intrinsic::x86_sse_ucomieq_ss:
6603 case Intrinsic::x86_sse_ucomilt_ss:
6604 case Intrinsic::x86_sse_ucomile_ss:
6605 case Intrinsic::x86_sse_ucomigt_ss:
6606 case Intrinsic::x86_sse_ucomige_ss:
6607 case Intrinsic::x86_sse_ucomineq_ss:
6608 case Intrinsic::x86_sse2_comieq_sd:
6609 case Intrinsic::x86_sse2_comilt_sd:
6610 case Intrinsic::x86_sse2_comile_sd:
6611 case Intrinsic::x86_sse2_comigt_sd:
6612 case Intrinsic::x86_sse2_comige_sd:
6613 case Intrinsic::x86_sse2_comineq_sd:
6614 case Intrinsic::x86_sse2_ucomieq_sd:
6615 case Intrinsic::x86_sse2_ucomilt_sd:
6616 case Intrinsic::x86_sse2_ucomile_sd:
6617 case Intrinsic::x86_sse2_ucomigt_sd:
6618 case Intrinsic::x86_sse2_ucomige_sd:
6619 case Intrinsic::x86_sse2_ucomineq_sd: {
6620 unsigned Opc = 0;
6621 ISD::CondCode CC = ISD::SETCC_INVALID;
6622 switch (IntNo) {
6623 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006624 case Intrinsic::x86_sse_comieq_ss:
6625 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626 Opc = X86ISD::COMI;
6627 CC = ISD::SETEQ;
6628 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006629 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006630 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631 Opc = X86ISD::COMI;
6632 CC = ISD::SETLT;
6633 break;
6634 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006635 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006636 Opc = X86ISD::COMI;
6637 CC = ISD::SETLE;
6638 break;
6639 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006640 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006641 Opc = X86ISD::COMI;
6642 CC = ISD::SETGT;
6643 break;
6644 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006645 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646 Opc = X86ISD::COMI;
6647 CC = ISD::SETGE;
6648 break;
6649 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006650 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006651 Opc = X86ISD::COMI;
6652 CC = ISD::SETNE;
6653 break;
6654 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006655 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006656 Opc = X86ISD::UCOMI;
6657 CC = ISD::SETEQ;
6658 break;
6659 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006660 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 Opc = X86ISD::UCOMI;
6662 CC = ISD::SETLT;
6663 break;
6664 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006665 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006666 Opc = X86ISD::UCOMI;
6667 CC = ISD::SETLE;
6668 break;
6669 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006670 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006671 Opc = X86ISD::UCOMI;
6672 CC = ISD::SETGT;
6673 break;
6674 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006675 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676 Opc = X86ISD::UCOMI;
6677 CC = ISD::SETGE;
6678 break;
6679 case Intrinsic::x86_sse_ucomineq_ss:
6680 case Intrinsic::x86_sse2_ucomineq_sd:
6681 Opc = X86ISD::UCOMI;
6682 CC = ISD::SETNE;
6683 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006684 }
Evan Cheng734503b2006-09-11 02:19:56 +00006685
Dan Gohman475871a2008-07-27 21:46:04 +00006686 SDValue LHS = Op.getOperand(1);
6687 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006688 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006689 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006690 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6691 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6692 DAG.getConstant(X86CC, MVT::i8), Cond);
6693 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006694 }
Eric Christopher71c67532009-07-29 00:28:05 +00006695 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006696 // an integer value, not just an instruction so lower it to the ptest
6697 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006698 case Intrinsic::x86_sse41_ptestz:
6699 case Intrinsic::x86_sse41_ptestc:
6700 case Intrinsic::x86_sse41_ptestnzc:{
6701 unsigned X86CC = 0;
6702 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006703 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006704 case Intrinsic::x86_sse41_ptestz:
6705 // ZF = 1
6706 X86CC = X86::COND_E;
6707 break;
6708 case Intrinsic::x86_sse41_ptestc:
6709 // CF = 1
6710 X86CC = X86::COND_B;
6711 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006712 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006713 // ZF and CF = 0
6714 X86CC = X86::COND_A;
6715 break;
6716 }
Eric Christopherfd179292009-08-27 18:07:15 +00006717
Eric Christopher71c67532009-07-29 00:28:05 +00006718 SDValue LHS = Op.getOperand(1);
6719 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006720 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6721 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6722 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6723 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006724 }
Evan Cheng5759f972008-05-04 09:15:50 +00006725
6726 // Fix vector shift instructions where the last operand is a non-immediate
6727 // i32 value.
6728 case Intrinsic::x86_sse2_pslli_w:
6729 case Intrinsic::x86_sse2_pslli_d:
6730 case Intrinsic::x86_sse2_pslli_q:
6731 case Intrinsic::x86_sse2_psrli_w:
6732 case Intrinsic::x86_sse2_psrli_d:
6733 case Intrinsic::x86_sse2_psrli_q:
6734 case Intrinsic::x86_sse2_psrai_w:
6735 case Intrinsic::x86_sse2_psrai_d:
6736 case Intrinsic::x86_mmx_pslli_w:
6737 case Intrinsic::x86_mmx_pslli_d:
6738 case Intrinsic::x86_mmx_pslli_q:
6739 case Intrinsic::x86_mmx_psrli_w:
6740 case Intrinsic::x86_mmx_psrli_d:
6741 case Intrinsic::x86_mmx_psrli_q:
6742 case Intrinsic::x86_mmx_psrai_w:
6743 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006744 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006745 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006746 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006747
6748 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006749 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006750 switch (IntNo) {
6751 case Intrinsic::x86_sse2_pslli_w:
6752 NewIntNo = Intrinsic::x86_sse2_psll_w;
6753 break;
6754 case Intrinsic::x86_sse2_pslli_d:
6755 NewIntNo = Intrinsic::x86_sse2_psll_d;
6756 break;
6757 case Intrinsic::x86_sse2_pslli_q:
6758 NewIntNo = Intrinsic::x86_sse2_psll_q;
6759 break;
6760 case Intrinsic::x86_sse2_psrli_w:
6761 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6762 break;
6763 case Intrinsic::x86_sse2_psrli_d:
6764 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6765 break;
6766 case Intrinsic::x86_sse2_psrli_q:
6767 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6768 break;
6769 case Intrinsic::x86_sse2_psrai_w:
6770 NewIntNo = Intrinsic::x86_sse2_psra_w;
6771 break;
6772 case Intrinsic::x86_sse2_psrai_d:
6773 NewIntNo = Intrinsic::x86_sse2_psra_d;
6774 break;
6775 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006776 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006777 switch (IntNo) {
6778 case Intrinsic::x86_mmx_pslli_w:
6779 NewIntNo = Intrinsic::x86_mmx_psll_w;
6780 break;
6781 case Intrinsic::x86_mmx_pslli_d:
6782 NewIntNo = Intrinsic::x86_mmx_psll_d;
6783 break;
6784 case Intrinsic::x86_mmx_pslli_q:
6785 NewIntNo = Intrinsic::x86_mmx_psll_q;
6786 break;
6787 case Intrinsic::x86_mmx_psrli_w:
6788 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6789 break;
6790 case Intrinsic::x86_mmx_psrli_d:
6791 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6792 break;
6793 case Intrinsic::x86_mmx_psrli_q:
6794 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6795 break;
6796 case Intrinsic::x86_mmx_psrai_w:
6797 NewIntNo = Intrinsic::x86_mmx_psra_w;
6798 break;
6799 case Intrinsic::x86_mmx_psrai_d:
6800 NewIntNo = Intrinsic::x86_mmx_psra_d;
6801 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006802 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006803 }
6804 break;
6805 }
6806 }
Mon P Wangefa42202009-09-03 19:56:25 +00006807
6808 // The vector shift intrinsics with scalars uses 32b shift amounts but
6809 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6810 // to be zero.
6811 SDValue ShOps[4];
6812 ShOps[0] = ShAmt;
6813 ShOps[1] = DAG.getConstant(0, MVT::i32);
6814 if (ShAmtVT == MVT::v4i32) {
6815 ShOps[2] = DAG.getUNDEF(MVT::i32);
6816 ShOps[3] = DAG.getUNDEF(MVT::i32);
6817 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6818 } else {
6819 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6820 }
6821
Owen Andersone50ed302009-08-10 22:56:29 +00006822 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006823 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006824 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006825 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006826 Op.getOperand(1), ShAmt);
6827 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006828 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006829}
Evan Cheng72261582005-12-20 06:22:03 +00006830
Dan Gohman475871a2008-07-27 21:46:04 +00006831SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006832 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006833 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006834
6835 if (Depth > 0) {
6836 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6837 SDValue Offset =
6838 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006839 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006840 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006841 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006842 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006843 NULL, 0);
6844 }
6845
6846 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006847 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006848 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006849 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006850}
6851
Dan Gohman475871a2008-07-27 21:46:04 +00006852SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006853 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6854 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006855 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006856 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006857 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6858 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006859 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006860 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006861 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006862 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006863}
6864
Dan Gohman475871a2008-07-27 21:46:04 +00006865SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006866 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006867 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006868}
6869
Dan Gohman475871a2008-07-27 21:46:04 +00006870SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006871{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006872 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006873 SDValue Chain = Op.getOperand(0);
6874 SDValue Offset = Op.getOperand(1);
6875 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006876 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006877
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006878 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6879 getPointerTy());
6880 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006881
Dale Johannesene4d209d2009-02-03 20:21:25 +00006882 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006883 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006884 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6885 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006886 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006887 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006888
Dale Johannesene4d209d2009-02-03 20:21:25 +00006889 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006890 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006891 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006892}
6893
Dan Gohman475871a2008-07-27 21:46:04 +00006894SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006895 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006896 SDValue Root = Op.getOperand(0);
6897 SDValue Trmp = Op.getOperand(1); // trampoline
6898 SDValue FPtr = Op.getOperand(2); // nested function
6899 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006900 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006901
Dan Gohman69de1932008-02-06 22:27:42 +00006902 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006903
Duncan Sands339e14f2008-01-16 22:55:25 +00006904 const X86InstrInfo *TII =
6905 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6906
Duncan Sandsb116fac2007-07-27 20:02:49 +00006907 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006908 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006909
6910 // Large code-model.
6911
6912 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6913 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6914
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006915 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6916 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006917
6918 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6919
6920 // Load the pointer to the nested function into R11.
6921 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006922 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006923 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006924 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006925
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6927 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006928 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006929
6930 // Load the 'nest' parameter value into R10.
6931 // R10 is specified in X86CallingConv.td
6932 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006933 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6934 DAG.getConstant(10, MVT::i64));
6935 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006936 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006937
Owen Anderson825b72b2009-08-11 20:47:22 +00006938 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6939 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006940 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006941
6942 // Jump to the nested function.
6943 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6945 DAG.getConstant(20, MVT::i64));
6946 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006947 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006948
6949 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006950 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6951 DAG.getConstant(22, MVT::i64));
6952 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006953 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006954
Dan Gohman475871a2008-07-27 21:46:04 +00006955 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006956 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006957 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006958 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006959 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006960 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006961 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006962 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006963
6964 switch (CC) {
6965 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006966 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006967 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006968 case CallingConv::X86_StdCall: {
6969 // Pass 'nest' parameter in ECX.
6970 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006971 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006972
6973 // Check that ECX wasn't needed by an 'inreg' parameter.
6974 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006975 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006976
Chris Lattner58d74912008-03-12 17:45:29 +00006977 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006978 unsigned InRegCount = 0;
6979 unsigned Idx = 1;
6980
6981 for (FunctionType::param_iterator I = FTy->param_begin(),
6982 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006983 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006984 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006985 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006986
6987 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006988 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006989 }
6990 }
6991 break;
6992 }
6993 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006994 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006995 // Pass 'nest' parameter in EAX.
6996 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006997 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006998 break;
6999 }
7000
Dan Gohman475871a2008-07-27 21:46:04 +00007001 SDValue OutChains[4];
7002 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007003
Owen Anderson825b72b2009-08-11 20:47:22 +00007004 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7005 DAG.getConstant(10, MVT::i32));
7006 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007007
Duncan Sands339e14f2008-01-16 22:55:25 +00007008 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007009 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007010 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00007012 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007013
Owen Anderson825b72b2009-08-11 20:47:22 +00007014 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7015 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007016 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007017
Duncan Sands339e14f2008-01-16 22:55:25 +00007018 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00007019 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7020 DAG.getConstant(5, MVT::i32));
7021 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00007022 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007023
Owen Anderson825b72b2009-08-11 20:47:22 +00007024 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7025 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007026 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007027
Dan Gohman475871a2008-07-27 21:46:04 +00007028 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007029 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007030 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007031 }
7032}
7033
Dan Gohman475871a2008-07-27 21:46:04 +00007034SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007035 /*
7036 The rounding mode is in bits 11:10 of FPSR, and has the following
7037 settings:
7038 00 Round to nearest
7039 01 Round to -inf
7040 10 Round to +inf
7041 11 Round to 0
7042
7043 FLT_ROUNDS, on the other hand, expects the following:
7044 -1 Undefined
7045 0 Round to 0
7046 1 Round to nearest
7047 2 Round to +inf
7048 3 Round to -inf
7049
7050 To perform the conversion, we do:
7051 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7052 */
7053
7054 MachineFunction &MF = DAG.getMachineFunction();
7055 const TargetMachine &TM = MF.getTarget();
7056 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7057 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007058 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007059 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007060
7061 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007062 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007063 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007064
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007066 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007067
7068 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007069 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007070
7071 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007072 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 DAG.getNode(ISD::SRL, dl, MVT::i16,
7074 DAG.getNode(ISD::AND, dl, MVT::i16,
7075 CWD, DAG.getConstant(0x800, MVT::i16)),
7076 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007077 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 DAG.getNode(ISD::SRL, dl, MVT::i16,
7079 DAG.getNode(ISD::AND, dl, MVT::i16,
7080 CWD, DAG.getConstant(0x400, MVT::i16)),
7081 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007082
Dan Gohman475871a2008-07-27 21:46:04 +00007083 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007084 DAG.getNode(ISD::AND, dl, MVT::i16,
7085 DAG.getNode(ISD::ADD, dl, MVT::i16,
7086 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7087 DAG.getConstant(1, MVT::i16)),
7088 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007089
7090
Duncan Sands83ec4b62008-06-06 12:08:01 +00007091 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007092 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007093}
7094
Dan Gohman475871a2008-07-27 21:46:04 +00007095SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007096 EVT VT = Op.getValueType();
7097 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007098 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007099 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007100
7101 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007103 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007104 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007105 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007106 }
Evan Cheng18efe262007-12-14 02:13:44 +00007107
Evan Cheng152804e2007-12-14 08:30:15 +00007108 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007110 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007111
7112 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007113 SDValue Ops[] = {
7114 Op,
7115 DAG.getConstant(NumBits+NumBits-1, OpVT),
7116 DAG.getConstant(X86::COND_E, MVT::i8),
7117 Op.getValue(1)
7118 };
7119 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007120
7121 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007122 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007123
Owen Anderson825b72b2009-08-11 20:47:22 +00007124 if (VT == MVT::i8)
7125 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007126 return Op;
7127}
7128
Dan Gohman475871a2008-07-27 21:46:04 +00007129SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007130 EVT VT = Op.getValueType();
7131 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007132 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007133 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007134
7135 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007136 if (VT == MVT::i8) {
7137 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007139 }
Evan Cheng152804e2007-12-14 08:30:15 +00007140
7141 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007142 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007143 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007144
7145 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007146 SDValue Ops[] = {
7147 Op,
7148 DAG.getConstant(NumBits, OpVT),
7149 DAG.getConstant(X86::COND_E, MVT::i8),
7150 Op.getValue(1)
7151 };
7152 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007153
Owen Anderson825b72b2009-08-11 20:47:22 +00007154 if (VT == MVT::i8)
7155 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007156 return Op;
7157}
7158
Mon P Wangaf9b9522008-12-18 21:42:19 +00007159SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007160 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007162 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007163
Mon P Wangaf9b9522008-12-18 21:42:19 +00007164 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7165 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7166 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7167 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7168 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7169 //
7170 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7171 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7172 // return AloBlo + AloBhi + AhiBlo;
7173
7174 SDValue A = Op.getOperand(0);
7175 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007176
Dale Johannesene4d209d2009-02-03 20:21:25 +00007177 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007178 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7179 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007180 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007181 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7182 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007183 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007184 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007185 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007188 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007189 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007191 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007192 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7194 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007195 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7197 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007198 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7199 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007200 return Res;
7201}
7202
7203
Bill Wendling74c37652008-12-09 22:08:41 +00007204SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7205 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7206 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007207 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7208 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007209 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007210 SDValue LHS = N->getOperand(0);
7211 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007212 unsigned BaseOp = 0;
7213 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007214 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007215
7216 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007217 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007218 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007219 // A subtract of one will be selected as a INC. Note that INC doesn't
7220 // set CF, so we can't do this for UADDO.
7221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7222 if (C->getAPIntValue() == 1) {
7223 BaseOp = X86ISD::INC;
7224 Cond = X86::COND_O;
7225 break;
7226 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007227 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007228 Cond = X86::COND_O;
7229 break;
7230 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007231 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007232 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007233 break;
7234 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007235 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7236 // set CF, so we can't do this for USUBO.
7237 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7238 if (C->getAPIntValue() == 1) {
7239 BaseOp = X86ISD::DEC;
7240 Cond = X86::COND_O;
7241 break;
7242 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007243 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007244 Cond = X86::COND_O;
7245 break;
7246 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007247 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007248 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007249 break;
7250 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007251 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007252 Cond = X86::COND_O;
7253 break;
7254 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007255 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007256 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007257 break;
7258 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007259
Bill Wendling61edeb52008-12-02 01:06:39 +00007260 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007263
Bill Wendling61edeb52008-12-02 01:06:39 +00007264 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007265 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007266 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007267
Bill Wendling61edeb52008-12-02 01:06:39 +00007268 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7269 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007270}
7271
Dan Gohman475871a2008-07-27 21:46:04 +00007272SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007273 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007274 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007275 unsigned Reg = 0;
7276 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007277 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007278 default:
7279 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007280 case MVT::i8: Reg = X86::AL; size = 1; break;
7281 case MVT::i16: Reg = X86::AX; size = 2; break;
7282 case MVT::i32: Reg = X86::EAX; size = 4; break;
7283 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007284 assert(Subtarget->is64Bit() && "Node not type legal!");
7285 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007286 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007287 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007288 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007289 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007290 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007291 Op.getOperand(1),
7292 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007293 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007294 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007295 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007296 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007297 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007298 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007299 return cpOut;
7300}
7301
Duncan Sands1607f052008-12-01 11:39:25 +00007302SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007303 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007304 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007305 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007306 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007307 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007308 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7310 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007311 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7313 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007314 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007315 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007316 rdx.getValue(1)
7317 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007318 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007319}
7320
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007321SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7322 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007323 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007324 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007325 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007326 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007327 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007328 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007329 Node->getOperand(0),
7330 Node->getOperand(1), negOp,
7331 cast<AtomicSDNode>(Node)->getSrcValue(),
7332 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007333}
7334
Evan Cheng0db9fe62006-04-25 20:13:52 +00007335/// LowerOperation - Provide custom lowering hooks for some operations.
7336///
Dan Gohman475871a2008-07-27 21:46:04 +00007337SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007338 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007339 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007340 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7341 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007343 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007344 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7345 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7346 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7347 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7348 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7349 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007350 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007351 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007352 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007353 case ISD::SHL_PARTS:
7354 case ISD::SRA_PARTS:
7355 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7356 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007357 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007358 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007359 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007360 case ISD::FABS: return LowerFABS(Op, DAG);
7361 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007362 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007363 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007364 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007365 case ISD::SELECT: return LowerSELECT(Op, DAG);
7366 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007367 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007368 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007369 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007370 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007371 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007372 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7373 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007374 case ISD::FRAME_TO_ARGS_OFFSET:
7375 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007376 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007377 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007378 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007379 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007380 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7381 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007382 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007383 case ISD::SADDO:
7384 case ISD::UADDO:
7385 case ISD::SSUBO:
7386 case ISD::USUBO:
7387 case ISD::SMULO:
7388 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007389 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007390 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007391}
7392
Duncan Sands1607f052008-12-01 11:39:25 +00007393void X86TargetLowering::
7394ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7395 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007396 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007397 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007398 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007399
7400 SDValue Chain = Node->getOperand(0);
7401 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007402 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007403 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007405 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007406 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007408 SDValue Result =
7409 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7410 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007411 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007412 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007413 Results.push_back(Result.getValue(2));
7414}
7415
Duncan Sands126d9072008-07-04 11:47:58 +00007416/// ReplaceNodeResults - Replace a node with an illegal result type
7417/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007418void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7419 SmallVectorImpl<SDValue>&Results,
7420 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007421 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007422 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007423 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007424 assert(false && "Do not know how to custom type legalize this operation!");
7425 return;
7426 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007427 std::pair<SDValue,SDValue> Vals =
7428 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007429 SDValue FIST = Vals.first, StackSlot = Vals.second;
7430 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007431 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007432 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007433 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007434 }
7435 return;
7436 }
7437 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007439 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007441 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007442 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007443 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007444 eax.getValue(2));
7445 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7446 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007448 Results.push_back(edx.getValue(1));
7449 return;
7450 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007451 case ISD::SDIV:
7452 case ISD::UDIV:
7453 case ISD::SREM:
7454 case ISD::UREM: {
7455 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7456 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7457 return;
7458 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007459 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007460 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007462 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007463 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7464 DAG.getConstant(0, MVT::i32));
7465 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7466 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007467 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7468 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007469 cpInL.getValue(1));
7470 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007471 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7472 DAG.getConstant(0, MVT::i32));
7473 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7474 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007475 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007476 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007477 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007478 swapInL.getValue(1));
7479 SDValue Ops[] = { swapInH.getValue(0),
7480 N->getOperand(1),
7481 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007482 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007483 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007484 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007485 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007486 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007487 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007488 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007490 Results.push_back(cpOutH.getValue(1));
7491 return;
7492 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007493 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007494 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7495 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007496 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007497 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7498 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007499 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007500 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7501 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007502 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007503 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7504 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007505 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007506 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7507 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007508 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007509 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7510 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007511 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007512 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7513 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007514 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007515}
7516
Evan Cheng72261582005-12-20 06:22:03 +00007517const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7518 switch (Opcode) {
7519 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007520 case X86ISD::BSF: return "X86ISD::BSF";
7521 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007522 case X86ISD::SHLD: return "X86ISD::SHLD";
7523 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007524 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007525 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007526 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007527 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007528 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007529 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007530 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7531 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7532 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007533 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007534 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007535 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007536 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007537 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007538 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007539 case X86ISD::COMI: return "X86ISD::COMI";
7540 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007541 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007542 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007543 case X86ISD::CMOV: return "X86ISD::CMOV";
7544 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007545 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007546 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7547 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007548 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007549 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007550 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007551 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007552 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007553 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7554 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007555 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007556 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007557 case X86ISD::FMAX: return "X86ISD::FMAX";
7558 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007559 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7560 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007561 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007562 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007563 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007564 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007565 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007566 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7567 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007568 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7569 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7570 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7571 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7572 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7573 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007574 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7575 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007576 case X86ISD::VSHL: return "X86ISD::VSHL";
7577 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007578 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7579 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7580 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7581 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7582 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7583 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7584 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7585 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7586 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7587 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007588 case X86ISD::ADD: return "X86ISD::ADD";
7589 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007590 case X86ISD::SMUL: return "X86ISD::SMUL";
7591 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007592 case X86ISD::INC: return "X86ISD::INC";
7593 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007594 case X86ISD::OR: return "X86ISD::OR";
7595 case X86ISD::XOR: return "X86ISD::XOR";
7596 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007597 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007598 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007599 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007600 }
7601}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007602
Chris Lattnerc9addb72007-03-30 23:15:24 +00007603// isLegalAddressingMode - Return true if the addressing mode represented
7604// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007605bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007606 const Type *Ty) const {
7607 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007608 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007609
Chris Lattnerc9addb72007-03-30 23:15:24 +00007610 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007611 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007612 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007613
Chris Lattnerc9addb72007-03-30 23:15:24 +00007614 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007615 unsigned GVFlags =
7616 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007617
Chris Lattnerdfed4132009-07-10 07:38:24 +00007618 // If a reference to this global requires an extra load, we can't fold it.
7619 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007620 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007621
Chris Lattnerdfed4132009-07-10 07:38:24 +00007622 // If BaseGV requires a register for the PIC base, we cannot also have a
7623 // BaseReg specified.
7624 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007625 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007626
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007627 // If lower 4G is not available, then we must use rip-relative addressing.
7628 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7629 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007631
Chris Lattnerc9addb72007-03-30 23:15:24 +00007632 switch (AM.Scale) {
7633 case 0:
7634 case 1:
7635 case 2:
7636 case 4:
7637 case 8:
7638 // These scales always work.
7639 break;
7640 case 3:
7641 case 5:
7642 case 9:
7643 // These scales are formed with basereg+scalereg. Only accept if there is
7644 // no basereg yet.
7645 if (AM.HasBaseReg)
7646 return false;
7647 break;
7648 default: // Other stuff never works.
7649 return false;
7650 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007651
Chris Lattnerc9addb72007-03-30 23:15:24 +00007652 return true;
7653}
7654
7655
Evan Cheng2bd122c2007-10-26 01:56:11 +00007656bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7657 if (!Ty1->isInteger() || !Ty2->isInteger())
7658 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007659 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7660 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007661 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007662 return false;
7663 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007664}
7665
Owen Andersone50ed302009-08-10 22:56:29 +00007666bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007667 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007668 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007669 unsigned NumBits1 = VT1.getSizeInBits();
7670 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007671 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007672 return false;
7673 return Subtarget->is64Bit() || NumBits1 < 64;
7674}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007675
Dan Gohman97121ba2009-04-08 00:15:30 +00007676bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007677 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007678 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007679}
7680
Owen Andersone50ed302009-08-10 22:56:29 +00007681bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007682 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007683 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007684}
7685
Owen Andersone50ed302009-08-10 22:56:29 +00007686bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007687 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007688 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007689}
7690
Evan Cheng60c07e12006-07-05 22:17:51 +00007691/// isShuffleMaskLegal - Targets can use this to indicate that they only
7692/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7693/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7694/// are assumed to be legal.
7695bool
Eric Christopherfd179292009-08-27 18:07:15 +00007696X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007697 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007698 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007699 if (VT.getSizeInBits() == 64)
7700 return false;
7701
Nate Begemana09008b2009-10-19 02:17:23 +00007702 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007703 return (VT.getVectorNumElements() == 2 ||
7704 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7705 isMOVLMask(M, VT) ||
7706 isSHUFPMask(M, VT) ||
7707 isPSHUFDMask(M, VT) ||
7708 isPSHUFHWMask(M, VT) ||
7709 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007710 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007711 isUNPCKLMask(M, VT) ||
7712 isUNPCKHMask(M, VT) ||
7713 isUNPCKL_v_undef_Mask(M, VT) ||
7714 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007715}
7716
Dan Gohman7d8143f2008-04-09 20:09:42 +00007717bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007718X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007719 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007720 unsigned NumElts = VT.getVectorNumElements();
7721 // FIXME: This collection of masks seems suspect.
7722 if (NumElts == 2)
7723 return true;
7724 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7725 return (isMOVLMask(Mask, VT) ||
7726 isCommutedMOVLMask(Mask, VT, true) ||
7727 isSHUFPMask(Mask, VT) ||
7728 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007729 }
7730 return false;
7731}
7732
7733//===----------------------------------------------------------------------===//
7734// X86 Scheduler Hooks
7735//===----------------------------------------------------------------------===//
7736
Mon P Wang63307c32008-05-05 19:05:59 +00007737// private utility function
7738MachineBasicBlock *
7739X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7740 MachineBasicBlock *MBB,
7741 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007742 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007743 unsigned LoadOpc,
7744 unsigned CXchgOpc,
7745 unsigned copyOpc,
7746 unsigned notOpc,
7747 unsigned EAXreg,
7748 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007749 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007750 // For the atomic bitwise operator, we generate
7751 // thisMBB:
7752 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007753 // ld t1 = [bitinstr.addr]
7754 // op t2 = t1, [bitinstr.val]
7755 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007756 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7757 // bz newMBB
7758 // fallthrough -->nextMBB
7759 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7760 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007761 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007762 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007763
Mon P Wang63307c32008-05-05 19:05:59 +00007764 /// First build the CFG
7765 MachineFunction *F = MBB->getParent();
7766 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007767 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7768 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7769 F->insert(MBBIter, newMBB);
7770 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007771
Mon P Wang63307c32008-05-05 19:05:59 +00007772 // Move all successors to thisMBB to nextMBB
7773 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007774
Mon P Wang63307c32008-05-05 19:05:59 +00007775 // Update thisMBB to fall through to newMBB
7776 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007777
Mon P Wang63307c32008-05-05 19:05:59 +00007778 // newMBB jumps to itself and fall through to nextMBB
7779 newMBB->addSuccessor(nextMBB);
7780 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007781
Mon P Wang63307c32008-05-05 19:05:59 +00007782 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007783 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007784 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007785 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007786 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007787 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007788 int numArgs = bInstr->getNumOperands() - 1;
7789 for (int i=0; i < numArgs; ++i)
7790 argOpers[i] = &bInstr->getOperand(i+1);
7791
7792 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007793 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7794 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007795
Dale Johannesen140be2d2008-08-19 18:47:28 +00007796 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007797 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007798 for (int i=0; i <= lastAddrIndx; ++i)
7799 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007800
Dale Johannesen140be2d2008-08-19 18:47:28 +00007801 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007802 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007803 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007804 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007805 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007806 tt = t1;
7807
Dale Johannesen140be2d2008-08-19 18:47:28 +00007808 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007809 assert((argOpers[valArgIndx]->isReg() ||
7810 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007811 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007812 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007813 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007814 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007815 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007816 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007817 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007818
Dale Johannesene4d209d2009-02-03 20:21:25 +00007819 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007820 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007821
Dale Johannesene4d209d2009-02-03 20:21:25 +00007822 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007823 for (int i=0; i <= lastAddrIndx; ++i)
7824 (*MIB).addOperand(*argOpers[i]);
7825 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007826 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007827 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7828 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007829
Dale Johannesene4d209d2009-02-03 20:21:25 +00007830 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007831 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007832
Mon P Wang63307c32008-05-05 19:05:59 +00007833 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007834 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007835
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007836 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007837 return nextMBB;
7838}
7839
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007840// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007841MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007842X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7843 MachineBasicBlock *MBB,
7844 unsigned regOpcL,
7845 unsigned regOpcH,
7846 unsigned immOpcL,
7847 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007848 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007849 // For the atomic bitwise operator, we generate
7850 // thisMBB (instructions are in pairs, except cmpxchg8b)
7851 // ld t1,t2 = [bitinstr.addr]
7852 // newMBB:
7853 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7854 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007855 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007856 // mov ECX, EBX <- t5, t6
7857 // mov EAX, EDX <- t1, t2
7858 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7859 // mov t3, t4 <- EAX, EDX
7860 // bz newMBB
7861 // result in out1, out2
7862 // fallthrough -->nextMBB
7863
7864 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7865 const unsigned LoadOpc = X86::MOV32rm;
7866 const unsigned copyOpc = X86::MOV32rr;
7867 const unsigned NotOpc = X86::NOT32r;
7868 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7869 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7870 MachineFunction::iterator MBBIter = MBB;
7871 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007872
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007873 /// First build the CFG
7874 MachineFunction *F = MBB->getParent();
7875 MachineBasicBlock *thisMBB = MBB;
7876 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7877 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7878 F->insert(MBBIter, newMBB);
7879 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007880
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007881 // Move all successors to thisMBB to nextMBB
7882 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007883
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007884 // Update thisMBB to fall through to newMBB
7885 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007886
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007887 // newMBB jumps to itself and fall through to nextMBB
7888 newMBB->addSuccessor(nextMBB);
7889 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007890
Dale Johannesene4d209d2009-02-03 20:21:25 +00007891 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007892 // Insert instructions into newMBB based on incoming instruction
7893 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007894 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007895 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007896 MachineOperand& dest1Oper = bInstr->getOperand(0);
7897 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007898 MachineOperand* argOpers[2 + X86AddrNumOperands];
7899 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007900 argOpers[i] = &bInstr->getOperand(i+2);
7901
Evan Chengad5b52f2010-01-08 19:14:57 +00007902 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007903 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007904
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007905 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007906 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007907 for (int i=0; i <= lastAddrIndx; ++i)
7908 (*MIB).addOperand(*argOpers[i]);
7909 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007910 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007911 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007912 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007913 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007914 MachineOperand newOp3 = *(argOpers[3]);
7915 if (newOp3.isImm())
7916 newOp3.setImm(newOp3.getImm()+4);
7917 else
7918 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007919 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007920 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007921
7922 // t3/4 are defined later, at the bottom of the loop
7923 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7924 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007925 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007926 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007927 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007928 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7929
Evan Cheng306b4ca2010-01-08 23:41:50 +00007930 // The subsequent operations should be using the destination registers of
7931 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007932 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007933 t1 = F->getRegInfo().createVirtualRegister(RC);
7934 t2 = F->getRegInfo().createVirtualRegister(RC);
7935 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7936 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007937 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007938 t1 = dest1Oper.getReg();
7939 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007940 }
7941
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007942 int valArgIndx = lastAddrIndx + 1;
7943 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007944 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007945 "invalid operand");
7946 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7947 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007948 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007949 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007950 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007951 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007952 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007953 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007954 (*MIB).addOperand(*argOpers[valArgIndx]);
7955 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007956 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007957 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007958 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007959 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007960 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007961 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007962 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007963 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007964 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007965 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007966
Dale Johannesene4d209d2009-02-03 20:21:25 +00007967 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007968 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007970 MIB.addReg(t2);
7971
Dale Johannesene4d209d2009-02-03 20:21:25 +00007972 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007973 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007974 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007975 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007976
Dale Johannesene4d209d2009-02-03 20:21:25 +00007977 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007978 for (int i=0; i <= lastAddrIndx; ++i)
7979 (*MIB).addOperand(*argOpers[i]);
7980
7981 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007982 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7983 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007984
Dale Johannesene4d209d2009-02-03 20:21:25 +00007985 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007986 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007987 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007988 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007989
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007990 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007991 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007992
7993 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7994 return nextMBB;
7995}
7996
7997// private utility function
7998MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007999X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8000 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008001 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008002 // For the atomic min/max operator, we generate
8003 // thisMBB:
8004 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008005 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008006 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008007 // cmp t1, t2
8008 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008009 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008010 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8011 // bz newMBB
8012 // fallthrough -->nextMBB
8013 //
8014 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8015 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008016 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008017 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008018
Mon P Wang63307c32008-05-05 19:05:59 +00008019 /// First build the CFG
8020 MachineFunction *F = MBB->getParent();
8021 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008022 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8023 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8024 F->insert(MBBIter, newMBB);
8025 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008026
Dan Gohmand6708ea2009-08-15 01:38:56 +00008027 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008028 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008029
Mon P Wang63307c32008-05-05 19:05:59 +00008030 // Update thisMBB to fall through to newMBB
8031 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008032
Mon P Wang63307c32008-05-05 19:05:59 +00008033 // newMBB jumps to newMBB and fall through to nextMBB
8034 newMBB->addSuccessor(nextMBB);
8035 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008036
Dale Johannesene4d209d2009-02-03 20:21:25 +00008037 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008038 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008039 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008040 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008041 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008042 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008043 int numArgs = mInstr->getNumOperands() - 1;
8044 for (int i=0; i < numArgs; ++i)
8045 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008046
Mon P Wang63307c32008-05-05 19:05:59 +00008047 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008048 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8049 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008050
Mon P Wangab3e7472008-05-05 22:56:23 +00008051 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008052 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008053 for (int i=0; i <= lastAddrIndx; ++i)
8054 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008055
Mon P Wang63307c32008-05-05 19:05:59 +00008056 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008057 assert((argOpers[valArgIndx]->isReg() ||
8058 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008059 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008060
8061 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008062 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008063 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008064 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008065 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008066 (*MIB).addOperand(*argOpers[valArgIndx]);
8067
Dale Johannesene4d209d2009-02-03 20:21:25 +00008068 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008069 MIB.addReg(t1);
8070
Dale Johannesene4d209d2009-02-03 20:21:25 +00008071 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008072 MIB.addReg(t1);
8073 MIB.addReg(t2);
8074
8075 // Generate movc
8076 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008077 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008078 MIB.addReg(t2);
8079 MIB.addReg(t1);
8080
8081 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008082 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008083 for (int i=0; i <= lastAddrIndx; ++i)
8084 (*MIB).addOperand(*argOpers[i]);
8085 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008086 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008087 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8088 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008089
Dale Johannesene4d209d2009-02-03 20:21:25 +00008090 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008091 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008092
Mon P Wang63307c32008-05-05 19:05:59 +00008093 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008095
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008096 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008097 return nextMBB;
8098}
8099
Eric Christopherf83a5de2009-08-27 18:08:16 +00008100// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8101// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008102MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008103X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008104 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008105
8106 MachineFunction *F = BB->getParent();
8107 DebugLoc dl = MI->getDebugLoc();
8108 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8109
8110 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008111 if (memArg)
8112 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8113 else
8114 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008115
8116 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8117
8118 for (unsigned i = 0; i < numArgs; ++i) {
8119 MachineOperand &Op = MI->getOperand(i+1);
8120
8121 if (!(Op.isReg() && Op.isImplicit()))
8122 MIB.addOperand(Op);
8123 }
8124
8125 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8126 .addReg(X86::XMM0);
8127
8128 F->DeleteMachineInstr(MI);
8129
8130 return BB;
8131}
8132
8133MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008134X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8135 MachineInstr *MI,
8136 MachineBasicBlock *MBB) const {
8137 // Emit code to save XMM registers to the stack. The ABI says that the
8138 // number of registers to save is given in %al, so it's theoretically
8139 // possible to do an indirect jump trick to avoid saving all of them,
8140 // however this code takes a simpler approach and just executes all
8141 // of the stores if %al is non-zero. It's less code, and it's probably
8142 // easier on the hardware branch predictor, and stores aren't all that
8143 // expensive anyway.
8144
8145 // Create the new basic blocks. One block contains all the XMM stores,
8146 // and one block is the final destination regardless of whether any
8147 // stores were performed.
8148 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8149 MachineFunction *F = MBB->getParent();
8150 MachineFunction::iterator MBBIter = MBB;
8151 ++MBBIter;
8152 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8153 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8154 F->insert(MBBIter, XMMSaveMBB);
8155 F->insert(MBBIter, EndMBB);
8156
8157 // Set up the CFG.
8158 // Move any original successors of MBB to the end block.
8159 EndMBB->transferSuccessors(MBB);
8160 // The original block will now fall through to the XMM save block.
8161 MBB->addSuccessor(XMMSaveMBB);
8162 // The XMMSaveMBB will fall through to the end block.
8163 XMMSaveMBB->addSuccessor(EndMBB);
8164
8165 // Now add the instructions.
8166 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8167 DebugLoc DL = MI->getDebugLoc();
8168
8169 unsigned CountReg = MI->getOperand(0).getReg();
8170 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8171 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8172
8173 if (!Subtarget->isTargetWin64()) {
8174 // If %al is 0, branch around the XMM save block.
8175 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8176 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8177 MBB->addSuccessor(EndMBB);
8178 }
8179
8180 // In the XMM save block, save all the XMM argument registers.
8181 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8182 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008183 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008184 F->getMachineMemOperand(
8185 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8186 MachineMemOperand::MOStore, Offset,
8187 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008188 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8189 .addFrameIndex(RegSaveFrameIndex)
8190 .addImm(/*Scale=*/1)
8191 .addReg(/*IndexReg=*/0)
8192 .addImm(/*Disp=*/Offset)
8193 .addReg(/*Segment=*/0)
8194 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008195 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008196 }
8197
8198 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8199
8200 return EndMBB;
8201}
Mon P Wang63307c32008-05-05 19:05:59 +00008202
Evan Cheng60c07e12006-07-05 22:17:51 +00008203MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008204X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008205 MachineBasicBlock *BB,
8206 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008207 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8208 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008209
Chris Lattner52600972009-09-02 05:57:00 +00008210 // To "insert" a SELECT_CC instruction, we actually have to insert the
8211 // diamond control-flow pattern. The incoming instruction knows the
8212 // destination vreg to set, the condition code register to branch on, the
8213 // true/false values to select between, and a branch opcode to use.
8214 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8215 MachineFunction::iterator It = BB;
8216 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008217
Chris Lattner52600972009-09-02 05:57:00 +00008218 // thisMBB:
8219 // ...
8220 // TrueVal = ...
8221 // cmpTY ccX, r1, r2
8222 // bCC copy1MBB
8223 // fallthrough --> copy0MBB
8224 MachineBasicBlock *thisMBB = BB;
8225 MachineFunction *F = BB->getParent();
8226 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8227 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8228 unsigned Opc =
8229 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8230 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8231 F->insert(It, copy0MBB);
8232 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008233 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008234 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008235 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008236 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008237 E = BB->succ_end(); I != E; ++I) {
8238 EM->insert(std::make_pair(*I, sinkMBB));
8239 sinkMBB->addSuccessor(*I);
8240 }
8241 // Next, remove all successors of the current block, and add the true
8242 // and fallthrough blocks as its successors.
8243 while (!BB->succ_empty())
8244 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008245 // Add the true and fallthrough blocks as its successors.
8246 BB->addSuccessor(copy0MBB);
8247 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008248
Chris Lattner52600972009-09-02 05:57:00 +00008249 // copy0MBB:
8250 // %FalseValue = ...
8251 // # fallthrough to sinkMBB
8252 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008253
Chris Lattner52600972009-09-02 05:57:00 +00008254 // Update machine-CFG edges
8255 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008256
Chris Lattner52600972009-09-02 05:57:00 +00008257 // sinkMBB:
8258 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8259 // ...
8260 BB = sinkMBB;
8261 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8262 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8263 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8264
8265 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8266 return BB;
8267}
8268
8269
8270MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008271X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008272 MachineBasicBlock *BB,
8273 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008274 switch (MI->getOpcode()) {
8275 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008276 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008277 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008278 case X86::CMOV_FR32:
8279 case X86::CMOV_FR64:
8280 case X86::CMOV_V4F32:
8281 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008282 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008283 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008284
Dale Johannesen849f2142007-07-03 00:53:03 +00008285 case X86::FP32_TO_INT16_IN_MEM:
8286 case X86::FP32_TO_INT32_IN_MEM:
8287 case X86::FP32_TO_INT64_IN_MEM:
8288 case X86::FP64_TO_INT16_IN_MEM:
8289 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008290 case X86::FP64_TO_INT64_IN_MEM:
8291 case X86::FP80_TO_INT16_IN_MEM:
8292 case X86::FP80_TO_INT32_IN_MEM:
8293 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008294 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8295 DebugLoc DL = MI->getDebugLoc();
8296
Evan Cheng60c07e12006-07-05 22:17:51 +00008297 // Change the floating point control register to use "round towards zero"
8298 // mode when truncating to an integer value.
8299 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008300 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008301 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008302
8303 // Load the old value of the high byte of the control word...
8304 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008305 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008306 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008307 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008308
8309 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008310 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008311 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008312
8313 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008314 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008315
8316 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008317 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008318 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008319
8320 // Get the X86 opcode to use.
8321 unsigned Opc;
8322 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008323 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008324 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8325 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8326 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8327 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8328 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8329 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008330 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8331 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8332 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008333 }
8334
8335 X86AddressMode AM;
8336 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008337 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008338 AM.BaseType = X86AddressMode::RegBase;
8339 AM.Base.Reg = Op.getReg();
8340 } else {
8341 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008342 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008343 }
8344 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008345 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008346 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008347 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008348 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008349 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008350 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008351 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008352 AM.GV = Op.getGlobal();
8353 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008354 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008355 }
Chris Lattner52600972009-09-02 05:57:00 +00008356 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008357 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008358
8359 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008360 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008361
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008362 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008363 return BB;
8364 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008365 // String/text processing lowering.
8366 case X86::PCMPISTRM128REG:
8367 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8368 case X86::PCMPISTRM128MEM:
8369 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8370 case X86::PCMPESTRM128REG:
8371 return EmitPCMP(MI, BB, 5, false /* in mem */);
8372 case X86::PCMPESTRM128MEM:
8373 return EmitPCMP(MI, BB, 5, true /* in mem */);
8374
8375 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008376 case X86::ATOMAND32:
8377 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008378 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008379 X86::LCMPXCHG32, X86::MOV32rr,
8380 X86::NOT32r, X86::EAX,
8381 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008382 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008383 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8384 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008385 X86::LCMPXCHG32, X86::MOV32rr,
8386 X86::NOT32r, X86::EAX,
8387 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008388 case X86::ATOMXOR32:
8389 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008390 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008391 X86::LCMPXCHG32, X86::MOV32rr,
8392 X86::NOT32r, X86::EAX,
8393 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008394 case X86::ATOMNAND32:
8395 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008396 X86::AND32ri, X86::MOV32rm,
8397 X86::LCMPXCHG32, X86::MOV32rr,
8398 X86::NOT32r, X86::EAX,
8399 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008400 case X86::ATOMMIN32:
8401 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8402 case X86::ATOMMAX32:
8403 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8404 case X86::ATOMUMIN32:
8405 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8406 case X86::ATOMUMAX32:
8407 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008408
8409 case X86::ATOMAND16:
8410 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8411 X86::AND16ri, X86::MOV16rm,
8412 X86::LCMPXCHG16, X86::MOV16rr,
8413 X86::NOT16r, X86::AX,
8414 X86::GR16RegisterClass);
8415 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008416 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008417 X86::OR16ri, X86::MOV16rm,
8418 X86::LCMPXCHG16, X86::MOV16rr,
8419 X86::NOT16r, X86::AX,
8420 X86::GR16RegisterClass);
8421 case X86::ATOMXOR16:
8422 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8423 X86::XOR16ri, X86::MOV16rm,
8424 X86::LCMPXCHG16, X86::MOV16rr,
8425 X86::NOT16r, X86::AX,
8426 X86::GR16RegisterClass);
8427 case X86::ATOMNAND16:
8428 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8429 X86::AND16ri, X86::MOV16rm,
8430 X86::LCMPXCHG16, X86::MOV16rr,
8431 X86::NOT16r, X86::AX,
8432 X86::GR16RegisterClass, true);
8433 case X86::ATOMMIN16:
8434 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8435 case X86::ATOMMAX16:
8436 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8437 case X86::ATOMUMIN16:
8438 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8439 case X86::ATOMUMAX16:
8440 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8441
8442 case X86::ATOMAND8:
8443 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8444 X86::AND8ri, X86::MOV8rm,
8445 X86::LCMPXCHG8, X86::MOV8rr,
8446 X86::NOT8r, X86::AL,
8447 X86::GR8RegisterClass);
8448 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008449 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008450 X86::OR8ri, X86::MOV8rm,
8451 X86::LCMPXCHG8, X86::MOV8rr,
8452 X86::NOT8r, X86::AL,
8453 X86::GR8RegisterClass);
8454 case X86::ATOMXOR8:
8455 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8456 X86::XOR8ri, X86::MOV8rm,
8457 X86::LCMPXCHG8, X86::MOV8rr,
8458 X86::NOT8r, X86::AL,
8459 X86::GR8RegisterClass);
8460 case X86::ATOMNAND8:
8461 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8462 X86::AND8ri, X86::MOV8rm,
8463 X86::LCMPXCHG8, X86::MOV8rr,
8464 X86::NOT8r, X86::AL,
8465 X86::GR8RegisterClass, true);
8466 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008467 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008468 case X86::ATOMAND64:
8469 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008470 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008471 X86::LCMPXCHG64, X86::MOV64rr,
8472 X86::NOT64r, X86::RAX,
8473 X86::GR64RegisterClass);
8474 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008475 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8476 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008477 X86::LCMPXCHG64, X86::MOV64rr,
8478 X86::NOT64r, X86::RAX,
8479 X86::GR64RegisterClass);
8480 case X86::ATOMXOR64:
8481 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008482 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008483 X86::LCMPXCHG64, X86::MOV64rr,
8484 X86::NOT64r, X86::RAX,
8485 X86::GR64RegisterClass);
8486 case X86::ATOMNAND64:
8487 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8488 X86::AND64ri32, X86::MOV64rm,
8489 X86::LCMPXCHG64, X86::MOV64rr,
8490 X86::NOT64r, X86::RAX,
8491 X86::GR64RegisterClass, true);
8492 case X86::ATOMMIN64:
8493 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8494 case X86::ATOMMAX64:
8495 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8496 case X86::ATOMUMIN64:
8497 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8498 case X86::ATOMUMAX64:
8499 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008500
8501 // This group does 64-bit operations on a 32-bit host.
8502 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008503 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008504 X86::AND32rr, X86::AND32rr,
8505 X86::AND32ri, X86::AND32ri,
8506 false);
8507 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008508 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008509 X86::OR32rr, X86::OR32rr,
8510 X86::OR32ri, X86::OR32ri,
8511 false);
8512 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008513 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008514 X86::XOR32rr, X86::XOR32rr,
8515 X86::XOR32ri, X86::XOR32ri,
8516 false);
8517 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008518 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008519 X86::AND32rr, X86::AND32rr,
8520 X86::AND32ri, X86::AND32ri,
8521 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008522 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008523 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008524 X86::ADD32rr, X86::ADC32rr,
8525 X86::ADD32ri, X86::ADC32ri,
8526 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008527 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008528 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008529 X86::SUB32rr, X86::SBB32rr,
8530 X86::SUB32ri, X86::SBB32ri,
8531 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008532 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008533 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008534 X86::MOV32rr, X86::MOV32rr,
8535 X86::MOV32ri, X86::MOV32ri,
8536 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008537 case X86::VASTART_SAVE_XMM_REGS:
8538 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008539 }
8540}
8541
8542//===----------------------------------------------------------------------===//
8543// X86 Optimization Hooks
8544//===----------------------------------------------------------------------===//
8545
Dan Gohman475871a2008-07-27 21:46:04 +00008546void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008547 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008548 APInt &KnownZero,
8549 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008550 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008551 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008552 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008553 assert((Opc >= ISD::BUILTIN_OP_END ||
8554 Opc == ISD::INTRINSIC_WO_CHAIN ||
8555 Opc == ISD::INTRINSIC_W_CHAIN ||
8556 Opc == ISD::INTRINSIC_VOID) &&
8557 "Should use MaskedValueIsZero if you don't know whether Op"
8558 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008559
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008560 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008561 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008562 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008563 case X86ISD::ADD:
8564 case X86ISD::SUB:
8565 case X86ISD::SMUL:
8566 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008567 case X86ISD::INC:
8568 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008569 case X86ISD::OR:
8570 case X86ISD::XOR:
8571 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008572 // These nodes' second result is a boolean.
8573 if (Op.getResNo() == 0)
8574 break;
8575 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008576 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008577 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8578 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008579 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008580 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008581}
Chris Lattner259e97c2006-01-31 19:43:35 +00008582
Evan Cheng206ee9d2006-07-07 08:33:52 +00008583/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008584/// node is a GlobalAddress + offset.
8585bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8586 GlobalValue* &GA, int64_t &Offset) const{
8587 if (N->getOpcode() == X86ISD::Wrapper) {
8588 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008589 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008590 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008591 return true;
8592 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008593 }
Evan Chengad4196b2008-05-12 19:56:52 +00008594 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008595}
8596
Nate Begeman9008ca62009-04-27 18:41:29 +00008597static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008598 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008599 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008600 SelectionDAG &DAG, MachineFrameInfo *MFI,
8601 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008602 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008603 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008604 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008605 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008606 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008607 return false;
8608 continue;
8609 }
8610
Dan Gohman475871a2008-07-27 21:46:04 +00008611 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008612 if (!Elt.getNode() ||
8613 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008614 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008615 if (!LDBase) {
8616 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008617 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008618 LDBase = cast<LoadSDNode>(Elt.getNode());
8619 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008620 continue;
8621 }
8622 if (Elt.getOpcode() == ISD::UNDEF)
8623 continue;
8624
Nate Begemanabc01992009-06-05 21:37:30 +00008625 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008626 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008627 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008628 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008629 }
8630 return true;
8631}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008632
8633/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8634/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8635/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008636/// order. In the case of v2i64, it will see if it can rewrite the
8637/// shuffle to be an appropriate build vector so it can take advantage of
8638// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008639static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008640 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008641 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008642 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008643 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008644 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8645 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008646
Eli Friedman7a5e5552009-06-07 06:52:44 +00008647 if (VT.getSizeInBits() != 128)
8648 return SDValue();
8649
Mon P Wang1e955802009-04-03 02:43:30 +00008650 // Try to combine a vector_shuffle into a 128-bit load.
8651 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008652 LoadSDNode *LD = NULL;
8653 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008654 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008655 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008656 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008657
Eli Friedman7a5e5552009-06-07 06:52:44 +00008658 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008659 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008660 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8661 LD->getSrcValue(), LD->getSrcValueOffset(),
8662 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008663 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008664 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008665 LD->isVolatile(), LD->getAlignment());
8666 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008667 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008668 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8669 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008670 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8671 }
8672 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008673}
Evan Chengd880b972008-05-09 21:53:03 +00008674
Chris Lattner83e6c992006-10-04 06:57:07 +00008675/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008676static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008677 const X86Subtarget *Subtarget) {
8678 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008679 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008680 // Get the LHS/RHS of the select.
8681 SDValue LHS = N->getOperand(1);
8682 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008683
Dan Gohman670e5392009-09-21 18:03:22 +00008684 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8685 // instructions have the peculiarity that if either operand is a NaN,
8686 // they chose what we call the RHS operand (and as such are not symmetric).
8687 // It happens that this matches the semantics of the common C idiom
8688 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008689 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008690 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008691 Cond.getOpcode() == ISD::SETCC) {
8692 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008693
Chris Lattner47b4ce82009-03-11 05:48:52 +00008694 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008695 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008696 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8697 switch (CC) {
8698 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008699 case ISD::SETULT:
8700 // This can be a min if we can prove that at least one of the operands
8701 // is not a nan.
8702 if (!FiniteOnlyFPMath()) {
8703 if (DAG.isKnownNeverNaN(RHS)) {
8704 // Put the potential NaN in the RHS so that SSE will preserve it.
8705 std::swap(LHS, RHS);
8706 } else if (!DAG.isKnownNeverNaN(LHS))
8707 break;
8708 }
8709 Opcode = X86ISD::FMIN;
8710 break;
8711 case ISD::SETOLE:
8712 // This can be a min if we can prove that at least one of the operands
8713 // is not a nan.
8714 if (!FiniteOnlyFPMath()) {
8715 if (DAG.isKnownNeverNaN(LHS)) {
8716 // Put the potential NaN in the RHS so that SSE will preserve it.
8717 std::swap(LHS, RHS);
8718 } else if (!DAG.isKnownNeverNaN(RHS))
8719 break;
8720 }
8721 Opcode = X86ISD::FMIN;
8722 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008723 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008724 // This can be a min, but if either operand is a NaN we need it to
8725 // preserve the original LHS.
8726 std::swap(LHS, RHS);
8727 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008728 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008729 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008730 Opcode = X86ISD::FMIN;
8731 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008732
Dan Gohman670e5392009-09-21 18:03:22 +00008733 case ISD::SETOGE:
8734 // This can be a max if we can prove that at least one of the operands
8735 // is not a nan.
8736 if (!FiniteOnlyFPMath()) {
8737 if (DAG.isKnownNeverNaN(LHS)) {
8738 // Put the potential NaN in the RHS so that SSE will preserve it.
8739 std::swap(LHS, RHS);
8740 } else if (!DAG.isKnownNeverNaN(RHS))
8741 break;
8742 }
8743 Opcode = X86ISD::FMAX;
8744 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008745 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008746 // This can be a max if we can prove that at least one of the operands
8747 // is not a nan.
8748 if (!FiniteOnlyFPMath()) {
8749 if (DAG.isKnownNeverNaN(RHS)) {
8750 // Put the potential NaN in the RHS so that SSE will preserve it.
8751 std::swap(LHS, RHS);
8752 } else if (!DAG.isKnownNeverNaN(LHS))
8753 break;
8754 }
8755 Opcode = X86ISD::FMAX;
8756 break;
8757 case ISD::SETUGE:
8758 // This can be a max, but if either operand is a NaN we need it to
8759 // preserve the original LHS.
8760 std::swap(LHS, RHS);
8761 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008762 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008763 case ISD::SETGE:
8764 Opcode = X86ISD::FMAX;
8765 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008766 }
Dan Gohman670e5392009-09-21 18:03:22 +00008767 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008768 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8769 switch (CC) {
8770 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008771 case ISD::SETOGE:
8772 // This can be a min if we can prove that at least one of the operands
8773 // is not a nan.
8774 if (!FiniteOnlyFPMath()) {
8775 if (DAG.isKnownNeverNaN(RHS)) {
8776 // Put the potential NaN in the RHS so that SSE will preserve it.
8777 std::swap(LHS, RHS);
8778 } else if (!DAG.isKnownNeverNaN(LHS))
8779 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008780 }
Dan Gohman670e5392009-09-21 18:03:22 +00008781 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008782 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008783 case ISD::SETUGT:
8784 // This can be a min if we can prove that at least one of the operands
8785 // is not a nan.
8786 if (!FiniteOnlyFPMath()) {
8787 if (DAG.isKnownNeverNaN(LHS)) {
8788 // Put the potential NaN in the RHS so that SSE will preserve it.
8789 std::swap(LHS, RHS);
8790 } else if (!DAG.isKnownNeverNaN(RHS))
8791 break;
8792 }
8793 Opcode = X86ISD::FMIN;
8794 break;
8795 case ISD::SETUGE:
8796 // This can be a min, but if either operand is a NaN we need it to
8797 // preserve the original LHS.
8798 std::swap(LHS, RHS);
8799 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008800 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008801 case ISD::SETGE:
8802 Opcode = X86ISD::FMIN;
8803 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008804
Dan Gohman670e5392009-09-21 18:03:22 +00008805 case ISD::SETULT:
8806 // This can be a max if we can prove that at least one of the operands
8807 // is not a nan.
8808 if (!FiniteOnlyFPMath()) {
8809 if (DAG.isKnownNeverNaN(LHS)) {
8810 // Put the potential NaN in the RHS so that SSE will preserve it.
8811 std::swap(LHS, RHS);
8812 } else if (!DAG.isKnownNeverNaN(RHS))
8813 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008814 }
Dan Gohman670e5392009-09-21 18:03:22 +00008815 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008816 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008817 case ISD::SETOLE:
8818 // This can be a max if we can prove that at least one of the operands
8819 // is not a nan.
8820 if (!FiniteOnlyFPMath()) {
8821 if (DAG.isKnownNeverNaN(RHS)) {
8822 // Put the potential NaN in the RHS so that SSE will preserve it.
8823 std::swap(LHS, RHS);
8824 } else if (!DAG.isKnownNeverNaN(LHS))
8825 break;
8826 }
8827 Opcode = X86ISD::FMAX;
8828 break;
8829 case ISD::SETULE:
8830 // This can be a max, but if either operand is a NaN we need it to
8831 // preserve the original LHS.
8832 std::swap(LHS, RHS);
8833 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008834 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008835 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008836 Opcode = X86ISD::FMAX;
8837 break;
8838 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008839 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008840
Chris Lattner47b4ce82009-03-11 05:48:52 +00008841 if (Opcode)
8842 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008843 }
Eric Christopherfd179292009-08-27 18:07:15 +00008844
Chris Lattnerd1980a52009-03-12 06:52:53 +00008845 // If this is a select between two integer constants, try to do some
8846 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008847 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8848 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008849 // Don't do this for crazy integer types.
8850 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8851 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008852 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008853 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008854
Chris Lattnercee56e72009-03-13 05:53:31 +00008855 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008856 // Efficiently invertible.
8857 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8858 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8859 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8860 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008861 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008862 }
Eric Christopherfd179292009-08-27 18:07:15 +00008863
Chris Lattnerd1980a52009-03-12 06:52:53 +00008864 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008865 if (FalseC->getAPIntValue() == 0 &&
8866 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008867 if (NeedsCondInvert) // Invert the condition if needed.
8868 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8869 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008870
Chris Lattnerd1980a52009-03-12 06:52:53 +00008871 // Zero extend the condition if needed.
8872 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008873
Chris Lattnercee56e72009-03-13 05:53:31 +00008874 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008875 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008876 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008877 }
Eric Christopherfd179292009-08-27 18:07:15 +00008878
Chris Lattner97a29a52009-03-13 05:22:11 +00008879 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008880 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008881 if (NeedsCondInvert) // Invert the condition if needed.
8882 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8883 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008884
Chris Lattner97a29a52009-03-13 05:22:11 +00008885 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008886 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8887 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008888 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008889 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008890 }
Eric Christopherfd179292009-08-27 18:07:15 +00008891
Chris Lattnercee56e72009-03-13 05:53:31 +00008892 // Optimize cases that will turn into an LEA instruction. This requires
8893 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008894 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008895 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008896 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008897
Chris Lattnercee56e72009-03-13 05:53:31 +00008898 bool isFastMultiplier = false;
8899 if (Diff < 10) {
8900 switch ((unsigned char)Diff) {
8901 default: break;
8902 case 1: // result = add base, cond
8903 case 2: // result = lea base( , cond*2)
8904 case 3: // result = lea base(cond, cond*2)
8905 case 4: // result = lea base( , cond*4)
8906 case 5: // result = lea base(cond, cond*4)
8907 case 8: // result = lea base( , cond*8)
8908 case 9: // result = lea base(cond, cond*8)
8909 isFastMultiplier = true;
8910 break;
8911 }
8912 }
Eric Christopherfd179292009-08-27 18:07:15 +00008913
Chris Lattnercee56e72009-03-13 05:53:31 +00008914 if (isFastMultiplier) {
8915 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8916 if (NeedsCondInvert) // Invert the condition if needed.
8917 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8918 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008919
Chris Lattnercee56e72009-03-13 05:53:31 +00008920 // Zero extend the condition if needed.
8921 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8922 Cond);
8923 // Scale the condition by the difference.
8924 if (Diff != 1)
8925 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8926 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008927
Chris Lattnercee56e72009-03-13 05:53:31 +00008928 // Add the base if non-zero.
8929 if (FalseC->getAPIntValue() != 0)
8930 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8931 SDValue(FalseC, 0));
8932 return Cond;
8933 }
Eric Christopherfd179292009-08-27 18:07:15 +00008934 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008935 }
8936 }
Eric Christopherfd179292009-08-27 18:07:15 +00008937
Dan Gohman475871a2008-07-27 21:46:04 +00008938 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008939}
8940
Chris Lattnerd1980a52009-03-12 06:52:53 +00008941/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8942static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8943 TargetLowering::DAGCombinerInfo &DCI) {
8944 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008945
Chris Lattnerd1980a52009-03-12 06:52:53 +00008946 // If the flag operand isn't dead, don't touch this CMOV.
8947 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8948 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008949
Chris Lattnerd1980a52009-03-12 06:52:53 +00008950 // If this is a select between two integer constants, try to do some
8951 // optimizations. Note that the operands are ordered the opposite of SELECT
8952 // operands.
8953 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8954 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8955 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8956 // larger than FalseC (the false value).
8957 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008958
Chris Lattnerd1980a52009-03-12 06:52:53 +00008959 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8960 CC = X86::GetOppositeBranchCondition(CC);
8961 std::swap(TrueC, FalseC);
8962 }
Eric Christopherfd179292009-08-27 18:07:15 +00008963
Chris Lattnerd1980a52009-03-12 06:52:53 +00008964 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008965 // This is efficient for any integer data type (including i8/i16) and
8966 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008967 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8968 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008969 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8970 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008971
Chris Lattnerd1980a52009-03-12 06:52:53 +00008972 // Zero extend the condition if needed.
8973 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008974
Chris Lattnerd1980a52009-03-12 06:52:53 +00008975 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8976 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008977 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008978 if (N->getNumValues() == 2) // Dead flag value?
8979 return DCI.CombineTo(N, Cond, SDValue());
8980 return Cond;
8981 }
Eric Christopherfd179292009-08-27 18:07:15 +00008982
Chris Lattnercee56e72009-03-13 05:53:31 +00008983 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8984 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008985 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8986 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008987 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8988 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008989
Chris Lattner97a29a52009-03-13 05:22:11 +00008990 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008991 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8992 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008993 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8994 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008995
Chris Lattner97a29a52009-03-13 05:22:11 +00008996 if (N->getNumValues() == 2) // Dead flag value?
8997 return DCI.CombineTo(N, Cond, SDValue());
8998 return Cond;
8999 }
Eric Christopherfd179292009-08-27 18:07:15 +00009000
Chris Lattnercee56e72009-03-13 05:53:31 +00009001 // Optimize cases that will turn into an LEA instruction. This requires
9002 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009003 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009004 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009005 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009006
Chris Lattnercee56e72009-03-13 05:53:31 +00009007 bool isFastMultiplier = false;
9008 if (Diff < 10) {
9009 switch ((unsigned char)Diff) {
9010 default: break;
9011 case 1: // result = add base, cond
9012 case 2: // result = lea base( , cond*2)
9013 case 3: // result = lea base(cond, cond*2)
9014 case 4: // result = lea base( , cond*4)
9015 case 5: // result = lea base(cond, cond*4)
9016 case 8: // result = lea base( , cond*8)
9017 case 9: // result = lea base(cond, cond*8)
9018 isFastMultiplier = true;
9019 break;
9020 }
9021 }
Eric Christopherfd179292009-08-27 18:07:15 +00009022
Chris Lattnercee56e72009-03-13 05:53:31 +00009023 if (isFastMultiplier) {
9024 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9025 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009026 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9027 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009028 // Zero extend the condition if needed.
9029 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9030 Cond);
9031 // Scale the condition by the difference.
9032 if (Diff != 1)
9033 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9034 DAG.getConstant(Diff, Cond.getValueType()));
9035
9036 // Add the base if non-zero.
9037 if (FalseC->getAPIntValue() != 0)
9038 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9039 SDValue(FalseC, 0));
9040 if (N->getNumValues() == 2) // Dead flag value?
9041 return DCI.CombineTo(N, Cond, SDValue());
9042 return Cond;
9043 }
Eric Christopherfd179292009-08-27 18:07:15 +00009044 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009045 }
9046 }
9047 return SDValue();
9048}
9049
9050
Evan Cheng0b0cd912009-03-28 05:57:29 +00009051/// PerformMulCombine - Optimize a single multiply with constant into two
9052/// in order to implement it with two cheaper instructions, e.g.
9053/// LEA + SHL, LEA + LEA.
9054static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9055 TargetLowering::DAGCombinerInfo &DCI) {
9056 if (DAG.getMachineFunction().
9057 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9058 return SDValue();
9059
9060 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9061 return SDValue();
9062
Owen Andersone50ed302009-08-10 22:56:29 +00009063 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009064 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009065 return SDValue();
9066
9067 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9068 if (!C)
9069 return SDValue();
9070 uint64_t MulAmt = C->getZExtValue();
9071 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9072 return SDValue();
9073
9074 uint64_t MulAmt1 = 0;
9075 uint64_t MulAmt2 = 0;
9076 if ((MulAmt % 9) == 0) {
9077 MulAmt1 = 9;
9078 MulAmt2 = MulAmt / 9;
9079 } else if ((MulAmt % 5) == 0) {
9080 MulAmt1 = 5;
9081 MulAmt2 = MulAmt / 5;
9082 } else if ((MulAmt % 3) == 0) {
9083 MulAmt1 = 3;
9084 MulAmt2 = MulAmt / 3;
9085 }
9086 if (MulAmt2 &&
9087 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9088 DebugLoc DL = N->getDebugLoc();
9089
9090 if (isPowerOf2_64(MulAmt2) &&
9091 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9092 // If second multiplifer is pow2, issue it first. We want the multiply by
9093 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9094 // is an add.
9095 std::swap(MulAmt1, MulAmt2);
9096
9097 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009098 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009099 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009101 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009102 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009103 DAG.getConstant(MulAmt1, VT));
9104
Eric Christopherfd179292009-08-27 18:07:15 +00009105 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009106 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009107 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009108 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009109 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009110 DAG.getConstant(MulAmt2, VT));
9111
9112 // Do not add new nodes to DAG combiner worklist.
9113 DCI.CombineTo(N, NewMul, false);
9114 }
9115 return SDValue();
9116}
9117
Evan Chengad9c0a32009-12-15 00:53:42 +00009118static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9119 SDValue N0 = N->getOperand(0);
9120 SDValue N1 = N->getOperand(1);
9121 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9122 EVT VT = N0.getValueType();
9123
9124 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9125 // since the result of setcc_c is all zero's or all ones.
9126 if (N1C && N0.getOpcode() == ISD::AND &&
9127 N0.getOperand(1).getOpcode() == ISD::Constant) {
9128 SDValue N00 = N0.getOperand(0);
9129 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9130 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9131 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9132 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9133 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9134 APInt ShAmt = N1C->getAPIntValue();
9135 Mask = Mask.shl(ShAmt);
9136 if (Mask != 0)
9137 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9138 N00, DAG.getConstant(Mask, VT));
9139 }
9140 }
9141
9142 return SDValue();
9143}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009144
Nate Begeman740ab032009-01-26 00:52:55 +00009145/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9146/// when possible.
9147static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9148 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009149 EVT VT = N->getValueType(0);
9150 if (!VT.isVector() && VT.isInteger() &&
9151 N->getOpcode() == ISD::SHL)
9152 return PerformSHLCombine(N, DAG);
9153
Nate Begeman740ab032009-01-26 00:52:55 +00009154 // On X86 with SSE2 support, we can transform this to a vector shift if
9155 // all elements are shifted by the same amount. We can't do this in legalize
9156 // because the a constant vector is typically transformed to a constant pool
9157 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009158 if (!Subtarget->hasSSE2())
9159 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009160
Owen Anderson825b72b2009-08-11 20:47:22 +00009161 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009162 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009163
Mon P Wang3becd092009-01-28 08:12:05 +00009164 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009165 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009166 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009167 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009168 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9169 unsigned NumElts = VT.getVectorNumElements();
9170 unsigned i = 0;
9171 for (; i != NumElts; ++i) {
9172 SDValue Arg = ShAmtOp.getOperand(i);
9173 if (Arg.getOpcode() == ISD::UNDEF) continue;
9174 BaseShAmt = Arg;
9175 break;
9176 }
9177 for (; i != NumElts; ++i) {
9178 SDValue Arg = ShAmtOp.getOperand(i);
9179 if (Arg.getOpcode() == ISD::UNDEF) continue;
9180 if (Arg != BaseShAmt) {
9181 return SDValue();
9182 }
9183 }
9184 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009185 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009186 SDValue InVec = ShAmtOp.getOperand(0);
9187 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9188 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9189 unsigned i = 0;
9190 for (; i != NumElts; ++i) {
9191 SDValue Arg = InVec.getOperand(i);
9192 if (Arg.getOpcode() == ISD::UNDEF) continue;
9193 BaseShAmt = Arg;
9194 break;
9195 }
9196 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9197 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9198 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9199 if (C->getZExtValue() == SplatIdx)
9200 BaseShAmt = InVec.getOperand(1);
9201 }
9202 }
9203 if (BaseShAmt.getNode() == 0)
9204 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9205 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009206 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009207 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009208
Mon P Wangefa42202009-09-03 19:56:25 +00009209 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009210 if (EltVT.bitsGT(MVT::i32))
9211 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9212 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009213 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009214
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009215 // The shift amount is identical so we can do a vector shift.
9216 SDValue ValOp = N->getOperand(0);
9217 switch (N->getOpcode()) {
9218 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009219 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009220 break;
9221 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009222 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009223 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009224 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009225 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009227 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009228 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009229 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009230 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009231 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009232 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009233 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009234 break;
9235 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009237 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009239 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009240 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009241 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009242 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009243 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009244 break;
9245 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009246 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009247 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009248 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009249 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009250 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009251 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009252 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009253 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009254 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009255 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009256 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009257 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009258 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009259 }
9260 return SDValue();
9261}
9262
Evan Cheng760d1942010-01-04 21:22:48 +00009263static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9264 const X86Subtarget *Subtarget) {
9265 EVT VT = N->getValueType(0);
9266 if (VT != MVT::i64 || !Subtarget->is64Bit())
9267 return SDValue();
9268
9269 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9270 SDValue N0 = N->getOperand(0);
9271 SDValue N1 = N->getOperand(1);
9272 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9273 std::swap(N0, N1);
9274 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9275 return SDValue();
9276
9277 SDValue ShAmt0 = N0.getOperand(1);
9278 if (ShAmt0.getValueType() != MVT::i8)
9279 return SDValue();
9280 SDValue ShAmt1 = N1.getOperand(1);
9281 if (ShAmt1.getValueType() != MVT::i8)
9282 return SDValue();
9283 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9284 ShAmt0 = ShAmt0.getOperand(0);
9285 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9286 ShAmt1 = ShAmt1.getOperand(0);
9287
9288 DebugLoc DL = N->getDebugLoc();
9289 unsigned Opc = X86ISD::SHLD;
9290 SDValue Op0 = N0.getOperand(0);
9291 SDValue Op1 = N1.getOperand(0);
9292 if (ShAmt0.getOpcode() == ISD::SUB) {
9293 Opc = X86ISD::SHRD;
9294 std::swap(Op0, Op1);
9295 std::swap(ShAmt0, ShAmt1);
9296 }
9297
9298 if (ShAmt1.getOpcode() == ISD::SUB) {
9299 SDValue Sum = ShAmt1.getOperand(0);
9300 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9301 if (SumC->getSExtValue() == 64 &&
9302 ShAmt1.getOperand(1) == ShAmt0)
9303 return DAG.getNode(Opc, DL, VT,
9304 Op0, Op1,
9305 DAG.getNode(ISD::TRUNCATE, DL,
9306 MVT::i8, ShAmt0));
9307 }
9308 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9309 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9310 if (ShAmt0C &&
9311 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9312 return DAG.getNode(Opc, DL, VT,
9313 N0.getOperand(0), N1.getOperand(0),
9314 DAG.getNode(ISD::TRUNCATE, DL,
9315 MVT::i8, ShAmt0));
9316 }
9317
9318 return SDValue();
9319}
9320
Chris Lattner149a4e52008-02-22 02:09:43 +00009321/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009322static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009323 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009324 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9325 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009326 // A preferable solution to the general problem is to figure out the right
9327 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009328
9329 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009330 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009331 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009332 if (VT.getSizeInBits() != 64)
9333 return SDValue();
9334
Devang Patel578efa92009-06-05 21:57:13 +00009335 const Function *F = DAG.getMachineFunction().getFunction();
9336 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009337 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009338 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009339 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009340 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009341 isa<LoadSDNode>(St->getValue()) &&
9342 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9343 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009344 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009345 LoadSDNode *Ld = 0;
9346 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009347 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009348 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009349 // Must be a store of a load. We currently handle two cases: the load
9350 // is a direct child, and it's under an intervening TokenFactor. It is
9351 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009352 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009353 Ld = cast<LoadSDNode>(St->getChain());
9354 else if (St->getValue().hasOneUse() &&
9355 ChainVal->getOpcode() == ISD::TokenFactor) {
9356 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009357 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009358 TokenFactorIndex = i;
9359 Ld = cast<LoadSDNode>(St->getValue());
9360 } else
9361 Ops.push_back(ChainVal->getOperand(i));
9362 }
9363 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009364
Evan Cheng536e6672009-03-12 05:59:15 +00009365 if (!Ld || !ISD::isNormalLoad(Ld))
9366 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009367
Evan Cheng536e6672009-03-12 05:59:15 +00009368 // If this is not the MMX case, i.e. we are just turning i64 load/store
9369 // into f64 load/store, avoid the transformation if there are multiple
9370 // uses of the loaded value.
9371 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9372 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009373
Evan Cheng536e6672009-03-12 05:59:15 +00009374 DebugLoc LdDL = Ld->getDebugLoc();
9375 DebugLoc StDL = N->getDebugLoc();
9376 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9377 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9378 // pair instead.
9379 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009380 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009381 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9382 Ld->getBasePtr(), Ld->getSrcValue(),
9383 Ld->getSrcValueOffset(), Ld->isVolatile(),
9384 Ld->getAlignment());
9385 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009386 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009387 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009389 Ops.size());
9390 }
Evan Cheng536e6672009-03-12 05:59:15 +00009391 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009392 St->getSrcValue(), St->getSrcValueOffset(),
9393 St->isVolatile(), St->getAlignment());
9394 }
Evan Cheng536e6672009-03-12 05:59:15 +00009395
9396 // Otherwise, lower to two pairs of 32-bit loads / stores.
9397 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009398 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9399 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009400
Owen Anderson825b72b2009-08-11 20:47:22 +00009401 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009402 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9403 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009405 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9406 Ld->isVolatile(),
9407 MinAlign(Ld->getAlignment(), 4));
9408
9409 SDValue NewChain = LoLd.getValue(1);
9410 if (TokenFactorIndex != -1) {
9411 Ops.push_back(LoLd);
9412 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009414 Ops.size());
9415 }
9416
9417 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009418 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9419 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009420
9421 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9422 St->getSrcValue(), St->getSrcValueOffset(),
9423 St->isVolatile(), St->getAlignment());
9424 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9425 St->getSrcValue(),
9426 St->getSrcValueOffset() + 4,
9427 St->isVolatile(),
9428 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009429 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009430 }
Dan Gohman475871a2008-07-27 21:46:04 +00009431 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009432}
9433
Chris Lattner6cf73262008-01-25 06:14:17 +00009434/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9435/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009436static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009437 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9438 // F[X]OR(0.0, x) -> x
9439 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009440 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9441 if (C->getValueAPF().isPosZero())
9442 return N->getOperand(1);
9443 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9444 if (C->getValueAPF().isPosZero())
9445 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009446 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009447}
9448
9449/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009450static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009451 // FAND(0.0, x) -> 0.0
9452 // FAND(x, 0.0) -> 0.0
9453 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9454 if (C->getValueAPF().isPosZero())
9455 return N->getOperand(0);
9456 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9457 if (C->getValueAPF().isPosZero())
9458 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009459 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009460}
9461
Dan Gohmane5af2d32009-01-29 01:59:02 +00009462static SDValue PerformBTCombine(SDNode *N,
9463 SelectionDAG &DAG,
9464 TargetLowering::DAGCombinerInfo &DCI) {
9465 // BT ignores high bits in the bit index operand.
9466 SDValue Op1 = N->getOperand(1);
9467 if (Op1.hasOneUse()) {
9468 unsigned BitWidth = Op1.getValueSizeInBits();
9469 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9470 APInt KnownZero, KnownOne;
9471 TargetLowering::TargetLoweringOpt TLO(DAG);
9472 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9473 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9474 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9475 DCI.CommitTargetLoweringOpt(TLO);
9476 }
9477 return SDValue();
9478}
Chris Lattner83e6c992006-10-04 06:57:07 +00009479
Eli Friedman7a5e5552009-06-07 06:52:44 +00009480static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9481 SDValue Op = N->getOperand(0);
9482 if (Op.getOpcode() == ISD::BIT_CONVERT)
9483 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009484 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009485 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009486 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009487 OpVT.getVectorElementType().getSizeInBits()) {
9488 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9489 }
9490 return SDValue();
9491}
9492
Owen Anderson99177002009-06-29 18:04:45 +00009493// On X86 and X86-64, atomic operations are lowered to locked instructions.
9494// Locked instructions, in turn, have implicit fence semantics (all memory
9495// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009496// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009497// fence-atomic-fence.
9498static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9499 SDValue atomic = N->getOperand(0);
9500 switch (atomic.getOpcode()) {
9501 case ISD::ATOMIC_CMP_SWAP:
9502 case ISD::ATOMIC_SWAP:
9503 case ISD::ATOMIC_LOAD_ADD:
9504 case ISD::ATOMIC_LOAD_SUB:
9505 case ISD::ATOMIC_LOAD_AND:
9506 case ISD::ATOMIC_LOAD_OR:
9507 case ISD::ATOMIC_LOAD_XOR:
9508 case ISD::ATOMIC_LOAD_NAND:
9509 case ISD::ATOMIC_LOAD_MIN:
9510 case ISD::ATOMIC_LOAD_MAX:
9511 case ISD::ATOMIC_LOAD_UMIN:
9512 case ISD::ATOMIC_LOAD_UMAX:
9513 break;
9514 default:
9515 return SDValue();
9516 }
Eric Christopherfd179292009-08-27 18:07:15 +00009517
Owen Anderson99177002009-06-29 18:04:45 +00009518 SDValue fence = atomic.getOperand(0);
9519 if (fence.getOpcode() != ISD::MEMBARRIER)
9520 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009521
Owen Anderson99177002009-06-29 18:04:45 +00009522 switch (atomic.getOpcode()) {
9523 case ISD::ATOMIC_CMP_SWAP:
9524 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9525 atomic.getOperand(1), atomic.getOperand(2),
9526 atomic.getOperand(3));
9527 case ISD::ATOMIC_SWAP:
9528 case ISD::ATOMIC_LOAD_ADD:
9529 case ISD::ATOMIC_LOAD_SUB:
9530 case ISD::ATOMIC_LOAD_AND:
9531 case ISD::ATOMIC_LOAD_OR:
9532 case ISD::ATOMIC_LOAD_XOR:
9533 case ISD::ATOMIC_LOAD_NAND:
9534 case ISD::ATOMIC_LOAD_MIN:
9535 case ISD::ATOMIC_LOAD_MAX:
9536 case ISD::ATOMIC_LOAD_UMIN:
9537 case ISD::ATOMIC_LOAD_UMAX:
9538 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9539 atomic.getOperand(1), atomic.getOperand(2));
9540 default:
9541 return SDValue();
9542 }
9543}
9544
Evan Cheng2e489c42009-12-16 00:53:11 +00009545static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9546 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9547 // (and (i32 x86isd::setcc_carry), 1)
9548 // This eliminates the zext. This transformation is necessary because
9549 // ISD::SETCC is always legalized to i8.
9550 DebugLoc dl = N->getDebugLoc();
9551 SDValue N0 = N->getOperand(0);
9552 EVT VT = N->getValueType(0);
9553 if (N0.getOpcode() == ISD::AND &&
9554 N0.hasOneUse() &&
9555 N0.getOperand(0).hasOneUse()) {
9556 SDValue N00 = N0.getOperand(0);
9557 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9558 return SDValue();
9559 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9560 if (!C || C->getZExtValue() != 1)
9561 return SDValue();
9562 return DAG.getNode(ISD::AND, dl, VT,
9563 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9564 N00.getOperand(0), N00.getOperand(1)),
9565 DAG.getConstant(1, VT));
9566 }
9567
9568 return SDValue();
9569}
9570
Dan Gohman475871a2008-07-27 21:46:04 +00009571SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009572 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009573 SelectionDAG &DAG = DCI.DAG;
9574 switch (N->getOpcode()) {
9575 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009576 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009577 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009578 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009579 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009580 case ISD::SHL:
9581 case ISD::SRA:
9582 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009583 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009584 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009585 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009586 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9587 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009588 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009589 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009590 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009591 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009592 }
9593
Dan Gohman475871a2008-07-27 21:46:04 +00009594 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009595}
9596
Evan Cheng60c07e12006-07-05 22:17:51 +00009597//===----------------------------------------------------------------------===//
9598// X86 Inline Assembly Support
9599//===----------------------------------------------------------------------===//
9600
Chris Lattnerb8105652009-07-20 17:51:36 +00009601static bool LowerToBSwap(CallInst *CI) {
9602 // FIXME: this should verify that we are targetting a 486 or better. If not,
9603 // we will turn this bswap into something that will be lowered to logical ops
9604 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9605 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009606
Chris Lattnerb8105652009-07-20 17:51:36 +00009607 // Verify this is a simple bswap.
9608 if (CI->getNumOperands() != 2 ||
9609 CI->getType() != CI->getOperand(1)->getType() ||
9610 !CI->getType()->isInteger())
9611 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009612
Chris Lattnerb8105652009-07-20 17:51:36 +00009613 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9614 if (!Ty || Ty->getBitWidth() % 16 != 0)
9615 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009616
Chris Lattnerb8105652009-07-20 17:51:36 +00009617 // Okay, we can do this xform, do so now.
9618 const Type *Tys[] = { Ty };
9619 Module *M = CI->getParent()->getParent()->getParent();
9620 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009621
Chris Lattnerb8105652009-07-20 17:51:36 +00009622 Value *Op = CI->getOperand(1);
9623 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009624
Chris Lattnerb8105652009-07-20 17:51:36 +00009625 CI->replaceAllUsesWith(Op);
9626 CI->eraseFromParent();
9627 return true;
9628}
9629
9630bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9631 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9632 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9633
9634 std::string AsmStr = IA->getAsmString();
9635
9636 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009637 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009638 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9639
9640 switch (AsmPieces.size()) {
9641 default: return false;
9642 case 1:
9643 AsmStr = AsmPieces[0];
9644 AsmPieces.clear();
9645 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9646
9647 // bswap $0
9648 if (AsmPieces.size() == 2 &&
9649 (AsmPieces[0] == "bswap" ||
9650 AsmPieces[0] == "bswapq" ||
9651 AsmPieces[0] == "bswapl") &&
9652 (AsmPieces[1] == "$0" ||
9653 AsmPieces[1] == "${0:q}")) {
9654 // No need to check constraints, nothing other than the equivalent of
9655 // "=r,0" would be valid here.
9656 return LowerToBSwap(CI);
9657 }
9658 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009659 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009660 AsmPieces.size() == 3 &&
9661 AsmPieces[0] == "rorw" &&
9662 AsmPieces[1] == "$$8," &&
9663 AsmPieces[2] == "${0:w}" &&
9664 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9665 return LowerToBSwap(CI);
9666 }
9667 break;
9668 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009669 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009670 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009671 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9672 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9673 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009674 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009675 SplitString(AsmPieces[0], Words, " \t");
9676 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9677 Words.clear();
9678 SplitString(AsmPieces[1], Words, " \t");
9679 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9680 Words.clear();
9681 SplitString(AsmPieces[2], Words, " \t,");
9682 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9683 Words[2] == "%edx") {
9684 return LowerToBSwap(CI);
9685 }
9686 }
9687 }
9688 }
9689 break;
9690 }
9691 return false;
9692}
9693
9694
9695
Chris Lattnerf4dff842006-07-11 02:54:03 +00009696/// getConstraintType - Given a constraint letter, return the type of
9697/// constraint it is for this target.
9698X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009699X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9700 if (Constraint.size() == 1) {
9701 switch (Constraint[0]) {
9702 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009703 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009704 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009705 case 'r':
9706 case 'R':
9707 case 'l':
9708 case 'q':
9709 case 'Q':
9710 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009711 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009712 case 'Y':
9713 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009714 case 'e':
9715 case 'Z':
9716 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009717 default:
9718 break;
9719 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009720 }
Chris Lattner4234f572007-03-25 02:14:49 +00009721 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009722}
9723
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009724/// LowerXConstraint - try to replace an X constraint, which matches anything,
9725/// with another that has more specific requirements based on the type of the
9726/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009727const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009728LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009729 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9730 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009731 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009732 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009733 return "Y";
9734 if (Subtarget->hasSSE1())
9735 return "x";
9736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009737
Chris Lattner5e764232008-04-26 23:02:14 +00009738 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009739}
9740
Chris Lattner48884cd2007-08-25 00:47:38 +00009741/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9742/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009743void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009744 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009745 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009746 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009747 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009748 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009749
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009750 switch (Constraint) {
9751 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009752 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009754 if (C->getZExtValue() <= 31) {
9755 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009756 break;
9757 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009758 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009759 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009760 case 'J':
9761 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009762 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009763 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9764 break;
9765 }
9766 }
9767 return;
9768 case 'K':
9769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009770 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009771 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9772 break;
9773 }
9774 }
9775 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009776 case 'N':
9777 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009778 if (C->getZExtValue() <= 255) {
9779 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009780 break;
9781 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009782 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009783 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009784 case 'e': {
9785 // 32-bit signed value
9786 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9787 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009788 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9789 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009790 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009791 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009792 break;
9793 }
9794 // FIXME gcc accepts some relocatable values here too, but only in certain
9795 // memory models; it's complicated.
9796 }
9797 return;
9798 }
9799 case 'Z': {
9800 // 32-bit unsigned value
9801 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9802 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009803 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9804 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009805 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9806 break;
9807 }
9808 }
9809 // FIXME gcc accepts some relocatable values here too, but only in certain
9810 // memory models; it's complicated.
9811 return;
9812 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009813 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009814 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009815 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009816 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009817 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009818 break;
9819 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009820
Chris Lattnerdc43a882007-05-03 16:52:29 +00009821 // If we are in non-pic codegen mode, we allow the address of a global (with
9822 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009823 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009824 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009825
Chris Lattner49921962009-05-08 18:23:14 +00009826 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9827 while (1) {
9828 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9829 Offset += GA->getOffset();
9830 break;
9831 } else if (Op.getOpcode() == ISD::ADD) {
9832 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9833 Offset += C->getZExtValue();
9834 Op = Op.getOperand(0);
9835 continue;
9836 }
9837 } else if (Op.getOpcode() == ISD::SUB) {
9838 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9839 Offset += -C->getZExtValue();
9840 Op = Op.getOperand(0);
9841 continue;
9842 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009843 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009844
Chris Lattner49921962009-05-08 18:23:14 +00009845 // Otherwise, this isn't something we can handle, reject it.
9846 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009847 }
Eric Christopherfd179292009-08-27 18:07:15 +00009848
Chris Lattner36c25012009-07-10 07:34:39 +00009849 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009850 // If we require an extra load to get this address, as in PIC mode, we
9851 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009852 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9853 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009854 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009855
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009856 if (hasMemory)
9857 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9858 else
9859 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009860 Result = Op;
9861 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009862 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009863 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009864
Gabor Greifba36cb52008-08-28 21:40:38 +00009865 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009866 Ops.push_back(Result);
9867 return;
9868 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009869 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9870 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009871}
9872
Chris Lattner259e97c2006-01-31 19:43:35 +00009873std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009874getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009875 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009876 if (Constraint.size() == 1) {
9877 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009878 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009879 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009880 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9881 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009882 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009883 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9884 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9885 X86::R10D,X86::R11D,X86::R12D,
9886 X86::R13D,X86::R14D,X86::R15D,
9887 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009888 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009889 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9890 X86::SI, X86::DI, X86::R8W,X86::R9W,
9891 X86::R10W,X86::R11W,X86::R12W,
9892 X86::R13W,X86::R14W,X86::R15W,
9893 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009894 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009895 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9896 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9897 X86::R10B,X86::R11B,X86::R12B,
9898 X86::R13B,X86::R14B,X86::R15B,
9899 X86::BPL, X86::SPL, 0);
9900
Owen Anderson825b72b2009-08-11 20:47:22 +00009901 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009902 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9903 X86::RSI, X86::RDI, X86::R8, X86::R9,
9904 X86::R10, X86::R11, X86::R12,
9905 X86::R13, X86::R14, X86::R15,
9906 X86::RBP, X86::RSP, 0);
9907
9908 break;
9909 }
Eric Christopherfd179292009-08-27 18:07:15 +00009910 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009911 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009912 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009913 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009914 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009915 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009916 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009917 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009918 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009919 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9920 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009921 }
9922 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009923
Chris Lattner1efa40f2006-02-22 00:56:39 +00009924 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009925}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009926
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009927std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009928X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009929 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009930 // First, see if this is a constraint that directly corresponds to an LLVM
9931 // register class.
9932 if (Constraint.size() == 1) {
9933 // GCC Constraint Letters
9934 switch (Constraint[0]) {
9935 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009936 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009937 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009938 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009939 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009941 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009942 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009943 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009944 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009945 case 'R': // LEGACY_REGS
9946 if (VT == MVT::i8)
9947 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9948 if (VT == MVT::i16)
9949 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9950 if (VT == MVT::i32 || !Subtarget->is64Bit())
9951 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9952 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009953 case 'f': // FP Stack registers.
9954 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9955 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009956 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009957 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009958 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009959 return std::make_pair(0U, X86::RFP64RegisterClass);
9960 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009961 case 'y': // MMX_REGS if MMX allowed.
9962 if (!Subtarget->hasMMX()) break;
9963 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009964 case 'Y': // SSE_REGS if SSE2 allowed
9965 if (!Subtarget->hasSSE2()) break;
9966 // FALL THROUGH.
9967 case 'x': // SSE_REGS if SSE1 allowed
9968 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009969
Owen Anderson825b72b2009-08-11 20:47:22 +00009970 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009971 default: break;
9972 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009973 case MVT::f32:
9974 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009975 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009976 case MVT::f64:
9977 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009978 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009979 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 case MVT::v16i8:
9981 case MVT::v8i16:
9982 case MVT::v4i32:
9983 case MVT::v2i64:
9984 case MVT::v4f32:
9985 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009986 return std::make_pair(0U, X86::VR128RegisterClass);
9987 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009988 break;
9989 }
9990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009991
Chris Lattnerf76d1802006-07-31 23:26:50 +00009992 // Use the default implementation in TargetLowering to convert the register
9993 // constraint into a member of a register class.
9994 std::pair<unsigned, const TargetRegisterClass*> Res;
9995 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009996
9997 // Not found as a standard register?
9998 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009999 // Map st(0) -> st(7) -> ST0
10000 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10001 tolower(Constraint[1]) == 's' &&
10002 tolower(Constraint[2]) == 't' &&
10003 Constraint[3] == '(' &&
10004 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10005 Constraint[5] == ')' &&
10006 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010007
Chris Lattner56d77c72009-09-13 22:41:48 +000010008 Res.first = X86::ST0+Constraint[4]-'0';
10009 Res.second = X86::RFP80RegisterClass;
10010 return Res;
10011 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010012
Chris Lattner56d77c72009-09-13 22:41:48 +000010013 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010014 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010015 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010016 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010017 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010018 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010019
10020 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010021 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010022 Res.first = X86::EFLAGS;
10023 Res.second = X86::CCRRegisterClass;
10024 return Res;
10025 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010026
Dale Johannesen330169f2008-11-13 21:52:36 +000010027 // 'A' means EAX + EDX.
10028 if (Constraint == "A") {
10029 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010030 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010031 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010032 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010033 return Res;
10034 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010035
Chris Lattnerf76d1802006-07-31 23:26:50 +000010036 // Otherwise, check to see if this is a register class of the wrong value
10037 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10038 // turn into {ax},{dx}.
10039 if (Res.second->hasType(VT))
10040 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010041
Chris Lattnerf76d1802006-07-31 23:26:50 +000010042 // All of the single-register GCC register classes map their values onto
10043 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10044 // really want an 8-bit or 32-bit register, map to the appropriate register
10045 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010046 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010047 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010048 unsigned DestReg = 0;
10049 switch (Res.first) {
10050 default: break;
10051 case X86::AX: DestReg = X86::AL; break;
10052 case X86::DX: DestReg = X86::DL; break;
10053 case X86::CX: DestReg = X86::CL; break;
10054 case X86::BX: DestReg = X86::BL; break;
10055 }
10056 if (DestReg) {
10057 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010058 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010059 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010060 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010061 unsigned DestReg = 0;
10062 switch (Res.first) {
10063 default: break;
10064 case X86::AX: DestReg = X86::EAX; break;
10065 case X86::DX: DestReg = X86::EDX; break;
10066 case X86::CX: DestReg = X86::ECX; break;
10067 case X86::BX: DestReg = X86::EBX; break;
10068 case X86::SI: DestReg = X86::ESI; break;
10069 case X86::DI: DestReg = X86::EDI; break;
10070 case X86::BP: DestReg = X86::EBP; break;
10071 case X86::SP: DestReg = X86::ESP; break;
10072 }
10073 if (DestReg) {
10074 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010075 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010076 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010077 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010078 unsigned DestReg = 0;
10079 switch (Res.first) {
10080 default: break;
10081 case X86::AX: DestReg = X86::RAX; break;
10082 case X86::DX: DestReg = X86::RDX; break;
10083 case X86::CX: DestReg = X86::RCX; break;
10084 case X86::BX: DestReg = X86::RBX; break;
10085 case X86::SI: DestReg = X86::RSI; break;
10086 case X86::DI: DestReg = X86::RDI; break;
10087 case X86::BP: DestReg = X86::RBP; break;
10088 case X86::SP: DestReg = X86::RSP; break;
10089 }
10090 if (DestReg) {
10091 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010092 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010093 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010094 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010095 } else if (Res.second == X86::FR32RegisterClass ||
10096 Res.second == X86::FR64RegisterClass ||
10097 Res.second == X86::VR128RegisterClass) {
10098 // Handle references to XMM physical registers that got mapped into the
10099 // wrong class. This can happen with constraints like {xmm0} where the
10100 // target independent register mapper will just pick the first match it can
10101 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010102 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010103 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010104 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010105 Res.second = X86::FR64RegisterClass;
10106 else if (X86::VR128RegisterClass->hasType(VT))
10107 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010108 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010109
Chris Lattnerf76d1802006-07-31 23:26:50 +000010110 return Res;
10111}
Mon P Wang0c397192008-10-30 08:01:45 +000010112
10113//===----------------------------------------------------------------------===//
10114// X86 Widen vector type
10115//===----------------------------------------------------------------------===//
10116
10117/// getWidenVectorType: given a vector type, returns the type to widen
10118/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010119/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010120/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010121/// scalarizing vs using the wider vector type.
10122
Owen Andersone50ed302009-08-10 22:56:29 +000010123EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010124 assert(VT.isVector());
10125 if (isTypeLegal(VT))
10126 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010127
Mon P Wang0c397192008-10-30 08:01:45 +000010128 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10129 // type based on element type. This would speed up our search (though
10130 // it may not be worth it since the size of the list is relatively
10131 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010132 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010133 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010134
Mon P Wang0c397192008-10-30 08:01:45 +000010135 // On X86, it make sense to widen any vector wider than 1
10136 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010137 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010138
Owen Anderson825b72b2009-08-11 20:47:22 +000010139 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10140 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10141 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010142
10143 if (isTypeLegal(SVT) &&
10144 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010145 SVT.getVectorNumElements() > NElts)
10146 return SVT;
10147 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010148 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010149}