blob: efe66d1981eea6940d84042885c97274bea0fadf [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) {
1447
Rafael Espindola7effac52007-09-14 15:48:13 +00001448 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Chengddc419c2010-01-26 19:04:47 +00001450 bool AlwaysUseMutable = X86::IsEligibleForTailCallOpt(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001451 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001452 EVT ValVT;
1453
1454 // If value is passed by pointer we have address passed instead of the value
1455 // itself.
1456 if (VA.getLocInfo() == CCValAssign::Indirect)
1457 ValVT = VA.getLocVT();
1458 else
1459 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001460
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001461 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001462 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001463 // In case of tail call optimization mark all arguments mutable. Since they
1464 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001465 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001466 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001467 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001468 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001469 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001470 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001471 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001472}
1473
Dan Gohman475871a2008-07-27 21:46:04 +00001474SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001476 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477 bool isVarArg,
1478 const SmallVectorImpl<ISD::InputArg> &Ins,
1479 DebugLoc dl,
1480 SelectionDAG &DAG,
1481 SmallVectorImpl<SDValue> &InVals) {
1482
Evan Cheng1bc78042006-04-26 01:20:17 +00001483 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001484 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001485
Gordon Henriksen86737662008-01-05 16:56:59 +00001486 const Function* Fn = MF.getFunction();
1487 if (Fn->hasExternalLinkage() &&
1488 Subtarget->isTargetCygMing() &&
1489 Fn->getName() == "main")
1490 FuncInfo->setForceFramePointer(true);
1491
1492 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001493 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001494
Evan Cheng1bc78042006-04-26 01:20:17 +00001495 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001496 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001497 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001498
Dan Gohman98ca4f22009-08-05 01:29:28 +00001499 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001500 "Var args not supported with calling convention fastcc");
1501
Chris Lattner638402b2007-02-28 07:00:42 +00001502 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001503 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1505 ArgLocs, *DAG.getContext());
1506 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001507
Chris Lattnerf39f7712007-02-28 05:46:49 +00001508 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001509 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001510 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1511 CCValAssign &VA = ArgLocs[i];
1512 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1513 // places.
1514 assert(VA.getValNo() != LastVal &&
1515 "Don't support value assigned to multiple locs yet");
1516 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001517
Chris Lattnerf39f7712007-02-28 05:46:49 +00001518 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001519 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001520 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001522 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001524 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001526 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001529 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001530 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001531 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1532 RC = X86::VR64RegisterClass;
1533 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001534 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001535
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001536 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001537 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001538
Chris Lattnerf39f7712007-02-28 05:46:49 +00001539 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1540 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1541 // right size.
1542 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001543 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 DAG.getValueType(VA.getValVT()));
1545 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001546 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001547 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001548 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001549 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001551 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001552 // Handle MMX values passed in XMM regs.
1553 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1555 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001556 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1557 } else
1558 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001559 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001560 } else {
1561 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001562 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001563 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001564
1565 // If value is passed via pointer - do a load.
1566 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001568
Dan Gohman98ca4f22009-08-05 01:29:28 +00001569 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001570 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001571
Dan Gohman61a92132008-04-21 23:59:07 +00001572 // The x86-64 ABI for returning structs by value requires that we copy
1573 // the sret argument into %rax for the return. Save the argument into
1574 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001575 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001576 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1577 unsigned Reg = FuncInfo->getSRetReturnReg();
1578 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001580 FuncInfo->setSRetReturnReg(Reg);
1581 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001582 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001583 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001584 }
1585
Chris Lattnerf39f7712007-02-28 05:46:49 +00001586 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001587 // align stack specially for tail calls
Evan Chengddc419c2010-01-26 19:04:47 +00001588 if (X86::IsEligibleForTailCallOpt(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001589 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001590
Evan Cheng1bc78042006-04-26 01:20:17 +00001591 // If the function takes variable number of arguments, make a frame index for
1592 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001593 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001594 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001595 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001596 }
1597 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001598 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1599
1600 // FIXME: We should really autogenerate these arrays
1601 static const unsigned GPR64ArgRegsWin64[] = {
1602 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001603 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001604 static const unsigned XMMArgRegsWin64[] = {
1605 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1606 };
1607 static const unsigned GPR64ArgRegs64Bit[] = {
1608 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1609 };
1610 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001611 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1612 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1613 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001614 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1615
1616 if (IsWin64) {
1617 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1618 GPR64ArgRegs = GPR64ArgRegsWin64;
1619 XMMArgRegs = XMMArgRegsWin64;
1620 } else {
1621 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1622 GPR64ArgRegs = GPR64ArgRegs64Bit;
1623 XMMArgRegs = XMMArgRegs64Bit;
1624 }
1625 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1626 TotalNumIntRegs);
1627 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1628 TotalNumXMMRegs);
1629
Devang Patel578efa92009-06-05 21:57:13 +00001630 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001631 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001632 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001633 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001634 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001635 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001636 // Kernel mode asks for SSE to be disabled, so don't push them
1637 // on the stack.
1638 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001639
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 // For X86-64, if there are vararg parameters that are passed via
1641 // registers, then we must store them to their spots on the stack so they
1642 // may be loaded by deferencing the result of va_next.
1643 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001644 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1645 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001646 TotalNumXMMRegs * 16, 16,
1647 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648
Gordon Henriksen86737662008-01-05 16:56:59 +00001649 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SmallVector<SDValue, 8> MemOps;
1651 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001652 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001653 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001654 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1655 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001656 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1657 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001659 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001660 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001661 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001662 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001664 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001665 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001666
Dan Gohmanface41a2009-08-16 21:24:25 +00001667 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1668 // Now store the XMM (fp + vector) parameter registers.
1669 SmallVector<SDValue, 11> SaveXMMOps;
1670 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001671
Dan Gohmanface41a2009-08-16 21:24:25 +00001672 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1673 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1674 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001675
Dan Gohmanface41a2009-08-16 21:24:25 +00001676 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1677 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001678
Dan Gohmanface41a2009-08-16 21:24:25 +00001679 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1680 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1681 X86::VR128RegisterClass);
1682 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1683 SaveXMMOps.push_back(Val);
1684 }
1685 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1686 MVT::Other,
1687 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001688 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001689
1690 if (!MemOps.empty())
1691 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1692 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001693 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001694 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001695
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001697 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001699 BytesCallerReserves = 0;
1700 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001701 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001702 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001703 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001704 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001705 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001706 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001707
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 if (!Is64Bit) {
1709 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001710 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001711 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1712 }
Evan Cheng25caf632006-05-23 21:06:34 +00001713
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001714 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001715
Dan Gohman98ca4f22009-08-05 01:29:28 +00001716 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001717}
1718
Dan Gohman475871a2008-07-27 21:46:04 +00001719SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001720X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1721 SDValue StackPtr, SDValue Arg,
1722 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001723 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001724 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001725 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001726 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001727 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001728 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001729 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001730 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001731 }
Dale Johannesenace16102009-02-03 19:33:06 +00001732 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001733 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001734}
1735
Bill Wendling64e87322009-01-16 19:25:27 +00001736/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001737/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001738SDValue
1739X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001740 SDValue &OutRetAddr, SDValue Chain,
1741 bool IsTailCall, bool Is64Bit,
1742 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001743 if (!IsTailCall || FPDiff==0) return Chain;
1744
1745 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001747 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001748
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001749 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001750 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001751 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001752}
1753
1754/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1755/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001756static SDValue
1757EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001759 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001760 // Store the return address to the appropriate stack slot.
1761 if (!FPDiff) return Chain;
1762 // Calculate the new stack slot for the return address.
1763 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001764 int NewReturnAddrFI =
Evan Chengddc419c2010-01-26 19:04:47 +00001765 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, true,false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001767 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001768 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001769 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001770 return Chain;
1771}
1772
Dan Gohman98ca4f22009-08-05 01:29:28 +00001773SDValue
1774X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001775 CallingConv::ID CallConv, bool isVarArg,
1776 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001777 const SmallVectorImpl<ISD::OutputArg> &Outs,
1778 const SmallVectorImpl<ISD::InputArg> &Ins,
1779 DebugLoc dl, SelectionDAG &DAG,
1780 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 MachineFunction &MF = DAG.getMachineFunction();
1783 bool Is64Bit = Subtarget->is64Bit();
1784 bool IsStructRet = CallIsStructReturn(Outs);
1785
Evan Chengddc419c2010-01-26 19:04:47 +00001786 assert((!isTailCall || X86::IsEligibleForTailCallOpt(CallConv)) &&
1787 "Call is not eligible for tail call optimization!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001789 "Var args not supported with calling convention fastcc");
1790
Chris Lattner638402b2007-02-28 07:00:42 +00001791 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001792 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1794 ArgLocs, *DAG.getContext());
1795 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001796
Chris Lattner423c5f42007-02-28 05:31:48 +00001797 // Get a count of how many bytes are to be pushed on the stack.
1798 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengddc419c2010-01-26 19:04:47 +00001799 if (X86::IsEligibleForTailCallOpt(CallConv))
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001800 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001801
Gordon Henriksen86737662008-01-05 16:56:59 +00001802 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001804 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001805 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001806 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1807 FPDiff = NumBytesCallerPushed - NumBytes;
1808
1809 // Set the delta of movement of the returnaddr stackslot.
1810 // But only set if delta is greater than previous delta.
1811 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1812 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1813 }
1814
Chris Lattnere563bbc2008-10-11 22:08:30 +00001815 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001816
Dan Gohman475871a2008-07-27 21:46:04 +00001817 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001818 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001819 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001820 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001821
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1823 SmallVector<SDValue, 8> MemOpChains;
1824 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001825
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001826 // Walk the register/memloc assignments, inserting copies/loads. In the case
1827 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001828 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1829 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001830 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001831 SDValue Arg = Outs[i].Val;
1832 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001833 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001834
Chris Lattner423c5f42007-02-28 05:31:48 +00001835 // Promote the value if needed.
1836 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001837 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001838 case CCValAssign::Full: break;
1839 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001840 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001841 break;
1842 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001843 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001844 break;
1845 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001846 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1847 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001848 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1849 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1850 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001851 } else
1852 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1853 break;
1854 case CCValAssign::BCvt:
1855 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001856 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001857 case CCValAssign::Indirect: {
1858 // Store the argument.
1859 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001860 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001861 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001862 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001863 Arg = SpillSlot;
1864 break;
1865 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001866 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Chris Lattner423c5f42007-02-28 05:31:48 +00001868 if (VA.isRegLoc()) {
1869 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1870 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001871 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001872 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001873 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001874 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001875
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1877 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001878 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001879 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001880 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Evan Cheng32fe1032006-05-25 00:59:30 +00001882 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001883 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001884 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001885
Evan Cheng347d5f72006-04-28 21:29:37 +00001886 // Build a sequence of copy-to-reg nodes chained together with token chain
1887 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001888 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001889 // Tail call byval lowering might overwrite argument registers so in case of
1890 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001891 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001892 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001893 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001894 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001895 InFlag = Chain.getValue(1);
1896 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001897
Eric Christopherfd179292009-08-27 18:07:15 +00001898
Chris Lattner88e1fd52009-07-09 04:24:46 +00001899 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001900 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1901 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001902 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001903 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1904 DAG.getNode(X86ISD::GlobalBaseReg,
1905 DebugLoc::getUnknownLoc(),
1906 getPointerTy()),
1907 InFlag);
1908 InFlag = Chain.getValue(1);
1909 } else {
1910 // If we are tail calling and generating PIC/GOT style code load the
1911 // address of the callee into ECX. The value in ecx is used as target of
1912 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1913 // for tail calls on PIC/GOT architectures. Normally we would just put the
1914 // address of GOT into ebx and then call target@PLT. But for tail calls
1915 // ebx would be restored (since ebx is callee saved) before jumping to the
1916 // target@PLT.
1917
1918 // Note: The actual moving to ECX is done further down.
1919 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1920 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1921 !G->getGlobal()->hasProtectedVisibility())
1922 Callee = LowerGlobalAddress(Callee, DAG);
1923 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001924 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001925 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001926 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001927
Gordon Henriksen86737662008-01-05 16:56:59 +00001928 if (Is64Bit && isVarArg) {
1929 // From AMD64 ABI document:
1930 // For calls that may call functions that use varargs or stdargs
1931 // (prototype-less calls or calls to functions containing ellipsis (...) in
1932 // the declaration) %al is used as hidden argument to specify the number
1933 // of SSE registers used. The contents of %al do not need to match exactly
1934 // the number of registers, but must be an ubound on the number of SSE
1935 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001936
1937 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001938 // Count the number of XMM registers allocated.
1939 static const unsigned XMMArgRegs[] = {
1940 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1941 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1942 };
1943 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001944 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001945 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001946
Dale Johannesendd64c412009-02-04 00:33:20 +00001947 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001948 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 InFlag = Chain.getValue(1);
1950 }
1951
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001952
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001953 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001954 if (isTailCall) {
1955 // Force all the incoming stack arguments to be loaded from the stack
1956 // before any new outgoing arguments are stored to the stack, because the
1957 // outgoing stack slots may alias the incoming argument stack slots, and
1958 // the alias isn't otherwise explicit. This is slightly more conservative
1959 // than necessary, because it means that each store effectively depends
1960 // on every argument instead of just those arguments it would clobber.
1961 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1962
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SmallVector<SDValue, 8> MemOpChains2;
1964 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001965 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001966 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001967 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001968 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1969 CCValAssign &VA = ArgLocs[i];
1970 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001971 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001972 SDValue Arg = Outs[i].Val;
1973 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 // Create frame index.
1975 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001976 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001977 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001978 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001979
Duncan Sands276dcbd2008-03-21 09:14:45 +00001980 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001981 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001982 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001983 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001984 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001985 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001986 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001987
Dan Gohman98ca4f22009-08-05 01:29:28 +00001988 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1989 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001990 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001991 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001992 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001993 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001994 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001995 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001996 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 }
1998 }
1999
2000 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002001 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002002 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002003
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002004 // Copy arguments to their registers.
2005 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002006 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002007 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002008 InFlag = Chain.getValue(1);
2009 }
Dan Gohman475871a2008-07-27 21:46:04 +00002010 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002011
Gordon Henriksen86737662008-01-05 16:56:59 +00002012 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002013 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002014 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 }
2016
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002017 bool WasGlobalOrExternal = false;
2018 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2019 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2020 // In the 64-bit large code model, we have to make all calls
2021 // through a register, since the call instruction's 32-bit
2022 // pc-relative offset may not be large enough to hold the whole
2023 // address.
2024 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2025 WasGlobalOrExternal = true;
2026 // If the callee is a GlobalAddress node (quite common, every direct call
2027 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2028 // it.
2029
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002030 // We should use extra load for direct calls to dllimported functions in
2031 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002032 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002033 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002034 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002035
Chris Lattner48a7d022009-07-09 05:02:21 +00002036 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2037 // external symbols most go through the PLT in PIC mode. If the symbol
2038 // has hidden or protected visibility, or if it is static or local, then
2039 // we don't need to use the PLT - we can directly call it.
2040 if (Subtarget->isTargetELF() &&
2041 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002042 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002043 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002044 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002045 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2046 Subtarget->getDarwinVers() < 9) {
2047 // PC-relative references to external symbols should go through $stub,
2048 // unless we're building with the leopard linker or later, which
2049 // automatically synthesizes these stubs.
2050 OpFlags = X86II::MO_DARWIN_STUB;
2051 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002052
Chris Lattner74e726e2009-07-09 05:27:35 +00002053 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002054 G->getOffset(), OpFlags);
2055 }
Bill Wendling056292f2008-09-16 21:48:12 +00002056 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002057 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002058 unsigned char OpFlags = 0;
2059
2060 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2061 // symbols should go through the PLT.
2062 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002063 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002064 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002065 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002066 Subtarget->getDarwinVers() < 9) {
2067 // PC-relative references to external symbols should go through $stub,
2068 // unless we're building with the leopard linker or later, which
2069 // automatically synthesizes these stubs.
2070 OpFlags = X86II::MO_DARWIN_STUB;
2071 }
Eric Christopherfd179292009-08-27 18:07:15 +00002072
Chris Lattner48a7d022009-07-09 05:02:21 +00002073 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2074 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002075 }
2076
2077 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002078 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002079
Dale Johannesendd64c412009-02-04 00:33:20 +00002080 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002081 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002082 Callee,InFlag);
2083 Callee = DAG.getRegister(Opc, getPointerTy());
2084 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002085 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002086 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002087
Chris Lattnerd96d0722007-02-25 06:40:16 +00002088 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002090 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002091
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002093 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2094 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002095 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002097
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002098 Ops.push_back(Chain);
2099 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002100
Dan Gohman98ca4f22009-08-05 01:29:28 +00002101 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002102 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002103
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 // Add argument registers to the end of the list so that they are known live
2105 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002106 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2107 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2108 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002109
Evan Cheng586ccac2008-03-18 23:36:35 +00002110 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002111 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002112 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2113
2114 // Add an implicit use of AL for x86 vararg functions.
2115 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002116 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002117
Gabor Greifba36cb52008-08-28 21:40:38 +00002118 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002119 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002120
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121 if (isTailCall) {
2122 // If this is the first return lowered for this function, add the regs
2123 // to the liveout set for the function.
2124 if (MF.getRegInfo().liveout_empty()) {
2125 SmallVector<CCValAssign, 16> RVLocs;
2126 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2127 *DAG.getContext());
2128 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2129 for (unsigned i = 0; i != RVLocs.size(); ++i)
2130 if (RVLocs[i].isRegLoc())
2131 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2132 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002133
Dan Gohman98ca4f22009-08-05 01:29:28 +00002134 assert(((Callee.getOpcode() == ISD::Register &&
2135 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002136 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002137 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2138 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002139 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140
2141 return DAG.getNode(X86ISD::TC_RETURN, dl,
2142 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002143 }
2144
Dale Johannesenace16102009-02-03 19:33:06 +00002145 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002146 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002147
Chris Lattner2d297092006-05-23 18:50:38 +00002148 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002150 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002151 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002152 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002153 // If this is is a call to a struct-return function, the callee
2154 // pops the hidden struct pointer, so we have to push it back.
2155 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002156 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002158 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002159
Gordon Henriksenae636f82008-01-03 16:47:34 +00002160 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002161 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002162 DAG.getIntPtrConstant(NumBytes, true),
2163 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2164 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002165 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002166 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002167
Chris Lattner3085e152007-02-25 08:59:22 +00002168 // Handle result values, copying them out of physregs into vregs that we
2169 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2171 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002172}
2173
Evan Cheng25ab6902006-09-08 06:48:29 +00002174
2175//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002176// Fast Calling Convention (tail call) implementation
2177//===----------------------------------------------------------------------===//
2178
2179// Like std call, callee cleans arguments, convention except that ECX is
2180// reserved for storing the tail called function address. Only 2 registers are
2181// free for argument passing (inreg). Tail call optimization is performed
2182// provided:
2183// * tailcallopt is enabled
2184// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002185// On X86_64 architecture with GOT-style position independent code only local
2186// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002187// To keep the stack aligned according to platform abi the function
2188// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2189// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002190// If a tail called function callee has more arguments than the caller the
2191// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002192// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002193// original REtADDR, but before the saved framepointer or the spilled registers
2194// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2195// stack layout:
2196// arg1
2197// arg2
2198// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002199// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002200// move area ]
2201// (possible EBP)
2202// ESI
2203// EDI
2204// local1 ..
2205
2206/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2207/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002208unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002209 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002210 MachineFunction &MF = DAG.getMachineFunction();
2211 const TargetMachine &TM = MF.getTarget();
2212 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2213 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002214 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002215 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002216 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002217 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2218 // Number smaller than 12 so just add the difference.
2219 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2220 } else {
2221 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002223 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002224 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002225 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002226}
2227
Dan Gohman98ca4f22009-08-05 01:29:28 +00002228/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2229/// for tail call optimization. Targets which want to do tail call
2230/// optimization should implement this function.
2231bool
2232X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002233 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234 bool isVarArg,
2235 const SmallVectorImpl<ISD::InputArg> &Ins,
2236 SelectionDAG& DAG) const {
Evan Chengddc419c2010-01-26 19:04:47 +00002237 return X86::IsEligibleForTailCallOpt(CalleeCC) &&
2238 DAG.getMachineFunction().getFunction()->getCallingConv() == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002239}
2240
Dan Gohman3df24e62008-09-03 23:12:08 +00002241FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002242X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2243 DwarfWriter *dw,
2244 DenseMap<const Value *, unsigned> &vm,
2245 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2246 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002247#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002248 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002249#endif
2250 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002251 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002252#ifndef NDEBUG
2253 , cil
2254#endif
2255 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002256}
2257
2258
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002259//===----------------------------------------------------------------------===//
2260// Other Lowering Hooks
2261//===----------------------------------------------------------------------===//
2262
2263
Dan Gohman475871a2008-07-27 21:46:04 +00002264SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002265 MachineFunction &MF = DAG.getMachineFunction();
2266 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2267 int ReturnAddrIndex = FuncInfo->getRAIndex();
2268
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002269 if (ReturnAddrIndex == 0) {
2270 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002271 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002272 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2273 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002274 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002275 }
2276
Evan Cheng25ab6902006-09-08 06:48:29 +00002277 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002278}
2279
2280
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002281bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2282 bool hasSymbolicDisplacement) {
2283 // Offset should fit into 32 bit immediate field.
2284 if (!isInt32(Offset))
2285 return false;
2286
2287 // If we don't have a symbolic displacement - we don't have any extra
2288 // restrictions.
2289 if (!hasSymbolicDisplacement)
2290 return true;
2291
2292 // FIXME: Some tweaks might be needed for medium code model.
2293 if (M != CodeModel::Small && M != CodeModel::Kernel)
2294 return false;
2295
2296 // For small code model we assume that latest object is 16MB before end of 31
2297 // bits boundary. We may also accept pretty large negative constants knowing
2298 // that all objects are in the positive half of address space.
2299 if (M == CodeModel::Small && Offset < 16*1024*1024)
2300 return true;
2301
2302 // For kernel code model we know that all object resist in the negative half
2303 // of 32bits address space. We may not accept negative offsets, since they may
2304 // be just off and we may accept pretty large positive ones.
2305 if (M == CodeModel::Kernel && Offset > 0)
2306 return true;
2307
2308 return false;
2309}
2310
Evan Chengddc419c2010-01-26 19:04:47 +00002311bool X86::IsEligibleForTailCallOpt(CallingConv::ID CC) {
2312 return PerformTailCallOpt && CC == CallingConv::Fast;
2313}
2314
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002315/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2316/// specific condition code, returning the condition code and the LHS/RHS of the
2317/// comparison to make.
2318static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2319 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002320 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002321 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2322 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2323 // X > -1 -> X == 0, jump !sign.
2324 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002325 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002326 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2327 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002328 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002329 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002330 // X < 1 -> X <= 0
2331 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002332 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002333 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002334 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002335
Evan Chengd9558e02006-01-06 00:43:03 +00002336 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002337 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002338 case ISD::SETEQ: return X86::COND_E;
2339 case ISD::SETGT: return X86::COND_G;
2340 case ISD::SETGE: return X86::COND_GE;
2341 case ISD::SETLT: return X86::COND_L;
2342 case ISD::SETLE: return X86::COND_LE;
2343 case ISD::SETNE: return X86::COND_NE;
2344 case ISD::SETULT: return X86::COND_B;
2345 case ISD::SETUGT: return X86::COND_A;
2346 case ISD::SETULE: return X86::COND_BE;
2347 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002348 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002349 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002350
Chris Lattner4c78e022008-12-23 23:42:27 +00002351 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002352
Chris Lattner4c78e022008-12-23 23:42:27 +00002353 // If LHS is a foldable load, but RHS is not, flip the condition.
2354 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2355 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2356 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2357 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002358 }
2359
Chris Lattner4c78e022008-12-23 23:42:27 +00002360 switch (SetCCOpcode) {
2361 default: break;
2362 case ISD::SETOLT:
2363 case ISD::SETOLE:
2364 case ISD::SETUGT:
2365 case ISD::SETUGE:
2366 std::swap(LHS, RHS);
2367 break;
2368 }
2369
2370 // On a floating point condition, the flags are set as follows:
2371 // ZF PF CF op
2372 // 0 | 0 | 0 | X > Y
2373 // 0 | 0 | 1 | X < Y
2374 // 1 | 0 | 0 | X == Y
2375 // 1 | 1 | 1 | unordered
2376 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002377 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002378 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002379 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002380 case ISD::SETOLT: // flipped
2381 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002382 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002383 case ISD::SETOLE: // flipped
2384 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002385 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002386 case ISD::SETUGT: // flipped
2387 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002388 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002389 case ISD::SETUGE: // flipped
2390 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002391 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002392 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002393 case ISD::SETNE: return X86::COND_NE;
2394 case ISD::SETUO: return X86::COND_P;
2395 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002396 case ISD::SETOEQ:
2397 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002398 }
Evan Chengd9558e02006-01-06 00:43:03 +00002399}
2400
Evan Cheng4a460802006-01-11 00:33:36 +00002401/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2402/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002403/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002404static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002405 switch (X86CC) {
2406 default:
2407 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002408 case X86::COND_B:
2409 case X86::COND_BE:
2410 case X86::COND_E:
2411 case X86::COND_P:
2412 case X86::COND_A:
2413 case X86::COND_AE:
2414 case X86::COND_NE:
2415 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002416 return true;
2417 }
2418}
2419
Evan Chengeb2f9692009-10-27 19:56:55 +00002420/// isFPImmLegal - Returns true if the target can instruction select the
2421/// specified FP immediate natively. If false, the legalizer will
2422/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002423bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002424 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2425 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2426 return true;
2427 }
2428 return false;
2429}
2430
Nate Begeman9008ca62009-04-27 18:41:29 +00002431/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2432/// the specified range (L, H].
2433static bool isUndefOrInRange(int Val, int Low, int Hi) {
2434 return (Val < 0) || (Val >= Low && Val < Hi);
2435}
2436
2437/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2438/// specified value.
2439static bool isUndefOrEqual(int Val, int CmpVal) {
2440 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002441 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002443}
2444
Nate Begeman9008ca62009-04-27 18:41:29 +00002445/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2446/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2447/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002448static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002449 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002450 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002451 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002452 return (Mask[0] < 2 && Mask[1] < 2);
2453 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002454}
2455
Nate Begeman9008ca62009-04-27 18:41:29 +00002456bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002457 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002458 N->getMask(M);
2459 return ::isPSHUFDMask(M, N->getValueType(0));
2460}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002461
Nate Begeman9008ca62009-04-27 18:41:29 +00002462/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2463/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002464static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002465 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002466 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002467
Nate Begeman9008ca62009-04-27 18:41:29 +00002468 // Lower quadword copied in order or undef.
2469 for (int i = 0; i != 4; ++i)
2470 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002472
Evan Cheng506d3df2006-03-29 23:07:14 +00002473 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002474 for (int i = 4; i != 8; ++i)
2475 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002476 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002477
Evan Cheng506d3df2006-03-29 23:07:14 +00002478 return true;
2479}
2480
Nate Begeman9008ca62009-04-27 18:41:29 +00002481bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002482 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002483 N->getMask(M);
2484 return ::isPSHUFHWMask(M, N->getValueType(0));
2485}
Evan Cheng506d3df2006-03-29 23:07:14 +00002486
Nate Begeman9008ca62009-04-27 18:41:29 +00002487/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2488/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002489static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002490 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002491 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002492
Rafael Espindola15684b22009-04-24 12:40:33 +00002493 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002494 for (int i = 4; i != 8; ++i)
2495 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002496 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002497
Rafael Espindola15684b22009-04-24 12:40:33 +00002498 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002499 for (int i = 0; i != 4; ++i)
2500 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002501 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002502
Rafael Espindola15684b22009-04-24 12:40:33 +00002503 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002504}
2505
Nate Begeman9008ca62009-04-27 18:41:29 +00002506bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002507 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002508 N->getMask(M);
2509 return ::isPSHUFLWMask(M, N->getValueType(0));
2510}
2511
Nate Begemana09008b2009-10-19 02:17:23 +00002512/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2513/// is suitable for input to PALIGNR.
2514static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2515 bool hasSSSE3) {
2516 int i, e = VT.getVectorNumElements();
2517
2518 // Do not handle v2i64 / v2f64 shuffles with palignr.
2519 if (e < 4 || !hasSSSE3)
2520 return false;
2521
2522 for (i = 0; i != e; ++i)
2523 if (Mask[i] >= 0)
2524 break;
2525
2526 // All undef, not a palignr.
2527 if (i == e)
2528 return false;
2529
2530 // Determine if it's ok to perform a palignr with only the LHS, since we
2531 // don't have access to the actual shuffle elements to see if RHS is undef.
2532 bool Unary = Mask[i] < (int)e;
2533 bool NeedsUnary = false;
2534
2535 int s = Mask[i] - i;
2536
2537 // Check the rest of the elements to see if they are consecutive.
2538 for (++i; i != e; ++i) {
2539 int m = Mask[i];
2540 if (m < 0)
2541 continue;
2542
2543 Unary = Unary && (m < (int)e);
2544 NeedsUnary = NeedsUnary || (m < s);
2545
2546 if (NeedsUnary && !Unary)
2547 return false;
2548 if (Unary && m != ((s+i) & (e-1)))
2549 return false;
2550 if (!Unary && m != (s+i))
2551 return false;
2552 }
2553 return true;
2554}
2555
2556bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2557 SmallVector<int, 8> M;
2558 N->getMask(M);
2559 return ::isPALIGNRMask(M, N->getValueType(0), true);
2560}
2561
Evan Cheng14aed5e2006-03-24 01:18:28 +00002562/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2563/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002564static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002565 int NumElems = VT.getVectorNumElements();
2566 if (NumElems != 2 && NumElems != 4)
2567 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002568
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 int Half = NumElems / 2;
2570 for (int i = 0; i < Half; ++i)
2571 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002572 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002573 for (int i = Half; i < NumElems; ++i)
2574 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002575 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002576
Evan Cheng14aed5e2006-03-24 01:18:28 +00002577 return true;
2578}
2579
Nate Begeman9008ca62009-04-27 18:41:29 +00002580bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2581 SmallVector<int, 8> M;
2582 N->getMask(M);
2583 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002584}
2585
Evan Cheng213d2cf2007-05-17 18:45:50 +00002586/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002587/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2588/// half elements to come from vector 1 (which would equal the dest.) and
2589/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002590static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002592
2593 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002595
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 int Half = NumElems / 2;
2597 for (int i = 0; i < Half; ++i)
2598 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002599 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002600 for (int i = Half; i < NumElems; ++i)
2601 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002602 return false;
2603 return true;
2604}
2605
Nate Begeman9008ca62009-04-27 18:41:29 +00002606static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2607 SmallVector<int, 8> M;
2608 N->getMask(M);
2609 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002610}
2611
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002612/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2613/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002614bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2615 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002616 return false;
2617
Evan Cheng2064a2b2006-03-28 06:50:32 +00002618 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2620 isUndefOrEqual(N->getMaskElt(1), 7) &&
2621 isUndefOrEqual(N->getMaskElt(2), 2) &&
2622 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002623}
2624
Nate Begeman0b10b912009-11-07 23:17:15 +00002625/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2626/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2627/// <2, 3, 2, 3>
2628bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2629 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2630
2631 if (NumElems != 4)
2632 return false;
2633
2634 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2635 isUndefOrEqual(N->getMaskElt(1), 3) &&
2636 isUndefOrEqual(N->getMaskElt(2), 2) &&
2637 isUndefOrEqual(N->getMaskElt(3), 3);
2638}
2639
Evan Cheng5ced1d82006-04-06 23:23:56 +00002640/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2641/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002642bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2643 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002644
Evan Cheng5ced1d82006-04-06 23:23:56 +00002645 if (NumElems != 2 && NumElems != 4)
2646 return false;
2647
Evan Chengc5cdff22006-04-07 21:53:05 +00002648 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002649 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002650 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002651
Evan Chengc5cdff22006-04-07 21:53:05 +00002652 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002653 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002654 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002655
2656 return true;
2657}
2658
Nate Begeman0b10b912009-11-07 23:17:15 +00002659/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2660/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2661bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002663
Evan Cheng5ced1d82006-04-06 23:23:56 +00002664 if (NumElems != 2 && NumElems != 4)
2665 return false;
2666
Evan Chengc5cdff22006-04-07 21:53:05 +00002667 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002668 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002669 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002670
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 for (unsigned i = 0; i < NumElems/2; ++i)
2672 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002673 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002674
2675 return true;
2676}
2677
Evan Cheng0038e592006-03-28 00:39:58 +00002678/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2679/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002680static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002681 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002683 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002685
Nate Begeman9008ca62009-04-27 18:41:29 +00002686 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2687 int BitI = Mask[i];
2688 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002689 if (!isUndefOrEqual(BitI, j))
2690 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002691 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002692 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002693 return false;
2694 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002695 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002696 return false;
2697 }
Evan Cheng0038e592006-03-28 00:39:58 +00002698 }
Evan Cheng0038e592006-03-28 00:39:58 +00002699 return true;
2700}
2701
Nate Begeman9008ca62009-04-27 18:41:29 +00002702bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2703 SmallVector<int, 8> M;
2704 N->getMask(M);
2705 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002706}
2707
Evan Cheng4fcb9222006-03-28 02:43:26 +00002708/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2709/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002710static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002711 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002712 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002713 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002714 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002715
Nate Begeman9008ca62009-04-27 18:41:29 +00002716 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2717 int BitI = Mask[i];
2718 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002719 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002720 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002721 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002722 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002723 return false;
2724 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002725 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002726 return false;
2727 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002728 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002729 return true;
2730}
2731
Nate Begeman9008ca62009-04-27 18:41:29 +00002732bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2733 SmallVector<int, 8> M;
2734 N->getMask(M);
2735 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002736}
2737
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002738/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2739/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2740/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002741static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002742 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002743 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002744 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002745
Nate Begeman9008ca62009-04-27 18:41:29 +00002746 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2747 int BitI = Mask[i];
2748 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002749 if (!isUndefOrEqual(BitI, j))
2750 return false;
2751 if (!isUndefOrEqual(BitI1, j))
2752 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002753 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002754 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002755}
2756
Nate Begeman9008ca62009-04-27 18:41:29 +00002757bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2758 SmallVector<int, 8> M;
2759 N->getMask(M);
2760 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2761}
2762
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002763/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2764/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2765/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002766static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002768 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2769 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002770
Nate Begeman9008ca62009-04-27 18:41:29 +00002771 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2772 int BitI = Mask[i];
2773 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002774 if (!isUndefOrEqual(BitI, j))
2775 return false;
2776 if (!isUndefOrEqual(BitI1, j))
2777 return false;
2778 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002779 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002780}
2781
Nate Begeman9008ca62009-04-27 18:41:29 +00002782bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2783 SmallVector<int, 8> M;
2784 N->getMask(M);
2785 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2786}
2787
Evan Cheng017dcc62006-04-21 01:05:10 +00002788/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2789/// specifies a shuffle of elements that is suitable for input to MOVSS,
2790/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002791static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002792 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002793 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002794
2795 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002796
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002798 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002799
Nate Begeman9008ca62009-04-27 18:41:29 +00002800 for (int i = 1; i < NumElts; ++i)
2801 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002802 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002803
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002804 return true;
2805}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002806
Nate Begeman9008ca62009-04-27 18:41:29 +00002807bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2808 SmallVector<int, 8> M;
2809 N->getMask(M);
2810 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002811}
2812
Evan Cheng017dcc62006-04-21 01:05:10 +00002813/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2814/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002815/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002816static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002817 bool V2IsSplat = false, bool V2IsUndef = false) {
2818 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002819 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002820 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002821
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002823 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002824
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 for (int i = 1; i < NumOps; ++i)
2826 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2827 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2828 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002829 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002830
Evan Cheng39623da2006-04-20 08:58:49 +00002831 return true;
2832}
2833
Nate Begeman9008ca62009-04-27 18:41:29 +00002834static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002835 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 SmallVector<int, 8> M;
2837 N->getMask(M);
2838 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002839}
2840
Evan Chengd9539472006-04-14 21:59:03 +00002841/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2842/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002843bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2844 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002845 return false;
2846
2847 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002848 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 int Elt = N->getMaskElt(i);
2850 if (Elt >= 0 && Elt != 1)
2851 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002852 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002853
2854 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002855 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002856 int Elt = N->getMaskElt(i);
2857 if (Elt >= 0 && Elt != 3)
2858 return false;
2859 if (Elt == 3)
2860 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002861 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002862 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002863 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002864 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002865}
2866
2867/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2868/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002869bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2870 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002871 return false;
2872
2873 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002874 for (unsigned i = 0; i < 2; ++i)
2875 if (N->getMaskElt(i) > 0)
2876 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002877
2878 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002879 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 int Elt = N->getMaskElt(i);
2881 if (Elt >= 0 && Elt != 2)
2882 return false;
2883 if (Elt == 2)
2884 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002885 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002886 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002887 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002888}
2889
Evan Cheng0b457f02008-09-25 20:50:48 +00002890/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2891/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002892bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2893 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002894
Nate Begeman9008ca62009-04-27 18:41:29 +00002895 for (int i = 0; i < e; ++i)
2896 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002897 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002898 for (int i = 0; i < e; ++i)
2899 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002900 return false;
2901 return true;
2902}
2903
Evan Cheng63d33002006-03-22 08:01:21 +00002904/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002905/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002906unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002907 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2908 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2909
Evan Chengb9df0ca2006-03-22 02:53:00 +00002910 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2911 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 for (int i = 0; i < NumOperands; ++i) {
2913 int Val = SVOp->getMaskElt(NumOperands-i-1);
2914 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002915 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002916 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002917 if (i != NumOperands - 1)
2918 Mask <<= Shift;
2919 }
Evan Cheng63d33002006-03-22 08:01:21 +00002920 return Mask;
2921}
2922
Evan Cheng506d3df2006-03-29 23:07:14 +00002923/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002924/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002925unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002927 unsigned Mask = 0;
2928 // 8 nodes, but we only care about the last 4.
2929 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 int Val = SVOp->getMaskElt(i);
2931 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002932 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002933 if (i != 4)
2934 Mask <<= 2;
2935 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002936 return Mask;
2937}
2938
2939/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002940/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002941unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002942 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002943 unsigned Mask = 0;
2944 // 8 nodes, but we only care about the first 4.
2945 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002946 int Val = SVOp->getMaskElt(i);
2947 if (Val >= 0)
2948 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002949 if (i != 0)
2950 Mask <<= 2;
2951 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002952 return Mask;
2953}
2954
Nate Begemana09008b2009-10-19 02:17:23 +00002955/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2956/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2957unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2958 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2959 EVT VVT = N->getValueType(0);
2960 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2961 int Val = 0;
2962
2963 unsigned i, e;
2964 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2965 Val = SVOp->getMaskElt(i);
2966 if (Val >= 0)
2967 break;
2968 }
2969 return (Val - i) * EltSize;
2970}
2971
Evan Cheng37b73872009-07-30 08:33:02 +00002972/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2973/// constant +0.0.
2974bool X86::isZeroNode(SDValue Elt) {
2975 return ((isa<ConstantSDNode>(Elt) &&
2976 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2977 (isa<ConstantFPSDNode>(Elt) &&
2978 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2979}
2980
Nate Begeman9008ca62009-04-27 18:41:29 +00002981/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2982/// their permute mask.
2983static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2984 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002985 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002986 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002988
Nate Begeman5a5ca152009-04-29 05:20:52 +00002989 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 int idx = SVOp->getMaskElt(i);
2991 if (idx < 0)
2992 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002993 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002995 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002996 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002997 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002998 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2999 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003000}
3001
Evan Cheng779ccea2007-12-07 21:30:01 +00003002/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3003/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003004static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003005 unsigned NumElems = VT.getVectorNumElements();
3006 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 int idx = Mask[i];
3008 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003009 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003010 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003012 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003013 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003014 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003015}
3016
Evan Cheng533a0aa2006-04-19 20:35:22 +00003017/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3018/// match movhlps. The lower half elements should come from upper half of
3019/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003020/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003021static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3022 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003023 return false;
3024 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003025 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003026 return false;
3027 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003029 return false;
3030 return true;
3031}
3032
Evan Cheng5ced1d82006-04-06 23:23:56 +00003033/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003034/// is promoted to a vector. It also returns the LoadSDNode by reference if
3035/// required.
3036static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003037 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3038 return false;
3039 N = N->getOperand(0).getNode();
3040 if (!ISD::isNON_EXTLoad(N))
3041 return false;
3042 if (LD)
3043 *LD = cast<LoadSDNode>(N);
3044 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003045}
3046
Evan Cheng533a0aa2006-04-19 20:35:22 +00003047/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3048/// match movlp{s|d}. The lower half elements should come from lower half of
3049/// V1 (and in order), and the upper half elements should come from the upper
3050/// half of V2 (and in order). And since V1 will become the source of the
3051/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003052static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3053 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003054 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003055 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003056 // Is V2 is a vector load, don't do this transformation. We will try to use
3057 // load folding shufps op.
3058 if (ISD::isNON_EXTLoad(V2))
3059 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003060
Nate Begeman5a5ca152009-04-29 05:20:52 +00003061 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003062
Evan Cheng533a0aa2006-04-19 20:35:22 +00003063 if (NumElems != 2 && NumElems != 4)
3064 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003065 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003067 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003068 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003070 return false;
3071 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003072}
3073
Evan Cheng39623da2006-04-20 08:58:49 +00003074/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3075/// all the same.
3076static bool isSplatVector(SDNode *N) {
3077 if (N->getOpcode() != ISD::BUILD_VECTOR)
3078 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003079
Dan Gohman475871a2008-07-27 21:46:04 +00003080 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003081 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3082 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003083 return false;
3084 return true;
3085}
3086
Evan Cheng213d2cf2007-05-17 18:45:50 +00003087/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003088/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003089/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003090static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003091 SDValue V1 = N->getOperand(0);
3092 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003093 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3094 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003095 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003096 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003098 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3099 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003100 if (Opc != ISD::BUILD_VECTOR ||
3101 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 return false;
3103 } else if (Idx >= 0) {
3104 unsigned Opc = V1.getOpcode();
3105 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3106 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003107 if (Opc != ISD::BUILD_VECTOR ||
3108 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003109 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003110 }
3111 }
3112 return true;
3113}
3114
3115/// getZeroVector - Returns a vector of specified type with all zero elements.
3116///
Owen Andersone50ed302009-08-10 22:56:29 +00003117static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003118 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003119 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003120
Chris Lattner8a594482007-11-25 00:24:49 +00003121 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3122 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003124 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3126 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003127 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003128 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3129 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003130 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3132 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003133 }
Dale Johannesenace16102009-02-03 19:33:06 +00003134 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003135}
3136
Chris Lattner8a594482007-11-25 00:24:49 +00003137/// getOnesVector - Returns a vector of specified type with all bits set.
3138///
Owen Andersone50ed302009-08-10 22:56:29 +00003139static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003140 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003141
Chris Lattner8a594482007-11-25 00:24:49 +00003142 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3143 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003145 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003146 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003148 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003150 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003151}
3152
3153
Evan Cheng39623da2006-04-20 08:58:49 +00003154/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3155/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003156static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003157 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003158 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003159
Evan Cheng39623da2006-04-20 08:58:49 +00003160 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 SmallVector<int, 8> MaskVec;
3162 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003163
Nate Begeman5a5ca152009-04-29 05:20:52 +00003164 for (unsigned i = 0; i != NumElems; ++i) {
3165 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 MaskVec[i] = NumElems;
3167 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003168 }
Evan Cheng39623da2006-04-20 08:58:49 +00003169 }
Evan Cheng39623da2006-04-20 08:58:49 +00003170 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3172 SVOp->getOperand(1), &MaskVec[0]);
3173 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003174}
3175
Evan Cheng017dcc62006-04-21 01:05:10 +00003176/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3177/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003178static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 SDValue V2) {
3180 unsigned NumElems = VT.getVectorNumElements();
3181 SmallVector<int, 8> Mask;
3182 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003183 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 Mask.push_back(i);
3185 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003186}
3187
Nate Begeman9008ca62009-04-27 18:41:29 +00003188/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003189static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 SDValue V2) {
3191 unsigned NumElems = VT.getVectorNumElements();
3192 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003193 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003194 Mask.push_back(i);
3195 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003196 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003198}
3199
Nate Begeman9008ca62009-04-27 18:41:29 +00003200/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003201static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 SDValue V2) {
3203 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003204 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003206 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 Mask.push_back(i + Half);
3208 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003209 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003211}
3212
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003213/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003214static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 bool HasSSE2) {
3216 if (SV->getValueType(0).getVectorNumElements() <= 4)
3217 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003218
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003220 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003221 DebugLoc dl = SV->getDebugLoc();
3222 SDValue V1 = SV->getOperand(0);
3223 int NumElems = VT.getVectorNumElements();
3224 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003225
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 // unpack elements to the correct location
3227 while (NumElems > 4) {
3228 if (EltNo < NumElems/2) {
3229 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3230 } else {
3231 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3232 EltNo -= NumElems/2;
3233 }
3234 NumElems >>= 1;
3235 }
Eric Christopherfd179292009-08-27 18:07:15 +00003236
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 // Perform the splat.
3238 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003239 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3241 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003242}
3243
Evan Chengba05f722006-04-21 23:03:30 +00003244/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003245/// vector of zero or undef vector. This produces a shuffle where the low
3246/// element of V2 is swizzled into the zero/undef vector, landing at element
3247/// 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 +00003248static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003249 bool isZero, bool HasSSE2,
3250 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003251 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003253 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3254 unsigned NumElems = VT.getVectorNumElements();
3255 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003256 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 // If this is the insertion idx, put the low elt of V2 here.
3258 MaskVec.push_back(i == Idx ? NumElems : i);
3259 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003260}
3261
Evan Chengf26ffe92008-05-29 08:22:04 +00003262/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3263/// a shuffle that is zero.
3264static
Nate Begeman9008ca62009-04-27 18:41:29 +00003265unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3266 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003267 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003268 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003269 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 int Idx = SVOp->getMaskElt(Index);
3271 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003272 ++NumZeros;
3273 continue;
3274 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003276 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003277 ++NumZeros;
3278 else
3279 break;
3280 }
3281 return NumZeros;
3282}
3283
3284/// isVectorShift - Returns true if the shuffle can be implemented as a
3285/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003286/// FIXME: split into pslldqi, psrldqi, palignr variants.
3287static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003288 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003290
3291 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003293 if (!NumZeros) {
3294 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003295 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003296 if (!NumZeros)
3297 return false;
3298 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003299 bool SeenV1 = false;
3300 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003301 for (int i = NumZeros; i < NumElems; ++i) {
3302 int Val = isLeft ? (i - NumZeros) : i;
3303 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3304 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003305 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003307 SeenV1 = true;
3308 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003309 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003310 SeenV2 = true;
3311 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003312 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003313 return false;
3314 }
3315 if (SeenV1 && SeenV2)
3316 return false;
3317
Nate Begeman9008ca62009-04-27 18:41:29 +00003318 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003319 ShAmt = NumZeros;
3320 return true;
3321}
3322
3323
Evan Chengc78d3b42006-04-24 18:01:45 +00003324/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3325///
Dan Gohman475871a2008-07-27 21:46:04 +00003326static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003327 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003328 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003329 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003330 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003331
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003332 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003333 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003334 bool First = true;
3335 for (unsigned i = 0; i < 16; ++i) {
3336 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3337 if (ThisIsNonZero && First) {
3338 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003340 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003342 First = false;
3343 }
3344
3345 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003347 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3348 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003349 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003351 }
3352 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3354 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3355 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003356 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003357 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003358 } else
3359 ThisElt = LastElt;
3360
Gabor Greifba36cb52008-08-28 21:40:38 +00003361 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003363 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003364 }
3365 }
3366
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003368}
3369
Bill Wendlinga348c562007-03-22 18:42:45 +00003370/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003371///
Dan Gohman475871a2008-07-27 21:46:04 +00003372static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003373 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003374 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003375 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003376 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003377
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003378 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003379 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003380 bool First = true;
3381 for (unsigned i = 0; i < 8; ++i) {
3382 bool isNonZero = (NonZeros & (1 << i)) != 0;
3383 if (isNonZero) {
3384 if (First) {
3385 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003387 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003389 First = false;
3390 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003391 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003393 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003394 }
3395 }
3396
3397 return V;
3398}
3399
Evan Chengf26ffe92008-05-29 08:22:04 +00003400/// getVShift - Return a vector logical shift node.
3401///
Owen Andersone50ed302009-08-10 22:56:29 +00003402static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003403 unsigned NumBits, SelectionDAG &DAG,
3404 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003405 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003407 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003408 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3409 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3410 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003411 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003412}
3413
Dan Gohman475871a2008-07-27 21:46:04 +00003414SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003415X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3416 SelectionDAG &DAG) {
3417
3418 // Check if the scalar load can be widened into a vector load. And if
3419 // the address is "base + cst" see if the cst can be "absorbed" into
3420 // the shuffle mask.
3421 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3422 SDValue Ptr = LD->getBasePtr();
3423 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3424 return SDValue();
3425 EVT PVT = LD->getValueType(0);
3426 if (PVT != MVT::i32 && PVT != MVT::f32)
3427 return SDValue();
3428
3429 int FI = -1;
3430 int64_t Offset = 0;
3431 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3432 FI = FINode->getIndex();
3433 Offset = 0;
3434 } else if (Ptr.getOpcode() == ISD::ADD &&
3435 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3436 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3437 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3438 Offset = Ptr.getConstantOperandVal(1);
3439 Ptr = Ptr.getOperand(0);
3440 } else {
3441 return SDValue();
3442 }
3443
3444 SDValue Chain = LD->getChain();
3445 // Make sure the stack object alignment is at least 16.
3446 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3447 if (DAG.InferPtrAlignment(Ptr) < 16) {
3448 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003449 // Can't change the alignment. FIXME: It's possible to compute
3450 // the exact stack offset and reference FI + adjust offset instead.
3451 // If someone *really* cares about this. That's the way to implement it.
3452 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003453 } else {
3454 MFI->setObjectAlignment(FI, 16);
3455 }
3456 }
3457
3458 // (Offset % 16) must be multiple of 4. Then address is then
3459 // Ptr + (Offset & ~15).
3460 if (Offset < 0)
3461 return SDValue();
3462 if ((Offset % 16) & 3)
3463 return SDValue();
3464 int64_t StartOffset = Offset & ~15;
3465 if (StartOffset)
3466 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3467 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3468
3469 int EltNo = (Offset - StartOffset) >> 2;
3470 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3471 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3472 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3473 // Canonicalize it to a v4i32 shuffle.
3474 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3475 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3476 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3477 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3478 }
3479
3480 return SDValue();
3481}
3482
3483SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003484X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003485 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003486 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003487 if (ISD::isBuildVectorAllZeros(Op.getNode())
3488 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003489 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3490 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3491 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003493 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003494
Gabor Greifba36cb52008-08-28 21:40:38 +00003495 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003496 return getOnesVector(Op.getValueType(), DAG, dl);
3497 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003499
Owen Andersone50ed302009-08-10 22:56:29 +00003500 EVT VT = Op.getValueType();
3501 EVT ExtVT = VT.getVectorElementType();
3502 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003503
3504 unsigned NumElems = Op.getNumOperands();
3505 unsigned NumZero = 0;
3506 unsigned NumNonZero = 0;
3507 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003508 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003509 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003510 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003511 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003512 if (Elt.getOpcode() == ISD::UNDEF)
3513 continue;
3514 Values.insert(Elt);
3515 if (Elt.getOpcode() != ISD::Constant &&
3516 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003517 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003518 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003519 NumZero++;
3520 else {
3521 NonZeros |= (1 << i);
3522 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003523 }
3524 }
3525
Dan Gohman7f321562007-06-25 16:23:39 +00003526 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003527 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003528 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003529 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003530
Chris Lattner67f453a2008-03-09 05:42:06 +00003531 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003532 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003533 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003534 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003535
Chris Lattner62098042008-03-09 01:05:04 +00003536 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3537 // the value are obviously zero, truncate the value to i32 and do the
3538 // insertion that way. Only do this if the value is non-constant or if the
3539 // value is a constant being inserted into element 0. It is cheaper to do
3540 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003542 (!IsAllConstants || Idx == 0)) {
3543 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3544 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003545 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3546 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003547
Chris Lattner62098042008-03-09 01:05:04 +00003548 // Truncate the value (which may itself be a constant) to i32, and
3549 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003551 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003552 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3553 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003554
Chris Lattner62098042008-03-09 01:05:04 +00003555 // Now we have our 32-bit value zero extended in the low element of
3556 // a vector. If Idx != 0, swizzle it into place.
3557 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003558 SmallVector<int, 4> Mask;
3559 Mask.push_back(Idx);
3560 for (unsigned i = 1; i != VecElts; ++i)
3561 Mask.push_back(i);
3562 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003563 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003564 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003565 }
Dale Johannesenace16102009-02-03 19:33:06 +00003566 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003567 }
3568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003569
Chris Lattner19f79692008-03-08 22:59:52 +00003570 // If we have a constant or non-constant insertion into the low element of
3571 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3572 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003573 // depending on what the source datatype is.
3574 if (Idx == 0) {
3575 if (NumZero == 0) {
3576 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3578 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003579 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3580 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3581 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3582 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3584 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3585 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003586 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3587 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3588 Subtarget->hasSSE2(), DAG);
3589 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3590 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003591 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003592
3593 // Is it a vector logical left shift?
3594 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003595 X86::isZeroNode(Op.getOperand(0)) &&
3596 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003597 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003598 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003599 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003600 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003601 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003603
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003604 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003605 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003606
Chris Lattner19f79692008-03-08 22:59:52 +00003607 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3608 // is a non-constant being inserted into an element other than the low one,
3609 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3610 // movd/movss) to move this into the low element, then shuffle it into
3611 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003612 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003613 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003614
Evan Cheng0db9fe62006-04-25 20:13:52 +00003615 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003616 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3617 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003619 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003620 MaskVec.push_back(i == Idx ? 0 : 1);
3621 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003622 }
3623 }
3624
Chris Lattner67f453a2008-03-09 05:42:06 +00003625 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003626 if (Values.size() == 1) {
3627 if (EVTBits == 32) {
3628 // Instead of a shuffle like this:
3629 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3630 // Check if it's possible to issue this instead.
3631 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3632 unsigned Idx = CountTrailingZeros_32(NonZeros);
3633 SDValue Item = Op.getOperand(Idx);
3634 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3635 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3636 }
Dan Gohman475871a2008-07-27 21:46:04 +00003637 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003638 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003639
Dan Gohmana3941172007-07-24 22:55:08 +00003640 // A vector full of immediates; various special cases are already
3641 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003642 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003643 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003644
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003645 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003646 if (EVTBits == 64) {
3647 if (NumNonZero == 1) {
3648 // One half is zero or undef.
3649 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003650 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003651 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003652 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3653 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003654 }
Dan Gohman475871a2008-07-27 21:46:04 +00003655 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003656 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003657
3658 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003659 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003660 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003661 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003662 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003663 }
3664
Bill Wendling826f36f2007-03-28 00:57:11 +00003665 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003666 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003667 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003668 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003669 }
3670
3671 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003672 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003673 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 if (NumElems == 4 && NumZero > 0) {
3675 for (unsigned i = 0; i < 4; ++i) {
3676 bool isZero = !(NonZeros & (1 << i));
3677 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003678 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003679 else
Dale Johannesenace16102009-02-03 19:33:06 +00003680 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681 }
3682
3683 for (unsigned i = 0; i < 2; ++i) {
3684 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3685 default: break;
3686 case 0:
3687 V[i] = V[i*2]; // Must be a zero vector.
3688 break;
3689 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003690 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003691 break;
3692 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 break;
3695 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003697 break;
3698 }
3699 }
3700
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003702 bool Reverse = (NonZeros & 0x3) == 2;
3703 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003705 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3706 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3708 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003709 }
3710
3711 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3713 // values to be inserted is equal to the number of elements, in which case
3714 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003715 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003716 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003717 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003718 getSubtarget()->hasSSE41()) {
3719 V[0] = DAG.getUNDEF(VT);
3720 for (unsigned i = 0; i < NumElems; ++i)
3721 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3722 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3723 Op.getOperand(i), DAG.getIntPtrConstant(i));
3724 return V[0];
3725 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003726 // Expand into a number of unpckl*.
3727 // e.g. for v4f32
3728 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3729 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3730 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003731 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003732 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733 NumElems >>= 1;
3734 while (NumElems != 0) {
3735 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003736 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003737 NumElems >>= 1;
3738 }
3739 return V[0];
3740 }
3741
Dan Gohman475871a2008-07-27 21:46:04 +00003742 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003743}
3744
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003745SDValue
3746X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3747 // We support concatenate two MMX registers and place them in a MMX
3748 // register. This is better than doing a stack convert.
3749 DebugLoc dl = Op.getDebugLoc();
3750 EVT ResVT = Op.getValueType();
3751 assert(Op.getNumOperands() == 2);
3752 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3753 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3754 int Mask[2];
3755 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3756 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3757 InVec = Op.getOperand(1);
3758 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3759 unsigned NumElts = ResVT.getVectorNumElements();
3760 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3761 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3762 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3763 } else {
3764 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3765 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3766 Mask[0] = 0; Mask[1] = 2;
3767 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3768 }
3769 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3770}
3771
Nate Begemanb9a47b82009-02-23 08:49:38 +00003772// v8i16 shuffles - Prefer shuffles in the following order:
3773// 1. [all] pshuflw, pshufhw, optional move
3774// 2. [ssse3] 1 x pshufb
3775// 3. [ssse3] 2 x pshufb + 1 x por
3776// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003777static
Nate Begeman9008ca62009-04-27 18:41:29 +00003778SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3779 SelectionDAG &DAG, X86TargetLowering &TLI) {
3780 SDValue V1 = SVOp->getOperand(0);
3781 SDValue V2 = SVOp->getOperand(1);
3782 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003783 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003784
Nate Begemanb9a47b82009-02-23 08:49:38 +00003785 // Determine if more than 1 of the words in each of the low and high quadwords
3786 // of the result come from the same quadword of one of the two inputs. Undef
3787 // mask values count as coming from any quadword, for better codegen.
3788 SmallVector<unsigned, 4> LoQuad(4);
3789 SmallVector<unsigned, 4> HiQuad(4);
3790 BitVector InputQuads(4);
3791 for (unsigned i = 0; i < 8; ++i) {
3792 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003793 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003794 MaskVals.push_back(EltIdx);
3795 if (EltIdx < 0) {
3796 ++Quad[0];
3797 ++Quad[1];
3798 ++Quad[2];
3799 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003800 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003801 }
3802 ++Quad[EltIdx / 4];
3803 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003804 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003805
Nate Begemanb9a47b82009-02-23 08:49:38 +00003806 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003807 unsigned MaxQuad = 1;
3808 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003809 if (LoQuad[i] > MaxQuad) {
3810 BestLoQuad = i;
3811 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003812 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003813 }
3814
Nate Begemanb9a47b82009-02-23 08:49:38 +00003815 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003816 MaxQuad = 1;
3817 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003818 if (HiQuad[i] > MaxQuad) {
3819 BestHiQuad = i;
3820 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003821 }
3822 }
3823
Nate Begemanb9a47b82009-02-23 08:49:38 +00003824 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003825 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003826 // single pshufb instruction is necessary. If There are more than 2 input
3827 // quads, disable the next transformation since it does not help SSSE3.
3828 bool V1Used = InputQuads[0] || InputQuads[1];
3829 bool V2Used = InputQuads[2] || InputQuads[3];
3830 if (TLI.getSubtarget()->hasSSSE3()) {
3831 if (InputQuads.count() == 2 && V1Used && V2Used) {
3832 BestLoQuad = InputQuads.find_first();
3833 BestHiQuad = InputQuads.find_next(BestLoQuad);
3834 }
3835 if (InputQuads.count() > 2) {
3836 BestLoQuad = -1;
3837 BestHiQuad = -1;
3838 }
3839 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003840
Nate Begemanb9a47b82009-02-23 08:49:38 +00003841 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3842 // the shuffle mask. If a quad is scored as -1, that means that it contains
3843 // words from all 4 input quadwords.
3844 SDValue NewV;
3845 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003846 SmallVector<int, 8> MaskV;
3847 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3848 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003849 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3851 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3852 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003853
Nate Begemanb9a47b82009-02-23 08:49:38 +00003854 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3855 // source words for the shuffle, to aid later transformations.
3856 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003857 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003858 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003859 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003860 if (idx != (int)i)
3861 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003862 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003863 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003864 AllWordsInNewV = false;
3865 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003866 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003867
Nate Begemanb9a47b82009-02-23 08:49:38 +00003868 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3869 if (AllWordsInNewV) {
3870 for (int i = 0; i != 8; ++i) {
3871 int idx = MaskVals[i];
3872 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003873 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003874 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003875 if ((idx != i) && idx < 4)
3876 pshufhw = false;
3877 if ((idx != i) && idx > 3)
3878 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003879 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003880 V1 = NewV;
3881 V2Used = false;
3882 BestLoQuad = 0;
3883 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003884 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003885
Nate Begemanb9a47b82009-02-23 08:49:38 +00003886 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3887 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003888 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003889 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003891 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003892 }
Eric Christopherfd179292009-08-27 18:07:15 +00003893
Nate Begemanb9a47b82009-02-23 08:49:38 +00003894 // If we have SSSE3, and all words of the result are from 1 input vector,
3895 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3896 // is present, fall back to case 4.
3897 if (TLI.getSubtarget()->hasSSSE3()) {
3898 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003899
Nate Begemanb9a47b82009-02-23 08:49:38 +00003900 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003901 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003902 // mask, and elements that come from V1 in the V2 mask, so that the two
3903 // results can be OR'd together.
3904 bool TwoInputs = V1Used && V2Used;
3905 for (unsigned i = 0; i != 8; ++i) {
3906 int EltIdx = MaskVals[i] * 2;
3907 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3909 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003910 continue;
3911 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3913 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003914 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003916 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003917 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003919 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003921
Nate Begemanb9a47b82009-02-23 08:49:38 +00003922 // Calculate the shuffle mask for the second input, shuffle it, and
3923 // OR it with the first shuffled input.
3924 pshufbMask.clear();
3925 for (unsigned i = 0; i != 8; ++i) {
3926 int EltIdx = MaskVals[i] * 2;
3927 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3929 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003930 continue;
3931 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3933 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003934 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003936 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003937 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003938 MVT::v16i8, &pshufbMask[0], 16));
3939 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3940 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003941 }
3942
3943 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3944 // and update MaskVals with new element order.
3945 BitVector InOrder(8);
3946 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003948 for (int i = 0; i != 4; ++i) {
3949 int idx = MaskVals[i];
3950 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003952 InOrder.set(i);
3953 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003955 InOrder.set(i);
3956 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003958 }
3959 }
3960 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003963 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003964 }
Eric Christopherfd179292009-08-27 18:07:15 +00003965
Nate Begemanb9a47b82009-02-23 08:49:38 +00003966 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3967 // and update MaskVals with the new element order.
3968 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003969 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003970 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003972 for (unsigned i = 4; i != 8; ++i) {
3973 int idx = MaskVals[i];
3974 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003975 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003976 InOrder.set(i);
3977 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003979 InOrder.set(i);
3980 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003982 }
3983 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003985 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003986 }
Eric Christopherfd179292009-08-27 18:07:15 +00003987
Nate Begemanb9a47b82009-02-23 08:49:38 +00003988 // In case BestHi & BestLo were both -1, which means each quadword has a word
3989 // from each of the four input quadwords, calculate the InOrder bitvector now
3990 // before falling through to the insert/extract cleanup.
3991 if (BestLoQuad == -1 && BestHiQuad == -1) {
3992 NewV = V1;
3993 for (int i = 0; i != 8; ++i)
3994 if (MaskVals[i] < 0 || MaskVals[i] == i)
3995 InOrder.set(i);
3996 }
Eric Christopherfd179292009-08-27 18:07:15 +00003997
Nate Begemanb9a47b82009-02-23 08:49:38 +00003998 // The other elements are put in the right place using pextrw and pinsrw.
3999 for (unsigned i = 0; i != 8; ++i) {
4000 if (InOrder[i])
4001 continue;
4002 int EltIdx = MaskVals[i];
4003 if (EltIdx < 0)
4004 continue;
4005 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004006 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004007 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004008 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004009 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004010 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004011 DAG.getIntPtrConstant(i));
4012 }
4013 return NewV;
4014}
4015
4016// v16i8 shuffles - Prefer shuffles in the following order:
4017// 1. [ssse3] 1 x pshufb
4018// 2. [ssse3] 2 x pshufb + 1 x por
4019// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4020static
Nate Begeman9008ca62009-04-27 18:41:29 +00004021SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4022 SelectionDAG &DAG, X86TargetLowering &TLI) {
4023 SDValue V1 = SVOp->getOperand(0);
4024 SDValue V2 = SVOp->getOperand(1);
4025 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004026 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004027 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004028
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004030 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004031 // present, fall back to case 3.
4032 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4033 bool V1Only = true;
4034 bool V2Only = true;
4035 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004036 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004037 if (EltIdx < 0)
4038 continue;
4039 if (EltIdx < 16)
4040 V2Only = false;
4041 else
4042 V1Only = false;
4043 }
Eric Christopherfd179292009-08-27 18:07:15 +00004044
Nate Begemanb9a47b82009-02-23 08:49:38 +00004045 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4046 if (TLI.getSubtarget()->hasSSSE3()) {
4047 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004048
Nate Begemanb9a47b82009-02-23 08:49:38 +00004049 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004050 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004051 //
4052 // Otherwise, we have elements from both input vectors, and must zero out
4053 // elements that come from V2 in the first mask, and V1 in the second mask
4054 // so that we can OR them together.
4055 bool TwoInputs = !(V1Only || V2Only);
4056 for (unsigned i = 0; i != 16; ++i) {
4057 int EltIdx = MaskVals[i];
4058 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004060 continue;
4061 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 }
4064 // If all the elements are from V2, assign it to V1 and return after
4065 // building the first pshufb.
4066 if (V2Only)
4067 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004069 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004071 if (!TwoInputs)
4072 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004073
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 // Calculate the shuffle mask for the second input, shuffle it, and
4075 // OR it with the first shuffled input.
4076 pshufbMask.clear();
4077 for (unsigned i = 0; i != 16; ++i) {
4078 int EltIdx = MaskVals[i];
4079 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004081 continue;
4082 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004084 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004086 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 MVT::v16i8, &pshufbMask[0], 16));
4088 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 }
Eric Christopherfd179292009-08-27 18:07:15 +00004090
Nate Begemanb9a47b82009-02-23 08:49:38 +00004091 // No SSSE3 - Calculate in place words and then fix all out of place words
4092 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4093 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004094 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4095 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 SDValue NewV = V2Only ? V2 : V1;
4097 for (int i = 0; i != 8; ++i) {
4098 int Elt0 = MaskVals[i*2];
4099 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004100
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 // This word of the result is all undef, skip it.
4102 if (Elt0 < 0 && Elt1 < 0)
4103 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004104
Nate Begemanb9a47b82009-02-23 08:49:38 +00004105 // This word of the result is already in the correct place, skip it.
4106 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4107 continue;
4108 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4109 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004110
Nate Begemanb9a47b82009-02-23 08:49:38 +00004111 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4112 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4113 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004114
4115 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4116 // using a single extract together, load it and store it.
4117 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004118 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004119 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004121 DAG.getIntPtrConstant(i));
4122 continue;
4123 }
4124
Nate Begemanb9a47b82009-02-23 08:49:38 +00004125 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004126 // source byte is not also odd, shift the extracted word left 8 bits
4127 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004128 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004130 DAG.getIntPtrConstant(Elt1 / 2));
4131 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004133 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004134 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4136 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004137 }
4138 // If Elt0 is defined, extract it from the appropriate source. If the
4139 // source byte is not also even, shift the extracted word right 8 bits. If
4140 // Elt1 was also defined, OR the extracted values together before
4141 // inserting them in the result.
4142 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004144 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4145 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004147 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004148 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4150 DAG.getConstant(0x00FF, MVT::i16));
4151 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004152 : InsElt0;
4153 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 DAG.getIntPtrConstant(i));
4156 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004158}
4159
Evan Cheng7a831ce2007-12-15 03:00:47 +00004160/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4161/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4162/// done when every pair / quad of shuffle mask elements point to elements in
4163/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004164/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4165static
Nate Begeman9008ca62009-04-27 18:41:29 +00004166SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4167 SelectionDAG &DAG,
4168 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004169 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 SDValue V1 = SVOp->getOperand(0);
4171 SDValue V2 = SVOp->getOperand(1);
4172 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004173 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004175 EVT MaskEltVT = MaskVT.getVectorElementType();
4176 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004178 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 case MVT::v4f32: NewVT = MVT::v2f64; break;
4180 case MVT::v4i32: NewVT = MVT::v2i64; break;
4181 case MVT::v8i16: NewVT = MVT::v4i32; break;
4182 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004183 }
4184
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004185 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004186 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004187 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004188 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004189 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004190 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 int Scale = NumElems / NewWidth;
4192 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004193 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 int StartIdx = -1;
4195 for (int j = 0; j < Scale; ++j) {
4196 int EltIdx = SVOp->getMaskElt(i+j);
4197 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004198 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004200 StartIdx = EltIdx - (EltIdx % Scale);
4201 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004202 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004203 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 if (StartIdx == -1)
4205 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004206 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004207 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004208 }
4209
Dale Johannesenace16102009-02-03 19:33:06 +00004210 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4211 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004213}
4214
Evan Chengd880b972008-05-09 21:53:03 +00004215/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004216///
Owen Andersone50ed302009-08-10 22:56:29 +00004217static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004218 SDValue SrcOp, SelectionDAG &DAG,
4219 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004221 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004222 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004223 LD = dyn_cast<LoadSDNode>(SrcOp);
4224 if (!LD) {
4225 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4226 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004227 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4228 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004229 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4230 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004231 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004232 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004234 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4235 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4236 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4237 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004238 SrcOp.getOperand(0)
4239 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004240 }
4241 }
4242 }
4243
Dale Johannesenace16102009-02-03 19:33:06 +00004244 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4245 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004246 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004247 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004248}
4249
Evan Chengace3c172008-07-22 21:13:36 +00004250/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4251/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004252static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004253LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4254 SDValue V1 = SVOp->getOperand(0);
4255 SDValue V2 = SVOp->getOperand(1);
4256 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004257 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004258
Evan Chengace3c172008-07-22 21:13:36 +00004259 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004260 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004261 SmallVector<int, 8> Mask1(4U, -1);
4262 SmallVector<int, 8> PermMask;
4263 SVOp->getMask(PermMask);
4264
Evan Chengace3c172008-07-22 21:13:36 +00004265 unsigned NumHi = 0;
4266 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004267 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 int Idx = PermMask[i];
4269 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004270 Locs[i] = std::make_pair(-1, -1);
4271 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004272 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4273 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004274 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004275 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004276 NumLo++;
4277 } else {
4278 Locs[i] = std::make_pair(1, NumHi);
4279 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004281 NumHi++;
4282 }
4283 }
4284 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004285
Evan Chengace3c172008-07-22 21:13:36 +00004286 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004287 // If no more than two elements come from either vector. This can be
4288 // implemented with two shuffles. First shuffle gather the elements.
4289 // The second shuffle, which takes the first shuffle as both of its
4290 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004291 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004292
Nate Begeman9008ca62009-04-27 18:41:29 +00004293 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004294
Evan Chengace3c172008-07-22 21:13:36 +00004295 for (unsigned i = 0; i != 4; ++i) {
4296 if (Locs[i].first == -1)
4297 continue;
4298 else {
4299 unsigned Idx = (i < 2) ? 0 : 4;
4300 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004302 }
4303 }
4304
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004306 } else if (NumLo == 3 || NumHi == 3) {
4307 // Otherwise, we must have three elements from one vector, call it X, and
4308 // one element from the other, call it Y. First, use a shufps to build an
4309 // intermediate vector with the one element from Y and the element from X
4310 // that will be in the same half in the final destination (the indexes don't
4311 // matter). Then, use a shufps to build the final vector, taking the half
4312 // containing the element from Y from the intermediate, and the other half
4313 // from X.
4314 if (NumHi == 3) {
4315 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004316 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004317 std::swap(V1, V2);
4318 }
4319
4320 // Find the element from V2.
4321 unsigned HiIndex;
4322 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 int Val = PermMask[HiIndex];
4324 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004325 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004326 if (Val >= 4)
4327 break;
4328 }
4329
Nate Begeman9008ca62009-04-27 18:41:29 +00004330 Mask1[0] = PermMask[HiIndex];
4331 Mask1[1] = -1;
4332 Mask1[2] = PermMask[HiIndex^1];
4333 Mask1[3] = -1;
4334 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004335
4336 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 Mask1[0] = PermMask[0];
4338 Mask1[1] = PermMask[1];
4339 Mask1[2] = HiIndex & 1 ? 6 : 4;
4340 Mask1[3] = HiIndex & 1 ? 4 : 6;
4341 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004342 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004343 Mask1[0] = HiIndex & 1 ? 2 : 0;
4344 Mask1[1] = HiIndex & 1 ? 0 : 2;
4345 Mask1[2] = PermMask[2];
4346 Mask1[3] = PermMask[3];
4347 if (Mask1[2] >= 0)
4348 Mask1[2] += 4;
4349 if (Mask1[3] >= 0)
4350 Mask1[3] += 4;
4351 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004352 }
Evan Chengace3c172008-07-22 21:13:36 +00004353 }
4354
4355 // Break it into (shuffle shuffle_hi, shuffle_lo).
4356 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 SmallVector<int,8> LoMask(4U, -1);
4358 SmallVector<int,8> HiMask(4U, -1);
4359
4360 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004361 unsigned MaskIdx = 0;
4362 unsigned LoIdx = 0;
4363 unsigned HiIdx = 2;
4364 for (unsigned i = 0; i != 4; ++i) {
4365 if (i == 2) {
4366 MaskPtr = &HiMask;
4367 MaskIdx = 1;
4368 LoIdx = 0;
4369 HiIdx = 2;
4370 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 int Idx = PermMask[i];
4372 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004373 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004375 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004377 LoIdx++;
4378 } else {
4379 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004381 HiIdx++;
4382 }
4383 }
4384
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4386 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4387 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004388 for (unsigned i = 0; i != 4; ++i) {
4389 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004391 } else {
4392 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004394 }
4395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004397}
4398
Dan Gohman475871a2008-07-27 21:46:04 +00004399SDValue
4400X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004402 SDValue V1 = Op.getOperand(0);
4403 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004404 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004405 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004407 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4409 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004410 bool V1IsSplat = false;
4411 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004412
Nate Begeman9008ca62009-04-27 18:41:29 +00004413 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004414 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004415
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 // Promote splats to v4f32.
4417 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004418 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 return Op;
4420 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004421 }
4422
Evan Cheng7a831ce2007-12-15 03:00:47 +00004423 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4424 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004425 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004427 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004428 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004429 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004431 // FIXME: Figure out a cleaner way to do this.
4432 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004433 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004434 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004435 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4437 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4438 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004439 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4442 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004443 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004445 }
4446 }
Eric Christopherfd179292009-08-27 18:07:15 +00004447
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 if (X86::isPSHUFDMask(SVOp))
4449 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004450
Evan Chengf26ffe92008-05-29 08:22:04 +00004451 // Check if this can be converted into a logical shift.
4452 bool isLeft = false;
4453 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004454 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004455 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004456 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004457 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004458 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004459 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004460 EVT EltVT = VT.getVectorElementType();
4461 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004462 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004463 }
Eric Christopherfd179292009-08-27 18:07:15 +00004464
Nate Begeman9008ca62009-04-27 18:41:29 +00004465 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004466 if (V1IsUndef)
4467 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004468 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004469 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004470 if (!isMMX)
4471 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004472 }
Eric Christopherfd179292009-08-27 18:07:15 +00004473
Nate Begeman9008ca62009-04-27 18:41:29 +00004474 // FIXME: fold these into legal mask.
4475 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4476 X86::isMOVSLDUPMask(SVOp) ||
4477 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004478 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004479 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004480 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 if (ShouldXformToMOVHLPS(SVOp) ||
4483 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4484 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485
Evan Chengf26ffe92008-05-29 08:22:04 +00004486 if (isShift) {
4487 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004488 EVT EltVT = VT.getVectorElementType();
4489 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004490 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004491 }
Eric Christopherfd179292009-08-27 18:07:15 +00004492
Evan Cheng9eca5e82006-10-25 21:49:50 +00004493 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004494 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4495 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004496 V1IsSplat = isSplatVector(V1.getNode());
4497 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004498
Chris Lattner8a594482007-11-25 00:24:49 +00004499 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004500 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004501 Op = CommuteVectorShuffle(SVOp, DAG);
4502 SVOp = cast<ShuffleVectorSDNode>(Op);
4503 V1 = SVOp->getOperand(0);
4504 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004505 std::swap(V1IsSplat, V2IsSplat);
4506 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004507 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004508 }
4509
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4511 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004512 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 return V1;
4514 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4515 // the instruction selector will not match, so get a canonical MOVL with
4516 // swapped operands to undo the commute.
4517 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004518 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004519
Nate Begeman9008ca62009-04-27 18:41:29 +00004520 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4521 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4522 X86::isUNPCKLMask(SVOp) ||
4523 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004524 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004525
Evan Cheng9bbbb982006-10-25 20:48:19 +00004526 if (V2IsSplat) {
4527 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004528 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004529 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 SDValue NewMask = NormalizeMask(SVOp, DAG);
4531 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4532 if (NSVOp != SVOp) {
4533 if (X86::isUNPCKLMask(NSVOp, true)) {
4534 return NewMask;
4535 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4536 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004537 }
4538 }
4539 }
4540
Evan Cheng9eca5e82006-10-25 21:49:50 +00004541 if (Commuted) {
4542 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004543 // FIXME: this seems wrong.
4544 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4545 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4546 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4547 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4548 X86::isUNPCKLMask(NewSVOp) ||
4549 X86::isUNPCKHMask(NewSVOp))
4550 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004551 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552
Nate Begemanb9a47b82009-02-23 08:49:38 +00004553 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004554
4555 // Normalize the node to match x86 shuffle ops if needed
4556 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4557 return CommuteVectorShuffle(SVOp, DAG);
4558
4559 // Check for legal shuffle and return?
4560 SmallVector<int, 16> PermMask;
4561 SVOp->getMask(PermMask);
4562 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004563 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004564
Evan Cheng14b32e12007-12-11 01:46:18 +00004565 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004566 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004567 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004568 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004569 return NewOp;
4570 }
4571
Owen Anderson825b72b2009-08-11 20:47:22 +00004572 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004573 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004574 if (NewOp.getNode())
4575 return NewOp;
4576 }
Eric Christopherfd179292009-08-27 18:07:15 +00004577
Evan Chengace3c172008-07-22 21:13:36 +00004578 // Handle all 4 wide cases with a number of shuffles except for MMX.
4579 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004580 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581
Dan Gohman475871a2008-07-27 21:46:04 +00004582 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004583}
4584
Dan Gohman475871a2008-07-27 21:46:04 +00004585SDValue
4586X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004587 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004588 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004589 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004590 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004592 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004593 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004594 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004595 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004596 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004597 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4598 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4599 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004600 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4601 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004602 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004603 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004604 Op.getOperand(0)),
4605 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004607 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004609 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004610 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004612 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4613 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004614 // result has a single use which is a store or a bitcast to i32. And in
4615 // the case of a store, it's not worth it if the index is a constant 0,
4616 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004617 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004618 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004619 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004620 if ((User->getOpcode() != ISD::STORE ||
4621 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4622 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004623 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004624 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004625 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004626 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4627 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004628 Op.getOperand(0)),
4629 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004630 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4631 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004632 // ExtractPS works with constant index.
4633 if (isa<ConstantSDNode>(Op.getOperand(1)))
4634 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004635 }
Dan Gohman475871a2008-07-27 21:46:04 +00004636 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004637}
4638
4639
Dan Gohman475871a2008-07-27 21:46:04 +00004640SDValue
4641X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004642 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004643 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004644
Evan Cheng62a3f152008-03-24 21:52:23 +00004645 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004646 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004647 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004648 return Res;
4649 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004650
Owen Andersone50ed302009-08-10 22:56:29 +00004651 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004652 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004654 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004655 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004656 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004657 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004658 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4659 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004660 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004661 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004662 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004663 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004664 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004665 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004667 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004668 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004669 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004670 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004671 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004672 if (Idx == 0)
4673 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004674
Evan Cheng0db9fe62006-04-25 20:13:52 +00004675 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004676 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004677 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004678 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004680 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004681 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004682 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004683 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4684 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4685 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004686 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004687 if (Idx == 0)
4688 return Op;
4689
4690 // UNPCKHPD the element to the lowest double word, then movsd.
4691 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4692 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004694 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004695 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004697 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004698 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004699 }
4700
Dan Gohman475871a2008-07-27 21:46:04 +00004701 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702}
4703
Dan Gohman475871a2008-07-27 21:46:04 +00004704SDValue
4705X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004706 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004707 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004708 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004709
Dan Gohman475871a2008-07-27 21:46:04 +00004710 SDValue N0 = Op.getOperand(0);
4711 SDValue N1 = Op.getOperand(1);
4712 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004713
Dan Gohman8a55ce42009-09-23 21:02:20 +00004714 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004715 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004716 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4717 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004718 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4719 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 if (N1.getValueType() != MVT::i32)
4721 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4722 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004723 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004724 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004725 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004726 // Bits [7:6] of the constant are the source select. This will always be
4727 // zero here. The DAG Combiner may combine an extract_elt index into these
4728 // bits. For example (insert (extract, 3), 2) could be matched by putting
4729 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004730 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004731 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004732 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004733 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004734 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004735 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004736 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004737 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004738 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004739 // PINSR* works with constant index.
4740 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004741 }
Dan Gohman475871a2008-07-27 21:46:04 +00004742 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004743}
4744
Dan Gohman475871a2008-07-27 21:46:04 +00004745SDValue
4746X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004747 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004748 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004749
4750 if (Subtarget->hasSSE41())
4751 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4752
Dan Gohman8a55ce42009-09-23 21:02:20 +00004753 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004754 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004755
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004756 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004757 SDValue N0 = Op.getOperand(0);
4758 SDValue N1 = Op.getOperand(1);
4759 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004760
Dan Gohman8a55ce42009-09-23 21:02:20 +00004761 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004762 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4763 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004764 if (N1.getValueType() != MVT::i32)
4765 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4766 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004767 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004768 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004769 }
Dan Gohman475871a2008-07-27 21:46:04 +00004770 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004771}
4772
Dan Gohman475871a2008-07-27 21:46:04 +00004773SDValue
4774X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004775 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004776 if (Op.getValueType() == MVT::v2f32)
4777 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4778 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4779 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004780 Op.getOperand(0))));
4781
Owen Anderson825b72b2009-08-11 20:47:22 +00004782 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4783 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004784
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4786 EVT VT = MVT::v2i32;
4787 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004788 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 case MVT::v16i8:
4790 case MVT::v8i16:
4791 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004792 break;
4793 }
Dale Johannesenace16102009-02-03 19:33:06 +00004794 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4795 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004796}
4797
Bill Wendling056292f2008-09-16 21:48:12 +00004798// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4799// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4800// one of the above mentioned nodes. It has to be wrapped because otherwise
4801// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4802// be used to form addressing mode. These wrapped nodes will be selected
4803// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004804SDValue
4805X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004806 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004807
Chris Lattner41621a22009-06-26 19:22:52 +00004808 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4809 // global base reg.
4810 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004811 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004812 CodeModel::Model M = getTargetMachine().getCodeModel();
4813
Chris Lattner4f066492009-07-11 20:29:19 +00004814 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004815 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004816 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004817 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004818 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004819 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004820 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004821
Evan Cheng1606e8e2009-03-13 07:51:59 +00004822 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004823 CP->getAlignment(),
4824 CP->getOffset(), OpFlag);
4825 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004826 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004827 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004828 if (OpFlag) {
4829 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004830 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004831 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004832 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004833 }
4834
4835 return Result;
4836}
4837
Chris Lattner18c59872009-06-27 04:16:01 +00004838SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4839 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004840
Chris Lattner18c59872009-06-27 04:16:01 +00004841 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4842 // global base reg.
4843 unsigned char OpFlag = 0;
4844 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004845 CodeModel::Model M = getTargetMachine().getCodeModel();
4846
Chris Lattner4f066492009-07-11 20:29:19 +00004847 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004848 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004849 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004850 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004851 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004852 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004853 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004854
Chris Lattner18c59872009-06-27 04:16:01 +00004855 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4856 OpFlag);
4857 DebugLoc DL = JT->getDebugLoc();
4858 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004859
Chris Lattner18c59872009-06-27 04:16:01 +00004860 // With PIC, the address is actually $g + Offset.
4861 if (OpFlag) {
4862 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4863 DAG.getNode(X86ISD::GlobalBaseReg,
4864 DebugLoc::getUnknownLoc(), getPointerTy()),
4865 Result);
4866 }
Eric Christopherfd179292009-08-27 18:07:15 +00004867
Chris Lattner18c59872009-06-27 04:16:01 +00004868 return Result;
4869}
4870
4871SDValue
4872X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4873 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004874
Chris Lattner18c59872009-06-27 04:16:01 +00004875 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4876 // global base reg.
4877 unsigned char OpFlag = 0;
4878 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004879 CodeModel::Model M = getTargetMachine().getCodeModel();
4880
Chris Lattner4f066492009-07-11 20:29:19 +00004881 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004882 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004883 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004884 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004885 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004886 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004887 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004888
Chris Lattner18c59872009-06-27 04:16:01 +00004889 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004890
Chris Lattner18c59872009-06-27 04:16:01 +00004891 DebugLoc DL = Op.getDebugLoc();
4892 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004893
4894
Chris Lattner18c59872009-06-27 04:16:01 +00004895 // With PIC, the address is actually $g + Offset.
4896 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004897 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004898 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4899 DAG.getNode(X86ISD::GlobalBaseReg,
4900 DebugLoc::getUnknownLoc(),
4901 getPointerTy()),
4902 Result);
4903 }
Eric Christopherfd179292009-08-27 18:07:15 +00004904
Chris Lattner18c59872009-06-27 04:16:01 +00004905 return Result;
4906}
4907
Dan Gohman475871a2008-07-27 21:46:04 +00004908SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004909X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004910 // Create the TargetBlockAddressAddress node.
4911 unsigned char OpFlags =
4912 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004913 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004914 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4915 DebugLoc dl = Op.getDebugLoc();
4916 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4917 /*isTarget=*/true, OpFlags);
4918
Dan Gohmanf705adb2009-10-30 01:28:02 +00004919 if (Subtarget->isPICStyleRIPRel() &&
4920 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004921 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4922 else
4923 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004924
Dan Gohman29cbade2009-11-20 23:18:13 +00004925 // With PIC, the address is actually $g + Offset.
4926 if (isGlobalRelativeToPICBase(OpFlags)) {
4927 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4928 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4929 Result);
4930 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004931
4932 return Result;
4933}
4934
4935SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004936X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004937 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004938 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004939 // Create the TargetGlobalAddress node, folding in the constant
4940 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004941 unsigned char OpFlags =
4942 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004943 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004944 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004945 if (OpFlags == X86II::MO_NO_FLAG &&
4946 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004947 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004948 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004949 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004950 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004951 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004952 }
Eric Christopherfd179292009-08-27 18:07:15 +00004953
Chris Lattner4f066492009-07-11 20:29:19 +00004954 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004955 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004956 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4957 else
4958 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004959
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004960 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004961 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004962 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4963 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004964 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004965 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004966
Chris Lattner36c25012009-07-10 07:34:39 +00004967 // For globals that require a load from a stub to get the address, emit the
4968 // load.
4969 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004970 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004971 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004972
Dan Gohman6520e202008-10-18 02:06:02 +00004973 // If there was a non-zero offset that we didn't fold, create an explicit
4974 // addition for it.
4975 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004976 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004977 DAG.getConstant(Offset, getPointerTy()));
4978
Evan Cheng0db9fe62006-04-25 20:13:52 +00004979 return Result;
4980}
4981
Evan Chengda43bcf2008-09-24 00:05:32 +00004982SDValue
4983X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4984 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004985 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004986 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004987}
4988
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004989static SDValue
4990GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004991 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004992 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004993 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004994 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004995 DebugLoc dl = GA->getDebugLoc();
4996 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4997 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004998 GA->getOffset(),
4999 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005000 if (InFlag) {
5001 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005002 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005003 } else {
5004 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005005 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005006 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005007
5008 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5009 MFI->setHasCalls(true);
5010
Rafael Espindola15f1b662009-04-24 12:59:40 +00005011 SDValue Flag = Chain.getValue(1);
5012 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005013}
5014
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005015// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005016static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005017LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005018 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005019 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005020 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5021 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005022 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005023 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005024 PtrVT), InFlag);
5025 InFlag = Chain.getValue(1);
5026
Chris Lattnerb903bed2009-06-26 21:20:29 +00005027 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005028}
5029
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005030// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005031static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005032LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005033 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005034 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5035 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005036}
5037
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005038// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5039// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005040static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005041 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005042 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005043 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005044 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005045 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5046 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005047 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005048 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005049
5050 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5051 NULL, 0);
5052
Chris Lattnerb903bed2009-06-26 21:20:29 +00005053 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005054 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5055 // initialexec.
5056 unsigned WrapperKind = X86ISD::Wrapper;
5057 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005058 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005059 } else if (is64Bit) {
5060 assert(model == TLSModel::InitialExec);
5061 OperandFlags = X86II::MO_GOTTPOFF;
5062 WrapperKind = X86ISD::WrapperRIP;
5063 } else {
5064 assert(model == TLSModel::InitialExec);
5065 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005066 }
Eric Christopherfd179292009-08-27 18:07:15 +00005067
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005068 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5069 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005070 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005071 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005072 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005073
Rafael Espindola9a580232009-02-27 13:37:18 +00005074 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005075 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005076 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005077
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005078 // The address of the thread local variable is the add of the thread
5079 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005080 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005081}
5082
Dan Gohman475871a2008-07-27 21:46:04 +00005083SDValue
5084X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005085 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005086 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005087 assert(Subtarget->isTargetELF() &&
5088 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005089 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005090 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005091
Chris Lattnerb903bed2009-06-26 21:20:29 +00005092 // If GV is an alias then use the aliasee for determining
5093 // thread-localness.
5094 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5095 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Chris Lattnerb903bed2009-06-26 21:20:29 +00005097 TLSModel::Model model = getTLSModel(GV,
5098 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005099
Chris Lattnerb903bed2009-06-26 21:20:29 +00005100 switch (model) {
5101 case TLSModel::GeneralDynamic:
5102 case TLSModel::LocalDynamic: // not implemented
5103 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005104 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005105 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005106
Chris Lattnerb903bed2009-06-26 21:20:29 +00005107 case TLSModel::InitialExec:
5108 case TLSModel::LocalExec:
5109 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5110 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005111 }
Eric Christopherfd179292009-08-27 18:07:15 +00005112
Torok Edwinc23197a2009-07-14 16:55:14 +00005113 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005114 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005115}
5116
Evan Cheng0db9fe62006-04-25 20:13:52 +00005117
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005118/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005119/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005120SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005121 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005122 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005123 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005124 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005125 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005126 SDValue ShOpLo = Op.getOperand(0);
5127 SDValue ShOpHi = Op.getOperand(1);
5128 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005129 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005130 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005131 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005132
Dan Gohman475871a2008-07-27 21:46:04 +00005133 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005134 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005135 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5136 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005137 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005138 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5139 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005140 }
Evan Chenge3413162006-01-09 18:33:28 +00005141
Owen Anderson825b72b2009-08-11 20:47:22 +00005142 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5143 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005144 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005146
Dan Gohman475871a2008-07-27 21:46:04 +00005147 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005149 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5150 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005151
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005152 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005153 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5154 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005155 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005156 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5157 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005158 }
5159
Dan Gohman475871a2008-07-27 21:46:04 +00005160 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005161 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005162}
Evan Chenga3195e82006-01-12 22:54:21 +00005163
Dan Gohman475871a2008-07-27 21:46:04 +00005164SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005165 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005166
5167 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005168 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005169 return Op;
5170 }
5171 return SDValue();
5172 }
5173
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005175 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005176
Eli Friedman36df4992009-05-27 00:47:34 +00005177 // These are really Legal; return the operand so the caller accepts it as
5178 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005180 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005182 Subtarget->is64Bit()) {
5183 return Op;
5184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005185
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005186 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005187 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005188 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005189 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005191 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005192 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005193 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005194 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5195}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005196
Owen Andersone50ed302009-08-10 22:56:29 +00005197SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005198 SDValue StackSlot,
5199 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005200 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005201 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005202 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005203 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005204 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005205 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005206 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005207 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005208 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005209 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005210 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005212 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005213 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005214 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005215
5216 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5217 // shouldn't be necessary except that RFP cannot be live across
5218 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005219 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005220 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005221 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005222 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005223 SDValue Ops[] = {
5224 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5225 };
5226 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005227 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005228 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005229 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005230
Evan Cheng0db9fe62006-04-25 20:13:52 +00005231 return Result;
5232}
5233
Bill Wendling8b8a6362009-01-17 03:56:04 +00005234// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5235SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5236 // This algorithm is not obvious. Here it is in C code, more or less:
5237 /*
5238 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5239 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5240 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005241
Bill Wendling8b8a6362009-01-17 03:56:04 +00005242 // Copy ints to xmm registers.
5243 __m128i xh = _mm_cvtsi32_si128( hi );
5244 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005245
Bill Wendling8b8a6362009-01-17 03:56:04 +00005246 // Combine into low half of a single xmm register.
5247 __m128i x = _mm_unpacklo_epi32( xh, xl );
5248 __m128d d;
5249 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005250
Bill Wendling8b8a6362009-01-17 03:56:04 +00005251 // Merge in appropriate exponents to give the integer bits the right
5252 // magnitude.
5253 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005254
Bill Wendling8b8a6362009-01-17 03:56:04 +00005255 // Subtract away the biases to deal with the IEEE-754 double precision
5256 // implicit 1.
5257 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005258
Bill Wendling8b8a6362009-01-17 03:56:04 +00005259 // All conversions up to here are exact. The correctly rounded result is
5260 // calculated using the current rounding mode using the following
5261 // horizontal add.
5262 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5263 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5264 // store doesn't really need to be here (except
5265 // maybe to zero the other double)
5266 return sd;
5267 }
5268 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005269
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005270 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005271 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005272
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005273 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005274 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005275 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5276 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5277 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5278 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005279 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005280 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005281
Bill Wendling8b8a6362009-01-17 03:56:04 +00005282 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005283 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005284 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005285 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005286 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005287 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005288 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005289
Owen Anderson825b72b2009-08-11 20:47:22 +00005290 SDValue XR1 = 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(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005294 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5295 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005296 Op.getOperand(0),
5297 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5299 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005300 PseudoSourceValue::getConstantPool(), 0,
5301 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5303 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5304 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005305 PseudoSourceValue::getConstantPool(), 0,
5306 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005307 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005308
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005309 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005310 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5312 DAG.getUNDEF(MVT::v2f64), ShufMask);
5313 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5314 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005315 DAG.getIntPtrConstant(0));
5316}
5317
Bill Wendling8b8a6362009-01-17 03:56:04 +00005318// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5319SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005320 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005321 // FP constant to bias correct the final result.
5322 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005323 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005324
5325 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005326 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5327 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005328 Op.getOperand(0),
5329 DAG.getIntPtrConstant(0)));
5330
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5332 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005333 DAG.getIntPtrConstant(0));
5334
5335 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005336 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5337 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005338 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005339 MVT::v2f64, Load)),
5340 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005341 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 MVT::v2f64, Bias)));
5343 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5344 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005345 DAG.getIntPtrConstant(0));
5346
5347 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005348 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005349
5350 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005351 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005352
Owen Anderson825b72b2009-08-11 20:47:22 +00005353 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005354 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005355 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005356 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005357 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005358 }
5359
5360 // Handle final rounding.
5361 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005362}
5363
5364SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005365 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005366 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005367
Evan Chenga06ec9e2009-01-19 08:08:22 +00005368 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5369 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5370 // the optimization here.
5371 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005372 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005373
Owen Andersone50ed302009-08-10 22:56:29 +00005374 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005375 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005376 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005377 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005378 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005379
Bill Wendling8b8a6362009-01-17 03:56:04 +00005380 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005382 return LowerUINT_TO_FP_i32(Op, DAG);
5383 }
5384
Owen Anderson825b72b2009-08-11 20:47:22 +00005385 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005386
5387 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005388 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005389 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5390 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5391 getPointerTy(), StackSlot, WordOff);
5392 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5393 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005394 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005395 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005396 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005397}
5398
Dan Gohman475871a2008-07-27 21:46:04 +00005399std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005400FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005401 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005402
Owen Andersone50ed302009-08-10 22:56:29 +00005403 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005404
5405 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005406 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5407 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005408 }
5409
Owen Anderson825b72b2009-08-11 20:47:22 +00005410 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5411 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005412 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005413
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005414 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005416 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005417 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005418 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005420 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005421 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005422
Evan Cheng87c89352007-10-15 20:11:21 +00005423 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5424 // stack slot.
5425 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005426 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005427 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005429
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005431 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005432 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005433 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5434 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5435 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005437
Dan Gohman475871a2008-07-27 21:46:04 +00005438 SDValue Chain = DAG.getEntryNode();
5439 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005440 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005441 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005442 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005443 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005445 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005446 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5447 };
Dale Johannesenace16102009-02-03 19:33:06 +00005448 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005450 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005451 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5452 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005453
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005455 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005457
Chris Lattner27a6c732007-11-24 07:07:01 +00005458 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459}
5460
Dan Gohman475871a2008-07-27 21:46:04 +00005461SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005462 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005463 if (Op.getValueType() == MVT::v2i32 &&
5464 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005465 return Op;
5466 }
5467 return SDValue();
5468 }
5469
Eli Friedman948e95a2009-05-23 09:59:16 +00005470 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005471 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005472 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5473 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005474
Chris Lattner27a6c732007-11-24 07:07:01 +00005475 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005476 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005477 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005478}
5479
Eli Friedman948e95a2009-05-23 09:59:16 +00005480SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5481 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5482 SDValue FIST = Vals.first, StackSlot = Vals.second;
5483 assert(FIST.getNode() && "Unexpected failure");
5484
5485 // Load the result.
5486 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5487 FIST, StackSlot, NULL, 0);
5488}
5489
Dan Gohman475871a2008-07-27 21:46:04 +00005490SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005491 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005492 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005493 EVT VT = Op.getValueType();
5494 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005495 if (VT.isVector())
5496 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005497 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005499 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005500 CV.push_back(C);
5501 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005502 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005503 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005504 CV.push_back(C);
5505 CV.push_back(C);
5506 CV.push_back(C);
5507 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005508 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005509 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005510 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005511 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005512 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005513 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005514 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515}
5516
Dan Gohman475871a2008-07-27 21:46:04 +00005517SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005518 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005519 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005520 EVT VT = Op.getValueType();
5521 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005522 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005523 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005525 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005526 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005527 CV.push_back(C);
5528 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005529 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005530 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005531 CV.push_back(C);
5532 CV.push_back(C);
5533 CV.push_back(C);
5534 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005535 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005536 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005537 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005538 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005539 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005540 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005541 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005542 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005543 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5544 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005545 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005547 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005548 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005549 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005550}
5551
Dan Gohman475871a2008-07-27 21:46:04 +00005552SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005553 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005554 SDValue Op0 = Op.getOperand(0);
5555 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005556 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005557 EVT VT = Op.getValueType();
5558 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005559
5560 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005561 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005562 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005563 SrcVT = VT;
5564 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005565 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005566 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005567 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005568 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005569 }
5570
5571 // At this point the operands and the result should have the same
5572 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005573
Evan Cheng68c47cb2007-01-05 07:55:56 +00005574 // First get the sign bit of second operand.
5575 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005577 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5578 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005579 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005580 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5581 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5582 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5583 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005584 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005585 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005586 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005587 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005588 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005589 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005590 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005591
5592 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005593 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 // Op0 is MVT::f32, Op1 is MVT::f64.
5595 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5596 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5597 DAG.getConstant(32, MVT::i32));
5598 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5599 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005600 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005601 }
5602
Evan Cheng73d6cf12007-01-05 21:37:56 +00005603 // Clear first operand sign bit.
5604 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005605 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005606 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5607 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005608 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005609 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5610 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5611 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5612 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005613 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005614 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005615 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005616 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005617 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005618 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005619 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005620
5621 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005622 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005623}
5624
Dan Gohman076aee32009-03-04 19:44:21 +00005625/// Emit nodes that will be selected as "test Op0,Op0", or something
5626/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005627SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5628 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005629 DebugLoc dl = Op.getDebugLoc();
5630
Dan Gohman31125812009-03-07 01:58:32 +00005631 // CF and OF aren't always set the way we want. Determine which
5632 // of these we need.
5633 bool NeedCF = false;
5634 bool NeedOF = false;
5635 switch (X86CC) {
5636 case X86::COND_A: case X86::COND_AE:
5637 case X86::COND_B: case X86::COND_BE:
5638 NeedCF = true;
5639 break;
5640 case X86::COND_G: case X86::COND_GE:
5641 case X86::COND_L: case X86::COND_LE:
5642 case X86::COND_O: case X86::COND_NO:
5643 NeedOF = true;
5644 break;
5645 default: break;
5646 }
5647
Dan Gohman076aee32009-03-04 19:44:21 +00005648 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005649 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5650 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5651 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005652 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005653 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005654 switch (Op.getNode()->getOpcode()) {
5655 case ISD::ADD:
5656 // Due to an isel shortcoming, be conservative if this add is likely to
5657 // be selected as part of a load-modify-store instruction. When the root
5658 // node in a match is a store, isel doesn't know how to remap non-chain
5659 // non-flag uses of other nodes in the match, such as the ADD in this
5660 // case. This leads to the ADD being left around and reselected, with
5661 // the result being two adds in the output.
5662 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5663 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5664 if (UI->getOpcode() == ISD::STORE)
5665 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005666 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005667 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5668 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005669 if (C->getAPIntValue() == 1) {
5670 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005671 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005672 break;
5673 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005674 // An add of negative one (subtract of one) will be selected as a DEC.
5675 if (C->getAPIntValue().isAllOnesValue()) {
5676 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005677 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005678 break;
5679 }
5680 }
Dan Gohman076aee32009-03-04 19:44:21 +00005681 // Otherwise use a regular EFLAGS-setting add.
5682 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005683 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005684 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005685 case ISD::AND: {
5686 // If the primary and result isn't used, don't bother using X86ISD::AND,
5687 // because a TEST instruction will be better.
5688 bool NonFlagUse = false;
5689 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005690 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5691 SDNode *User = *UI;
5692 unsigned UOpNo = UI.getOperandNo();
5693 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5694 // Look pass truncate.
5695 UOpNo = User->use_begin().getOperandNo();
5696 User = *User->use_begin();
5697 }
5698 if (User->getOpcode() != ISD::BRCOND &&
5699 User->getOpcode() != ISD::SETCC &&
5700 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005701 NonFlagUse = true;
5702 break;
5703 }
Evan Cheng17751da2010-01-07 00:54:06 +00005704 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005705 if (!NonFlagUse)
5706 break;
5707 }
5708 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005709 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005710 case ISD::OR:
5711 case ISD::XOR:
5712 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005713 // likely to be selected as part of a load-modify-store instruction.
5714 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5715 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5716 if (UI->getOpcode() == ISD::STORE)
5717 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005718 // Otherwise use a regular EFLAGS-setting instruction.
5719 switch (Op.getNode()->getOpcode()) {
5720 case ISD::SUB: Opcode = X86ISD::SUB; break;
5721 case ISD::OR: Opcode = X86ISD::OR; break;
5722 case ISD::XOR: Opcode = X86ISD::XOR; break;
5723 case ISD::AND: Opcode = X86ISD::AND; break;
5724 default: llvm_unreachable("unexpected operator!");
5725 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005726 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005727 break;
5728 case X86ISD::ADD:
5729 case X86ISD::SUB:
5730 case X86ISD::INC:
5731 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005732 case X86ISD::OR:
5733 case X86ISD::XOR:
5734 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005735 return SDValue(Op.getNode(), 1);
5736 default:
5737 default_case:
5738 break;
5739 }
5740 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005741 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005742 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005743 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005744 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005745 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005746 DAG.ReplaceAllUsesWith(Op, New);
5747 return SDValue(New.getNode(), 1);
5748 }
5749 }
5750
5751 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005753 DAG.getConstant(0, Op.getValueType()));
5754}
5755
5756/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5757/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005758SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5759 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005760 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5761 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005762 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005763
5764 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005766}
5767
Evan Chengd40d03e2010-01-06 19:38:29 +00005768/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5769/// if it's possible.
5770static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005771 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005772 SDValue LHS, RHS;
5773 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5774 if (ConstantSDNode *Op010C =
5775 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5776 if (Op010C->getZExtValue() == 1) {
5777 LHS = Op0.getOperand(0);
5778 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005779 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005780 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5781 if (ConstantSDNode *Op000C =
5782 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5783 if (Op000C->getZExtValue() == 1) {
5784 LHS = Op0.getOperand(1);
5785 RHS = Op0.getOperand(0).getOperand(1);
5786 }
5787 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5788 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5789 SDValue AndLHS = Op0.getOperand(0);
5790 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5791 LHS = AndLHS.getOperand(0);
5792 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005793 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005794 }
Evan Cheng0488db92007-09-25 01:57:46 +00005795
Evan Chengd40d03e2010-01-06 19:38:29 +00005796 if (LHS.getNode()) {
5797 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5798 // instruction. Since the shift amount is in-range-or-undefined, we know
5799 // that doing a bittest on the i16 value is ok. We extend to i32 because
5800 // the encoding for the i16 version is larger than the i32 version.
5801 if (LHS.getValueType() == MVT::i8)
5802 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005803
Evan Chengd40d03e2010-01-06 19:38:29 +00005804 // If the operand types disagree, extend the shift amount to match. Since
5805 // BT ignores high bits (like shifts) we can use anyextend.
5806 if (LHS.getValueType() != RHS.getValueType())
5807 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005808
Evan Chengd40d03e2010-01-06 19:38:29 +00005809 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5810 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5811 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5812 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005813 }
5814
Evan Cheng54de3ea2010-01-05 06:52:31 +00005815 return SDValue();
5816}
5817
5818SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5819 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5820 SDValue Op0 = Op.getOperand(0);
5821 SDValue Op1 = Op.getOperand(1);
5822 DebugLoc dl = Op.getDebugLoc();
5823 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5824
5825 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005826 // Lower (X & (1 << N)) == 0 to BT(X, N).
5827 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5828 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5829 if (Op0.getOpcode() == ISD::AND &&
5830 Op0.hasOneUse() &&
5831 Op1.getOpcode() == ISD::Constant &&
5832 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5833 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5834 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5835 if (NewSetCC.getNode())
5836 return NewSetCC;
5837 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005838
Chris Lattnere55484e2008-12-25 05:34:37 +00005839 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5840 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005841 if (X86CC == X86::COND_INVALID)
5842 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005843
Dan Gohman31125812009-03-07 01:58:32 +00005844 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005845
5846 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005847 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005848 return DAG.getNode(ISD::AND, dl, MVT::i8,
5849 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5850 DAG.getConstant(X86CC, MVT::i8), Cond),
5851 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005852
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5854 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005855}
5856
Dan Gohman475871a2008-07-27 21:46:04 +00005857SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5858 SDValue Cond;
5859 SDValue Op0 = Op.getOperand(0);
5860 SDValue Op1 = Op.getOperand(1);
5861 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005862 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005863 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5864 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005865 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005866
5867 if (isFP) {
5868 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005869 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005870 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5871 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005872 bool Swap = false;
5873
5874 switch (SetCCOpcode) {
5875 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005876 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005877 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005878 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005879 case ISD::SETGT: Swap = true; // Fallthrough
5880 case ISD::SETLT:
5881 case ISD::SETOLT: SSECC = 1; break;
5882 case ISD::SETOGE:
5883 case ISD::SETGE: Swap = true; // Fallthrough
5884 case ISD::SETLE:
5885 case ISD::SETOLE: SSECC = 2; break;
5886 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005887 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005888 case ISD::SETNE: SSECC = 4; break;
5889 case ISD::SETULE: Swap = true;
5890 case ISD::SETUGE: SSECC = 5; break;
5891 case ISD::SETULT: Swap = true;
5892 case ISD::SETUGT: SSECC = 6; break;
5893 case ISD::SETO: SSECC = 7; break;
5894 }
5895 if (Swap)
5896 std::swap(Op0, Op1);
5897
Nate Begemanfb8ead02008-07-25 19:05:58 +00005898 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005899 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005900 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005901 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5903 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005904 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005905 }
5906 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005907 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5909 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005910 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005911 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005912 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005913 }
5914 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005915 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005916 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005917
Nate Begeman30a0de92008-07-17 16:51:19 +00005918 // We are handling one of the integer comparisons here. Since SSE only has
5919 // GT and EQ comparisons for integer, swapping operands and multiple
5920 // operations may be required for some comparisons.
5921 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5922 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005923
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005925 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005926 case MVT::v8i8:
5927 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5928 case MVT::v4i16:
5929 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5930 case MVT::v2i32:
5931 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5932 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005934
Nate Begeman30a0de92008-07-17 16:51:19 +00005935 switch (SetCCOpcode) {
5936 default: break;
5937 case ISD::SETNE: Invert = true;
5938 case ISD::SETEQ: Opc = EQOpc; break;
5939 case ISD::SETLT: Swap = true;
5940 case ISD::SETGT: Opc = GTOpc; break;
5941 case ISD::SETGE: Swap = true;
5942 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5943 case ISD::SETULT: Swap = true;
5944 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5945 case ISD::SETUGE: Swap = true;
5946 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5947 }
5948 if (Swap)
5949 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005950
Nate Begeman30a0de92008-07-17 16:51:19 +00005951 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5952 // bits of the inputs before performing those operations.
5953 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005954 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005955 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5956 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005957 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005958 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5959 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005960 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5961 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005962 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005963
Dale Johannesenace16102009-02-03 19:33:06 +00005964 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005965
5966 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005967 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005968 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005969
Nate Begeman30a0de92008-07-17 16:51:19 +00005970 return Result;
5971}
Evan Cheng0488db92007-09-25 01:57:46 +00005972
Evan Cheng370e5342008-12-03 08:38:43 +00005973// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005974static bool isX86LogicalCmp(SDValue Op) {
5975 unsigned Opc = Op.getNode()->getOpcode();
5976 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5977 return true;
5978 if (Op.getResNo() == 1 &&
5979 (Opc == X86ISD::ADD ||
5980 Opc == X86ISD::SUB ||
5981 Opc == X86ISD::SMUL ||
5982 Opc == X86ISD::UMUL ||
5983 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005984 Opc == X86ISD::DEC ||
5985 Opc == X86ISD::OR ||
5986 Opc == X86ISD::XOR ||
5987 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005988 return true;
5989
5990 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005991}
5992
Dan Gohman475871a2008-07-27 21:46:04 +00005993SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005994 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005995 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005996 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005997 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005998
Dan Gohman1a492952009-10-20 16:22:37 +00005999 if (Cond.getOpcode() == ISD::SETCC) {
6000 SDValue NewCond = LowerSETCC(Cond, DAG);
6001 if (NewCond.getNode())
6002 Cond = NewCond;
6003 }
Evan Cheng734503b2006-09-11 02:19:56 +00006004
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006005 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6006 SDValue Op1 = Op.getOperand(1);
6007 SDValue Op2 = Op.getOperand(2);
6008 if (Cond.getOpcode() == X86ISD::SETCC &&
6009 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6010 SDValue Cmp = Cond.getOperand(1);
6011 if (Cmp.getOpcode() == X86ISD::CMP) {
6012 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6013 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6014 ConstantSDNode *RHSC =
6015 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6016 if (N1C && N1C->isAllOnesValue() &&
6017 N2C && N2C->isNullValue() &&
6018 RHSC && RHSC->isNullValue()) {
6019 SDValue CmpOp0 = Cmp.getOperand(0);
6020 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
6021 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6022 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6023 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6024 }
6025 }
6026 }
6027
Evan Chengad9c0a32009-12-15 00:53:42 +00006028 // Look pass (and (setcc_carry (cmp ...)), 1).
6029 if (Cond.getOpcode() == ISD::AND &&
6030 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6031 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6032 if (C && C->getAPIntValue() == 1)
6033 Cond = Cond.getOperand(0);
6034 }
6035
Evan Cheng3f41d662007-10-08 22:16:29 +00006036 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6037 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006038 if (Cond.getOpcode() == X86ISD::SETCC ||
6039 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006040 CC = Cond.getOperand(0);
6041
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006043 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006044 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006045
Evan Cheng3f41d662007-10-08 22:16:29 +00006046 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006047 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006048 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006049 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006050
Chris Lattnerd1980a52009-03-12 06:52:53 +00006051 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6052 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006053 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006054 addTest = false;
6055 }
6056 }
6057
6058 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006059 // Look pass the truncate.
6060 if (Cond.getOpcode() == ISD::TRUNCATE)
6061 Cond = Cond.getOperand(0);
6062
6063 // We know the result of AND is compared against zero. Try to match
6064 // it to BT.
6065 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6066 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6067 if (NewSetCC.getNode()) {
6068 CC = NewSetCC.getOperand(0);
6069 Cond = NewSetCC.getOperand(1);
6070 addTest = false;
6071 }
6072 }
6073 }
6074
6075 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006076 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006077 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006078 }
6079
Evan Cheng0488db92007-09-25 01:57:46 +00006080 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6081 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006082 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6083 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006084 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006085}
6086
Evan Cheng370e5342008-12-03 08:38:43 +00006087// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6088// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6089// from the AND / OR.
6090static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6091 Opc = Op.getOpcode();
6092 if (Opc != ISD::OR && Opc != ISD::AND)
6093 return false;
6094 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6095 Op.getOperand(0).hasOneUse() &&
6096 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6097 Op.getOperand(1).hasOneUse());
6098}
6099
Evan Cheng961d6d42009-02-02 08:19:07 +00006100// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6101// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006102static bool isXor1OfSetCC(SDValue Op) {
6103 if (Op.getOpcode() != ISD::XOR)
6104 return false;
6105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6106 if (N1C && N1C->getAPIntValue() == 1) {
6107 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6108 Op.getOperand(0).hasOneUse();
6109 }
6110 return false;
6111}
6112
Dan Gohman475871a2008-07-27 21:46:04 +00006113SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006114 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006115 SDValue Chain = Op.getOperand(0);
6116 SDValue Cond = Op.getOperand(1);
6117 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006118 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006119 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006120
Dan Gohman1a492952009-10-20 16:22:37 +00006121 if (Cond.getOpcode() == ISD::SETCC) {
6122 SDValue NewCond = LowerSETCC(Cond, DAG);
6123 if (NewCond.getNode())
6124 Cond = NewCond;
6125 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006126#if 0
6127 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006128 else if (Cond.getOpcode() == X86ISD::ADD ||
6129 Cond.getOpcode() == X86ISD::SUB ||
6130 Cond.getOpcode() == X86ISD::SMUL ||
6131 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006132 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006133#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006134
Evan Chengad9c0a32009-12-15 00:53:42 +00006135 // Look pass (and (setcc_carry (cmp ...)), 1).
6136 if (Cond.getOpcode() == ISD::AND &&
6137 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6138 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6139 if (C && C->getAPIntValue() == 1)
6140 Cond = Cond.getOperand(0);
6141 }
6142
Evan Cheng3f41d662007-10-08 22:16:29 +00006143 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6144 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006145 if (Cond.getOpcode() == X86ISD::SETCC ||
6146 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006147 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006148
Dan Gohman475871a2008-07-27 21:46:04 +00006149 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006150 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006151 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006152 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006153 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006154 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006155 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006156 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006157 default: break;
6158 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006159 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006160 // These can only come from an arithmetic instruction with overflow,
6161 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006162 Cond = Cond.getNode()->getOperand(1);
6163 addTest = false;
6164 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006165 }
Evan Cheng0488db92007-09-25 01:57:46 +00006166 }
Evan Cheng370e5342008-12-03 08:38:43 +00006167 } else {
6168 unsigned CondOpc;
6169 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6170 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006171 if (CondOpc == ISD::OR) {
6172 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6173 // two branches instead of an explicit OR instruction with a
6174 // separate test.
6175 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006176 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006177 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006178 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006179 Chain, Dest, CC, Cmp);
6180 CC = Cond.getOperand(1).getOperand(0);
6181 Cond = Cmp;
6182 addTest = false;
6183 }
6184 } else { // ISD::AND
6185 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6186 // two branches instead of an explicit AND instruction with a
6187 // separate test. However, we only do this if this block doesn't
6188 // have a fall-through edge, because this requires an explicit
6189 // jmp when the condition is false.
6190 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006191 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006192 Op.getNode()->hasOneUse()) {
6193 X86::CondCode CCode =
6194 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6195 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006196 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006197 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6198 // Look for an unconditional branch following this conditional branch.
6199 // We need this because we need to reverse the successors in order
6200 // to implement FCMP_OEQ.
6201 if (User.getOpcode() == ISD::BR) {
6202 SDValue FalseBB = User.getOperand(1);
6203 SDValue NewBR =
6204 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6205 assert(NewBR == User);
6206 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006207
Dale Johannesene4d209d2009-02-03 20:21:25 +00006208 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006209 Chain, Dest, CC, Cmp);
6210 X86::CondCode CCode =
6211 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6212 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006213 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006214 Cond = Cmp;
6215 addTest = false;
6216 }
6217 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006218 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006219 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6220 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6221 // It should be transformed during dag combiner except when the condition
6222 // is set by a arithmetics with overflow node.
6223 X86::CondCode CCode =
6224 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6225 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006226 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006227 Cond = Cond.getOperand(0).getOperand(1);
6228 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006229 }
Evan Cheng0488db92007-09-25 01:57:46 +00006230 }
6231
6232 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006233 // Look pass the truncate.
6234 if (Cond.getOpcode() == ISD::TRUNCATE)
6235 Cond = Cond.getOperand(0);
6236
6237 // We know the result of AND is compared against zero. Try to match
6238 // it to BT.
6239 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6240 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6241 if (NewSetCC.getNode()) {
6242 CC = NewSetCC.getOperand(0);
6243 Cond = NewSetCC.getOperand(1);
6244 addTest = false;
6245 }
6246 }
6247 }
6248
6249 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006250 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006251 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006252 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006253 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006254 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006255}
6256
Anton Korobeynikove060b532007-04-17 19:34:00 +00006257
6258// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6259// Calls to _alloca is needed to probe the stack when allocating more than 4k
6260// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6261// that the guard pages used by the OS virtual memory manager are allocated in
6262// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006263SDValue
6264X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006265 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006266 assert(Subtarget->isTargetCygMing() &&
6267 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006268 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006269
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006270 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue Chain = Op.getOperand(0);
6272 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006273 // FIXME: Ensure alignment here
6274
Dan Gohman475871a2008-07-27 21:46:04 +00006275 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006276
Owen Andersone50ed302009-08-10 22:56:29 +00006277 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006278 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006279
Chris Lattnere563bbc2008-10-11 22:08:30 +00006280 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006281
Dale Johannesendd64c412009-02-04 00:33:20 +00006282 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006283 Flag = Chain.getValue(1);
6284
Owen Anderson825b72b2009-08-11 20:47:22 +00006285 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006287 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006288 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006289 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006290 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006291 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006292 Flag = Chain.getValue(1);
6293
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006294 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006295 DAG.getIntPtrConstant(0, true),
6296 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006297 Flag);
6298
Dale Johannesendd64c412009-02-04 00:33:20 +00006299 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006300
Dan Gohman475871a2008-07-27 21:46:04 +00006301 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006302 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006303}
6304
Dan Gohman475871a2008-07-27 21:46:04 +00006305SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006306X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006307 SDValue Chain,
6308 SDValue Dst, SDValue Src,
6309 SDValue Size, unsigned Align,
6310 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006311 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006312 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006313
Bill Wendling6f287b22008-09-30 21:22:07 +00006314 // If not DWORD aligned or size is more than the threshold, call the library.
6315 // The libc version is likely to be faster for these cases. It can use the
6316 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006317 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006318 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006319 ConstantSize->getZExtValue() >
6320 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006322
6323 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006324 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006325
Bill Wendling6158d842008-10-01 00:59:58 +00006326 if (const char *bzeroEntry = V &&
6327 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006328 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006329 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006330 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006331 TargetLowering::ArgListEntry Entry;
6332 Entry.Node = Dst;
6333 Entry.Ty = IntPtrTy;
6334 Args.push_back(Entry);
6335 Entry.Node = Size;
6336 Args.push_back(Entry);
6337 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006338 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6339 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006340 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006341 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6342 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006343 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006344 }
6345
Dan Gohman707e0182008-04-12 04:36:06 +00006346 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006347 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006348 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006349
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006350 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006351 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006352 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006353 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006354 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006355 unsigned BytesLeft = 0;
6356 bool TwoRepStos = false;
6357 if (ValC) {
6358 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006359 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006360
Evan Cheng0db9fe62006-04-25 20:13:52 +00006361 // If the value is a constant, then we can potentially use larger sets.
6362 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006363 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006364 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006365 ValReg = X86::AX;
6366 Val = (Val << 8) | Val;
6367 break;
6368 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006369 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006370 ValReg = X86::EAX;
6371 Val = (Val << 8) | Val;
6372 Val = (Val << 16) | Val;
6373 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006374 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006375 ValReg = X86::RAX;
6376 Val = (Val << 32) | Val;
6377 }
6378 break;
6379 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006380 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006381 ValReg = X86::AL;
6382 Count = DAG.getIntPtrConstant(SizeVal);
6383 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006384 }
6385
Owen Anderson825b72b2009-08-11 20:47:22 +00006386 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006387 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006388 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6389 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006390 }
6391
Dale Johannesen0f502f62009-02-03 22:26:09 +00006392 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006393 InFlag);
6394 InFlag = Chain.getValue(1);
6395 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006397 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006398 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006399 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006400 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006401
Scott Michelfdc40a02009-02-17 22:15:04 +00006402 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006403 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006404 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006405 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006406 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006407 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006408 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006409 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006410
Owen Anderson825b72b2009-08-11 20:47:22 +00006411 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006412 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6413 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006414
Evan Cheng0db9fe62006-04-25 20:13:52 +00006415 if (TwoRepStos) {
6416 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006417 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006418 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006419 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006420 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6421 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006422 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006423 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006424 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006425 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006426 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6427 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006428 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006429 // Handle the last 1 - 7 bytes.
6430 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006431 EVT AddrVT = Dst.getValueType();
6432 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006433
Dale Johannesen0f502f62009-02-03 22:26:09 +00006434 Chain = DAG.getMemset(Chain, dl,
6435 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006436 DAG.getConstant(Offset, AddrVT)),
6437 Src,
6438 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006439 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006440 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006441
Dan Gohman707e0182008-04-12 04:36:06 +00006442 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006443 return Chain;
6444}
Evan Cheng11e15b32006-04-03 20:53:28 +00006445
Dan Gohman475871a2008-07-27 21:46:04 +00006446SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006447X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006448 SDValue Chain, SDValue Dst, SDValue Src,
6449 SDValue Size, unsigned Align,
6450 bool AlwaysInline,
6451 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006452 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006453 // This requires the copy size to be a constant, preferrably
6454 // within a subtarget-specific limit.
6455 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6456 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006457 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006458 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006459 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006460 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006461
Evan Cheng1887c1c2008-08-21 21:00:15 +00006462 /// If not DWORD aligned, call the library.
6463 if ((Align & 3) != 0)
6464 return SDValue();
6465
6466 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006467 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006468 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006469 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006470
Duncan Sands83ec4b62008-06-06 12:08:01 +00006471 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006472 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006473 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006474 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006475
Dan Gohman475871a2008-07-27 21:46:04 +00006476 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006477 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006478 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006479 Count, 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::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006482 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006483 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006484 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006485 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006486 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006487 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006488 InFlag = Chain.getValue(1);
6489
Owen Anderson825b72b2009-08-11 20:47:22 +00006490 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006491 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6492 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6493 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006494
Dan Gohman475871a2008-07-27 21:46:04 +00006495 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006496 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006497 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006498 // Handle the last 1 - 7 bytes.
6499 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006500 EVT DstVT = Dst.getValueType();
6501 EVT SrcVT = Src.getValueType();
6502 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006503 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006504 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006505 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006506 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006507 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006508 DAG.getConstant(BytesLeft, SizeVT),
6509 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006510 DstSV, DstSVOff + Offset,
6511 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006512 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006513
Owen Anderson825b72b2009-08-11 20:47:22 +00006514 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006515 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006516}
6517
Dan Gohman475871a2008-07-27 21:46:04 +00006518SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006519 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006520 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006521
Evan Cheng25ab6902006-09-08 06:48:29 +00006522 if (!Subtarget->is64Bit()) {
6523 // vastart just stores the address of the VarArgsFrameIndex slot into the
6524 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006525 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006526 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006527 }
6528
6529 // __va_list_tag:
6530 // gp_offset (0 - 6 * 8)
6531 // fp_offset (48 - 48 + 8 * 16)
6532 // overflow_arg_area (point to parameters coming in memory).
6533 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006534 SmallVector<SDValue, 8> MemOps;
6535 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006536 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006537 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006538 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006539 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006540 MemOps.push_back(Store);
6541
6542 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006543 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006544 FIN, DAG.getIntPtrConstant(4));
6545 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006546 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006547 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006548 MemOps.push_back(Store);
6549
6550 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006551 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006552 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006553 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006554 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006555 MemOps.push_back(Store);
6556
6557 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006558 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006559 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006560 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006561 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006562 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006563 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006564 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006565}
6566
Dan Gohman475871a2008-07-27 21:46:04 +00006567SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006568 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6569 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006570 SDValue Chain = Op.getOperand(0);
6571 SDValue SrcPtr = Op.getOperand(1);
6572 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006573
Torok Edwindac237e2009-07-08 20:53:28 +00006574 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006575 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006576}
6577
Dan Gohman475871a2008-07-27 21:46:04 +00006578SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006579 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006580 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006581 SDValue Chain = Op.getOperand(0);
6582 SDValue DstPtr = Op.getOperand(1);
6583 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006584 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6585 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006586 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006587
Dale Johannesendd64c412009-02-04 00:33:20 +00006588 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006589 DAG.getIntPtrConstant(24), 8, false,
6590 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006591}
6592
Dan Gohman475871a2008-07-27 21:46:04 +00006593SDValue
6594X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006595 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006596 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006597 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006598 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006599 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 case Intrinsic::x86_sse_comieq_ss:
6601 case Intrinsic::x86_sse_comilt_ss:
6602 case Intrinsic::x86_sse_comile_ss:
6603 case Intrinsic::x86_sse_comigt_ss:
6604 case Intrinsic::x86_sse_comige_ss:
6605 case Intrinsic::x86_sse_comineq_ss:
6606 case Intrinsic::x86_sse_ucomieq_ss:
6607 case Intrinsic::x86_sse_ucomilt_ss:
6608 case Intrinsic::x86_sse_ucomile_ss:
6609 case Intrinsic::x86_sse_ucomigt_ss:
6610 case Intrinsic::x86_sse_ucomige_ss:
6611 case Intrinsic::x86_sse_ucomineq_ss:
6612 case Intrinsic::x86_sse2_comieq_sd:
6613 case Intrinsic::x86_sse2_comilt_sd:
6614 case Intrinsic::x86_sse2_comile_sd:
6615 case Intrinsic::x86_sse2_comigt_sd:
6616 case Intrinsic::x86_sse2_comige_sd:
6617 case Intrinsic::x86_sse2_comineq_sd:
6618 case Intrinsic::x86_sse2_ucomieq_sd:
6619 case Intrinsic::x86_sse2_ucomilt_sd:
6620 case Intrinsic::x86_sse2_ucomile_sd:
6621 case Intrinsic::x86_sse2_ucomigt_sd:
6622 case Intrinsic::x86_sse2_ucomige_sd:
6623 case Intrinsic::x86_sse2_ucomineq_sd: {
6624 unsigned Opc = 0;
6625 ISD::CondCode CC = ISD::SETCC_INVALID;
6626 switch (IntNo) {
6627 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006628 case Intrinsic::x86_sse_comieq_ss:
6629 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630 Opc = X86ISD::COMI;
6631 CC = ISD::SETEQ;
6632 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006633 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006634 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635 Opc = X86ISD::COMI;
6636 CC = ISD::SETLT;
6637 break;
6638 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006639 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006640 Opc = X86ISD::COMI;
6641 CC = ISD::SETLE;
6642 break;
6643 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006644 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645 Opc = X86ISD::COMI;
6646 CC = ISD::SETGT;
6647 break;
6648 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006649 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006650 Opc = X86ISD::COMI;
6651 CC = ISD::SETGE;
6652 break;
6653 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006654 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655 Opc = X86ISD::COMI;
6656 CC = ISD::SETNE;
6657 break;
6658 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006659 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660 Opc = X86ISD::UCOMI;
6661 CC = ISD::SETEQ;
6662 break;
6663 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006664 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665 Opc = X86ISD::UCOMI;
6666 CC = ISD::SETLT;
6667 break;
6668 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006669 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006670 Opc = X86ISD::UCOMI;
6671 CC = ISD::SETLE;
6672 break;
6673 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006674 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 Opc = X86ISD::UCOMI;
6676 CC = ISD::SETGT;
6677 break;
6678 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006679 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680 Opc = X86ISD::UCOMI;
6681 CC = ISD::SETGE;
6682 break;
6683 case Intrinsic::x86_sse_ucomineq_ss:
6684 case Intrinsic::x86_sse2_ucomineq_sd:
6685 Opc = X86ISD::UCOMI;
6686 CC = ISD::SETNE;
6687 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006688 }
Evan Cheng734503b2006-09-11 02:19:56 +00006689
Dan Gohman475871a2008-07-27 21:46:04 +00006690 SDValue LHS = Op.getOperand(1);
6691 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006692 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006693 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006694 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6695 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6696 DAG.getConstant(X86CC, MVT::i8), Cond);
6697 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006698 }
Eric Christopher71c67532009-07-29 00:28:05 +00006699 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006700 // an integer value, not just an instruction so lower it to the ptest
6701 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006702 case Intrinsic::x86_sse41_ptestz:
6703 case Intrinsic::x86_sse41_ptestc:
6704 case Intrinsic::x86_sse41_ptestnzc:{
6705 unsigned X86CC = 0;
6706 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006707 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006708 case Intrinsic::x86_sse41_ptestz:
6709 // ZF = 1
6710 X86CC = X86::COND_E;
6711 break;
6712 case Intrinsic::x86_sse41_ptestc:
6713 // CF = 1
6714 X86CC = X86::COND_B;
6715 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006716 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006717 // ZF and CF = 0
6718 X86CC = X86::COND_A;
6719 break;
6720 }
Eric Christopherfd179292009-08-27 18:07:15 +00006721
Eric Christopher71c67532009-07-29 00:28:05 +00006722 SDValue LHS = Op.getOperand(1);
6723 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006724 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6725 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6726 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6727 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006728 }
Evan Cheng5759f972008-05-04 09:15:50 +00006729
6730 // Fix vector shift instructions where the last operand is a non-immediate
6731 // i32 value.
6732 case Intrinsic::x86_sse2_pslli_w:
6733 case Intrinsic::x86_sse2_pslli_d:
6734 case Intrinsic::x86_sse2_pslli_q:
6735 case Intrinsic::x86_sse2_psrli_w:
6736 case Intrinsic::x86_sse2_psrli_d:
6737 case Intrinsic::x86_sse2_psrli_q:
6738 case Intrinsic::x86_sse2_psrai_w:
6739 case Intrinsic::x86_sse2_psrai_d:
6740 case Intrinsic::x86_mmx_pslli_w:
6741 case Intrinsic::x86_mmx_pslli_d:
6742 case Intrinsic::x86_mmx_pslli_q:
6743 case Intrinsic::x86_mmx_psrli_w:
6744 case Intrinsic::x86_mmx_psrli_d:
6745 case Intrinsic::x86_mmx_psrli_q:
6746 case Intrinsic::x86_mmx_psrai_w:
6747 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006749 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006750 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006751
6752 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006753 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006754 switch (IntNo) {
6755 case Intrinsic::x86_sse2_pslli_w:
6756 NewIntNo = Intrinsic::x86_sse2_psll_w;
6757 break;
6758 case Intrinsic::x86_sse2_pslli_d:
6759 NewIntNo = Intrinsic::x86_sse2_psll_d;
6760 break;
6761 case Intrinsic::x86_sse2_pslli_q:
6762 NewIntNo = Intrinsic::x86_sse2_psll_q;
6763 break;
6764 case Intrinsic::x86_sse2_psrli_w:
6765 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6766 break;
6767 case Intrinsic::x86_sse2_psrli_d:
6768 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6769 break;
6770 case Intrinsic::x86_sse2_psrli_q:
6771 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6772 break;
6773 case Intrinsic::x86_sse2_psrai_w:
6774 NewIntNo = Intrinsic::x86_sse2_psra_w;
6775 break;
6776 case Intrinsic::x86_sse2_psrai_d:
6777 NewIntNo = Intrinsic::x86_sse2_psra_d;
6778 break;
6779 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006780 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006781 switch (IntNo) {
6782 case Intrinsic::x86_mmx_pslli_w:
6783 NewIntNo = Intrinsic::x86_mmx_psll_w;
6784 break;
6785 case Intrinsic::x86_mmx_pslli_d:
6786 NewIntNo = Intrinsic::x86_mmx_psll_d;
6787 break;
6788 case Intrinsic::x86_mmx_pslli_q:
6789 NewIntNo = Intrinsic::x86_mmx_psll_q;
6790 break;
6791 case Intrinsic::x86_mmx_psrli_w:
6792 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6793 break;
6794 case Intrinsic::x86_mmx_psrli_d:
6795 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6796 break;
6797 case Intrinsic::x86_mmx_psrli_q:
6798 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6799 break;
6800 case Intrinsic::x86_mmx_psrai_w:
6801 NewIntNo = Intrinsic::x86_mmx_psra_w;
6802 break;
6803 case Intrinsic::x86_mmx_psrai_d:
6804 NewIntNo = Intrinsic::x86_mmx_psra_d;
6805 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006806 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006807 }
6808 break;
6809 }
6810 }
Mon P Wangefa42202009-09-03 19:56:25 +00006811
6812 // The vector shift intrinsics with scalars uses 32b shift amounts but
6813 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6814 // to be zero.
6815 SDValue ShOps[4];
6816 ShOps[0] = ShAmt;
6817 ShOps[1] = DAG.getConstant(0, MVT::i32);
6818 if (ShAmtVT == MVT::v4i32) {
6819 ShOps[2] = DAG.getUNDEF(MVT::i32);
6820 ShOps[3] = DAG.getUNDEF(MVT::i32);
6821 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6822 } else {
6823 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6824 }
6825
Owen Andersone50ed302009-08-10 22:56:29 +00006826 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006827 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006828 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006830 Op.getOperand(1), ShAmt);
6831 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006832 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006833}
Evan Cheng72261582005-12-20 06:22:03 +00006834
Dan Gohman475871a2008-07-27 21:46:04 +00006835SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006836 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006837 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006838
6839 if (Depth > 0) {
6840 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6841 SDValue Offset =
6842 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006843 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006844 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006845 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006846 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006847 NULL, 0);
6848 }
6849
6850 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006851 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006852 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006853 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006854}
6855
Dan Gohman475871a2008-07-27 21:46:04 +00006856SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006857 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6858 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006859 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006860 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006861 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6862 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006863 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006864 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006865 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006866 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006867}
6868
Dan Gohman475871a2008-07-27 21:46:04 +00006869SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006870 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006871 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006872}
6873
Dan Gohman475871a2008-07-27 21:46:04 +00006874SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006875{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006876 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006877 SDValue Chain = Op.getOperand(0);
6878 SDValue Offset = Op.getOperand(1);
6879 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006880 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006881
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006882 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6883 getPointerTy());
6884 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006885
Dale Johannesene4d209d2009-02-03 20:21:25 +00006886 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006887 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006888 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6889 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006890 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006891 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006892
Dale Johannesene4d209d2009-02-03 20:21:25 +00006893 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006894 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006895 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006896}
6897
Dan Gohman475871a2008-07-27 21:46:04 +00006898SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006899 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006900 SDValue Root = Op.getOperand(0);
6901 SDValue Trmp = Op.getOperand(1); // trampoline
6902 SDValue FPtr = Op.getOperand(2); // nested function
6903 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006904 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006905
Dan Gohman69de1932008-02-06 22:27:42 +00006906 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006907
Duncan Sands339e14f2008-01-16 22:55:25 +00006908 const X86InstrInfo *TII =
6909 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6910
Duncan Sandsb116fac2007-07-27 20:02:49 +00006911 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006912 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006913
6914 // Large code-model.
6915
6916 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6917 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6918
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006919 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6920 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006921
6922 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6923
6924 // Load the pointer to the nested function into R11.
6925 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006926 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006927 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006928 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006929
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6931 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006932 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006933
6934 // Load the 'nest' parameter value into R10.
6935 // R10 is specified in X86CallingConv.td
6936 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006937 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6938 DAG.getConstant(10, MVT::i64));
6939 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006940 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006941
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6943 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006944 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006945
6946 // Jump to the nested function.
6947 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006948 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6949 DAG.getConstant(20, MVT::i64));
6950 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006951 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006952
6953 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006954 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6955 DAG.getConstant(22, MVT::i64));
6956 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006957 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006958
Dan Gohman475871a2008-07-27 21:46:04 +00006959 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006961 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006962 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006963 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006964 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006965 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006966 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006967
6968 switch (CC) {
6969 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006970 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006971 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006972 case CallingConv::X86_StdCall: {
6973 // Pass 'nest' parameter in ECX.
6974 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006975 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006976
6977 // Check that ECX wasn't needed by an 'inreg' parameter.
6978 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006979 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006980
Chris Lattner58d74912008-03-12 17:45:29 +00006981 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006982 unsigned InRegCount = 0;
6983 unsigned Idx = 1;
6984
6985 for (FunctionType::param_iterator I = FTy->param_begin(),
6986 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006987 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006988 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006989 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006990
6991 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006992 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006993 }
6994 }
6995 break;
6996 }
6997 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006998 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006999 // Pass 'nest' parameter in EAX.
7000 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007001 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007002 break;
7003 }
7004
Dan Gohman475871a2008-07-27 21:46:04 +00007005 SDValue OutChains[4];
7006 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007007
Owen Anderson825b72b2009-08-11 20:47:22 +00007008 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7009 DAG.getConstant(10, MVT::i32));
7010 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007011
Duncan Sands339e14f2008-01-16 22:55:25 +00007012 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007013 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007014 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007015 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00007016 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007017
Owen Anderson825b72b2009-08-11 20:47:22 +00007018 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7019 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007020 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007021
Duncan Sands339e14f2008-01-16 22:55:25 +00007022 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7024 DAG.getConstant(5, MVT::i32));
7025 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00007026 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007027
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7029 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007030 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007031
Dan Gohman475871a2008-07-27 21:46:04 +00007032 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007034 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007035 }
7036}
7037
Dan Gohman475871a2008-07-27 21:46:04 +00007038SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007039 /*
7040 The rounding mode is in bits 11:10 of FPSR, and has the following
7041 settings:
7042 00 Round to nearest
7043 01 Round to -inf
7044 10 Round to +inf
7045 11 Round to 0
7046
7047 FLT_ROUNDS, on the other hand, expects the following:
7048 -1 Undefined
7049 0 Round to 0
7050 1 Round to nearest
7051 2 Round to +inf
7052 3 Round to -inf
7053
7054 To perform the conversion, we do:
7055 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7056 */
7057
7058 MachineFunction &MF = DAG.getMachineFunction();
7059 const TargetMachine &TM = MF.getTarget();
7060 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7061 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007062 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007063 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007064
7065 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007066 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007068
Owen Anderson825b72b2009-08-11 20:47:22 +00007069 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007070 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007071
7072 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007074
7075 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007076 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007077 DAG.getNode(ISD::SRL, dl, MVT::i16,
7078 DAG.getNode(ISD::AND, dl, MVT::i16,
7079 CWD, DAG.getConstant(0x800, MVT::i16)),
7080 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 DAG.getNode(ISD::SRL, dl, MVT::i16,
7083 DAG.getNode(ISD::AND, dl, MVT::i16,
7084 CWD, DAG.getConstant(0x400, MVT::i16)),
7085 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007086
Dan Gohman475871a2008-07-27 21:46:04 +00007087 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 DAG.getNode(ISD::AND, dl, MVT::i16,
7089 DAG.getNode(ISD::ADD, dl, MVT::i16,
7090 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7091 DAG.getConstant(1, MVT::i16)),
7092 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007093
7094
Duncan Sands83ec4b62008-06-06 12:08:01 +00007095 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007096 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007097}
7098
Dan Gohman475871a2008-07-27 21:46:04 +00007099SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007100 EVT VT = Op.getValueType();
7101 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007102 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007103 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007104
7105 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007107 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007109 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007110 }
Evan Cheng18efe262007-12-14 02:13:44 +00007111
Evan Cheng152804e2007-12-14 08:30:15 +00007112 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007114 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007115
7116 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007117 SDValue Ops[] = {
7118 Op,
7119 DAG.getConstant(NumBits+NumBits-1, OpVT),
7120 DAG.getConstant(X86::COND_E, MVT::i8),
7121 Op.getValue(1)
7122 };
7123 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007124
7125 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007126 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007127
Owen Anderson825b72b2009-08-11 20:47:22 +00007128 if (VT == MVT::i8)
7129 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007130 return Op;
7131}
7132
Dan Gohman475871a2008-07-27 21:46:04 +00007133SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007134 EVT VT = Op.getValueType();
7135 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007136 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007137 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007138
7139 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 if (VT == MVT::i8) {
7141 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007142 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007143 }
Evan Cheng152804e2007-12-14 08:30:15 +00007144
7145 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007146 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007148
7149 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007150 SDValue Ops[] = {
7151 Op,
7152 DAG.getConstant(NumBits, OpVT),
7153 DAG.getConstant(X86::COND_E, MVT::i8),
7154 Op.getValue(1)
7155 };
7156 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007157
Owen Anderson825b72b2009-08-11 20:47:22 +00007158 if (VT == MVT::i8)
7159 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007160 return Op;
7161}
7162
Mon P Wangaf9b9522008-12-18 21:42:19 +00007163SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007164 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007166 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007167
Mon P Wangaf9b9522008-12-18 21:42:19 +00007168 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7169 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7170 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7171 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7172 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7173 //
7174 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7175 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7176 // return AloBlo + AloBhi + AhiBlo;
7177
7178 SDValue A = Op.getOperand(0);
7179 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007180
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007182 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7183 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007184 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7186 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007187 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007188 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007189 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007190 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007191 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007192 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007193 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007194 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007195 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007196 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7198 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007199 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007200 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7201 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007202 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7203 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007204 return Res;
7205}
7206
7207
Bill Wendling74c37652008-12-09 22:08:41 +00007208SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7209 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7210 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007211 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7212 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007213 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007214 SDValue LHS = N->getOperand(0);
7215 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007216 unsigned BaseOp = 0;
7217 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007218 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007219
7220 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007221 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007222 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007223 // A subtract of one will be selected as a INC. Note that INC doesn't
7224 // set CF, so we can't do this for UADDO.
7225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7226 if (C->getAPIntValue() == 1) {
7227 BaseOp = X86ISD::INC;
7228 Cond = X86::COND_O;
7229 break;
7230 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007231 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007232 Cond = X86::COND_O;
7233 break;
7234 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007235 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007236 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007237 break;
7238 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007239 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7240 // set CF, so we can't do this for USUBO.
7241 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7242 if (C->getAPIntValue() == 1) {
7243 BaseOp = X86ISD::DEC;
7244 Cond = X86::COND_O;
7245 break;
7246 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007247 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007248 Cond = X86::COND_O;
7249 break;
7250 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007251 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007252 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007253 break;
7254 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007255 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007256 Cond = X86::COND_O;
7257 break;
7258 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007259 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007260 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007261 break;
7262 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007263
Bill Wendling61edeb52008-12-02 01:06:39 +00007264 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007265 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007266 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007267
Bill Wendling61edeb52008-12-02 01:06:39 +00007268 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007269 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007270 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007271
Bill Wendling61edeb52008-12-02 01:06:39 +00007272 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7273 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007274}
7275
Dan Gohman475871a2008-07-27 21:46:04 +00007276SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007277 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007278 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007279 unsigned Reg = 0;
7280 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007281 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007282 default:
7283 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007284 case MVT::i8: Reg = X86::AL; size = 1; break;
7285 case MVT::i16: Reg = X86::AX; size = 2; break;
7286 case MVT::i32: Reg = X86::EAX; size = 4; break;
7287 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007288 assert(Subtarget->is64Bit() && "Node not type legal!");
7289 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007290 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007291 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007292 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007293 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007294 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007295 Op.getOperand(1),
7296 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007297 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007298 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007299 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007300 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007301 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007302 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007303 return cpOut;
7304}
7305
Duncan Sands1607f052008-12-01 11:39:25 +00007306SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007307 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007308 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007310 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007311 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007312 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007313 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7314 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007315 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7317 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007318 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007320 rdx.getValue(1)
7321 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007322 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007323}
7324
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007325SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7326 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007327 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007328 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007329 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007330 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007331 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007332 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007333 Node->getOperand(0),
7334 Node->getOperand(1), negOp,
7335 cast<AtomicSDNode>(Node)->getSrcValue(),
7336 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007337}
7338
Evan Cheng0db9fe62006-04-25 20:13:52 +00007339/// LowerOperation - Provide custom lowering hooks for some operations.
7340///
Dan Gohman475871a2008-07-27 21:46:04 +00007341SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007343 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007344 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7345 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007346 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007347 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007348 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7349 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7350 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7351 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7352 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7353 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007354 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007355 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007356 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007357 case ISD::SHL_PARTS:
7358 case ISD::SRA_PARTS:
7359 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7360 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007361 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007362 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007363 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007364 case ISD::FABS: return LowerFABS(Op, DAG);
7365 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007366 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007367 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007368 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007369 case ISD::SELECT: return LowerSELECT(Op, DAG);
7370 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007371 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007372 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007373 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007374 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007375 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007376 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7377 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007378 case ISD::FRAME_TO_ARGS_OFFSET:
7379 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007380 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007381 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007382 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007383 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007384 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7385 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007386 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007387 case ISD::SADDO:
7388 case ISD::UADDO:
7389 case ISD::SSUBO:
7390 case ISD::USUBO:
7391 case ISD::SMULO:
7392 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007393 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007394 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007395}
7396
Duncan Sands1607f052008-12-01 11:39:25 +00007397void X86TargetLowering::
7398ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7399 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007400 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007401 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007402 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007403
7404 SDValue Chain = Node->getOperand(0);
7405 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007407 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007408 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007409 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007410 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007412 SDValue Result =
7413 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7414 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007415 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007417 Results.push_back(Result.getValue(2));
7418}
7419
Duncan Sands126d9072008-07-04 11:47:58 +00007420/// ReplaceNodeResults - Replace a node with an illegal result type
7421/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007422void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7423 SmallVectorImpl<SDValue>&Results,
7424 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007425 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007426 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007427 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007428 assert(false && "Do not know how to custom type legalize this operation!");
7429 return;
7430 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007431 std::pair<SDValue,SDValue> Vals =
7432 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007433 SDValue FIST = Vals.first, StackSlot = Vals.second;
7434 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007435 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007436 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007437 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007438 }
7439 return;
7440 }
7441 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007442 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007443 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007444 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007446 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007448 eax.getValue(2));
7449 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7450 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007451 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007452 Results.push_back(edx.getValue(1));
7453 return;
7454 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007455 case ISD::SDIV:
7456 case ISD::UDIV:
7457 case ISD::SREM:
7458 case ISD::UREM: {
7459 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7460 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7461 return;
7462 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007463 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007464 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007465 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007466 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7468 DAG.getConstant(0, MVT::i32));
7469 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7470 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007471 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7472 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007473 cpInL.getValue(1));
7474 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007475 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7476 DAG.getConstant(0, MVT::i32));
7477 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7478 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007479 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007480 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007481 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007482 swapInL.getValue(1));
7483 SDValue Ops[] = { swapInH.getValue(0),
7484 N->getOperand(1),
7485 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007486 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007487 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007488 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007490 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007492 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007493 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007494 Results.push_back(cpOutH.getValue(1));
7495 return;
7496 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007497 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007498 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7499 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007500 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007501 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7502 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007503 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007504 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7505 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007506 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007507 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7508 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007509 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007510 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7511 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007512 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007513 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7514 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007515 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007516 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7517 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007518 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007519}
7520
Evan Cheng72261582005-12-20 06:22:03 +00007521const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7522 switch (Opcode) {
7523 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007524 case X86ISD::BSF: return "X86ISD::BSF";
7525 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007526 case X86ISD::SHLD: return "X86ISD::SHLD";
7527 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007528 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007529 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007530 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007531 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007532 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007533 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007534 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7535 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7536 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007537 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007538 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007539 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007540 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007541 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007542 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007543 case X86ISD::COMI: return "X86ISD::COMI";
7544 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007545 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007546 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007547 case X86ISD::CMOV: return "X86ISD::CMOV";
7548 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007549 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007550 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7551 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007552 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007553 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007554 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007555 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007556 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007557 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7558 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007559 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007560 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007561 case X86ISD::FMAX: return "X86ISD::FMAX";
7562 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007563 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7564 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007565 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007566 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007567 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007568 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007569 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007570 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7571 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007572 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7573 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7574 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7575 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7576 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7577 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007578 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7579 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007580 case X86ISD::VSHL: return "X86ISD::VSHL";
7581 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007582 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7583 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7584 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7585 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7586 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7587 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7588 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7589 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7590 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7591 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007592 case X86ISD::ADD: return "X86ISD::ADD";
7593 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007594 case X86ISD::SMUL: return "X86ISD::SMUL";
7595 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007596 case X86ISD::INC: return "X86ISD::INC";
7597 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007598 case X86ISD::OR: return "X86ISD::OR";
7599 case X86ISD::XOR: return "X86ISD::XOR";
7600 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007601 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007602 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007603 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007604 }
7605}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007606
Chris Lattnerc9addb72007-03-30 23:15:24 +00007607// isLegalAddressingMode - Return true if the addressing mode represented
7608// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007609bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007610 const Type *Ty) const {
7611 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007612 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007613
Chris Lattnerc9addb72007-03-30 23:15:24 +00007614 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007615 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007616 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007617
Chris Lattnerc9addb72007-03-30 23:15:24 +00007618 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007619 unsigned GVFlags =
7620 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007621
Chris Lattnerdfed4132009-07-10 07:38:24 +00007622 // If a reference to this global requires an extra load, we can't fold it.
7623 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007624 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007625
Chris Lattnerdfed4132009-07-10 07:38:24 +00007626 // If BaseGV requires a register for the PIC base, we cannot also have a
7627 // BaseReg specified.
7628 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007629 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007630
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007631 // If lower 4G is not available, then we must use rip-relative addressing.
7632 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7633 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007635
Chris Lattnerc9addb72007-03-30 23:15:24 +00007636 switch (AM.Scale) {
7637 case 0:
7638 case 1:
7639 case 2:
7640 case 4:
7641 case 8:
7642 // These scales always work.
7643 break;
7644 case 3:
7645 case 5:
7646 case 9:
7647 // These scales are formed with basereg+scalereg. Only accept if there is
7648 // no basereg yet.
7649 if (AM.HasBaseReg)
7650 return false;
7651 break;
7652 default: // Other stuff never works.
7653 return false;
7654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007655
Chris Lattnerc9addb72007-03-30 23:15:24 +00007656 return true;
7657}
7658
7659
Evan Cheng2bd122c2007-10-26 01:56:11 +00007660bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7661 if (!Ty1->isInteger() || !Ty2->isInteger())
7662 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007663 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7664 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007665 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007666 return false;
7667 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007668}
7669
Owen Andersone50ed302009-08-10 22:56:29 +00007670bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007671 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007672 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007673 unsigned NumBits1 = VT1.getSizeInBits();
7674 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007675 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007676 return false;
7677 return Subtarget->is64Bit() || NumBits1 < 64;
7678}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007679
Dan Gohman97121ba2009-04-08 00:15:30 +00007680bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007681 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007682 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007683}
7684
Owen Andersone50ed302009-08-10 22:56:29 +00007685bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007686 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007687 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007688}
7689
Owen Andersone50ed302009-08-10 22:56:29 +00007690bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007691 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007692 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007693}
7694
Evan Cheng60c07e12006-07-05 22:17:51 +00007695/// isShuffleMaskLegal - Targets can use this to indicate that they only
7696/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7697/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7698/// are assumed to be legal.
7699bool
Eric Christopherfd179292009-08-27 18:07:15 +00007700X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007701 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007702 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007703 if (VT.getSizeInBits() == 64)
7704 return false;
7705
Nate Begemana09008b2009-10-19 02:17:23 +00007706 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007707 return (VT.getVectorNumElements() == 2 ||
7708 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7709 isMOVLMask(M, VT) ||
7710 isSHUFPMask(M, VT) ||
7711 isPSHUFDMask(M, VT) ||
7712 isPSHUFHWMask(M, VT) ||
7713 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007714 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007715 isUNPCKLMask(M, VT) ||
7716 isUNPCKHMask(M, VT) ||
7717 isUNPCKL_v_undef_Mask(M, VT) ||
7718 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007719}
7720
Dan Gohman7d8143f2008-04-09 20:09:42 +00007721bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007722X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007723 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007724 unsigned NumElts = VT.getVectorNumElements();
7725 // FIXME: This collection of masks seems suspect.
7726 if (NumElts == 2)
7727 return true;
7728 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7729 return (isMOVLMask(Mask, VT) ||
7730 isCommutedMOVLMask(Mask, VT, true) ||
7731 isSHUFPMask(Mask, VT) ||
7732 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007733 }
7734 return false;
7735}
7736
7737//===----------------------------------------------------------------------===//
7738// X86 Scheduler Hooks
7739//===----------------------------------------------------------------------===//
7740
Mon P Wang63307c32008-05-05 19:05:59 +00007741// private utility function
7742MachineBasicBlock *
7743X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7744 MachineBasicBlock *MBB,
7745 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007746 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007747 unsigned LoadOpc,
7748 unsigned CXchgOpc,
7749 unsigned copyOpc,
7750 unsigned notOpc,
7751 unsigned EAXreg,
7752 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007753 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007754 // For the atomic bitwise operator, we generate
7755 // thisMBB:
7756 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007757 // ld t1 = [bitinstr.addr]
7758 // op t2 = t1, [bitinstr.val]
7759 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007760 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7761 // bz newMBB
7762 // fallthrough -->nextMBB
7763 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7764 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007765 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007766 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007767
Mon P Wang63307c32008-05-05 19:05:59 +00007768 /// First build the CFG
7769 MachineFunction *F = MBB->getParent();
7770 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007771 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7772 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7773 F->insert(MBBIter, newMBB);
7774 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007775
Mon P Wang63307c32008-05-05 19:05:59 +00007776 // Move all successors to thisMBB to nextMBB
7777 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007778
Mon P Wang63307c32008-05-05 19:05:59 +00007779 // Update thisMBB to fall through to newMBB
7780 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007781
Mon P Wang63307c32008-05-05 19:05:59 +00007782 // newMBB jumps to itself and fall through to nextMBB
7783 newMBB->addSuccessor(nextMBB);
7784 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007785
Mon P Wang63307c32008-05-05 19:05:59 +00007786 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007787 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007788 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007789 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007790 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007791 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007792 int numArgs = bInstr->getNumOperands() - 1;
7793 for (int i=0; i < numArgs; ++i)
7794 argOpers[i] = &bInstr->getOperand(i+1);
7795
7796 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007797 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7798 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007799
Dale Johannesen140be2d2008-08-19 18:47:28 +00007800 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007801 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007802 for (int i=0; i <= lastAddrIndx; ++i)
7803 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007804
Dale Johannesen140be2d2008-08-19 18:47:28 +00007805 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007806 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007807 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007808 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007809 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007810 tt = t1;
7811
Dale Johannesen140be2d2008-08-19 18:47:28 +00007812 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007813 assert((argOpers[valArgIndx]->isReg() ||
7814 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007815 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007816 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007817 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007818 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007819 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007820 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007821 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007822
Dale Johannesene4d209d2009-02-03 20:21:25 +00007823 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007824 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007825
Dale Johannesene4d209d2009-02-03 20:21:25 +00007826 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007827 for (int i=0; i <= lastAddrIndx; ++i)
7828 (*MIB).addOperand(*argOpers[i]);
7829 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007830 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007831 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7832 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007833
Dale Johannesene4d209d2009-02-03 20:21:25 +00007834 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007835 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007836
Mon P Wang63307c32008-05-05 19:05:59 +00007837 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007838 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007839
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007840 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007841 return nextMBB;
7842}
7843
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007844// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007845MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007846X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7847 MachineBasicBlock *MBB,
7848 unsigned regOpcL,
7849 unsigned regOpcH,
7850 unsigned immOpcL,
7851 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007852 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007853 // For the atomic bitwise operator, we generate
7854 // thisMBB (instructions are in pairs, except cmpxchg8b)
7855 // ld t1,t2 = [bitinstr.addr]
7856 // newMBB:
7857 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7858 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007859 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007860 // mov ECX, EBX <- t5, t6
7861 // mov EAX, EDX <- t1, t2
7862 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7863 // mov t3, t4 <- EAX, EDX
7864 // bz newMBB
7865 // result in out1, out2
7866 // fallthrough -->nextMBB
7867
7868 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7869 const unsigned LoadOpc = X86::MOV32rm;
7870 const unsigned copyOpc = X86::MOV32rr;
7871 const unsigned NotOpc = X86::NOT32r;
7872 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7873 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7874 MachineFunction::iterator MBBIter = MBB;
7875 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007876
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007877 /// First build the CFG
7878 MachineFunction *F = MBB->getParent();
7879 MachineBasicBlock *thisMBB = MBB;
7880 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7881 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7882 F->insert(MBBIter, newMBB);
7883 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007884
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007885 // Move all successors to thisMBB to nextMBB
7886 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007887
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007888 // Update thisMBB to fall through to newMBB
7889 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007890
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007891 // newMBB jumps to itself and fall through to nextMBB
7892 newMBB->addSuccessor(nextMBB);
7893 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007894
Dale Johannesene4d209d2009-02-03 20:21:25 +00007895 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007896 // Insert instructions into newMBB based on incoming instruction
7897 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007898 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007899 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007900 MachineOperand& dest1Oper = bInstr->getOperand(0);
7901 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007902 MachineOperand* argOpers[2 + X86AddrNumOperands];
7903 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007904 argOpers[i] = &bInstr->getOperand(i+2);
7905
Evan Chengad5b52f2010-01-08 19:14:57 +00007906 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007907 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007908
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007909 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007910 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007911 for (int i=0; i <= lastAddrIndx; ++i)
7912 (*MIB).addOperand(*argOpers[i]);
7913 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007914 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007915 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007916 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007917 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007918 MachineOperand newOp3 = *(argOpers[3]);
7919 if (newOp3.isImm())
7920 newOp3.setImm(newOp3.getImm()+4);
7921 else
7922 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007923 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007924 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007925
7926 // t3/4 are defined later, at the bottom of the loop
7927 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7928 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007929 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007930 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007931 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007932 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7933
Evan Cheng306b4ca2010-01-08 23:41:50 +00007934 // The subsequent operations should be using the destination registers of
7935 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007936 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007937 t1 = F->getRegInfo().createVirtualRegister(RC);
7938 t2 = F->getRegInfo().createVirtualRegister(RC);
7939 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7940 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007941 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007942 t1 = dest1Oper.getReg();
7943 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007944 }
7945
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007946 int valArgIndx = lastAddrIndx + 1;
7947 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007948 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007949 "invalid operand");
7950 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7951 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007952 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007953 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007954 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007955 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007956 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007957 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007958 (*MIB).addOperand(*argOpers[valArgIndx]);
7959 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007960 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007961 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007962 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007963 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007964 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007965 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007966 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007967 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007968 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007969 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007970
Dale Johannesene4d209d2009-02-03 20:21:25 +00007971 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007972 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007973 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007974 MIB.addReg(t2);
7975
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007978 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007979 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007980
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007982 for (int i=0; i <= lastAddrIndx; ++i)
7983 (*MIB).addOperand(*argOpers[i]);
7984
7985 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007986 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7987 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007988
Dale Johannesene4d209d2009-02-03 20:21:25 +00007989 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007990 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007991 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007992 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007993
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007994 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007995 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007996
7997 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7998 return nextMBB;
7999}
8000
8001// private utility function
8002MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008003X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8004 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008005 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008006 // For the atomic min/max operator, we generate
8007 // thisMBB:
8008 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008009 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008010 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008011 // cmp t1, t2
8012 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008013 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008014 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8015 // bz newMBB
8016 // fallthrough -->nextMBB
8017 //
8018 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8019 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008020 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008021 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008022
Mon P Wang63307c32008-05-05 19:05:59 +00008023 /// First build the CFG
8024 MachineFunction *F = MBB->getParent();
8025 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008026 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8027 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8028 F->insert(MBBIter, newMBB);
8029 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008030
Dan Gohmand6708ea2009-08-15 01:38:56 +00008031 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008032 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008033
Mon P Wang63307c32008-05-05 19:05:59 +00008034 // Update thisMBB to fall through to newMBB
8035 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008036
Mon P Wang63307c32008-05-05 19:05:59 +00008037 // newMBB jumps to newMBB and fall through to nextMBB
8038 newMBB->addSuccessor(nextMBB);
8039 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008040
Dale Johannesene4d209d2009-02-03 20:21:25 +00008041 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008042 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008043 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008044 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008045 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008046 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008047 int numArgs = mInstr->getNumOperands() - 1;
8048 for (int i=0; i < numArgs; ++i)
8049 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008050
Mon P Wang63307c32008-05-05 19:05:59 +00008051 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008052 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8053 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008054
Mon P Wangab3e7472008-05-05 22:56:23 +00008055 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008056 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008057 for (int i=0; i <= lastAddrIndx; ++i)
8058 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008059
Mon P Wang63307c32008-05-05 19:05:59 +00008060 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008061 assert((argOpers[valArgIndx]->isReg() ||
8062 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008063 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008064
8065 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008066 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008067 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008068 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008069 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008070 (*MIB).addOperand(*argOpers[valArgIndx]);
8071
Dale Johannesene4d209d2009-02-03 20:21:25 +00008072 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008073 MIB.addReg(t1);
8074
Dale Johannesene4d209d2009-02-03 20:21:25 +00008075 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008076 MIB.addReg(t1);
8077 MIB.addReg(t2);
8078
8079 // Generate movc
8080 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008081 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008082 MIB.addReg(t2);
8083 MIB.addReg(t1);
8084
8085 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008086 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008087 for (int i=0; i <= lastAddrIndx; ++i)
8088 (*MIB).addOperand(*argOpers[i]);
8089 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008090 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008091 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8092 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008093
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008095 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008096
Mon P Wang63307c32008-05-05 19:05:59 +00008097 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008098 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008099
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008100 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008101 return nextMBB;
8102}
8103
Eric Christopherf83a5de2009-08-27 18:08:16 +00008104// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8105// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008106MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008107X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008108 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008109
8110 MachineFunction *F = BB->getParent();
8111 DebugLoc dl = MI->getDebugLoc();
8112 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8113
8114 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008115 if (memArg)
8116 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8117 else
8118 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008119
8120 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8121
8122 for (unsigned i = 0; i < numArgs; ++i) {
8123 MachineOperand &Op = MI->getOperand(i+1);
8124
8125 if (!(Op.isReg() && Op.isImplicit()))
8126 MIB.addOperand(Op);
8127 }
8128
8129 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8130 .addReg(X86::XMM0);
8131
8132 F->DeleteMachineInstr(MI);
8133
8134 return BB;
8135}
8136
8137MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008138X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8139 MachineInstr *MI,
8140 MachineBasicBlock *MBB) const {
8141 // Emit code to save XMM registers to the stack. The ABI says that the
8142 // number of registers to save is given in %al, so it's theoretically
8143 // possible to do an indirect jump trick to avoid saving all of them,
8144 // however this code takes a simpler approach and just executes all
8145 // of the stores if %al is non-zero. It's less code, and it's probably
8146 // easier on the hardware branch predictor, and stores aren't all that
8147 // expensive anyway.
8148
8149 // Create the new basic blocks. One block contains all the XMM stores,
8150 // and one block is the final destination regardless of whether any
8151 // stores were performed.
8152 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8153 MachineFunction *F = MBB->getParent();
8154 MachineFunction::iterator MBBIter = MBB;
8155 ++MBBIter;
8156 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8157 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8158 F->insert(MBBIter, XMMSaveMBB);
8159 F->insert(MBBIter, EndMBB);
8160
8161 // Set up the CFG.
8162 // Move any original successors of MBB to the end block.
8163 EndMBB->transferSuccessors(MBB);
8164 // The original block will now fall through to the XMM save block.
8165 MBB->addSuccessor(XMMSaveMBB);
8166 // The XMMSaveMBB will fall through to the end block.
8167 XMMSaveMBB->addSuccessor(EndMBB);
8168
8169 // Now add the instructions.
8170 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8171 DebugLoc DL = MI->getDebugLoc();
8172
8173 unsigned CountReg = MI->getOperand(0).getReg();
8174 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8175 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8176
8177 if (!Subtarget->isTargetWin64()) {
8178 // If %al is 0, branch around the XMM save block.
8179 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8180 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8181 MBB->addSuccessor(EndMBB);
8182 }
8183
8184 // In the XMM save block, save all the XMM argument registers.
8185 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8186 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008187 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008188 F->getMachineMemOperand(
8189 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8190 MachineMemOperand::MOStore, Offset,
8191 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008192 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8193 .addFrameIndex(RegSaveFrameIndex)
8194 .addImm(/*Scale=*/1)
8195 .addReg(/*IndexReg=*/0)
8196 .addImm(/*Disp=*/Offset)
8197 .addReg(/*Segment=*/0)
8198 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008199 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008200 }
8201
8202 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8203
8204 return EndMBB;
8205}
Mon P Wang63307c32008-05-05 19:05:59 +00008206
Evan Cheng60c07e12006-07-05 22:17:51 +00008207MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008208X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008209 MachineBasicBlock *BB,
8210 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008211 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8212 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008213
Chris Lattner52600972009-09-02 05:57:00 +00008214 // To "insert" a SELECT_CC instruction, we actually have to insert the
8215 // diamond control-flow pattern. The incoming instruction knows the
8216 // destination vreg to set, the condition code register to branch on, the
8217 // true/false values to select between, and a branch opcode to use.
8218 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8219 MachineFunction::iterator It = BB;
8220 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008221
Chris Lattner52600972009-09-02 05:57:00 +00008222 // thisMBB:
8223 // ...
8224 // TrueVal = ...
8225 // cmpTY ccX, r1, r2
8226 // bCC copy1MBB
8227 // fallthrough --> copy0MBB
8228 MachineBasicBlock *thisMBB = BB;
8229 MachineFunction *F = BB->getParent();
8230 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8231 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8232 unsigned Opc =
8233 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8234 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8235 F->insert(It, copy0MBB);
8236 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008237 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008238 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008239 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008240 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008241 E = BB->succ_end(); I != E; ++I) {
8242 EM->insert(std::make_pair(*I, sinkMBB));
8243 sinkMBB->addSuccessor(*I);
8244 }
8245 // Next, remove all successors of the current block, and add the true
8246 // and fallthrough blocks as its successors.
8247 while (!BB->succ_empty())
8248 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008249 // Add the true and fallthrough blocks as its successors.
8250 BB->addSuccessor(copy0MBB);
8251 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008252
Chris Lattner52600972009-09-02 05:57:00 +00008253 // copy0MBB:
8254 // %FalseValue = ...
8255 // # fallthrough to sinkMBB
8256 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008257
Chris Lattner52600972009-09-02 05:57:00 +00008258 // Update machine-CFG edges
8259 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008260
Chris Lattner52600972009-09-02 05:57:00 +00008261 // sinkMBB:
8262 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8263 // ...
8264 BB = sinkMBB;
8265 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8266 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8267 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8268
8269 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8270 return BB;
8271}
8272
8273
8274MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008275X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008276 MachineBasicBlock *BB,
8277 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008278 switch (MI->getOpcode()) {
8279 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008280 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008281 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008282 case X86::CMOV_FR32:
8283 case X86::CMOV_FR64:
8284 case X86::CMOV_V4F32:
8285 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008286 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008287 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008288
Dale Johannesen849f2142007-07-03 00:53:03 +00008289 case X86::FP32_TO_INT16_IN_MEM:
8290 case X86::FP32_TO_INT32_IN_MEM:
8291 case X86::FP32_TO_INT64_IN_MEM:
8292 case X86::FP64_TO_INT16_IN_MEM:
8293 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008294 case X86::FP64_TO_INT64_IN_MEM:
8295 case X86::FP80_TO_INT16_IN_MEM:
8296 case X86::FP80_TO_INT32_IN_MEM:
8297 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008298 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8299 DebugLoc DL = MI->getDebugLoc();
8300
Evan Cheng60c07e12006-07-05 22:17:51 +00008301 // Change the floating point control register to use "round towards zero"
8302 // mode when truncating to an integer value.
8303 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008304 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008305 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008306
8307 // Load the old value of the high byte of the control word...
8308 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008309 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008310 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008311 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008312
8313 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008314 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008315 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008316
8317 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008318 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008319
8320 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008321 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008322 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008323
8324 // Get the X86 opcode to use.
8325 unsigned Opc;
8326 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008327 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008328 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8329 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8330 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8331 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8332 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8333 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008334 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8335 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8336 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008337 }
8338
8339 X86AddressMode AM;
8340 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008341 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008342 AM.BaseType = X86AddressMode::RegBase;
8343 AM.Base.Reg = Op.getReg();
8344 } else {
8345 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008346 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008347 }
8348 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008349 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008350 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008351 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008352 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008353 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008354 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008355 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008356 AM.GV = Op.getGlobal();
8357 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008358 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008359 }
Chris Lattner52600972009-09-02 05:57:00 +00008360 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008361 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008362
8363 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008364 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008365
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008366 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008367 return BB;
8368 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008369 // String/text processing lowering.
8370 case X86::PCMPISTRM128REG:
8371 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8372 case X86::PCMPISTRM128MEM:
8373 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8374 case X86::PCMPESTRM128REG:
8375 return EmitPCMP(MI, BB, 5, false /* in mem */);
8376 case X86::PCMPESTRM128MEM:
8377 return EmitPCMP(MI, BB, 5, true /* in mem */);
8378
8379 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008380 case X86::ATOMAND32:
8381 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008382 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008383 X86::LCMPXCHG32, X86::MOV32rr,
8384 X86::NOT32r, X86::EAX,
8385 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008386 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008387 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8388 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008389 X86::LCMPXCHG32, X86::MOV32rr,
8390 X86::NOT32r, X86::EAX,
8391 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008392 case X86::ATOMXOR32:
8393 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008394 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008395 X86::LCMPXCHG32, X86::MOV32rr,
8396 X86::NOT32r, X86::EAX,
8397 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008398 case X86::ATOMNAND32:
8399 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008400 X86::AND32ri, X86::MOV32rm,
8401 X86::LCMPXCHG32, X86::MOV32rr,
8402 X86::NOT32r, X86::EAX,
8403 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008404 case X86::ATOMMIN32:
8405 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8406 case X86::ATOMMAX32:
8407 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8408 case X86::ATOMUMIN32:
8409 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8410 case X86::ATOMUMAX32:
8411 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008412
8413 case X86::ATOMAND16:
8414 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8415 X86::AND16ri, X86::MOV16rm,
8416 X86::LCMPXCHG16, X86::MOV16rr,
8417 X86::NOT16r, X86::AX,
8418 X86::GR16RegisterClass);
8419 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008420 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008421 X86::OR16ri, X86::MOV16rm,
8422 X86::LCMPXCHG16, X86::MOV16rr,
8423 X86::NOT16r, X86::AX,
8424 X86::GR16RegisterClass);
8425 case X86::ATOMXOR16:
8426 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8427 X86::XOR16ri, X86::MOV16rm,
8428 X86::LCMPXCHG16, X86::MOV16rr,
8429 X86::NOT16r, X86::AX,
8430 X86::GR16RegisterClass);
8431 case X86::ATOMNAND16:
8432 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8433 X86::AND16ri, X86::MOV16rm,
8434 X86::LCMPXCHG16, X86::MOV16rr,
8435 X86::NOT16r, X86::AX,
8436 X86::GR16RegisterClass, true);
8437 case X86::ATOMMIN16:
8438 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8439 case X86::ATOMMAX16:
8440 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8441 case X86::ATOMUMIN16:
8442 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8443 case X86::ATOMUMAX16:
8444 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8445
8446 case X86::ATOMAND8:
8447 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8448 X86::AND8ri, X86::MOV8rm,
8449 X86::LCMPXCHG8, X86::MOV8rr,
8450 X86::NOT8r, X86::AL,
8451 X86::GR8RegisterClass);
8452 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008453 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008454 X86::OR8ri, X86::MOV8rm,
8455 X86::LCMPXCHG8, X86::MOV8rr,
8456 X86::NOT8r, X86::AL,
8457 X86::GR8RegisterClass);
8458 case X86::ATOMXOR8:
8459 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8460 X86::XOR8ri, X86::MOV8rm,
8461 X86::LCMPXCHG8, X86::MOV8rr,
8462 X86::NOT8r, X86::AL,
8463 X86::GR8RegisterClass);
8464 case X86::ATOMNAND8:
8465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8466 X86::AND8ri, X86::MOV8rm,
8467 X86::LCMPXCHG8, X86::MOV8rr,
8468 X86::NOT8r, X86::AL,
8469 X86::GR8RegisterClass, true);
8470 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008471 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008472 case X86::ATOMAND64:
8473 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008474 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008475 X86::LCMPXCHG64, X86::MOV64rr,
8476 X86::NOT64r, X86::RAX,
8477 X86::GR64RegisterClass);
8478 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008479 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8480 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008481 X86::LCMPXCHG64, X86::MOV64rr,
8482 X86::NOT64r, X86::RAX,
8483 X86::GR64RegisterClass);
8484 case X86::ATOMXOR64:
8485 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008486 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008487 X86::LCMPXCHG64, X86::MOV64rr,
8488 X86::NOT64r, X86::RAX,
8489 X86::GR64RegisterClass);
8490 case X86::ATOMNAND64:
8491 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8492 X86::AND64ri32, X86::MOV64rm,
8493 X86::LCMPXCHG64, X86::MOV64rr,
8494 X86::NOT64r, X86::RAX,
8495 X86::GR64RegisterClass, true);
8496 case X86::ATOMMIN64:
8497 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8498 case X86::ATOMMAX64:
8499 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8500 case X86::ATOMUMIN64:
8501 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8502 case X86::ATOMUMAX64:
8503 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008504
8505 // This group does 64-bit operations on a 32-bit host.
8506 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008507 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008508 X86::AND32rr, X86::AND32rr,
8509 X86::AND32ri, X86::AND32ri,
8510 false);
8511 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008512 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008513 X86::OR32rr, X86::OR32rr,
8514 X86::OR32ri, X86::OR32ri,
8515 false);
8516 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008517 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008518 X86::XOR32rr, X86::XOR32rr,
8519 X86::XOR32ri, X86::XOR32ri,
8520 false);
8521 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008522 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008523 X86::AND32rr, X86::AND32rr,
8524 X86::AND32ri, X86::AND32ri,
8525 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008526 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008527 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008528 X86::ADD32rr, X86::ADC32rr,
8529 X86::ADD32ri, X86::ADC32ri,
8530 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008531 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008532 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008533 X86::SUB32rr, X86::SBB32rr,
8534 X86::SUB32ri, X86::SBB32ri,
8535 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008536 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008537 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008538 X86::MOV32rr, X86::MOV32rr,
8539 X86::MOV32ri, X86::MOV32ri,
8540 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008541 case X86::VASTART_SAVE_XMM_REGS:
8542 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008543 }
8544}
8545
8546//===----------------------------------------------------------------------===//
8547// X86 Optimization Hooks
8548//===----------------------------------------------------------------------===//
8549
Dan Gohman475871a2008-07-27 21:46:04 +00008550void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008551 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008552 APInt &KnownZero,
8553 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008554 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008555 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008556 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008557 assert((Opc >= ISD::BUILTIN_OP_END ||
8558 Opc == ISD::INTRINSIC_WO_CHAIN ||
8559 Opc == ISD::INTRINSIC_W_CHAIN ||
8560 Opc == ISD::INTRINSIC_VOID) &&
8561 "Should use MaskedValueIsZero if you don't know whether Op"
8562 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008563
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008564 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008565 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008566 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008567 case X86ISD::ADD:
8568 case X86ISD::SUB:
8569 case X86ISD::SMUL:
8570 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008571 case X86ISD::INC:
8572 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008573 case X86ISD::OR:
8574 case X86ISD::XOR:
8575 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008576 // These nodes' second result is a boolean.
8577 if (Op.getResNo() == 0)
8578 break;
8579 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008580 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008581 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8582 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008583 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008584 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008585}
Chris Lattner259e97c2006-01-31 19:43:35 +00008586
Evan Cheng206ee9d2006-07-07 08:33:52 +00008587/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008588/// node is a GlobalAddress + offset.
8589bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8590 GlobalValue* &GA, int64_t &Offset) const{
8591 if (N->getOpcode() == X86ISD::Wrapper) {
8592 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008593 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008594 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008595 return true;
8596 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008597 }
Evan Chengad4196b2008-05-12 19:56:52 +00008598 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008599}
8600
Nate Begeman9008ca62009-04-27 18:41:29 +00008601static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008602 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008603 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008604 SelectionDAG &DAG, MachineFrameInfo *MFI,
8605 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008606 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008607 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008608 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008609 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008610 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008611 return false;
8612 continue;
8613 }
8614
Dan Gohman475871a2008-07-27 21:46:04 +00008615 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008616 if (!Elt.getNode() ||
8617 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008618 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008619 if (!LDBase) {
8620 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008621 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008622 LDBase = cast<LoadSDNode>(Elt.getNode());
8623 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008624 continue;
8625 }
8626 if (Elt.getOpcode() == ISD::UNDEF)
8627 continue;
8628
Nate Begemanabc01992009-06-05 21:37:30 +00008629 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008630 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008631 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008632 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008633 }
8634 return true;
8635}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008636
8637/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8638/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8639/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008640/// order. In the case of v2i64, it will see if it can rewrite the
8641/// shuffle to be an appropriate build vector so it can take advantage of
8642// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008643static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008644 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008645 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008646 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008647 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008648 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8649 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008650
Eli Friedman7a5e5552009-06-07 06:52:44 +00008651 if (VT.getSizeInBits() != 128)
8652 return SDValue();
8653
Mon P Wang1e955802009-04-03 02:43:30 +00008654 // Try to combine a vector_shuffle into a 128-bit load.
8655 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008656 LoadSDNode *LD = NULL;
8657 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008658 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008659 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008660 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008661
Eli Friedman7a5e5552009-06-07 06:52:44 +00008662 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008663 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008664 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8665 LD->getSrcValue(), LD->getSrcValueOffset(),
8666 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008667 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008668 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008669 LD->isVolatile(), LD->getAlignment());
8670 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008671 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008672 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8673 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008674 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8675 }
8676 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008677}
Evan Chengd880b972008-05-09 21:53:03 +00008678
Chris Lattner83e6c992006-10-04 06:57:07 +00008679/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008680static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008681 const X86Subtarget *Subtarget) {
8682 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008683 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008684 // Get the LHS/RHS of the select.
8685 SDValue LHS = N->getOperand(1);
8686 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008687
Dan Gohman670e5392009-09-21 18:03:22 +00008688 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8689 // instructions have the peculiarity that if either operand is a NaN,
8690 // they chose what we call the RHS operand (and as such are not symmetric).
8691 // It happens that this matches the semantics of the common C idiom
8692 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008693 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008694 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008695 Cond.getOpcode() == ISD::SETCC) {
8696 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008697
Chris Lattner47b4ce82009-03-11 05:48:52 +00008698 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008699 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008700 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8701 switch (CC) {
8702 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008703 case ISD::SETULT:
8704 // This can be a min if we can prove that at least one of the operands
8705 // is not a nan.
8706 if (!FiniteOnlyFPMath()) {
8707 if (DAG.isKnownNeverNaN(RHS)) {
8708 // Put the potential NaN in the RHS so that SSE will preserve it.
8709 std::swap(LHS, RHS);
8710 } else if (!DAG.isKnownNeverNaN(LHS))
8711 break;
8712 }
8713 Opcode = X86ISD::FMIN;
8714 break;
8715 case ISD::SETOLE:
8716 // This can be a min if we can prove that at least one of the operands
8717 // is not a nan.
8718 if (!FiniteOnlyFPMath()) {
8719 if (DAG.isKnownNeverNaN(LHS)) {
8720 // Put the potential NaN in the RHS so that SSE will preserve it.
8721 std::swap(LHS, RHS);
8722 } else if (!DAG.isKnownNeverNaN(RHS))
8723 break;
8724 }
8725 Opcode = X86ISD::FMIN;
8726 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008727 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008728 // This can be a min, but if either operand is a NaN we need it to
8729 // preserve the original LHS.
8730 std::swap(LHS, RHS);
8731 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008732 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008733 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008734 Opcode = X86ISD::FMIN;
8735 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008736
Dan Gohman670e5392009-09-21 18:03:22 +00008737 case ISD::SETOGE:
8738 // This can be a max if we can prove that at least one of the operands
8739 // is not a nan.
8740 if (!FiniteOnlyFPMath()) {
8741 if (DAG.isKnownNeverNaN(LHS)) {
8742 // Put the potential NaN in the RHS so that SSE will preserve it.
8743 std::swap(LHS, RHS);
8744 } else if (!DAG.isKnownNeverNaN(RHS))
8745 break;
8746 }
8747 Opcode = X86ISD::FMAX;
8748 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008749 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008750 // This can be a max if we can prove that at least one of the operands
8751 // is not a nan.
8752 if (!FiniteOnlyFPMath()) {
8753 if (DAG.isKnownNeverNaN(RHS)) {
8754 // Put the potential NaN in the RHS so that SSE will preserve it.
8755 std::swap(LHS, RHS);
8756 } else if (!DAG.isKnownNeverNaN(LHS))
8757 break;
8758 }
8759 Opcode = X86ISD::FMAX;
8760 break;
8761 case ISD::SETUGE:
8762 // This can be a max, but if either operand is a NaN we need it to
8763 // preserve the original LHS.
8764 std::swap(LHS, RHS);
8765 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008766 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008767 case ISD::SETGE:
8768 Opcode = X86ISD::FMAX;
8769 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008770 }
Dan Gohman670e5392009-09-21 18:03:22 +00008771 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008772 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8773 switch (CC) {
8774 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008775 case ISD::SETOGE:
8776 // This can be a min if we can prove that at least one of the operands
8777 // is not a nan.
8778 if (!FiniteOnlyFPMath()) {
8779 if (DAG.isKnownNeverNaN(RHS)) {
8780 // Put the potential NaN in the RHS so that SSE will preserve it.
8781 std::swap(LHS, RHS);
8782 } else if (!DAG.isKnownNeverNaN(LHS))
8783 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008784 }
Dan Gohman670e5392009-09-21 18:03:22 +00008785 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008786 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008787 case ISD::SETUGT:
8788 // This can be a min if we can prove that at least one of the operands
8789 // is not a nan.
8790 if (!FiniteOnlyFPMath()) {
8791 if (DAG.isKnownNeverNaN(LHS)) {
8792 // Put the potential NaN in the RHS so that SSE will preserve it.
8793 std::swap(LHS, RHS);
8794 } else if (!DAG.isKnownNeverNaN(RHS))
8795 break;
8796 }
8797 Opcode = X86ISD::FMIN;
8798 break;
8799 case ISD::SETUGE:
8800 // This can be a min, but if either operand is a NaN we need it to
8801 // preserve the original LHS.
8802 std::swap(LHS, RHS);
8803 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008804 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008805 case ISD::SETGE:
8806 Opcode = X86ISD::FMIN;
8807 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008808
Dan Gohman670e5392009-09-21 18:03:22 +00008809 case ISD::SETULT:
8810 // This can be a max if we can prove that at least one of the operands
8811 // is not a nan.
8812 if (!FiniteOnlyFPMath()) {
8813 if (DAG.isKnownNeverNaN(LHS)) {
8814 // Put the potential NaN in the RHS so that SSE will preserve it.
8815 std::swap(LHS, RHS);
8816 } else if (!DAG.isKnownNeverNaN(RHS))
8817 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008818 }
Dan Gohman670e5392009-09-21 18:03:22 +00008819 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008820 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008821 case ISD::SETOLE:
8822 // This can be a max if we can prove that at least one of the operands
8823 // is not a nan.
8824 if (!FiniteOnlyFPMath()) {
8825 if (DAG.isKnownNeverNaN(RHS)) {
8826 // Put the potential NaN in the RHS so that SSE will preserve it.
8827 std::swap(LHS, RHS);
8828 } else if (!DAG.isKnownNeverNaN(LHS))
8829 break;
8830 }
8831 Opcode = X86ISD::FMAX;
8832 break;
8833 case ISD::SETULE:
8834 // This can be a max, but if either operand is a NaN we need it to
8835 // preserve the original LHS.
8836 std::swap(LHS, RHS);
8837 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008838 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008839 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008840 Opcode = X86ISD::FMAX;
8841 break;
8842 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008843 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008844
Chris Lattner47b4ce82009-03-11 05:48:52 +00008845 if (Opcode)
8846 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008847 }
Eric Christopherfd179292009-08-27 18:07:15 +00008848
Chris Lattnerd1980a52009-03-12 06:52:53 +00008849 // If this is a select between two integer constants, try to do some
8850 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008851 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8852 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008853 // Don't do this for crazy integer types.
8854 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8855 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008856 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008857 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008858
Chris Lattnercee56e72009-03-13 05:53:31 +00008859 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008860 // Efficiently invertible.
8861 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8862 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8863 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8864 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008865 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008866 }
Eric Christopherfd179292009-08-27 18:07:15 +00008867
Chris Lattnerd1980a52009-03-12 06:52:53 +00008868 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008869 if (FalseC->getAPIntValue() == 0 &&
8870 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008871 if (NeedsCondInvert) // Invert the condition if needed.
8872 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8873 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008874
Chris Lattnerd1980a52009-03-12 06:52:53 +00008875 // Zero extend the condition if needed.
8876 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008877
Chris Lattnercee56e72009-03-13 05:53:31 +00008878 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008879 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008880 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008881 }
Eric Christopherfd179292009-08-27 18:07:15 +00008882
Chris Lattner97a29a52009-03-13 05:22:11 +00008883 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008884 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008885 if (NeedsCondInvert) // Invert the condition if needed.
8886 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8887 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008888
Chris Lattner97a29a52009-03-13 05:22:11 +00008889 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008890 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8891 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008892 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008893 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008894 }
Eric Christopherfd179292009-08-27 18:07:15 +00008895
Chris Lattnercee56e72009-03-13 05:53:31 +00008896 // Optimize cases that will turn into an LEA instruction. This requires
8897 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008898 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008899 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008900 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008901
Chris Lattnercee56e72009-03-13 05:53:31 +00008902 bool isFastMultiplier = false;
8903 if (Diff < 10) {
8904 switch ((unsigned char)Diff) {
8905 default: break;
8906 case 1: // result = add base, cond
8907 case 2: // result = lea base( , cond*2)
8908 case 3: // result = lea base(cond, cond*2)
8909 case 4: // result = lea base( , cond*4)
8910 case 5: // result = lea base(cond, cond*4)
8911 case 8: // result = lea base( , cond*8)
8912 case 9: // result = lea base(cond, cond*8)
8913 isFastMultiplier = true;
8914 break;
8915 }
8916 }
Eric Christopherfd179292009-08-27 18:07:15 +00008917
Chris Lattnercee56e72009-03-13 05:53:31 +00008918 if (isFastMultiplier) {
8919 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8920 if (NeedsCondInvert) // Invert the condition if needed.
8921 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8922 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008923
Chris Lattnercee56e72009-03-13 05:53:31 +00008924 // Zero extend the condition if needed.
8925 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8926 Cond);
8927 // Scale the condition by the difference.
8928 if (Diff != 1)
8929 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8930 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008931
Chris Lattnercee56e72009-03-13 05:53:31 +00008932 // Add the base if non-zero.
8933 if (FalseC->getAPIntValue() != 0)
8934 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8935 SDValue(FalseC, 0));
8936 return Cond;
8937 }
Eric Christopherfd179292009-08-27 18:07:15 +00008938 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008939 }
8940 }
Eric Christopherfd179292009-08-27 18:07:15 +00008941
Dan Gohman475871a2008-07-27 21:46:04 +00008942 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008943}
8944
Chris Lattnerd1980a52009-03-12 06:52:53 +00008945/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8946static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8947 TargetLowering::DAGCombinerInfo &DCI) {
8948 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008949
Chris Lattnerd1980a52009-03-12 06:52:53 +00008950 // If the flag operand isn't dead, don't touch this CMOV.
8951 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8952 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008953
Chris Lattnerd1980a52009-03-12 06:52:53 +00008954 // If this is a select between two integer constants, try to do some
8955 // optimizations. Note that the operands are ordered the opposite of SELECT
8956 // operands.
8957 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8958 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8959 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8960 // larger than FalseC (the false value).
8961 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008962
Chris Lattnerd1980a52009-03-12 06:52:53 +00008963 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8964 CC = X86::GetOppositeBranchCondition(CC);
8965 std::swap(TrueC, FalseC);
8966 }
Eric Christopherfd179292009-08-27 18:07:15 +00008967
Chris Lattnerd1980a52009-03-12 06:52:53 +00008968 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008969 // This is efficient for any integer data type (including i8/i16) and
8970 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008971 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8972 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008973 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8974 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008975
Chris Lattnerd1980a52009-03-12 06:52:53 +00008976 // Zero extend the condition if needed.
8977 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008978
Chris Lattnerd1980a52009-03-12 06:52:53 +00008979 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8980 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008981 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008982 if (N->getNumValues() == 2) // Dead flag value?
8983 return DCI.CombineTo(N, Cond, SDValue());
8984 return Cond;
8985 }
Eric Christopherfd179292009-08-27 18:07:15 +00008986
Chris Lattnercee56e72009-03-13 05:53:31 +00008987 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8988 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008989 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8990 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008991 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8992 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008993
Chris Lattner97a29a52009-03-13 05:22:11 +00008994 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008995 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8996 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008997 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8998 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008999
Chris Lattner97a29a52009-03-13 05:22:11 +00009000 if (N->getNumValues() == 2) // Dead flag value?
9001 return DCI.CombineTo(N, Cond, SDValue());
9002 return Cond;
9003 }
Eric Christopherfd179292009-08-27 18:07:15 +00009004
Chris Lattnercee56e72009-03-13 05:53:31 +00009005 // Optimize cases that will turn into an LEA instruction. This requires
9006 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009007 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009008 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009009 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009010
Chris Lattnercee56e72009-03-13 05:53:31 +00009011 bool isFastMultiplier = false;
9012 if (Diff < 10) {
9013 switch ((unsigned char)Diff) {
9014 default: break;
9015 case 1: // result = add base, cond
9016 case 2: // result = lea base( , cond*2)
9017 case 3: // result = lea base(cond, cond*2)
9018 case 4: // result = lea base( , cond*4)
9019 case 5: // result = lea base(cond, cond*4)
9020 case 8: // result = lea base( , cond*8)
9021 case 9: // result = lea base(cond, cond*8)
9022 isFastMultiplier = true;
9023 break;
9024 }
9025 }
Eric Christopherfd179292009-08-27 18:07:15 +00009026
Chris Lattnercee56e72009-03-13 05:53:31 +00009027 if (isFastMultiplier) {
9028 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9029 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009030 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9031 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009032 // Zero extend the condition if needed.
9033 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9034 Cond);
9035 // Scale the condition by the difference.
9036 if (Diff != 1)
9037 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9038 DAG.getConstant(Diff, Cond.getValueType()));
9039
9040 // Add the base if non-zero.
9041 if (FalseC->getAPIntValue() != 0)
9042 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9043 SDValue(FalseC, 0));
9044 if (N->getNumValues() == 2) // Dead flag value?
9045 return DCI.CombineTo(N, Cond, SDValue());
9046 return Cond;
9047 }
Eric Christopherfd179292009-08-27 18:07:15 +00009048 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009049 }
9050 }
9051 return SDValue();
9052}
9053
9054
Evan Cheng0b0cd912009-03-28 05:57:29 +00009055/// PerformMulCombine - Optimize a single multiply with constant into two
9056/// in order to implement it with two cheaper instructions, e.g.
9057/// LEA + SHL, LEA + LEA.
9058static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9059 TargetLowering::DAGCombinerInfo &DCI) {
9060 if (DAG.getMachineFunction().
9061 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9062 return SDValue();
9063
9064 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9065 return SDValue();
9066
Owen Andersone50ed302009-08-10 22:56:29 +00009067 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009068 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009069 return SDValue();
9070
9071 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9072 if (!C)
9073 return SDValue();
9074 uint64_t MulAmt = C->getZExtValue();
9075 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9076 return SDValue();
9077
9078 uint64_t MulAmt1 = 0;
9079 uint64_t MulAmt2 = 0;
9080 if ((MulAmt % 9) == 0) {
9081 MulAmt1 = 9;
9082 MulAmt2 = MulAmt / 9;
9083 } else if ((MulAmt % 5) == 0) {
9084 MulAmt1 = 5;
9085 MulAmt2 = MulAmt / 5;
9086 } else if ((MulAmt % 3) == 0) {
9087 MulAmt1 = 3;
9088 MulAmt2 = MulAmt / 3;
9089 }
9090 if (MulAmt2 &&
9091 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9092 DebugLoc DL = N->getDebugLoc();
9093
9094 if (isPowerOf2_64(MulAmt2) &&
9095 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9096 // If second multiplifer is pow2, issue it first. We want the multiply by
9097 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9098 // is an add.
9099 std::swap(MulAmt1, MulAmt2);
9100
9101 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009102 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009103 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009104 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009105 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009106 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009107 DAG.getConstant(MulAmt1, VT));
9108
Eric Christopherfd179292009-08-27 18:07:15 +00009109 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009110 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009111 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009112 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009113 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009114 DAG.getConstant(MulAmt2, VT));
9115
9116 // Do not add new nodes to DAG combiner worklist.
9117 DCI.CombineTo(N, NewMul, false);
9118 }
9119 return SDValue();
9120}
9121
Evan Chengad9c0a32009-12-15 00:53:42 +00009122static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9123 SDValue N0 = N->getOperand(0);
9124 SDValue N1 = N->getOperand(1);
9125 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9126 EVT VT = N0.getValueType();
9127
9128 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9129 // since the result of setcc_c is all zero's or all ones.
9130 if (N1C && N0.getOpcode() == ISD::AND &&
9131 N0.getOperand(1).getOpcode() == ISD::Constant) {
9132 SDValue N00 = N0.getOperand(0);
9133 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9134 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9135 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9136 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9137 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9138 APInt ShAmt = N1C->getAPIntValue();
9139 Mask = Mask.shl(ShAmt);
9140 if (Mask != 0)
9141 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9142 N00, DAG.getConstant(Mask, VT));
9143 }
9144 }
9145
9146 return SDValue();
9147}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009148
Nate Begeman740ab032009-01-26 00:52:55 +00009149/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9150/// when possible.
9151static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9152 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009153 EVT VT = N->getValueType(0);
9154 if (!VT.isVector() && VT.isInteger() &&
9155 N->getOpcode() == ISD::SHL)
9156 return PerformSHLCombine(N, DAG);
9157
Nate Begeman740ab032009-01-26 00:52:55 +00009158 // On X86 with SSE2 support, we can transform this to a vector shift if
9159 // all elements are shifted by the same amount. We can't do this in legalize
9160 // because the a constant vector is typically transformed to a constant pool
9161 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009162 if (!Subtarget->hasSSE2())
9163 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009164
Owen Anderson825b72b2009-08-11 20:47:22 +00009165 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009166 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009167
Mon P Wang3becd092009-01-28 08:12:05 +00009168 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009169 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009170 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009171 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009172 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9173 unsigned NumElts = VT.getVectorNumElements();
9174 unsigned i = 0;
9175 for (; i != NumElts; ++i) {
9176 SDValue Arg = ShAmtOp.getOperand(i);
9177 if (Arg.getOpcode() == ISD::UNDEF) continue;
9178 BaseShAmt = Arg;
9179 break;
9180 }
9181 for (; i != NumElts; ++i) {
9182 SDValue Arg = ShAmtOp.getOperand(i);
9183 if (Arg.getOpcode() == ISD::UNDEF) continue;
9184 if (Arg != BaseShAmt) {
9185 return SDValue();
9186 }
9187 }
9188 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009189 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009190 SDValue InVec = ShAmtOp.getOperand(0);
9191 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9192 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9193 unsigned i = 0;
9194 for (; i != NumElts; ++i) {
9195 SDValue Arg = InVec.getOperand(i);
9196 if (Arg.getOpcode() == ISD::UNDEF) continue;
9197 BaseShAmt = Arg;
9198 break;
9199 }
9200 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9201 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9202 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9203 if (C->getZExtValue() == SplatIdx)
9204 BaseShAmt = InVec.getOperand(1);
9205 }
9206 }
9207 if (BaseShAmt.getNode() == 0)
9208 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9209 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009210 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009211 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009212
Mon P Wangefa42202009-09-03 19:56:25 +00009213 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009214 if (EltVT.bitsGT(MVT::i32))
9215 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9216 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009217 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009218
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009219 // The shift amount is identical so we can do a vector shift.
9220 SDValue ValOp = N->getOperand(0);
9221 switch (N->getOpcode()) {
9222 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009223 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009224 break;
9225 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 if (VT == MVT::v2i64)
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_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009229 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009230 if (VT == MVT::v4i32)
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_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009233 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009234 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009235 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009237 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009238 break;
9239 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009240 if (VT == MVT::v4i32)
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_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009243 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009244 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009245 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009246 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009247 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009248 break;
9249 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009250 if (VT == MVT::v2i64)
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_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009253 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009254 if (VT == MVT::v4i32)
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_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009257 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009258 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009259 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009260 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009261 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009262 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009263 }
9264 return SDValue();
9265}
9266
Evan Cheng760d1942010-01-04 21:22:48 +00009267static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9268 const X86Subtarget *Subtarget) {
9269 EVT VT = N->getValueType(0);
9270 if (VT != MVT::i64 || !Subtarget->is64Bit())
9271 return SDValue();
9272
9273 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9274 SDValue N0 = N->getOperand(0);
9275 SDValue N1 = N->getOperand(1);
9276 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9277 std::swap(N0, N1);
9278 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9279 return SDValue();
9280
9281 SDValue ShAmt0 = N0.getOperand(1);
9282 if (ShAmt0.getValueType() != MVT::i8)
9283 return SDValue();
9284 SDValue ShAmt1 = N1.getOperand(1);
9285 if (ShAmt1.getValueType() != MVT::i8)
9286 return SDValue();
9287 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9288 ShAmt0 = ShAmt0.getOperand(0);
9289 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9290 ShAmt1 = ShAmt1.getOperand(0);
9291
9292 DebugLoc DL = N->getDebugLoc();
9293 unsigned Opc = X86ISD::SHLD;
9294 SDValue Op0 = N0.getOperand(0);
9295 SDValue Op1 = N1.getOperand(0);
9296 if (ShAmt0.getOpcode() == ISD::SUB) {
9297 Opc = X86ISD::SHRD;
9298 std::swap(Op0, Op1);
9299 std::swap(ShAmt0, ShAmt1);
9300 }
9301
9302 if (ShAmt1.getOpcode() == ISD::SUB) {
9303 SDValue Sum = ShAmt1.getOperand(0);
9304 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9305 if (SumC->getSExtValue() == 64 &&
9306 ShAmt1.getOperand(1) == ShAmt0)
9307 return DAG.getNode(Opc, DL, VT,
9308 Op0, Op1,
9309 DAG.getNode(ISD::TRUNCATE, DL,
9310 MVT::i8, ShAmt0));
9311 }
9312 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9313 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9314 if (ShAmt0C &&
9315 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9316 return DAG.getNode(Opc, DL, VT,
9317 N0.getOperand(0), N1.getOperand(0),
9318 DAG.getNode(ISD::TRUNCATE, DL,
9319 MVT::i8, ShAmt0));
9320 }
9321
9322 return SDValue();
9323}
9324
Chris Lattner149a4e52008-02-22 02:09:43 +00009325/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009326static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009327 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009328 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9329 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009330 // A preferable solution to the general problem is to figure out the right
9331 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009332
9333 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009334 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009335 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009336 if (VT.getSizeInBits() != 64)
9337 return SDValue();
9338
Devang Patel578efa92009-06-05 21:57:13 +00009339 const Function *F = DAG.getMachineFunction().getFunction();
9340 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009341 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009342 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009343 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009344 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009345 isa<LoadSDNode>(St->getValue()) &&
9346 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9347 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009348 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009349 LoadSDNode *Ld = 0;
9350 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009351 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009352 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009353 // Must be a store of a load. We currently handle two cases: the load
9354 // is a direct child, and it's under an intervening TokenFactor. It is
9355 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009356 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009357 Ld = cast<LoadSDNode>(St->getChain());
9358 else if (St->getValue().hasOneUse() &&
9359 ChainVal->getOpcode() == ISD::TokenFactor) {
9360 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009361 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009362 TokenFactorIndex = i;
9363 Ld = cast<LoadSDNode>(St->getValue());
9364 } else
9365 Ops.push_back(ChainVal->getOperand(i));
9366 }
9367 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009368
Evan Cheng536e6672009-03-12 05:59:15 +00009369 if (!Ld || !ISD::isNormalLoad(Ld))
9370 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009371
Evan Cheng536e6672009-03-12 05:59:15 +00009372 // If this is not the MMX case, i.e. we are just turning i64 load/store
9373 // into f64 load/store, avoid the transformation if there are multiple
9374 // uses of the loaded value.
9375 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9376 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009377
Evan Cheng536e6672009-03-12 05:59:15 +00009378 DebugLoc LdDL = Ld->getDebugLoc();
9379 DebugLoc StDL = N->getDebugLoc();
9380 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9381 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9382 // pair instead.
9383 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009384 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009385 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9386 Ld->getBasePtr(), Ld->getSrcValue(),
9387 Ld->getSrcValueOffset(), Ld->isVolatile(),
9388 Ld->getAlignment());
9389 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009390 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009391 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009392 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009393 Ops.size());
9394 }
Evan Cheng536e6672009-03-12 05:59:15 +00009395 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009396 St->getSrcValue(), St->getSrcValueOffset(),
9397 St->isVolatile(), St->getAlignment());
9398 }
Evan Cheng536e6672009-03-12 05:59:15 +00009399
9400 // Otherwise, lower to two pairs of 32-bit loads / stores.
9401 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009402 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9403 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009404
Owen Anderson825b72b2009-08-11 20:47:22 +00009405 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009406 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9407 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009408 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009409 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9410 Ld->isVolatile(),
9411 MinAlign(Ld->getAlignment(), 4));
9412
9413 SDValue NewChain = LoLd.getValue(1);
9414 if (TokenFactorIndex != -1) {
9415 Ops.push_back(LoLd);
9416 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009417 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009418 Ops.size());
9419 }
9420
9421 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009422 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9423 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009424
9425 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9426 St->getSrcValue(), St->getSrcValueOffset(),
9427 St->isVolatile(), St->getAlignment());
9428 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9429 St->getSrcValue(),
9430 St->getSrcValueOffset() + 4,
9431 St->isVolatile(),
9432 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009433 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009434 }
Dan Gohman475871a2008-07-27 21:46:04 +00009435 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009436}
9437
Chris Lattner6cf73262008-01-25 06:14:17 +00009438/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9439/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009440static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009441 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9442 // F[X]OR(0.0, x) -> x
9443 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009444 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9445 if (C->getValueAPF().isPosZero())
9446 return N->getOperand(1);
9447 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9448 if (C->getValueAPF().isPosZero())
9449 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009450 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009451}
9452
9453/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009454static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009455 // FAND(0.0, x) -> 0.0
9456 // FAND(x, 0.0) -> 0.0
9457 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9458 if (C->getValueAPF().isPosZero())
9459 return N->getOperand(0);
9460 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9461 if (C->getValueAPF().isPosZero())
9462 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009463 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009464}
9465
Dan Gohmane5af2d32009-01-29 01:59:02 +00009466static SDValue PerformBTCombine(SDNode *N,
9467 SelectionDAG &DAG,
9468 TargetLowering::DAGCombinerInfo &DCI) {
9469 // BT ignores high bits in the bit index operand.
9470 SDValue Op1 = N->getOperand(1);
9471 if (Op1.hasOneUse()) {
9472 unsigned BitWidth = Op1.getValueSizeInBits();
9473 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9474 APInt KnownZero, KnownOne;
9475 TargetLowering::TargetLoweringOpt TLO(DAG);
9476 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9477 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9478 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9479 DCI.CommitTargetLoweringOpt(TLO);
9480 }
9481 return SDValue();
9482}
Chris Lattner83e6c992006-10-04 06:57:07 +00009483
Eli Friedman7a5e5552009-06-07 06:52:44 +00009484static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9485 SDValue Op = N->getOperand(0);
9486 if (Op.getOpcode() == ISD::BIT_CONVERT)
9487 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009488 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009489 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009490 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009491 OpVT.getVectorElementType().getSizeInBits()) {
9492 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9493 }
9494 return SDValue();
9495}
9496
Owen Anderson99177002009-06-29 18:04:45 +00009497// On X86 and X86-64, atomic operations are lowered to locked instructions.
9498// Locked instructions, in turn, have implicit fence semantics (all memory
9499// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009500// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009501// fence-atomic-fence.
9502static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9503 SDValue atomic = N->getOperand(0);
9504 switch (atomic.getOpcode()) {
9505 case ISD::ATOMIC_CMP_SWAP:
9506 case ISD::ATOMIC_SWAP:
9507 case ISD::ATOMIC_LOAD_ADD:
9508 case ISD::ATOMIC_LOAD_SUB:
9509 case ISD::ATOMIC_LOAD_AND:
9510 case ISD::ATOMIC_LOAD_OR:
9511 case ISD::ATOMIC_LOAD_XOR:
9512 case ISD::ATOMIC_LOAD_NAND:
9513 case ISD::ATOMIC_LOAD_MIN:
9514 case ISD::ATOMIC_LOAD_MAX:
9515 case ISD::ATOMIC_LOAD_UMIN:
9516 case ISD::ATOMIC_LOAD_UMAX:
9517 break;
9518 default:
9519 return SDValue();
9520 }
Eric Christopherfd179292009-08-27 18:07:15 +00009521
Owen Anderson99177002009-06-29 18:04:45 +00009522 SDValue fence = atomic.getOperand(0);
9523 if (fence.getOpcode() != ISD::MEMBARRIER)
9524 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009525
Owen Anderson99177002009-06-29 18:04:45 +00009526 switch (atomic.getOpcode()) {
9527 case ISD::ATOMIC_CMP_SWAP:
9528 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9529 atomic.getOperand(1), atomic.getOperand(2),
9530 atomic.getOperand(3));
9531 case ISD::ATOMIC_SWAP:
9532 case ISD::ATOMIC_LOAD_ADD:
9533 case ISD::ATOMIC_LOAD_SUB:
9534 case ISD::ATOMIC_LOAD_AND:
9535 case ISD::ATOMIC_LOAD_OR:
9536 case ISD::ATOMIC_LOAD_XOR:
9537 case ISD::ATOMIC_LOAD_NAND:
9538 case ISD::ATOMIC_LOAD_MIN:
9539 case ISD::ATOMIC_LOAD_MAX:
9540 case ISD::ATOMIC_LOAD_UMIN:
9541 case ISD::ATOMIC_LOAD_UMAX:
9542 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9543 atomic.getOperand(1), atomic.getOperand(2));
9544 default:
9545 return SDValue();
9546 }
9547}
9548
Evan Cheng2e489c42009-12-16 00:53:11 +00009549static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9550 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9551 // (and (i32 x86isd::setcc_carry), 1)
9552 // This eliminates the zext. This transformation is necessary because
9553 // ISD::SETCC is always legalized to i8.
9554 DebugLoc dl = N->getDebugLoc();
9555 SDValue N0 = N->getOperand(0);
9556 EVT VT = N->getValueType(0);
9557 if (N0.getOpcode() == ISD::AND &&
9558 N0.hasOneUse() &&
9559 N0.getOperand(0).hasOneUse()) {
9560 SDValue N00 = N0.getOperand(0);
9561 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9562 return SDValue();
9563 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9564 if (!C || C->getZExtValue() != 1)
9565 return SDValue();
9566 return DAG.getNode(ISD::AND, dl, VT,
9567 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9568 N00.getOperand(0), N00.getOperand(1)),
9569 DAG.getConstant(1, VT));
9570 }
9571
9572 return SDValue();
9573}
9574
Dan Gohman475871a2008-07-27 21:46:04 +00009575SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009576 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009577 SelectionDAG &DAG = DCI.DAG;
9578 switch (N->getOpcode()) {
9579 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009580 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009581 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009582 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009583 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009584 case ISD::SHL:
9585 case ISD::SRA:
9586 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009587 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009588 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009589 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009590 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9591 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009592 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009593 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009594 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009595 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009596 }
9597
Dan Gohman475871a2008-07-27 21:46:04 +00009598 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009599}
9600
Evan Cheng60c07e12006-07-05 22:17:51 +00009601//===----------------------------------------------------------------------===//
9602// X86 Inline Assembly Support
9603//===----------------------------------------------------------------------===//
9604
Chris Lattnerb8105652009-07-20 17:51:36 +00009605static bool LowerToBSwap(CallInst *CI) {
9606 // FIXME: this should verify that we are targetting a 486 or better. If not,
9607 // we will turn this bswap into something that will be lowered to logical ops
9608 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9609 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009610
Chris Lattnerb8105652009-07-20 17:51:36 +00009611 // Verify this is a simple bswap.
9612 if (CI->getNumOperands() != 2 ||
9613 CI->getType() != CI->getOperand(1)->getType() ||
9614 !CI->getType()->isInteger())
9615 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009616
Chris Lattnerb8105652009-07-20 17:51:36 +00009617 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9618 if (!Ty || Ty->getBitWidth() % 16 != 0)
9619 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009620
Chris Lattnerb8105652009-07-20 17:51:36 +00009621 // Okay, we can do this xform, do so now.
9622 const Type *Tys[] = { Ty };
9623 Module *M = CI->getParent()->getParent()->getParent();
9624 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009625
Chris Lattnerb8105652009-07-20 17:51:36 +00009626 Value *Op = CI->getOperand(1);
9627 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009628
Chris Lattnerb8105652009-07-20 17:51:36 +00009629 CI->replaceAllUsesWith(Op);
9630 CI->eraseFromParent();
9631 return true;
9632}
9633
9634bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9635 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9636 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9637
9638 std::string AsmStr = IA->getAsmString();
9639
9640 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009641 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009642 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9643
9644 switch (AsmPieces.size()) {
9645 default: return false;
9646 case 1:
9647 AsmStr = AsmPieces[0];
9648 AsmPieces.clear();
9649 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9650
9651 // bswap $0
9652 if (AsmPieces.size() == 2 &&
9653 (AsmPieces[0] == "bswap" ||
9654 AsmPieces[0] == "bswapq" ||
9655 AsmPieces[0] == "bswapl") &&
9656 (AsmPieces[1] == "$0" ||
9657 AsmPieces[1] == "${0:q}")) {
9658 // No need to check constraints, nothing other than the equivalent of
9659 // "=r,0" would be valid here.
9660 return LowerToBSwap(CI);
9661 }
9662 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009663 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009664 AsmPieces.size() == 3 &&
9665 AsmPieces[0] == "rorw" &&
9666 AsmPieces[1] == "$$8," &&
9667 AsmPieces[2] == "${0:w}" &&
9668 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9669 return LowerToBSwap(CI);
9670 }
9671 break;
9672 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009673 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009674 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009675 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9676 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9677 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009678 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009679 SplitString(AsmPieces[0], Words, " \t");
9680 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9681 Words.clear();
9682 SplitString(AsmPieces[1], Words, " \t");
9683 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9684 Words.clear();
9685 SplitString(AsmPieces[2], Words, " \t,");
9686 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9687 Words[2] == "%edx") {
9688 return LowerToBSwap(CI);
9689 }
9690 }
9691 }
9692 }
9693 break;
9694 }
9695 return false;
9696}
9697
9698
9699
Chris Lattnerf4dff842006-07-11 02:54:03 +00009700/// getConstraintType - Given a constraint letter, return the type of
9701/// constraint it is for this target.
9702X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009703X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9704 if (Constraint.size() == 1) {
9705 switch (Constraint[0]) {
9706 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009707 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009708 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009709 case 'r':
9710 case 'R':
9711 case 'l':
9712 case 'q':
9713 case 'Q':
9714 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009715 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009716 case 'Y':
9717 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009718 case 'e':
9719 case 'Z':
9720 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009721 default:
9722 break;
9723 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009724 }
Chris Lattner4234f572007-03-25 02:14:49 +00009725 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009726}
9727
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009728/// LowerXConstraint - try to replace an X constraint, which matches anything,
9729/// with another that has more specific requirements based on the type of the
9730/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009731const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009732LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009733 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9734 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009735 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009736 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009737 return "Y";
9738 if (Subtarget->hasSSE1())
9739 return "x";
9740 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009741
Chris Lattner5e764232008-04-26 23:02:14 +00009742 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009743}
9744
Chris Lattner48884cd2007-08-25 00:47:38 +00009745/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9746/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009747void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009748 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009749 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009750 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009751 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009752 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009753
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009754 switch (Constraint) {
9755 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009756 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009758 if (C->getZExtValue() <= 31) {
9759 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009760 break;
9761 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009762 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009763 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009764 case 'J':
9765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009766 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009767 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9768 break;
9769 }
9770 }
9771 return;
9772 case 'K':
9773 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009774 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009775 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9776 break;
9777 }
9778 }
9779 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009780 case 'N':
9781 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009782 if (C->getZExtValue() <= 255) {
9783 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009784 break;
9785 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009786 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009787 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009788 case 'e': {
9789 // 32-bit signed value
9790 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9791 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009792 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9793 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009794 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009795 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009796 break;
9797 }
9798 // FIXME gcc accepts some relocatable values here too, but only in certain
9799 // memory models; it's complicated.
9800 }
9801 return;
9802 }
9803 case 'Z': {
9804 // 32-bit unsigned value
9805 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9806 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009807 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9808 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009809 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9810 break;
9811 }
9812 }
9813 // FIXME gcc accepts some relocatable values here too, but only in certain
9814 // memory models; it's complicated.
9815 return;
9816 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009817 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009818 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009819 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009820 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009821 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009822 break;
9823 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009824
Chris Lattnerdc43a882007-05-03 16:52:29 +00009825 // If we are in non-pic codegen mode, we allow the address of a global (with
9826 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009827 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009828 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009829
Chris Lattner49921962009-05-08 18:23:14 +00009830 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9831 while (1) {
9832 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9833 Offset += GA->getOffset();
9834 break;
9835 } else if (Op.getOpcode() == ISD::ADD) {
9836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9837 Offset += C->getZExtValue();
9838 Op = Op.getOperand(0);
9839 continue;
9840 }
9841 } else if (Op.getOpcode() == ISD::SUB) {
9842 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9843 Offset += -C->getZExtValue();
9844 Op = Op.getOperand(0);
9845 continue;
9846 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009847 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009848
Chris Lattner49921962009-05-08 18:23:14 +00009849 // Otherwise, this isn't something we can handle, reject it.
9850 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009851 }
Eric Christopherfd179292009-08-27 18:07:15 +00009852
Chris Lattner36c25012009-07-10 07:34:39 +00009853 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009854 // If we require an extra load to get this address, as in PIC mode, we
9855 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009856 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9857 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009858 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009859
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009860 if (hasMemory)
9861 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9862 else
9863 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009864 Result = Op;
9865 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009866 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009867 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009868
Gabor Greifba36cb52008-08-28 21:40:38 +00009869 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009870 Ops.push_back(Result);
9871 return;
9872 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009873 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9874 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009875}
9876
Chris Lattner259e97c2006-01-31 19:43:35 +00009877std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009878getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009879 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009880 if (Constraint.size() == 1) {
9881 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009882 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009883 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009884 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9885 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009886 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009887 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9888 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9889 X86::R10D,X86::R11D,X86::R12D,
9890 X86::R13D,X86::R14D,X86::R15D,
9891 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009892 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009893 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9894 X86::SI, X86::DI, X86::R8W,X86::R9W,
9895 X86::R10W,X86::R11W,X86::R12W,
9896 X86::R13W,X86::R14W,X86::R15W,
9897 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009898 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009899 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9900 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9901 X86::R10B,X86::R11B,X86::R12B,
9902 X86::R13B,X86::R14B,X86::R15B,
9903 X86::BPL, X86::SPL, 0);
9904
Owen Anderson825b72b2009-08-11 20:47:22 +00009905 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009906 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9907 X86::RSI, X86::RDI, X86::R8, X86::R9,
9908 X86::R10, X86::R11, X86::R12,
9909 X86::R13, X86::R14, X86::R15,
9910 X86::RBP, X86::RSP, 0);
9911
9912 break;
9913 }
Eric Christopherfd179292009-08-27 18:07:15 +00009914 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009915 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009916 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009917 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009918 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009919 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009920 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009921 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009922 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009923 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9924 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009925 }
9926 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009927
Chris Lattner1efa40f2006-02-22 00:56:39 +00009928 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009929}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009930
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009931std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009932X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009933 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009934 // First, see if this is a constraint that directly corresponds to an LLVM
9935 // register class.
9936 if (Constraint.size() == 1) {
9937 // GCC Constraint Letters
9938 switch (Constraint[0]) {
9939 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009940 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009941 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009942 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009943 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009944 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009945 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009946 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009947 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009948 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009949 case 'R': // LEGACY_REGS
9950 if (VT == MVT::i8)
9951 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9952 if (VT == MVT::i16)
9953 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9954 if (VT == MVT::i32 || !Subtarget->is64Bit())
9955 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9956 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009957 case 'f': // FP Stack registers.
9958 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9959 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009961 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009962 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009963 return std::make_pair(0U, X86::RFP64RegisterClass);
9964 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009965 case 'y': // MMX_REGS if MMX allowed.
9966 if (!Subtarget->hasMMX()) break;
9967 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009968 case 'Y': // SSE_REGS if SSE2 allowed
9969 if (!Subtarget->hasSSE2()) break;
9970 // FALL THROUGH.
9971 case 'x': // SSE_REGS if SSE1 allowed
9972 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009973
Owen Anderson825b72b2009-08-11 20:47:22 +00009974 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009975 default: break;
9976 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009977 case MVT::f32:
9978 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009979 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 case MVT::f64:
9981 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009982 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009983 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009984 case MVT::v16i8:
9985 case MVT::v8i16:
9986 case MVT::v4i32:
9987 case MVT::v2i64:
9988 case MVT::v4f32:
9989 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009990 return std::make_pair(0U, X86::VR128RegisterClass);
9991 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009992 break;
9993 }
9994 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009995
Chris Lattnerf76d1802006-07-31 23:26:50 +00009996 // Use the default implementation in TargetLowering to convert the register
9997 // constraint into a member of a register class.
9998 std::pair<unsigned, const TargetRegisterClass*> Res;
9999 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010000
10001 // Not found as a standard register?
10002 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010003 // Map st(0) -> st(7) -> ST0
10004 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10005 tolower(Constraint[1]) == 's' &&
10006 tolower(Constraint[2]) == 't' &&
10007 Constraint[3] == '(' &&
10008 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10009 Constraint[5] == ')' &&
10010 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010011
Chris Lattner56d77c72009-09-13 22:41:48 +000010012 Res.first = X86::ST0+Constraint[4]-'0';
10013 Res.second = X86::RFP80RegisterClass;
10014 return Res;
10015 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010016
Chris Lattner56d77c72009-09-13 22:41:48 +000010017 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010018 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010019 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010020 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010021 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010022 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010023
10024 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010025 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010026 Res.first = X86::EFLAGS;
10027 Res.second = X86::CCRRegisterClass;
10028 return Res;
10029 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010030
Dale Johannesen330169f2008-11-13 21:52:36 +000010031 // 'A' means EAX + EDX.
10032 if (Constraint == "A") {
10033 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010034 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010035 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010036 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010037 return Res;
10038 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010039
Chris Lattnerf76d1802006-07-31 23:26:50 +000010040 // Otherwise, check to see if this is a register class of the wrong value
10041 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10042 // turn into {ax},{dx}.
10043 if (Res.second->hasType(VT))
10044 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010045
Chris Lattnerf76d1802006-07-31 23:26:50 +000010046 // All of the single-register GCC register classes map their values onto
10047 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10048 // really want an 8-bit or 32-bit register, map to the appropriate register
10049 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010050 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010051 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010052 unsigned DestReg = 0;
10053 switch (Res.first) {
10054 default: break;
10055 case X86::AX: DestReg = X86::AL; break;
10056 case X86::DX: DestReg = X86::DL; break;
10057 case X86::CX: DestReg = X86::CL; break;
10058 case X86::BX: DestReg = X86::BL; break;
10059 }
10060 if (DestReg) {
10061 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010062 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010063 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010064 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010065 unsigned DestReg = 0;
10066 switch (Res.first) {
10067 default: break;
10068 case X86::AX: DestReg = X86::EAX; break;
10069 case X86::DX: DestReg = X86::EDX; break;
10070 case X86::CX: DestReg = X86::ECX; break;
10071 case X86::BX: DestReg = X86::EBX; break;
10072 case X86::SI: DestReg = X86::ESI; break;
10073 case X86::DI: DestReg = X86::EDI; break;
10074 case X86::BP: DestReg = X86::EBP; break;
10075 case X86::SP: DestReg = X86::ESP; break;
10076 }
10077 if (DestReg) {
10078 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010079 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010080 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010081 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010082 unsigned DestReg = 0;
10083 switch (Res.first) {
10084 default: break;
10085 case X86::AX: DestReg = X86::RAX; break;
10086 case X86::DX: DestReg = X86::RDX; break;
10087 case X86::CX: DestReg = X86::RCX; break;
10088 case X86::BX: DestReg = X86::RBX; break;
10089 case X86::SI: DestReg = X86::RSI; break;
10090 case X86::DI: DestReg = X86::RDI; break;
10091 case X86::BP: DestReg = X86::RBP; break;
10092 case X86::SP: DestReg = X86::RSP; break;
10093 }
10094 if (DestReg) {
10095 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010096 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010097 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010098 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010099 } else if (Res.second == X86::FR32RegisterClass ||
10100 Res.second == X86::FR64RegisterClass ||
10101 Res.second == X86::VR128RegisterClass) {
10102 // Handle references to XMM physical registers that got mapped into the
10103 // wrong class. This can happen with constraints like {xmm0} where the
10104 // target independent register mapper will just pick the first match it can
10105 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010106 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010107 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010108 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010109 Res.second = X86::FR64RegisterClass;
10110 else if (X86::VR128RegisterClass->hasType(VT))
10111 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010112 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010113
Chris Lattnerf76d1802006-07-31 23:26:50 +000010114 return Res;
10115}
Mon P Wang0c397192008-10-30 08:01:45 +000010116
10117//===----------------------------------------------------------------------===//
10118// X86 Widen vector type
10119//===----------------------------------------------------------------------===//
10120
10121/// getWidenVectorType: given a vector type, returns the type to widen
10122/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010123/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010124/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010125/// scalarizing vs using the wider vector type.
10126
Owen Andersone50ed302009-08-10 22:56:29 +000010127EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010128 assert(VT.isVector());
10129 if (isTypeLegal(VT))
10130 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010131
Mon P Wang0c397192008-10-30 08:01:45 +000010132 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10133 // type based on element type. This would speed up our search (though
10134 // it may not be worth it since the size of the list is relatively
10135 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010136 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010137 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010138
Mon P Wang0c397192008-10-30 08:01:45 +000010139 // On X86, it make sense to widen any vector wider than 1
10140 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010141 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010142
Owen Anderson825b72b2009-08-11 20:47:22 +000010143 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10144 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10145 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010146
10147 if (isTypeLegal(SVT) &&
10148 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010149 SVT.getVectorNumElements() > NElts)
10150 return SVT;
10151 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010152 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010153}