blob: 6e6447574e4917ace6a45e518e07e23a22e3b537 [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
Evan Cheng0c439eb2010-01-27 00:07:07 +00001439/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1440/// a tailcall target by changing its ABI.
1441static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
1442 return PerformTailCallOpt && CC == CallingConv::Fast;
1443}
1444
Dan Gohman98ca4f22009-08-05 01:29:28 +00001445SDValue
1446X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001447 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001448 const SmallVectorImpl<ISD::InputArg> &Ins,
1449 DebugLoc dl, SelectionDAG &DAG,
1450 const CCValAssign &VA,
1451 MachineFrameInfo *MFI,
1452 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001453 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001454 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001455 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001456 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001457 EVT ValVT;
1458
1459 // If value is passed by pointer we have address passed instead of the value
1460 // itself.
1461 if (VA.getLocInfo() == CCValAssign::Indirect)
1462 ValVT = VA.getLocVT();
1463 else
1464 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001465
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001466 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001467 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001468 // In case of tail call optimization mark all arguments mutable. Since they
1469 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001470 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001471 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001472 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001473 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001474 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001475 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001476 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001477}
1478
Dan Gohman475871a2008-07-27 21:46:04 +00001479SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 bool isVarArg,
1483 const SmallVectorImpl<ISD::InputArg> &Ins,
1484 DebugLoc dl,
1485 SelectionDAG &DAG,
1486 SmallVectorImpl<SDValue> &InVals) {
1487
Evan Cheng1bc78042006-04-26 01:20:17 +00001488 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001489 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Gordon Henriksen86737662008-01-05 16:56:59 +00001491 const Function* Fn = MF.getFunction();
1492 if (Fn->hasExternalLinkage() &&
1493 Subtarget->isTargetCygMing() &&
1494 Fn->getName() == "main")
1495 FuncInfo->setForceFramePointer(true);
1496
1497 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001498 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001499
Evan Cheng1bc78042006-04-26 01:20:17 +00001500 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001501 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001502 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001503
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001505 "Var args not supported with calling convention fastcc");
1506
Chris Lattner638402b2007-02-28 07:00:42 +00001507 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001508 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001509 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1510 ArgLocs, *DAG.getContext());
1511 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001512
Chris Lattnerf39f7712007-02-28 05:46:49 +00001513 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001514 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1516 CCValAssign &VA = ArgLocs[i];
1517 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1518 // places.
1519 assert(VA.getValNo() != LastVal &&
1520 "Don't support value assigned to multiple locs yet");
1521 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001522
Chris Lattnerf39f7712007-02-28 05:46:49 +00001523 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001524 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001525 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001527 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001529 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001533 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001534 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001535 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001536 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1537 RC = X86::VR64RegisterClass;
1538 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001539 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001540
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001541 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001542 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1545 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1546 // right size.
1547 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001548 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 DAG.getValueType(VA.getValVT()));
1550 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001551 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001552 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001553 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001554 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001555
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001556 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001557 // Handle MMX values passed in XMM regs.
1558 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1560 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001561 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1562 } else
1563 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001564 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001565 } else {
1566 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001567 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001568 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001569
1570 // If value is passed via pointer - do a load.
1571 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001573
Dan Gohman98ca4f22009-08-05 01:29:28 +00001574 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001575 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001576
Dan Gohman61a92132008-04-21 23:59:07 +00001577 // The x86-64 ABI for returning structs by value requires that we copy
1578 // the sret argument into %rax for the return. Save the argument into
1579 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001580 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001581 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1582 unsigned Reg = FuncInfo->getSRetReturnReg();
1583 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001585 FuncInfo->setSRetReturnReg(Reg);
1586 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001587 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001588 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001589 }
1590
Chris Lattnerf39f7712007-02-28 05:46:49 +00001591 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001592 // Align stack specially for tail calls.
1593 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001594 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001595
Evan Cheng1bc78042006-04-26 01:20:17 +00001596 // If the function takes variable number of arguments, make a frame index for
1597 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001599 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001600 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001601 }
1602 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001603 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1604
1605 // FIXME: We should really autogenerate these arrays
1606 static const unsigned GPR64ArgRegsWin64[] = {
1607 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001609 static const unsigned XMMArgRegsWin64[] = {
1610 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1611 };
1612 static const unsigned GPR64ArgRegs64Bit[] = {
1613 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1614 };
1615 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001616 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1617 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1618 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001619 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1620
1621 if (IsWin64) {
1622 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1623 GPR64ArgRegs = GPR64ArgRegsWin64;
1624 XMMArgRegs = XMMArgRegsWin64;
1625 } else {
1626 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1627 GPR64ArgRegs = GPR64ArgRegs64Bit;
1628 XMMArgRegs = XMMArgRegs64Bit;
1629 }
1630 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1631 TotalNumIntRegs);
1632 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1633 TotalNumXMMRegs);
1634
Devang Patel578efa92009-06-05 21:57:13 +00001635 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001636 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001637 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001638 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001639 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001640 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001641 // Kernel mode asks for SSE to be disabled, so don't push them
1642 // on the stack.
1643 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001644
Gordon Henriksen86737662008-01-05 16:56:59 +00001645 // For X86-64, if there are vararg parameters that are passed via
1646 // registers, then we must store them to their spots on the stack so they
1647 // may be loaded by deferencing the result of va_next.
1648 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001649 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1650 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001651 TotalNumXMMRegs * 16, 16,
1652 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001653
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001655 SmallVector<SDValue, 8> MemOps;
1656 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001657 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001658 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001659 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1660 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001661 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1662 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001664 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001665 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001666 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001667 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001669 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001671
Dan Gohmanface41a2009-08-16 21:24:25 +00001672 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1673 // Now store the XMM (fp + vector) parameter registers.
1674 SmallVector<SDValue, 11> SaveXMMOps;
1675 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001676
Dan Gohmanface41a2009-08-16 21:24:25 +00001677 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1678 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1679 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001680
Dan Gohmanface41a2009-08-16 21:24:25 +00001681 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1682 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001683
Dan Gohmanface41a2009-08-16 21:24:25 +00001684 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1685 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1686 X86::VR128RegisterClass);
1687 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1688 SaveXMMOps.push_back(Val);
1689 }
1690 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1691 MVT::Other,
1692 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001693 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001694
1695 if (!MemOps.empty())
1696 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1697 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001700
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001702 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001704 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001705 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001706 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001707 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001708 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001709 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001710
Gordon Henriksen86737662008-01-05 16:56:59 +00001711 if (!Is64Bit) {
1712 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001713 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001714 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1715 }
Evan Cheng25caf632006-05-23 21:06:34 +00001716
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001717 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001718
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001720}
1721
Dan Gohman475871a2008-07-27 21:46:04 +00001722SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001723X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1724 SDValue StackPtr, SDValue Arg,
1725 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001726 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001727 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001728 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001729 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001730 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001731 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001732 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001733 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001734 }
Dale Johannesenace16102009-02-03 19:33:06 +00001735 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001736 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001737}
1738
Bill Wendling64e87322009-01-16 19:25:27 +00001739/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001740/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001741SDValue
1742X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001743 SDValue &OutRetAddr, SDValue Chain,
1744 bool IsTailCall, bool Is64Bit,
1745 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001746 if (!IsTailCall || FPDiff==0) return Chain;
1747
1748 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001749 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001750 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001751
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001752 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001753 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001754 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001755}
1756
1757/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1758/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001759static SDValue
1760EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001762 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001763 // Store the return address to the appropriate stack slot.
1764 if (!FPDiff) return Chain;
1765 // Calculate the new stack slot for the return address.
1766 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001767 int NewReturnAddrFI =
Evan Chengddc419c2010-01-26 19:04:47 +00001768 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, true,false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001769 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001770 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001771 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001772 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001773 return Chain;
1774}
1775
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776SDValue
1777X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001778 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001779 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 const SmallVectorImpl<ISD::OutputArg> &Outs,
1781 const SmallVectorImpl<ISD::InputArg> &Ins,
1782 DebugLoc dl, SelectionDAG &DAG,
1783 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 MachineFunction &MF = DAG.getMachineFunction();
1785 bool Is64Bit = Subtarget->is64Bit();
1786 bool IsStructRet = CallIsStructReturn(Outs);
1787
Evan Cheng0c439eb2010-01-27 00:07:07 +00001788 if (isTailCall)
1789 // Check if it's really possible to do a tail call.
1790 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
1791 Ins, DAG);
1792
Dan Gohman98ca4f22009-08-05 01:29:28 +00001793 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001794 "Var args not supported with calling convention fastcc");
1795
Chris Lattner638402b2007-02-28 07:00:42 +00001796 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001797 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001798 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1799 ArgLocs, *DAG.getContext());
1800 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001801
Chris Lattner423c5f42007-02-28 05:31:48 +00001802 // Get a count of how many bytes are to be pushed on the stack.
1803 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001804 if (FuncIsMadeTailCallSafe(CallConv))
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001805 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001806
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001808 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001809 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001810 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001811 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1812 FPDiff = NumBytesCallerPushed - NumBytes;
1813
1814 // Set the delta of movement of the returnaddr stackslot.
1815 // But only set if delta is greater than previous delta.
1816 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1817 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1818 }
1819
Chris Lattnere563bbc2008-10-11 22:08:30 +00001820 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001821
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001823 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001824 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001825 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001826
Dan Gohman475871a2008-07-27 21:46:04 +00001827 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1828 SmallVector<SDValue, 8> MemOpChains;
1829 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001830
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001831 // Walk the register/memloc assignments, inserting copies/loads. In the case
1832 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001833 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1834 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001835 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 SDValue Arg = Outs[i].Val;
1837 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001838 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001839
Chris Lattner423c5f42007-02-28 05:31:48 +00001840 // Promote the value if needed.
1841 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001842 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001843 case CCValAssign::Full: break;
1844 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001845 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001846 break;
1847 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001848 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001849 break;
1850 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001851 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1852 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1854 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1855 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001856 } else
1857 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1858 break;
1859 case CCValAssign::BCvt:
1860 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001861 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001862 case CCValAssign::Indirect: {
1863 // Store the argument.
1864 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001865 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001866 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001867 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001868 Arg = SpillSlot;
1869 break;
1870 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001872
Chris Lattner423c5f42007-02-28 05:31:48 +00001873 if (VA.isRegLoc()) {
1874 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1875 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001876 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001877 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001879 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001880
Dan Gohman98ca4f22009-08-05 01:29:28 +00001881 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1882 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001883 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001884 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001885 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001886
Evan Cheng32fe1032006-05-25 00:59:30 +00001887 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001888 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001889 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001890
Evan Cheng347d5f72006-04-28 21:29:37 +00001891 // Build a sequence of copy-to-reg nodes chained together with token chain
1892 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001894 // Tail call byval lowering might overwrite argument registers so in case of
1895 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001896 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001897 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001898 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001899 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001900 InFlag = Chain.getValue(1);
1901 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001902
Eric Christopherfd179292009-08-27 18:07:15 +00001903
Chris Lattner88e1fd52009-07-09 04:24:46 +00001904 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001905 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1906 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001907 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001908 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1909 DAG.getNode(X86ISD::GlobalBaseReg,
1910 DebugLoc::getUnknownLoc(),
1911 getPointerTy()),
1912 InFlag);
1913 InFlag = Chain.getValue(1);
1914 } else {
1915 // If we are tail calling and generating PIC/GOT style code load the
1916 // address of the callee into ECX. The value in ecx is used as target of
1917 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1918 // for tail calls on PIC/GOT architectures. Normally we would just put the
1919 // address of GOT into ebx and then call target@PLT. But for tail calls
1920 // ebx would be restored (since ebx is callee saved) before jumping to the
1921 // target@PLT.
1922
1923 // Note: The actual moving to ECX is done further down.
1924 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1925 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1926 !G->getGlobal()->hasProtectedVisibility())
1927 Callee = LowerGlobalAddress(Callee, DAG);
1928 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001929 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001930 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001931 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932
Gordon Henriksen86737662008-01-05 16:56:59 +00001933 if (Is64Bit && isVarArg) {
1934 // From AMD64 ABI document:
1935 // For calls that may call functions that use varargs or stdargs
1936 // (prototype-less calls or calls to functions containing ellipsis (...) in
1937 // the declaration) %al is used as hidden argument to specify the number
1938 // of SSE registers used. The contents of %al do not need to match exactly
1939 // the number of registers, but must be an ubound on the number of SSE
1940 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001941
1942 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001943 // Count the number of XMM registers allocated.
1944 static const unsigned XMMArgRegs[] = {
1945 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1946 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1947 };
1948 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001949 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001950 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001951
Dale Johannesendd64c412009-02-04 00:33:20 +00001952 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001953 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001954 InFlag = Chain.getValue(1);
1955 }
1956
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001957
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001958 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001959 if (isTailCall) {
1960 // Force all the incoming stack arguments to be loaded from the stack
1961 // before any new outgoing arguments are stored to the stack, because the
1962 // outgoing stack slots may alias the incoming argument stack slots, and
1963 // the alias isn't otherwise explicit. This is slightly more conservative
1964 // than necessary, because it means that each store effectively depends
1965 // on every argument instead of just those arguments it would clobber.
1966 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1967
Dan Gohman475871a2008-07-27 21:46:04 +00001968 SmallVector<SDValue, 8> MemOpChains2;
1969 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001970 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001971 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001972 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001973 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1974 CCValAssign &VA = ArgLocs[i];
1975 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001976 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001977 SDValue Arg = Outs[i].Val;
1978 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 // Create frame index.
1980 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001981 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001982 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001983 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001984
Duncan Sands276dcbd2008-03-21 09:14:45 +00001985 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001986 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001988 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001989 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001990 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001991 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001992
Dan Gohman98ca4f22009-08-05 01:29:28 +00001993 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1994 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001995 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001996 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001997 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001998 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00002000 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002001 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002002 }
2003 }
2004
2005 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002006 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002007 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002008
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002009 // Copy arguments to their registers.
2010 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002011 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002012 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002013 InFlag = Chain.getValue(1);
2014 }
Dan Gohman475871a2008-07-27 21:46:04 +00002015 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002016
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002018 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002019 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002020 }
2021
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002022 bool WasGlobalOrExternal = false;
2023 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2024 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2025 // In the 64-bit large code model, we have to make all calls
2026 // through a register, since the call instruction's 32-bit
2027 // pc-relative offset may not be large enough to hold the whole
2028 // address.
2029 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2030 WasGlobalOrExternal = true;
2031 // If the callee is a GlobalAddress node (quite common, every direct call
2032 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2033 // it.
2034
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002035 // We should use extra load for direct calls to dllimported functions in
2036 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002037 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002038 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002039 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002040
Chris Lattner48a7d022009-07-09 05:02:21 +00002041 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2042 // external symbols most go through the PLT in PIC mode. If the symbol
2043 // has hidden or protected visibility, or if it is static or local, then
2044 // we don't need to use the PLT - we can directly call it.
2045 if (Subtarget->isTargetELF() &&
2046 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002047 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002048 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002049 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002050 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2051 Subtarget->getDarwinVers() < 9) {
2052 // PC-relative references to external symbols should go through $stub,
2053 // unless we're building with the leopard linker or later, which
2054 // automatically synthesizes these stubs.
2055 OpFlags = X86II::MO_DARWIN_STUB;
2056 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002057
Chris Lattner74e726e2009-07-09 05:27:35 +00002058 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002059 G->getOffset(), OpFlags);
2060 }
Bill Wendling056292f2008-09-16 21:48:12 +00002061 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002062 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002063 unsigned char OpFlags = 0;
2064
2065 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2066 // symbols should go through the PLT.
2067 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002068 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002069 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002070 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002071 Subtarget->getDarwinVers() < 9) {
2072 // PC-relative references to external symbols should go through $stub,
2073 // unless we're building with the leopard linker or later, which
2074 // automatically synthesizes these stubs.
2075 OpFlags = X86II::MO_DARWIN_STUB;
2076 }
Eric Christopherfd179292009-08-27 18:07:15 +00002077
Chris Lattner48a7d022009-07-09 05:02:21 +00002078 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2079 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002080 }
2081
2082 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002083 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002084
Dale Johannesendd64c412009-02-04 00:33:20 +00002085 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002086 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002087 Callee,InFlag);
2088 Callee = DAG.getRegister(Opc, getPointerTy());
2089 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002090 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002092
Chris Lattnerd96d0722007-02-25 06:40:16 +00002093 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002094 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002095 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002096
Dan Gohman98ca4f22009-08-05 01:29:28 +00002097 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002098 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2099 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002102
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002103 Ops.push_back(Chain);
2104 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002105
Dan Gohman98ca4f22009-08-05 01:29:28 +00002106 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002107 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002108
Gordon Henriksen86737662008-01-05 16:56:59 +00002109 // Add argument registers to the end of the list so that they are known live
2110 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002111 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2112 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2113 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Evan Cheng586ccac2008-03-18 23:36:35 +00002115 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002116 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002117 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2118
2119 // Add an implicit use of AL for x86 vararg functions.
2120 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002121 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002122
Gabor Greifba36cb52008-08-28 21:40:38 +00002123 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002124 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002125
Dan Gohman98ca4f22009-08-05 01:29:28 +00002126 if (isTailCall) {
2127 // If this is the first return lowered for this function, add the regs
2128 // to the liveout set for the function.
2129 if (MF.getRegInfo().liveout_empty()) {
2130 SmallVector<CCValAssign, 16> RVLocs;
2131 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2132 *DAG.getContext());
2133 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2134 for (unsigned i = 0; i != RVLocs.size(); ++i)
2135 if (RVLocs[i].isRegLoc())
2136 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2137 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002138
Dan Gohman98ca4f22009-08-05 01:29:28 +00002139 assert(((Callee.getOpcode() == ISD::Register &&
2140 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002141 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2143 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002144 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002145
2146 return DAG.getNode(X86ISD::TC_RETURN, dl,
2147 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002148 }
2149
Dale Johannesenace16102009-02-03 19:33:06 +00002150 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002151 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002152
Chris Lattner2d297092006-05-23 18:50:38 +00002153 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002154 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002157 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002158 // If this is is a call to a struct-return function, the callee
2159 // pops the hidden struct pointer, so we have to push it back.
2160 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002161 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002162 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002163 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002164
Gordon Henriksenae636f82008-01-03 16:47:34 +00002165 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002166 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002167 DAG.getIntPtrConstant(NumBytes, true),
2168 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2169 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002170 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002171 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002172
Chris Lattner3085e152007-02-25 08:59:22 +00002173 // Handle result values, copying them out of physregs into vregs that we
2174 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2176 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002177}
2178
Evan Cheng25ab6902006-09-08 06:48:29 +00002179
2180//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002181// Fast Calling Convention (tail call) implementation
2182//===----------------------------------------------------------------------===//
2183
2184// Like std call, callee cleans arguments, convention except that ECX is
2185// reserved for storing the tail called function address. Only 2 registers are
2186// free for argument passing (inreg). Tail call optimization is performed
2187// provided:
2188// * tailcallopt is enabled
2189// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002190// On X86_64 architecture with GOT-style position independent code only local
2191// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002192// To keep the stack aligned according to platform abi the function
2193// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2194// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002195// If a tail called function callee has more arguments than the caller the
2196// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002197// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002198// original REtADDR, but before the saved framepointer or the spilled registers
2199// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2200// stack layout:
2201// arg1
2202// arg2
2203// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002204// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002205// move area ]
2206// (possible EBP)
2207// ESI
2208// EDI
2209// local1 ..
2210
2211/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2212/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002213unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002214 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002215 MachineFunction &MF = DAG.getMachineFunction();
2216 const TargetMachine &TM = MF.getTarget();
2217 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2218 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002219 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002220 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002221 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002222 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2223 // Number smaller than 12 so just add the difference.
2224 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2225 } else {
2226 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002227 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002228 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002229 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002230 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002231}
2232
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2234/// for tail call optimization. Targets which want to do tail call
2235/// optimization should implement this function.
2236bool
2237X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002238 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002239 bool isVarArg,
2240 const SmallVectorImpl<ISD::InputArg> &Ins,
2241 SelectionDAG& DAG) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +00002242 if (CalleeCC == CallingConv::Fast &&
2243 DAG.getMachineFunction().getFunction()->getCallingConv() == CalleeCC)
2244 return true;
2245 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002246}
2247
Dan Gohman3df24e62008-09-03 23:12:08 +00002248FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002249X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2250 DwarfWriter *dw,
2251 DenseMap<const Value *, unsigned> &vm,
2252 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2253 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002254#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002255 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002256#endif
2257 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002258 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002259#ifndef NDEBUG
2260 , cil
2261#endif
2262 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002263}
2264
2265
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002266//===----------------------------------------------------------------------===//
2267// Other Lowering Hooks
2268//===----------------------------------------------------------------------===//
2269
2270
Dan Gohman475871a2008-07-27 21:46:04 +00002271SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002272 MachineFunction &MF = DAG.getMachineFunction();
2273 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2274 int ReturnAddrIndex = FuncInfo->getRAIndex();
2275
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002276 if (ReturnAddrIndex == 0) {
2277 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002278 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002279 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2280 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002281 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002282 }
2283
Evan Cheng25ab6902006-09-08 06:48:29 +00002284 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002285}
2286
2287
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002288bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2289 bool hasSymbolicDisplacement) {
2290 // Offset should fit into 32 bit immediate field.
2291 if (!isInt32(Offset))
2292 return false;
2293
2294 // If we don't have a symbolic displacement - we don't have any extra
2295 // restrictions.
2296 if (!hasSymbolicDisplacement)
2297 return true;
2298
2299 // FIXME: Some tweaks might be needed for medium code model.
2300 if (M != CodeModel::Small && M != CodeModel::Kernel)
2301 return false;
2302
2303 // For small code model we assume that latest object is 16MB before end of 31
2304 // bits boundary. We may also accept pretty large negative constants knowing
2305 // that all objects are in the positive half of address space.
2306 if (M == CodeModel::Small && Offset < 16*1024*1024)
2307 return true;
2308
2309 // For kernel code model we know that all object resist in the negative half
2310 // of 32bits address space. We may not accept negative offsets, since they may
2311 // be just off and we may accept pretty large positive ones.
2312 if (M == CodeModel::Kernel && Offset > 0)
2313 return true;
2314
2315 return false;
2316}
2317
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002318/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2319/// specific condition code, returning the condition code and the LHS/RHS of the
2320/// comparison to make.
2321static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2322 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002323 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002324 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2325 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2326 // X > -1 -> X == 0, jump !sign.
2327 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002328 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002329 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2330 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002331 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002332 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002333 // X < 1 -> X <= 0
2334 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002335 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002336 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002337 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002338
Evan Chengd9558e02006-01-06 00:43:03 +00002339 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002340 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002341 case ISD::SETEQ: return X86::COND_E;
2342 case ISD::SETGT: return X86::COND_G;
2343 case ISD::SETGE: return X86::COND_GE;
2344 case ISD::SETLT: return X86::COND_L;
2345 case ISD::SETLE: return X86::COND_LE;
2346 case ISD::SETNE: return X86::COND_NE;
2347 case ISD::SETULT: return X86::COND_B;
2348 case ISD::SETUGT: return X86::COND_A;
2349 case ISD::SETULE: return X86::COND_BE;
2350 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002351 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002352 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002353
Chris Lattner4c78e022008-12-23 23:42:27 +00002354 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002355
Chris Lattner4c78e022008-12-23 23:42:27 +00002356 // If LHS is a foldable load, but RHS is not, flip the condition.
2357 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2358 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2359 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2360 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002361 }
2362
Chris Lattner4c78e022008-12-23 23:42:27 +00002363 switch (SetCCOpcode) {
2364 default: break;
2365 case ISD::SETOLT:
2366 case ISD::SETOLE:
2367 case ISD::SETUGT:
2368 case ISD::SETUGE:
2369 std::swap(LHS, RHS);
2370 break;
2371 }
2372
2373 // On a floating point condition, the flags are set as follows:
2374 // ZF PF CF op
2375 // 0 | 0 | 0 | X > Y
2376 // 0 | 0 | 1 | X < Y
2377 // 1 | 0 | 0 | X == Y
2378 // 1 | 1 | 1 | unordered
2379 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002380 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002381 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002382 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002383 case ISD::SETOLT: // flipped
2384 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002385 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002386 case ISD::SETOLE: // flipped
2387 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002388 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002389 case ISD::SETUGT: // flipped
2390 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002391 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002392 case ISD::SETUGE: // flipped
2393 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002394 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002395 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002396 case ISD::SETNE: return X86::COND_NE;
2397 case ISD::SETUO: return X86::COND_P;
2398 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002399 case ISD::SETOEQ:
2400 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002401 }
Evan Chengd9558e02006-01-06 00:43:03 +00002402}
2403
Evan Cheng4a460802006-01-11 00:33:36 +00002404/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2405/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002406/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002407static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002408 switch (X86CC) {
2409 default:
2410 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002411 case X86::COND_B:
2412 case X86::COND_BE:
2413 case X86::COND_E:
2414 case X86::COND_P:
2415 case X86::COND_A:
2416 case X86::COND_AE:
2417 case X86::COND_NE:
2418 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002419 return true;
2420 }
2421}
2422
Evan Chengeb2f9692009-10-27 19:56:55 +00002423/// isFPImmLegal - Returns true if the target can instruction select the
2424/// specified FP immediate natively. If false, the legalizer will
2425/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002426bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002427 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2428 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2429 return true;
2430 }
2431 return false;
2432}
2433
Nate Begeman9008ca62009-04-27 18:41:29 +00002434/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2435/// the specified range (L, H].
2436static bool isUndefOrInRange(int Val, int Low, int Hi) {
2437 return (Val < 0) || (Val >= Low && Val < Hi);
2438}
2439
2440/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2441/// specified value.
2442static bool isUndefOrEqual(int Val, int CmpVal) {
2443 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002444 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002445 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002446}
2447
Nate Begeman9008ca62009-04-27 18:41:29 +00002448/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2449/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2450/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002451static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002452 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002453 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 return (Mask[0] < 2 && Mask[1] < 2);
2456 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002457}
2458
Nate Begeman9008ca62009-04-27 18:41:29 +00002459bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002460 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002461 N->getMask(M);
2462 return ::isPSHUFDMask(M, N->getValueType(0));
2463}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002464
Nate Begeman9008ca62009-04-27 18:41:29 +00002465/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2466/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002467static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002468 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002469 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002470
Nate Begeman9008ca62009-04-27 18:41:29 +00002471 // Lower quadword copied in order or undef.
2472 for (int i = 0; i != 4; ++i)
2473 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002474 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002475
Evan Cheng506d3df2006-03-29 23:07:14 +00002476 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002477 for (int i = 4; i != 8; ++i)
2478 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002479 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002480
Evan Cheng506d3df2006-03-29 23:07:14 +00002481 return true;
2482}
2483
Nate Begeman9008ca62009-04-27 18:41:29 +00002484bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002485 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002486 N->getMask(M);
2487 return ::isPSHUFHWMask(M, N->getValueType(0));
2488}
Evan Cheng506d3df2006-03-29 23:07:14 +00002489
Nate Begeman9008ca62009-04-27 18:41:29 +00002490/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2491/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002492static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002493 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002494 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002495
Rafael Espindola15684b22009-04-24 12:40:33 +00002496 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002497 for (int i = 4; i != 8; ++i)
2498 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002499 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002500
Rafael Espindola15684b22009-04-24 12:40:33 +00002501 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002502 for (int i = 0; i != 4; ++i)
2503 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002504 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002505
Rafael Espindola15684b22009-04-24 12:40:33 +00002506 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002507}
2508
Nate Begeman9008ca62009-04-27 18:41:29 +00002509bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002510 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002511 N->getMask(M);
2512 return ::isPSHUFLWMask(M, N->getValueType(0));
2513}
2514
Nate Begemana09008b2009-10-19 02:17:23 +00002515/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2516/// is suitable for input to PALIGNR.
2517static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2518 bool hasSSSE3) {
2519 int i, e = VT.getVectorNumElements();
2520
2521 // Do not handle v2i64 / v2f64 shuffles with palignr.
2522 if (e < 4 || !hasSSSE3)
2523 return false;
2524
2525 for (i = 0; i != e; ++i)
2526 if (Mask[i] >= 0)
2527 break;
2528
2529 // All undef, not a palignr.
2530 if (i == e)
2531 return false;
2532
2533 // Determine if it's ok to perform a palignr with only the LHS, since we
2534 // don't have access to the actual shuffle elements to see if RHS is undef.
2535 bool Unary = Mask[i] < (int)e;
2536 bool NeedsUnary = false;
2537
2538 int s = Mask[i] - i;
2539
2540 // Check the rest of the elements to see if they are consecutive.
2541 for (++i; i != e; ++i) {
2542 int m = Mask[i];
2543 if (m < 0)
2544 continue;
2545
2546 Unary = Unary && (m < (int)e);
2547 NeedsUnary = NeedsUnary || (m < s);
2548
2549 if (NeedsUnary && !Unary)
2550 return false;
2551 if (Unary && m != ((s+i) & (e-1)))
2552 return false;
2553 if (!Unary && m != (s+i))
2554 return false;
2555 }
2556 return true;
2557}
2558
2559bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2560 SmallVector<int, 8> M;
2561 N->getMask(M);
2562 return ::isPALIGNRMask(M, N->getValueType(0), true);
2563}
2564
Evan Cheng14aed5e2006-03-24 01:18:28 +00002565/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2566/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002567static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002568 int NumElems = VT.getVectorNumElements();
2569 if (NumElems != 2 && NumElems != 4)
2570 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002571
Nate Begeman9008ca62009-04-27 18:41:29 +00002572 int Half = NumElems / 2;
2573 for (int i = 0; i < Half; ++i)
2574 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002575 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002576 for (int i = Half; i < NumElems; ++i)
2577 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002578 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002579
Evan Cheng14aed5e2006-03-24 01:18:28 +00002580 return true;
2581}
2582
Nate Begeman9008ca62009-04-27 18:41:29 +00002583bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2584 SmallVector<int, 8> M;
2585 N->getMask(M);
2586 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002587}
2588
Evan Cheng213d2cf2007-05-17 18:45:50 +00002589/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002590/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2591/// half elements to come from vector 1 (which would equal the dest.) and
2592/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002593static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002595
2596 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002597 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002598
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 int Half = NumElems / 2;
2600 for (int i = 0; i < Half; ++i)
2601 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002602 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 for (int i = Half; i < NumElems; ++i)
2604 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002605 return false;
2606 return true;
2607}
2608
Nate Begeman9008ca62009-04-27 18:41:29 +00002609static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2610 SmallVector<int, 8> M;
2611 N->getMask(M);
2612 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002613}
2614
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002615/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2616/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002617bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2618 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002619 return false;
2620
Evan Cheng2064a2b2006-03-28 06:50:32 +00002621 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2623 isUndefOrEqual(N->getMaskElt(1), 7) &&
2624 isUndefOrEqual(N->getMaskElt(2), 2) &&
2625 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002626}
2627
Nate Begeman0b10b912009-11-07 23:17:15 +00002628/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2629/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2630/// <2, 3, 2, 3>
2631bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2632 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2633
2634 if (NumElems != 4)
2635 return false;
2636
2637 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2638 isUndefOrEqual(N->getMaskElt(1), 3) &&
2639 isUndefOrEqual(N->getMaskElt(2), 2) &&
2640 isUndefOrEqual(N->getMaskElt(3), 3);
2641}
2642
Evan Cheng5ced1d82006-04-06 23:23:56 +00002643/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2644/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002645bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2646 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002647
Evan Cheng5ced1d82006-04-06 23:23:56 +00002648 if (NumElems != 2 && NumElems != 4)
2649 return false;
2650
Evan Chengc5cdff22006-04-07 21:53:05 +00002651 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002652 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002653 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002654
Evan Chengc5cdff22006-04-07 21:53:05 +00002655 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002657 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002658
2659 return true;
2660}
2661
Nate Begeman0b10b912009-11-07 23:17:15 +00002662/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2663/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2664bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002665 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002666
Evan Cheng5ced1d82006-04-06 23:23:56 +00002667 if (NumElems != 2 && NumElems != 4)
2668 return false;
2669
Evan Chengc5cdff22006-04-07 21:53:05 +00002670 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002672 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002673
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 for (unsigned i = 0; i < NumElems/2; ++i)
2675 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002676 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002677
2678 return true;
2679}
2680
Evan Cheng0038e592006-03-28 00:39:58 +00002681/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2682/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002683static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002684 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002685 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002686 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002687 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002688
Nate Begeman9008ca62009-04-27 18:41:29 +00002689 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2690 int BitI = Mask[i];
2691 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002692 if (!isUndefOrEqual(BitI, j))
2693 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002694 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002695 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002696 return false;
2697 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002698 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002699 return false;
2700 }
Evan Cheng0038e592006-03-28 00:39:58 +00002701 }
Evan Cheng0038e592006-03-28 00:39:58 +00002702 return true;
2703}
2704
Nate Begeman9008ca62009-04-27 18:41:29 +00002705bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2706 SmallVector<int, 8> M;
2707 N->getMask(M);
2708 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002709}
2710
Evan Cheng4fcb9222006-03-28 02:43:26 +00002711/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2712/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002713static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002714 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002715 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002716 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002717 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002718
Nate Begeman9008ca62009-04-27 18:41:29 +00002719 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2720 int BitI = Mask[i];
2721 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002722 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002723 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002724 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002725 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002726 return false;
2727 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002728 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002729 return false;
2730 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002731 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002732 return true;
2733}
2734
Nate Begeman9008ca62009-04-27 18:41:29 +00002735bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2736 SmallVector<int, 8> M;
2737 N->getMask(M);
2738 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002739}
2740
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002741/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2742/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2743/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002744static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002746 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002747 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002748
Nate Begeman9008ca62009-04-27 18:41:29 +00002749 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2750 int BitI = Mask[i];
2751 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002752 if (!isUndefOrEqual(BitI, j))
2753 return false;
2754 if (!isUndefOrEqual(BitI1, j))
2755 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002756 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002757 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002758}
2759
Nate Begeman9008ca62009-04-27 18:41:29 +00002760bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2761 SmallVector<int, 8> M;
2762 N->getMask(M);
2763 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2764}
2765
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002766/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2767/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2768/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002769static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002770 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002771 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2772 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002773
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2775 int BitI = Mask[i];
2776 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002777 if (!isUndefOrEqual(BitI, j))
2778 return false;
2779 if (!isUndefOrEqual(BitI1, j))
2780 return false;
2781 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002782 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002783}
2784
Nate Begeman9008ca62009-04-27 18:41:29 +00002785bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2786 SmallVector<int, 8> M;
2787 N->getMask(M);
2788 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2789}
2790
Evan Cheng017dcc62006-04-21 01:05:10 +00002791/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2792/// specifies a shuffle of elements that is suitable for input to MOVSS,
2793/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002794static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002795 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002796 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002797
2798 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002799
Nate Begeman9008ca62009-04-27 18:41:29 +00002800 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002801 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002802
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 for (int i = 1; i < NumElts; ++i)
2804 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002805 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002806
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002807 return true;
2808}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002809
Nate Begeman9008ca62009-04-27 18:41:29 +00002810bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2811 SmallVector<int, 8> M;
2812 N->getMask(M);
2813 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002814}
2815
Evan Cheng017dcc62006-04-21 01:05:10 +00002816/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2817/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002818/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002819static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 bool V2IsSplat = false, bool V2IsUndef = false) {
2821 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002822 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
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 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002826 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002827
Nate Begeman9008ca62009-04-27 18:41:29 +00002828 for (int i = 1; i < NumOps; ++i)
2829 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2830 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2831 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002833
Evan Cheng39623da2006-04-20 08:58:49 +00002834 return true;
2835}
2836
Nate Begeman9008ca62009-04-27 18:41:29 +00002837static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002838 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002839 SmallVector<int, 8> M;
2840 N->getMask(M);
2841 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002842}
2843
Evan Chengd9539472006-04-14 21:59:03 +00002844/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2845/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002846bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2847 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002848 return false;
2849
2850 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002851 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002852 int Elt = N->getMaskElt(i);
2853 if (Elt >= 0 && Elt != 1)
2854 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002855 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002856
2857 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002858 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002859 int Elt = N->getMaskElt(i);
2860 if (Elt >= 0 && Elt != 3)
2861 return false;
2862 if (Elt == 3)
2863 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002864 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002865 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002866 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002867 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002868}
2869
2870/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2871/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002872bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2873 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002874 return false;
2875
2876 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002877 for (unsigned i = 0; i < 2; ++i)
2878 if (N->getMaskElt(i) > 0)
2879 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002880
2881 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002882 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 int Elt = N->getMaskElt(i);
2884 if (Elt >= 0 && Elt != 2)
2885 return false;
2886 if (Elt == 2)
2887 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002888 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002889 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002890 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002891}
2892
Evan Cheng0b457f02008-09-25 20:50:48 +00002893/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2894/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002895bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2896 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002897
Nate Begeman9008ca62009-04-27 18:41:29 +00002898 for (int i = 0; i < e; ++i)
2899 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002900 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002901 for (int i = 0; i < e; ++i)
2902 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002903 return false;
2904 return true;
2905}
2906
Evan Cheng63d33002006-03-22 08:01:21 +00002907/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002908/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002909unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002910 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2911 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2912
Evan Chengb9df0ca2006-03-22 02:53:00 +00002913 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2914 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002915 for (int i = 0; i < NumOperands; ++i) {
2916 int Val = SVOp->getMaskElt(NumOperands-i-1);
2917 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002918 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002919 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002920 if (i != NumOperands - 1)
2921 Mask <<= Shift;
2922 }
Evan Cheng63d33002006-03-22 08:01:21 +00002923 return Mask;
2924}
2925
Evan Cheng506d3df2006-03-29 23:07:14 +00002926/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002927/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002928unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002929 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002930 unsigned Mask = 0;
2931 // 8 nodes, but we only care about the last 4.
2932 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 int Val = SVOp->getMaskElt(i);
2934 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002935 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002936 if (i != 4)
2937 Mask <<= 2;
2938 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002939 return Mask;
2940}
2941
2942/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002943/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002944unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002945 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002946 unsigned Mask = 0;
2947 // 8 nodes, but we only care about the first 4.
2948 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002949 int Val = SVOp->getMaskElt(i);
2950 if (Val >= 0)
2951 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002952 if (i != 0)
2953 Mask <<= 2;
2954 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002955 return Mask;
2956}
2957
Nate Begemana09008b2009-10-19 02:17:23 +00002958/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2959/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2960unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2961 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2962 EVT VVT = N->getValueType(0);
2963 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2964 int Val = 0;
2965
2966 unsigned i, e;
2967 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2968 Val = SVOp->getMaskElt(i);
2969 if (Val >= 0)
2970 break;
2971 }
2972 return (Val - i) * EltSize;
2973}
2974
Evan Cheng37b73872009-07-30 08:33:02 +00002975/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2976/// constant +0.0.
2977bool X86::isZeroNode(SDValue Elt) {
2978 return ((isa<ConstantSDNode>(Elt) &&
2979 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2980 (isa<ConstantFPSDNode>(Elt) &&
2981 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2982}
2983
Nate Begeman9008ca62009-04-27 18:41:29 +00002984/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2985/// their permute mask.
2986static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2987 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002988 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002989 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002991
Nate Begeman5a5ca152009-04-29 05:20:52 +00002992 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002993 int idx = SVOp->getMaskElt(i);
2994 if (idx < 0)
2995 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002996 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002997 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002998 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002999 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003000 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3002 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003003}
3004
Evan Cheng779ccea2007-12-07 21:30:01 +00003005/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3006/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003007static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003008 unsigned NumElems = VT.getVectorNumElements();
3009 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 int idx = Mask[i];
3011 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003012 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003013 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003015 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003017 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003018}
3019
Evan Cheng533a0aa2006-04-19 20:35:22 +00003020/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3021/// match movhlps. The lower half elements should come from upper half of
3022/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003023/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003024static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3025 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003026 return false;
3027 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003029 return false;
3030 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003032 return false;
3033 return true;
3034}
3035
Evan Cheng5ced1d82006-04-06 23:23:56 +00003036/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003037/// is promoted to a vector. It also returns the LoadSDNode by reference if
3038/// required.
3039static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003040 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3041 return false;
3042 N = N->getOperand(0).getNode();
3043 if (!ISD::isNON_EXTLoad(N))
3044 return false;
3045 if (LD)
3046 *LD = cast<LoadSDNode>(N);
3047 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003048}
3049
Evan Cheng533a0aa2006-04-19 20:35:22 +00003050/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3051/// match movlp{s|d}. The lower half elements should come from lower half of
3052/// V1 (and in order), and the upper half elements should come from the upper
3053/// half of V2 (and in order). And since V1 will become the source of the
3054/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003055static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3056 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003057 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003058 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003059 // Is V2 is a vector load, don't do this transformation. We will try to use
3060 // load folding shufps op.
3061 if (ISD::isNON_EXTLoad(V2))
3062 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003063
Nate Begeman5a5ca152009-04-29 05:20:52 +00003064 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003065
Evan Cheng533a0aa2006-04-19 20:35:22 +00003066 if (NumElems != 2 && NumElems != 4)
3067 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003068 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003069 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003070 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003071 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003072 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003073 return false;
3074 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003075}
3076
Evan Cheng39623da2006-04-20 08:58:49 +00003077/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3078/// all the same.
3079static bool isSplatVector(SDNode *N) {
3080 if (N->getOpcode() != ISD::BUILD_VECTOR)
3081 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003082
Dan Gohman475871a2008-07-27 21:46:04 +00003083 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003084 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3085 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003086 return false;
3087 return true;
3088}
3089
Evan Cheng213d2cf2007-05-17 18:45:50 +00003090/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003091/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003092/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003093static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003094 SDValue V1 = N->getOperand(0);
3095 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003096 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3097 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003099 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003101 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3102 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003103 if (Opc != ISD::BUILD_VECTOR ||
3104 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003105 return false;
3106 } else if (Idx >= 0) {
3107 unsigned Opc = V1.getOpcode();
3108 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3109 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003110 if (Opc != ISD::BUILD_VECTOR ||
3111 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003112 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003113 }
3114 }
3115 return true;
3116}
3117
3118/// getZeroVector - Returns a vector of specified type with all zero elements.
3119///
Owen Andersone50ed302009-08-10 22:56:29 +00003120static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003121 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003122 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003123
Chris Lattner8a594482007-11-25 00:24:49 +00003124 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3125 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003126 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003127 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003128 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3129 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003130 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3132 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003133 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3135 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003136 }
Dale Johannesenace16102009-02-03 19:33:06 +00003137 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003138}
3139
Chris Lattner8a594482007-11-25 00:24:49 +00003140/// getOnesVector - Returns a vector of specified type with all bits set.
3141///
Owen Andersone50ed302009-08-10 22:56:29 +00003142static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003143 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003144
Chris Lattner8a594482007-11-25 00:24:49 +00003145 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3146 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003148 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003149 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003151 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003153 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003154}
3155
3156
Evan Cheng39623da2006-04-20 08:58:49 +00003157/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3158/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003159static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003160 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003161 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003162
Evan Cheng39623da2006-04-20 08:58:49 +00003163 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 SmallVector<int, 8> MaskVec;
3165 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003166
Nate Begeman5a5ca152009-04-29 05:20:52 +00003167 for (unsigned i = 0; i != NumElems; ++i) {
3168 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 MaskVec[i] = NumElems;
3170 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003171 }
Evan Cheng39623da2006-04-20 08:58:49 +00003172 }
Evan Cheng39623da2006-04-20 08:58:49 +00003173 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3175 SVOp->getOperand(1), &MaskVec[0]);
3176 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003177}
3178
Evan Cheng017dcc62006-04-21 01:05:10 +00003179/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3180/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003181static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 SDValue V2) {
3183 unsigned NumElems = VT.getVectorNumElements();
3184 SmallVector<int, 8> Mask;
3185 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003186 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 Mask.push_back(i);
3188 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003189}
3190
Nate Begeman9008ca62009-04-27 18:41:29 +00003191/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003192static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003193 SDValue V2) {
3194 unsigned NumElems = VT.getVectorNumElements();
3195 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003196 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 Mask.push_back(i);
3198 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003199 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003201}
3202
Nate Begeman9008ca62009-04-27 18:41:29 +00003203/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003204static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003205 SDValue V2) {
3206 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003207 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003208 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003209 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 Mask.push_back(i + Half);
3211 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003212 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003214}
3215
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003216/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003217static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 bool HasSSE2) {
3219 if (SV->getValueType(0).getVectorNumElements() <= 4)
3220 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003221
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003223 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 DebugLoc dl = SV->getDebugLoc();
3225 SDValue V1 = SV->getOperand(0);
3226 int NumElems = VT.getVectorNumElements();
3227 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003228
Nate Begeman9008ca62009-04-27 18:41:29 +00003229 // unpack elements to the correct location
3230 while (NumElems > 4) {
3231 if (EltNo < NumElems/2) {
3232 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3233 } else {
3234 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3235 EltNo -= NumElems/2;
3236 }
3237 NumElems >>= 1;
3238 }
Eric Christopherfd179292009-08-27 18:07:15 +00003239
Nate Begeman9008ca62009-04-27 18:41:29 +00003240 // Perform the splat.
3241 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003242 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003243 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3244 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003245}
3246
Evan Chengba05f722006-04-21 23:03:30 +00003247/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003248/// vector of zero or undef vector. This produces a shuffle where the low
3249/// element of V2 is swizzled into the zero/undef vector, landing at element
3250/// 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 +00003251static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003252 bool isZero, bool HasSSE2,
3253 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003254 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003255 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3257 unsigned NumElems = VT.getVectorNumElements();
3258 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003259 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003260 // If this is the insertion idx, put the low elt of V2 here.
3261 MaskVec.push_back(i == Idx ? NumElems : i);
3262 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003263}
3264
Evan Chengf26ffe92008-05-29 08:22:04 +00003265/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3266/// a shuffle that is zero.
3267static
Nate Begeman9008ca62009-04-27 18:41:29 +00003268unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3269 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003270 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003271 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003272 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 int Idx = SVOp->getMaskElt(Index);
3274 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003275 ++NumZeros;
3276 continue;
3277 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003278 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003279 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003280 ++NumZeros;
3281 else
3282 break;
3283 }
3284 return NumZeros;
3285}
3286
3287/// isVectorShift - Returns true if the shuffle can be implemented as a
3288/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003289/// FIXME: split into pslldqi, psrldqi, palignr variants.
3290static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003291 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003293
3294 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003295 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003296 if (!NumZeros) {
3297 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003299 if (!NumZeros)
3300 return false;
3301 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003302 bool SeenV1 = false;
3303 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003304 for (int i = NumZeros; i < NumElems; ++i) {
3305 int Val = isLeft ? (i - NumZeros) : i;
3306 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3307 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003308 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003309 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003310 SeenV1 = true;
3311 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003312 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003313 SeenV2 = true;
3314 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003316 return false;
3317 }
3318 if (SeenV1 && SeenV2)
3319 return false;
3320
Nate Begeman9008ca62009-04-27 18:41:29 +00003321 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003322 ShAmt = NumZeros;
3323 return true;
3324}
3325
3326
Evan Chengc78d3b42006-04-24 18:01:45 +00003327/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3328///
Dan Gohman475871a2008-07-27 21:46:04 +00003329static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003330 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003331 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003332 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003333 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003334
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003335 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003336 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003337 bool First = true;
3338 for (unsigned i = 0; i < 16; ++i) {
3339 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3340 if (ThisIsNonZero && First) {
3341 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003343 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003345 First = false;
3346 }
3347
3348 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003349 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003350 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3351 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003352 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003354 }
3355 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3357 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3358 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003359 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003361 } else
3362 ThisElt = LastElt;
3363
Gabor Greifba36cb52008-08-28 21:40:38 +00003364 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003366 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003367 }
3368 }
3369
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003371}
3372
Bill Wendlinga348c562007-03-22 18:42:45 +00003373/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003374///
Dan Gohman475871a2008-07-27 21:46:04 +00003375static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003376 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003377 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003378 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003379 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003380
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003381 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003382 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003383 bool First = true;
3384 for (unsigned i = 0; i < 8; ++i) {
3385 bool isNonZero = (NonZeros & (1 << i)) != 0;
3386 if (isNonZero) {
3387 if (First) {
3388 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003390 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003392 First = false;
3393 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003394 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003396 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003397 }
3398 }
3399
3400 return V;
3401}
3402
Evan Chengf26ffe92008-05-29 08:22:04 +00003403/// getVShift - Return a vector logical shift node.
3404///
Owen Andersone50ed302009-08-10 22:56:29 +00003405static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003406 unsigned NumBits, SelectionDAG &DAG,
3407 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003408 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003410 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003411 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3412 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3413 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003414 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003415}
3416
Dan Gohman475871a2008-07-27 21:46:04 +00003417SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003418X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3419 SelectionDAG &DAG) {
3420
3421 // Check if the scalar load can be widened into a vector load. And if
3422 // the address is "base + cst" see if the cst can be "absorbed" into
3423 // the shuffle mask.
3424 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3425 SDValue Ptr = LD->getBasePtr();
3426 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3427 return SDValue();
3428 EVT PVT = LD->getValueType(0);
3429 if (PVT != MVT::i32 && PVT != MVT::f32)
3430 return SDValue();
3431
3432 int FI = -1;
3433 int64_t Offset = 0;
3434 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3435 FI = FINode->getIndex();
3436 Offset = 0;
3437 } else if (Ptr.getOpcode() == ISD::ADD &&
3438 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3439 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3440 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3441 Offset = Ptr.getConstantOperandVal(1);
3442 Ptr = Ptr.getOperand(0);
3443 } else {
3444 return SDValue();
3445 }
3446
3447 SDValue Chain = LD->getChain();
3448 // Make sure the stack object alignment is at least 16.
3449 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3450 if (DAG.InferPtrAlignment(Ptr) < 16) {
3451 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003452 // Can't change the alignment. FIXME: It's possible to compute
3453 // the exact stack offset and reference FI + adjust offset instead.
3454 // If someone *really* cares about this. That's the way to implement it.
3455 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003456 } else {
3457 MFI->setObjectAlignment(FI, 16);
3458 }
3459 }
3460
3461 // (Offset % 16) must be multiple of 4. Then address is then
3462 // Ptr + (Offset & ~15).
3463 if (Offset < 0)
3464 return SDValue();
3465 if ((Offset % 16) & 3)
3466 return SDValue();
3467 int64_t StartOffset = Offset & ~15;
3468 if (StartOffset)
3469 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3470 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3471
3472 int EltNo = (Offset - StartOffset) >> 2;
3473 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3474 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3475 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3476 // Canonicalize it to a v4i32 shuffle.
3477 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3478 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3479 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3480 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3481 }
3482
3483 return SDValue();
3484}
3485
3486SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003487X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003488 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003489 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003490 if (ISD::isBuildVectorAllZeros(Op.getNode())
3491 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003492 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3493 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3494 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003496 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003497
Gabor Greifba36cb52008-08-28 21:40:38 +00003498 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003499 return getOnesVector(Op.getValueType(), DAG, dl);
3500 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003501 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003502
Owen Andersone50ed302009-08-10 22:56:29 +00003503 EVT VT = Op.getValueType();
3504 EVT ExtVT = VT.getVectorElementType();
3505 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003506
3507 unsigned NumElems = Op.getNumOperands();
3508 unsigned NumZero = 0;
3509 unsigned NumNonZero = 0;
3510 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003511 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003512 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003513 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003514 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003515 if (Elt.getOpcode() == ISD::UNDEF)
3516 continue;
3517 Values.insert(Elt);
3518 if (Elt.getOpcode() != ISD::Constant &&
3519 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003520 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003521 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003522 NumZero++;
3523 else {
3524 NonZeros |= (1 << i);
3525 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003526 }
3527 }
3528
Dan Gohman7f321562007-06-25 16:23:39 +00003529 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003530 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003531 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003532 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003533
Chris Lattner67f453a2008-03-09 05:42:06 +00003534 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003535 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003536 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003537 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003538
Chris Lattner62098042008-03-09 01:05:04 +00003539 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3540 // the value are obviously zero, truncate the value to i32 and do the
3541 // insertion that way. Only do this if the value is non-constant or if the
3542 // value is a constant being inserted into element 0. It is cheaper to do
3543 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003545 (!IsAllConstants || Idx == 0)) {
3546 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3547 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3549 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003550
Chris Lattner62098042008-03-09 01:05:04 +00003551 // Truncate the value (which may itself be a constant) to i32, and
3552 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003554 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003555 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3556 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003557
Chris Lattner62098042008-03-09 01:05:04 +00003558 // Now we have our 32-bit value zero extended in the low element of
3559 // a vector. If Idx != 0, swizzle it into place.
3560 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003561 SmallVector<int, 4> Mask;
3562 Mask.push_back(Idx);
3563 for (unsigned i = 1; i != VecElts; ++i)
3564 Mask.push_back(i);
3565 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003566 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003568 }
Dale Johannesenace16102009-02-03 19:33:06 +00003569 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003570 }
3571 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003572
Chris Lattner19f79692008-03-08 22:59:52 +00003573 // If we have a constant or non-constant insertion into the low element of
3574 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3575 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003576 // depending on what the source datatype is.
3577 if (Idx == 0) {
3578 if (NumZero == 0) {
3579 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3581 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003582 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3583 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3584 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3585 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3587 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3588 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003589 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3590 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3591 Subtarget->hasSSE2(), DAG);
3592 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3593 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003594 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003595
3596 // Is it a vector logical left shift?
3597 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003598 X86::isZeroNode(Op.getOperand(0)) &&
3599 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003600 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003601 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003602 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003603 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003604 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003605 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003606
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003607 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003608 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003609
Chris Lattner19f79692008-03-08 22:59:52 +00003610 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3611 // is a non-constant being inserted into an element other than the low one,
3612 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3613 // movd/movss) to move this into the low element, then shuffle it into
3614 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003615 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003616 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003617
Evan Cheng0db9fe62006-04-25 20:13:52 +00003618 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003619 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3620 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003621 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003622 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 MaskVec.push_back(i == Idx ? 0 : 1);
3624 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003625 }
3626 }
3627
Chris Lattner67f453a2008-03-09 05:42:06 +00003628 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003629 if (Values.size() == 1) {
3630 if (EVTBits == 32) {
3631 // Instead of a shuffle like this:
3632 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3633 // Check if it's possible to issue this instead.
3634 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3635 unsigned Idx = CountTrailingZeros_32(NonZeros);
3636 SDValue Item = Op.getOperand(Idx);
3637 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3638 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3639 }
Dan Gohman475871a2008-07-27 21:46:04 +00003640 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003641 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003642
Dan Gohmana3941172007-07-24 22:55:08 +00003643 // A vector full of immediates; various special cases are already
3644 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003645 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003646 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003647
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003648 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003649 if (EVTBits == 64) {
3650 if (NumNonZero == 1) {
3651 // One half is zero or undef.
3652 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003653 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003654 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003655 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3656 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003657 }
Dan Gohman475871a2008-07-27 21:46:04 +00003658 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003659 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003660
3661 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003662 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003663 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003664 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003665 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003666 }
3667
Bill Wendling826f36f2007-03-28 00:57:11 +00003668 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003669 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003670 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003671 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003672 }
3673
3674 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003675 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003676 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003677 if (NumElems == 4 && NumZero > 0) {
3678 for (unsigned i = 0; i < 4; ++i) {
3679 bool isZero = !(NonZeros & (1 << i));
3680 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003681 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003682 else
Dale Johannesenace16102009-02-03 19:33:06 +00003683 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684 }
3685
3686 for (unsigned i = 0; i < 2; ++i) {
3687 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3688 default: break;
3689 case 0:
3690 V[i] = V[i*2]; // Must be a zero vector.
3691 break;
3692 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 break;
3695 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003697 break;
3698 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003699 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003700 break;
3701 }
3702 }
3703
Nate Begeman9008ca62009-04-27 18:41:29 +00003704 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003705 bool Reverse = (NonZeros & 0x3) == 2;
3706 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003707 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003708 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3709 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003710 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3711 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003712 }
3713
3714 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003715 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3716 // values to be inserted is equal to the number of elements, in which case
3717 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003718 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003719 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003720 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003721 getSubtarget()->hasSSE41()) {
3722 V[0] = DAG.getUNDEF(VT);
3723 for (unsigned i = 0; i < NumElems; ++i)
3724 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3725 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3726 Op.getOperand(i), DAG.getIntPtrConstant(i));
3727 return V[0];
3728 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 // Expand into a number of unpckl*.
3730 // e.g. for v4f32
3731 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3732 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3733 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003735 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736 NumElems >>= 1;
3737 while (NumElems != 0) {
3738 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003739 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003740 NumElems >>= 1;
3741 }
3742 return V[0];
3743 }
3744
Dan Gohman475871a2008-07-27 21:46:04 +00003745 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003746}
3747
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003748SDValue
3749X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3750 // We support concatenate two MMX registers and place them in a MMX
3751 // register. This is better than doing a stack convert.
3752 DebugLoc dl = Op.getDebugLoc();
3753 EVT ResVT = Op.getValueType();
3754 assert(Op.getNumOperands() == 2);
3755 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3756 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3757 int Mask[2];
3758 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3759 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3760 InVec = Op.getOperand(1);
3761 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3762 unsigned NumElts = ResVT.getVectorNumElements();
3763 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3764 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3765 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3766 } else {
3767 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3768 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3769 Mask[0] = 0; Mask[1] = 2;
3770 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3771 }
3772 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3773}
3774
Nate Begemanb9a47b82009-02-23 08:49:38 +00003775// v8i16 shuffles - Prefer shuffles in the following order:
3776// 1. [all] pshuflw, pshufhw, optional move
3777// 2. [ssse3] 1 x pshufb
3778// 3. [ssse3] 2 x pshufb + 1 x por
3779// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003780static
Nate Begeman9008ca62009-04-27 18:41:29 +00003781SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3782 SelectionDAG &DAG, X86TargetLowering &TLI) {
3783 SDValue V1 = SVOp->getOperand(0);
3784 SDValue V2 = SVOp->getOperand(1);
3785 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003786 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003787
Nate Begemanb9a47b82009-02-23 08:49:38 +00003788 // Determine if more than 1 of the words in each of the low and high quadwords
3789 // of the result come from the same quadword of one of the two inputs. Undef
3790 // mask values count as coming from any quadword, for better codegen.
3791 SmallVector<unsigned, 4> LoQuad(4);
3792 SmallVector<unsigned, 4> HiQuad(4);
3793 BitVector InputQuads(4);
3794 for (unsigned i = 0; i < 8; ++i) {
3795 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003796 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003797 MaskVals.push_back(EltIdx);
3798 if (EltIdx < 0) {
3799 ++Quad[0];
3800 ++Quad[1];
3801 ++Quad[2];
3802 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003803 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003804 }
3805 ++Quad[EltIdx / 4];
3806 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003807 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003808
Nate Begemanb9a47b82009-02-23 08:49:38 +00003809 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003810 unsigned MaxQuad = 1;
3811 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003812 if (LoQuad[i] > MaxQuad) {
3813 BestLoQuad = i;
3814 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003815 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003816 }
3817
Nate Begemanb9a47b82009-02-23 08:49:38 +00003818 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003819 MaxQuad = 1;
3820 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003821 if (HiQuad[i] > MaxQuad) {
3822 BestHiQuad = i;
3823 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003824 }
3825 }
3826
Nate Begemanb9a47b82009-02-23 08:49:38 +00003827 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003828 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003829 // single pshufb instruction is necessary. If There are more than 2 input
3830 // quads, disable the next transformation since it does not help SSSE3.
3831 bool V1Used = InputQuads[0] || InputQuads[1];
3832 bool V2Used = InputQuads[2] || InputQuads[3];
3833 if (TLI.getSubtarget()->hasSSSE3()) {
3834 if (InputQuads.count() == 2 && V1Used && V2Used) {
3835 BestLoQuad = InputQuads.find_first();
3836 BestHiQuad = InputQuads.find_next(BestLoQuad);
3837 }
3838 if (InputQuads.count() > 2) {
3839 BestLoQuad = -1;
3840 BestHiQuad = -1;
3841 }
3842 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003843
Nate Begemanb9a47b82009-02-23 08:49:38 +00003844 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3845 // the shuffle mask. If a quad is scored as -1, that means that it contains
3846 // words from all 4 input quadwords.
3847 SDValue NewV;
3848 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 SmallVector<int, 8> MaskV;
3850 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3851 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003852 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003853 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3854 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3855 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003856
Nate Begemanb9a47b82009-02-23 08:49:38 +00003857 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3858 // source words for the shuffle, to aid later transformations.
3859 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003860 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003861 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003862 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003863 if (idx != (int)i)
3864 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003865 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003866 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003867 AllWordsInNewV = false;
3868 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003869 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003870
Nate Begemanb9a47b82009-02-23 08:49:38 +00003871 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3872 if (AllWordsInNewV) {
3873 for (int i = 0; i != 8; ++i) {
3874 int idx = MaskVals[i];
3875 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003876 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003877 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003878 if ((idx != i) && idx < 4)
3879 pshufhw = false;
3880 if ((idx != i) && idx > 3)
3881 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003882 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003883 V1 = NewV;
3884 V2Used = false;
3885 BestLoQuad = 0;
3886 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003887 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003888
Nate Begemanb9a47b82009-02-23 08:49:38 +00003889 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3890 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003891 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003892 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003893 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003894 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003895 }
Eric Christopherfd179292009-08-27 18:07:15 +00003896
Nate Begemanb9a47b82009-02-23 08:49:38 +00003897 // If we have SSSE3, and all words of the result are from 1 input vector,
3898 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3899 // is present, fall back to case 4.
3900 if (TLI.getSubtarget()->hasSSSE3()) {
3901 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003902
Nate Begemanb9a47b82009-02-23 08:49:38 +00003903 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003904 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003905 // mask, and elements that come from V1 in the V2 mask, so that the two
3906 // results can be OR'd together.
3907 bool TwoInputs = V1Used && V2Used;
3908 for (unsigned i = 0; i != 8; ++i) {
3909 int EltIdx = MaskVals[i] * 2;
3910 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3912 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003913 continue;
3914 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3916 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003917 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003919 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003920 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003921 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003922 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003924
Nate Begemanb9a47b82009-02-23 08:49:38 +00003925 // Calculate the shuffle mask for the second input, shuffle it, and
3926 // OR it with the first shuffled input.
3927 pshufbMask.clear();
3928 for (unsigned i = 0; i != 8; ++i) {
3929 int EltIdx = MaskVals[i] * 2;
3930 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003931 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3932 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003933 continue;
3934 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3936 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003937 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003938 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003939 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003940 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 MVT::v16i8, &pshufbMask[0], 16));
3942 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3943 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003944 }
3945
3946 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3947 // and update MaskVals with new element order.
3948 BitVector InOrder(8);
3949 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003950 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003951 for (int i = 0; i != 4; ++i) {
3952 int idx = MaskVals[i];
3953 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003955 InOrder.set(i);
3956 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003957 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003958 InOrder.set(i);
3959 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003961 }
3962 }
3963 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003964 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003967 }
Eric Christopherfd179292009-08-27 18:07:15 +00003968
Nate Begemanb9a47b82009-02-23 08:49:38 +00003969 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3970 // and update MaskVals with the new element order.
3971 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003972 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003973 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003975 for (unsigned i = 4; i != 8; ++i) {
3976 int idx = MaskVals[i];
3977 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003979 InOrder.set(i);
3980 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003981 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003982 InOrder.set(i);
3983 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003984 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003985 }
3986 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003988 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003989 }
Eric Christopherfd179292009-08-27 18:07:15 +00003990
Nate Begemanb9a47b82009-02-23 08:49:38 +00003991 // In case BestHi & BestLo were both -1, which means each quadword has a word
3992 // from each of the four input quadwords, calculate the InOrder bitvector now
3993 // before falling through to the insert/extract cleanup.
3994 if (BestLoQuad == -1 && BestHiQuad == -1) {
3995 NewV = V1;
3996 for (int i = 0; i != 8; ++i)
3997 if (MaskVals[i] < 0 || MaskVals[i] == i)
3998 InOrder.set(i);
3999 }
Eric Christopherfd179292009-08-27 18:07:15 +00004000
Nate Begemanb9a47b82009-02-23 08:49:38 +00004001 // The other elements are put in the right place using pextrw and pinsrw.
4002 for (unsigned i = 0; i != 8; ++i) {
4003 if (InOrder[i])
4004 continue;
4005 int EltIdx = MaskVals[i];
4006 if (EltIdx < 0)
4007 continue;
4008 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004010 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004012 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004013 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004014 DAG.getIntPtrConstant(i));
4015 }
4016 return NewV;
4017}
4018
4019// v16i8 shuffles - Prefer shuffles in the following order:
4020// 1. [ssse3] 1 x pshufb
4021// 2. [ssse3] 2 x pshufb + 1 x por
4022// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4023static
Nate Begeman9008ca62009-04-27 18:41:29 +00004024SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4025 SelectionDAG &DAG, X86TargetLowering &TLI) {
4026 SDValue V1 = SVOp->getOperand(0);
4027 SDValue V2 = SVOp->getOperand(1);
4028 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004030 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004031
Nate Begemanb9a47b82009-02-23 08:49:38 +00004032 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004033 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004034 // present, fall back to case 3.
4035 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4036 bool V1Only = true;
4037 bool V2Only = true;
4038 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004039 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004040 if (EltIdx < 0)
4041 continue;
4042 if (EltIdx < 16)
4043 V2Only = false;
4044 else
4045 V1Only = false;
4046 }
Eric Christopherfd179292009-08-27 18:07:15 +00004047
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4049 if (TLI.getSubtarget()->hasSSSE3()) {
4050 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004051
Nate Begemanb9a47b82009-02-23 08:49:38 +00004052 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004053 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 //
4055 // Otherwise, we have elements from both input vectors, and must zero out
4056 // elements that come from V2 in the first mask, and V1 in the second mask
4057 // so that we can OR them together.
4058 bool TwoInputs = !(V1Only || V2Only);
4059 for (unsigned i = 0; i != 16; ++i) {
4060 int EltIdx = MaskVals[i];
4061 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 continue;
4064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004066 }
4067 // If all the elements are from V2, assign it to V1 and return after
4068 // building the first pshufb.
4069 if (V2Only)
4070 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004072 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 if (!TwoInputs)
4075 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004076
Nate Begemanb9a47b82009-02-23 08:49:38 +00004077 // Calculate the shuffle mask for the second input, shuffle it, and
4078 // OR it with the first shuffled input.
4079 pshufbMask.clear();
4080 for (unsigned i = 0; i != 16; ++i) {
4081 int EltIdx = MaskVals[i];
4082 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004084 continue;
4085 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004089 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 MVT::v16i8, &pshufbMask[0], 16));
4091 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 }
Eric Christopherfd179292009-08-27 18:07:15 +00004093
Nate Begemanb9a47b82009-02-23 08:49:38 +00004094 // No SSSE3 - Calculate in place words and then fix all out of place words
4095 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4096 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4098 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004099 SDValue NewV = V2Only ? V2 : V1;
4100 for (int i = 0; i != 8; ++i) {
4101 int Elt0 = MaskVals[i*2];
4102 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004103
Nate Begemanb9a47b82009-02-23 08:49:38 +00004104 // This word of the result is all undef, skip it.
4105 if (Elt0 < 0 && Elt1 < 0)
4106 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004107
Nate Begemanb9a47b82009-02-23 08:49:38 +00004108 // This word of the result is already in the correct place, skip it.
4109 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4110 continue;
4111 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4112 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004113
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4115 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4116 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004117
4118 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4119 // using a single extract together, load it and store it.
4120 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004122 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004124 DAG.getIntPtrConstant(i));
4125 continue;
4126 }
4127
Nate Begemanb9a47b82009-02-23 08:49:38 +00004128 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004129 // source byte is not also odd, shift the extracted word left 8 bits
4130 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004131 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004133 DAG.getIntPtrConstant(Elt1 / 2));
4134 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004136 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004137 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4139 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 }
4141 // If Elt0 is defined, extract it from the appropriate source. If the
4142 // source byte is not also even, shift the extracted word right 8 bits. If
4143 // Elt1 was also defined, OR the extracted values together before
4144 // inserting them in the result.
4145 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004147 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4148 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004150 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004151 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4153 DAG.getConstant(0x00FF, MVT::i16));
4154 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 : InsElt0;
4156 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004158 DAG.getIntPtrConstant(i));
4159 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004161}
4162
Evan Cheng7a831ce2007-12-15 03:00:47 +00004163/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4164/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4165/// done when every pair / quad of shuffle mask elements point to elements in
4166/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004167/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4168static
Nate Begeman9008ca62009-04-27 18:41:29 +00004169SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4170 SelectionDAG &DAG,
4171 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004172 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 SDValue V1 = SVOp->getOperand(0);
4174 SDValue V2 = SVOp->getOperand(1);
4175 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004176 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004178 EVT MaskEltVT = MaskVT.getVectorElementType();
4179 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 case MVT::v4f32: NewVT = MVT::v2f64; break;
4183 case MVT::v4i32: NewVT = MVT::v2i64; break;
4184 case MVT::v8i16: NewVT = MVT::v4i32; break;
4185 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004186 }
4187
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004188 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004189 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004191 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004193 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004194 int Scale = NumElems / NewWidth;
4195 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004196 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004197 int StartIdx = -1;
4198 for (int j = 0; j < Scale; ++j) {
4199 int EltIdx = SVOp->getMaskElt(i+j);
4200 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004201 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004203 StartIdx = EltIdx - (EltIdx % Scale);
4204 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004205 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004206 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004207 if (StartIdx == -1)
4208 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004209 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004210 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004211 }
4212
Dale Johannesenace16102009-02-03 19:33:06 +00004213 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4214 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004215 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004216}
4217
Evan Chengd880b972008-05-09 21:53:03 +00004218/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004219///
Owen Andersone50ed302009-08-10 22:56:29 +00004220static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004221 SDValue SrcOp, SelectionDAG &DAG,
4222 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004224 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004225 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004226 LD = dyn_cast<LoadSDNode>(SrcOp);
4227 if (!LD) {
4228 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4229 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004230 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4231 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004232 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4233 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004234 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004235 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004237 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4238 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4239 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4240 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004241 SrcOp.getOperand(0)
4242 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004243 }
4244 }
4245 }
4246
Dale Johannesenace16102009-02-03 19:33:06 +00004247 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4248 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004249 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004250 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004251}
4252
Evan Chengace3c172008-07-22 21:13:36 +00004253/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4254/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004255static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004256LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4257 SDValue V1 = SVOp->getOperand(0);
4258 SDValue V2 = SVOp->getOperand(1);
4259 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004260 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004261
Evan Chengace3c172008-07-22 21:13:36 +00004262 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004263 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004264 SmallVector<int, 8> Mask1(4U, -1);
4265 SmallVector<int, 8> PermMask;
4266 SVOp->getMask(PermMask);
4267
Evan Chengace3c172008-07-22 21:13:36 +00004268 unsigned NumHi = 0;
4269 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004270 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 int Idx = PermMask[i];
4272 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004273 Locs[i] = std::make_pair(-1, -1);
4274 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004275 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4276 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004277 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004278 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004279 NumLo++;
4280 } else {
4281 Locs[i] = std::make_pair(1, NumHi);
4282 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004283 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004284 NumHi++;
4285 }
4286 }
4287 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004288
Evan Chengace3c172008-07-22 21:13:36 +00004289 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004290 // If no more than two elements come from either vector. This can be
4291 // implemented with two shuffles. First shuffle gather the elements.
4292 // The second shuffle, which takes the first shuffle as both of its
4293 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004294 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004295
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004297
Evan Chengace3c172008-07-22 21:13:36 +00004298 for (unsigned i = 0; i != 4; ++i) {
4299 if (Locs[i].first == -1)
4300 continue;
4301 else {
4302 unsigned Idx = (i < 2) ? 0 : 4;
4303 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004304 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004305 }
4306 }
4307
Nate Begeman9008ca62009-04-27 18:41:29 +00004308 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004309 } else if (NumLo == 3 || NumHi == 3) {
4310 // Otherwise, we must have three elements from one vector, call it X, and
4311 // one element from the other, call it Y. First, use a shufps to build an
4312 // intermediate vector with the one element from Y and the element from X
4313 // that will be in the same half in the final destination (the indexes don't
4314 // matter). Then, use a shufps to build the final vector, taking the half
4315 // containing the element from Y from the intermediate, and the other half
4316 // from X.
4317 if (NumHi == 3) {
4318 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004319 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004320 std::swap(V1, V2);
4321 }
4322
4323 // Find the element from V2.
4324 unsigned HiIndex;
4325 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004326 int Val = PermMask[HiIndex];
4327 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004328 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004329 if (Val >= 4)
4330 break;
4331 }
4332
Nate Begeman9008ca62009-04-27 18:41:29 +00004333 Mask1[0] = PermMask[HiIndex];
4334 Mask1[1] = -1;
4335 Mask1[2] = PermMask[HiIndex^1];
4336 Mask1[3] = -1;
4337 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004338
4339 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004340 Mask1[0] = PermMask[0];
4341 Mask1[1] = PermMask[1];
4342 Mask1[2] = HiIndex & 1 ? 6 : 4;
4343 Mask1[3] = HiIndex & 1 ? 4 : 6;
4344 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004345 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 Mask1[0] = HiIndex & 1 ? 2 : 0;
4347 Mask1[1] = HiIndex & 1 ? 0 : 2;
4348 Mask1[2] = PermMask[2];
4349 Mask1[3] = PermMask[3];
4350 if (Mask1[2] >= 0)
4351 Mask1[2] += 4;
4352 if (Mask1[3] >= 0)
4353 Mask1[3] += 4;
4354 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004355 }
Evan Chengace3c172008-07-22 21:13:36 +00004356 }
4357
4358 // Break it into (shuffle shuffle_hi, shuffle_lo).
4359 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 SmallVector<int,8> LoMask(4U, -1);
4361 SmallVector<int,8> HiMask(4U, -1);
4362
4363 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004364 unsigned MaskIdx = 0;
4365 unsigned LoIdx = 0;
4366 unsigned HiIdx = 2;
4367 for (unsigned i = 0; i != 4; ++i) {
4368 if (i == 2) {
4369 MaskPtr = &HiMask;
4370 MaskIdx = 1;
4371 LoIdx = 0;
4372 HiIdx = 2;
4373 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004374 int Idx = PermMask[i];
4375 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004376 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004377 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004378 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004380 LoIdx++;
4381 } else {
4382 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004383 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004384 HiIdx++;
4385 }
4386 }
4387
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4389 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4390 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004391 for (unsigned i = 0; i != 4; ++i) {
4392 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004394 } else {
4395 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004397 }
4398 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004400}
4401
Dan Gohman475871a2008-07-27 21:46:04 +00004402SDValue
4403X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004404 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue V1 = Op.getOperand(0);
4406 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004407 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004408 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004409 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004410 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004411 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4412 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004413 bool V1IsSplat = false;
4414 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004415
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004417 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004418
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 // Promote splats to v4f32.
4420 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004421 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004422 return Op;
4423 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004424 }
4425
Evan Cheng7a831ce2007-12-15 03:00:47 +00004426 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4427 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004430 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004431 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004432 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004433 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004434 // FIXME: Figure out a cleaner way to do this.
4435 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004436 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004437 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004438 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4440 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4441 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004442 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004443 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4445 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004446 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004448 }
4449 }
Eric Christopherfd179292009-08-27 18:07:15 +00004450
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 if (X86::isPSHUFDMask(SVOp))
4452 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004453
Evan Chengf26ffe92008-05-29 08:22:04 +00004454 // Check if this can be converted into a logical shift.
4455 bool isLeft = false;
4456 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004457 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004458 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004459 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004460 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004461 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004462 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004463 EVT EltVT = VT.getVectorElementType();
4464 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004465 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004466 }
Eric Christopherfd179292009-08-27 18:07:15 +00004467
Nate Begeman9008ca62009-04-27 18:41:29 +00004468 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004469 if (V1IsUndef)
4470 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004471 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004472 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004473 if (!isMMX)
4474 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004475 }
Eric Christopherfd179292009-08-27 18:07:15 +00004476
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 // FIXME: fold these into legal mask.
4478 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4479 X86::isMOVSLDUPMask(SVOp) ||
4480 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004481 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004483 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 if (ShouldXformToMOVHLPS(SVOp) ||
4486 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4487 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488
Evan Chengf26ffe92008-05-29 08:22:04 +00004489 if (isShift) {
4490 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004491 EVT EltVT = VT.getVectorElementType();
4492 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004493 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004494 }
Eric Christopherfd179292009-08-27 18:07:15 +00004495
Evan Cheng9eca5e82006-10-25 21:49:50 +00004496 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004497 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4498 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004499 V1IsSplat = isSplatVector(V1.getNode());
4500 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004501
Chris Lattner8a594482007-11-25 00:24:49 +00004502 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004503 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004504 Op = CommuteVectorShuffle(SVOp, DAG);
4505 SVOp = cast<ShuffleVectorSDNode>(Op);
4506 V1 = SVOp->getOperand(0);
4507 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004508 std::swap(V1IsSplat, V2IsSplat);
4509 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004510 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004511 }
4512
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4514 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004515 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 return V1;
4517 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4518 // the instruction selector will not match, so get a canonical MOVL with
4519 // swapped operands to undo the commute.
4520 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004521 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4524 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4525 X86::isUNPCKLMask(SVOp) ||
4526 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004527 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004528
Evan Cheng9bbbb982006-10-25 20:48:19 +00004529 if (V2IsSplat) {
4530 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004531 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004532 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004533 SDValue NewMask = NormalizeMask(SVOp, DAG);
4534 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4535 if (NSVOp != SVOp) {
4536 if (X86::isUNPCKLMask(NSVOp, true)) {
4537 return NewMask;
4538 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4539 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004540 }
4541 }
4542 }
4543
Evan Cheng9eca5e82006-10-25 21:49:50 +00004544 if (Commuted) {
4545 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 // FIXME: this seems wrong.
4547 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4548 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4549 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4550 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4551 X86::isUNPCKLMask(NewSVOp) ||
4552 X86::isUNPCKHMask(NewSVOp))
4553 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004554 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004555
Nate Begemanb9a47b82009-02-23 08:49:38 +00004556 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004557
4558 // Normalize the node to match x86 shuffle ops if needed
4559 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4560 return CommuteVectorShuffle(SVOp, DAG);
4561
4562 // Check for legal shuffle and return?
4563 SmallVector<int, 16> PermMask;
4564 SVOp->getMask(PermMask);
4565 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004566 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004567
Evan Cheng14b32e12007-12-11 01:46:18 +00004568 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004569 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004570 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004571 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004572 return NewOp;
4573 }
4574
Owen Anderson825b72b2009-08-11 20:47:22 +00004575 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004576 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004577 if (NewOp.getNode())
4578 return NewOp;
4579 }
Eric Christopherfd179292009-08-27 18:07:15 +00004580
Evan Chengace3c172008-07-22 21:13:36 +00004581 // Handle all 4 wide cases with a number of shuffles except for MMX.
4582 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004584
Dan Gohman475871a2008-07-27 21:46:04 +00004585 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586}
4587
Dan Gohman475871a2008-07-27 21:46:04 +00004588SDValue
4589X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004590 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004591 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004592 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004593 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004594 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004595 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004597 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004598 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004599 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004600 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4601 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4602 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004603 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4604 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004605 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004607 Op.getOperand(0)),
4608 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004609 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004610 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004612 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004613 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004614 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004615 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4616 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004617 // result has a single use which is a store or a bitcast to i32. And in
4618 // the case of a store, it's not worth it if the index is a constant 0,
4619 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004620 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004621 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004622 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004623 if ((User->getOpcode() != ISD::STORE ||
4624 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4625 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004626 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004627 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004628 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4630 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004631 Op.getOperand(0)),
4632 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004633 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4634 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004635 // ExtractPS works with constant index.
4636 if (isa<ConstantSDNode>(Op.getOperand(1)))
4637 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004638 }
Dan Gohman475871a2008-07-27 21:46:04 +00004639 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004640}
4641
4642
Dan Gohman475871a2008-07-27 21:46:04 +00004643SDValue
4644X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004645 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004646 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647
Evan Cheng62a3f152008-03-24 21:52:23 +00004648 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004649 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004650 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004651 return Res;
4652 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004653
Owen Andersone50ed302009-08-10 22:56:29 +00004654 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004655 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004656 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004657 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004658 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004659 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004660 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004661 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4662 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004663 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004664 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004665 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004667 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004668 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004670 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004672 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004673 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004674 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004675 if (Idx == 0)
4676 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004677
Evan Cheng0db9fe62006-04-25 20:13:52 +00004678 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004680 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004681 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004683 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004684 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004685 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004686 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4687 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4688 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004689 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004690 if (Idx == 0)
4691 return Op;
4692
4693 // UNPCKHPD the element to the lowest double word, then movsd.
4694 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4695 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004697 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004698 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004700 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004701 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702 }
4703
Dan Gohman475871a2008-07-27 21:46:04 +00004704 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004705}
4706
Dan Gohman475871a2008-07-27 21:46:04 +00004707SDValue
4708X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004709 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004710 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004711 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004712
Dan Gohman475871a2008-07-27 21:46:04 +00004713 SDValue N0 = Op.getOperand(0);
4714 SDValue N1 = Op.getOperand(1);
4715 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004716
Dan Gohman8a55ce42009-09-23 21:02:20 +00004717 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004718 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004719 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4720 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004721 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4722 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004723 if (N1.getValueType() != MVT::i32)
4724 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4725 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004726 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004727 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004728 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004729 // Bits [7:6] of the constant are the source select. This will always be
4730 // zero here. The DAG Combiner may combine an extract_elt index into these
4731 // bits. For example (insert (extract, 3), 2) could be matched by putting
4732 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004733 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004734 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004735 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004736 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004737 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004738 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004740 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004741 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004742 // PINSR* works with constant index.
4743 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004744 }
Dan Gohman475871a2008-07-27 21:46:04 +00004745 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004746}
4747
Dan Gohman475871a2008-07-27 21:46:04 +00004748SDValue
4749X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004750 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004751 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004752
4753 if (Subtarget->hasSSE41())
4754 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4755
Dan Gohman8a55ce42009-09-23 21:02:20 +00004756 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004757 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004758
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004759 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004760 SDValue N0 = Op.getOperand(0);
4761 SDValue N1 = Op.getOperand(1);
4762 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004763
Dan Gohman8a55ce42009-09-23 21:02:20 +00004764 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004765 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4766 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 if (N1.getValueType() != MVT::i32)
4768 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4769 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004770 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004771 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004772 }
Dan Gohman475871a2008-07-27 21:46:04 +00004773 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004774}
4775
Dan Gohman475871a2008-07-27 21:46:04 +00004776SDValue
4777X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004778 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 if (Op.getValueType() == MVT::v2f32)
4780 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4781 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4782 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004783 Op.getOperand(0))));
4784
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4786 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004787
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4789 EVT VT = MVT::v2i32;
4790 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004791 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 case MVT::v16i8:
4793 case MVT::v8i16:
4794 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004795 break;
4796 }
Dale Johannesenace16102009-02-03 19:33:06 +00004797 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4798 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004799}
4800
Bill Wendling056292f2008-09-16 21:48:12 +00004801// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4802// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4803// one of the above mentioned nodes. It has to be wrapped because otherwise
4804// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4805// be used to form addressing mode. These wrapped nodes will be selected
4806// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004807SDValue
4808X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004810
Chris Lattner41621a22009-06-26 19:22:52 +00004811 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4812 // global base reg.
4813 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004814 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004815 CodeModel::Model M = getTargetMachine().getCodeModel();
4816
Chris Lattner4f066492009-07-11 20:29:19 +00004817 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004818 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004819 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004820 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004821 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004822 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004823 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004824
Evan Cheng1606e8e2009-03-13 07:51:59 +00004825 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004826 CP->getAlignment(),
4827 CP->getOffset(), OpFlag);
4828 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004829 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004830 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004831 if (OpFlag) {
4832 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004833 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004834 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004835 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 }
4837
4838 return Result;
4839}
4840
Chris Lattner18c59872009-06-27 04:16:01 +00004841SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4842 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004843
Chris Lattner18c59872009-06-27 04:16:01 +00004844 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4845 // global base reg.
4846 unsigned char OpFlag = 0;
4847 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004848 CodeModel::Model M = getTargetMachine().getCodeModel();
4849
Chris Lattner4f066492009-07-11 20:29:19 +00004850 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004851 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004852 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004853 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004854 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004855 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004856 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004857
Chris Lattner18c59872009-06-27 04:16:01 +00004858 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4859 OpFlag);
4860 DebugLoc DL = JT->getDebugLoc();
4861 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004862
Chris Lattner18c59872009-06-27 04:16:01 +00004863 // With PIC, the address is actually $g + Offset.
4864 if (OpFlag) {
4865 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4866 DAG.getNode(X86ISD::GlobalBaseReg,
4867 DebugLoc::getUnknownLoc(), getPointerTy()),
4868 Result);
4869 }
Eric Christopherfd179292009-08-27 18:07:15 +00004870
Chris Lattner18c59872009-06-27 04:16:01 +00004871 return Result;
4872}
4873
4874SDValue
4875X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4876 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004877
Chris Lattner18c59872009-06-27 04:16:01 +00004878 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4879 // global base reg.
4880 unsigned char OpFlag = 0;
4881 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004882 CodeModel::Model M = getTargetMachine().getCodeModel();
4883
Chris Lattner4f066492009-07-11 20:29:19 +00004884 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004885 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004886 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004887 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004888 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004889 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004890 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004891
Chris Lattner18c59872009-06-27 04:16:01 +00004892 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004893
Chris Lattner18c59872009-06-27 04:16:01 +00004894 DebugLoc DL = Op.getDebugLoc();
4895 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004896
4897
Chris Lattner18c59872009-06-27 04:16:01 +00004898 // With PIC, the address is actually $g + Offset.
4899 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004900 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004901 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4902 DAG.getNode(X86ISD::GlobalBaseReg,
4903 DebugLoc::getUnknownLoc(),
4904 getPointerTy()),
4905 Result);
4906 }
Eric Christopherfd179292009-08-27 18:07:15 +00004907
Chris Lattner18c59872009-06-27 04:16:01 +00004908 return Result;
4909}
4910
Dan Gohman475871a2008-07-27 21:46:04 +00004911SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004912X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004913 // Create the TargetBlockAddressAddress node.
4914 unsigned char OpFlags =
4915 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004916 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004917 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4918 DebugLoc dl = Op.getDebugLoc();
4919 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4920 /*isTarget=*/true, OpFlags);
4921
Dan Gohmanf705adb2009-10-30 01:28:02 +00004922 if (Subtarget->isPICStyleRIPRel() &&
4923 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004924 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4925 else
4926 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004927
Dan Gohman29cbade2009-11-20 23:18:13 +00004928 // With PIC, the address is actually $g + Offset.
4929 if (isGlobalRelativeToPICBase(OpFlags)) {
4930 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4931 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4932 Result);
4933 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004934
4935 return Result;
4936}
4937
4938SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004939X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004940 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004941 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004942 // Create the TargetGlobalAddress node, folding in the constant
4943 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004944 unsigned char OpFlags =
4945 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004946 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004947 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004948 if (OpFlags == X86II::MO_NO_FLAG &&
4949 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004950 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004951 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004952 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004953 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004954 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004955 }
Eric Christopherfd179292009-08-27 18:07:15 +00004956
Chris Lattner4f066492009-07-11 20:29:19 +00004957 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004958 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004959 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4960 else
4961 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004962
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004963 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004964 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004965 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4966 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004967 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004968 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004969
Chris Lattner36c25012009-07-10 07:34:39 +00004970 // For globals that require a load from a stub to get the address, emit the
4971 // load.
4972 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004973 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004974 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004975
Dan Gohman6520e202008-10-18 02:06:02 +00004976 // If there was a non-zero offset that we didn't fold, create an explicit
4977 // addition for it.
4978 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004979 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004980 DAG.getConstant(Offset, getPointerTy()));
4981
Evan Cheng0db9fe62006-04-25 20:13:52 +00004982 return Result;
4983}
4984
Evan Chengda43bcf2008-09-24 00:05:32 +00004985SDValue
4986X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4987 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004988 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004989 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004990}
4991
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004992static SDValue
4993GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004994 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004995 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004996 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004997 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004998 DebugLoc dl = GA->getDebugLoc();
4999 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5000 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005001 GA->getOffset(),
5002 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005003 if (InFlag) {
5004 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005005 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005006 } else {
5007 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005008 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005009 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005010
5011 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5012 MFI->setHasCalls(true);
5013
Rafael Espindola15f1b662009-04-24 12:59:40 +00005014 SDValue Flag = Chain.getValue(1);
5015 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005016}
5017
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005018// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005019static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005020LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005021 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005022 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005023 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5024 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005025 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005026 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005027 PtrVT), InFlag);
5028 InFlag = Chain.getValue(1);
5029
Chris Lattnerb903bed2009-06-26 21:20:29 +00005030 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005031}
5032
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005033// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005034static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005035LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005036 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005037 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5038 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005039}
5040
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005041// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5042// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005043static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005044 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005045 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005046 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005047 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005048 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5049 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005050 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005052
5053 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5054 NULL, 0);
5055
Chris Lattnerb903bed2009-06-26 21:20:29 +00005056 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005057 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5058 // initialexec.
5059 unsigned WrapperKind = X86ISD::Wrapper;
5060 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005061 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005062 } else if (is64Bit) {
5063 assert(model == TLSModel::InitialExec);
5064 OperandFlags = X86II::MO_GOTTPOFF;
5065 WrapperKind = X86ISD::WrapperRIP;
5066 } else {
5067 assert(model == TLSModel::InitialExec);
5068 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005069 }
Eric Christopherfd179292009-08-27 18:07:15 +00005070
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005071 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5072 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005073 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005074 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005075 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005076
Rafael Espindola9a580232009-02-27 13:37:18 +00005077 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005078 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005079 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005080
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005081 // The address of the thread local variable is the add of the thread
5082 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005083 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005084}
5085
Dan Gohman475871a2008-07-27 21:46:04 +00005086SDValue
5087X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005088 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005089 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005090 assert(Subtarget->isTargetELF() &&
5091 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005092 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005093 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005094
Chris Lattnerb903bed2009-06-26 21:20:29 +00005095 // If GV is an alias then use the aliasee for determining
5096 // thread-localness.
5097 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5098 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005099
Chris Lattnerb903bed2009-06-26 21:20:29 +00005100 TLSModel::Model model = getTLSModel(GV,
5101 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005102
Chris Lattnerb903bed2009-06-26 21:20:29 +00005103 switch (model) {
5104 case TLSModel::GeneralDynamic:
5105 case TLSModel::LocalDynamic: // not implemented
5106 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005107 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005108 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005109
Chris Lattnerb903bed2009-06-26 21:20:29 +00005110 case TLSModel::InitialExec:
5111 case TLSModel::LocalExec:
5112 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5113 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005114 }
Eric Christopherfd179292009-08-27 18:07:15 +00005115
Torok Edwinc23197a2009-07-14 16:55:14 +00005116 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005117 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005118}
5119
Evan Cheng0db9fe62006-04-25 20:13:52 +00005120
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005121/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005122/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005123SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005124 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005125 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005126 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005127 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005128 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005129 SDValue ShOpLo = Op.getOperand(0);
5130 SDValue ShOpHi = Op.getOperand(1);
5131 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005132 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005133 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005134 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005135
Dan Gohman475871a2008-07-27 21:46:04 +00005136 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005137 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005138 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5139 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005140 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005141 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5142 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005143 }
Evan Chenge3413162006-01-09 18:33:28 +00005144
Owen Anderson825b72b2009-08-11 20:47:22 +00005145 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5146 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005147 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005149
Dan Gohman475871a2008-07-27 21:46:04 +00005150 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005151 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005152 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5153 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005154
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005155 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005156 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5157 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005158 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005159 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5160 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005161 }
5162
Dan Gohman475871a2008-07-27 21:46:04 +00005163 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005164 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005165}
Evan Chenga3195e82006-01-12 22:54:21 +00005166
Dan Gohman475871a2008-07-27 21:46:04 +00005167SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005168 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005169
5170 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005171 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005172 return Op;
5173 }
5174 return SDValue();
5175 }
5176
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005178 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005179
Eli Friedman36df4992009-05-27 00:47:34 +00005180 // These are really Legal; return the operand so the caller accepts it as
5181 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005182 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005183 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005185 Subtarget->is64Bit()) {
5186 return Op;
5187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005188
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005189 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005190 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005192 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005193 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005194 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005195 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005196 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005197 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5198}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005199
Owen Andersone50ed302009-08-10 22:56:29 +00005200SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005201 SDValue StackSlot,
5202 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005203 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005204 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005205 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005206 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005207 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005208 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005209 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005210 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005211 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005212 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005213 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005214
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005215 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005216 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005217 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005218
5219 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5220 // shouldn't be necessary except that RFP cannot be live across
5221 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005222 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005223 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005224 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005225 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005226 SDValue Ops[] = {
5227 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5228 };
5229 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005230 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005231 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005232 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005233
Evan Cheng0db9fe62006-04-25 20:13:52 +00005234 return Result;
5235}
5236
Bill Wendling8b8a6362009-01-17 03:56:04 +00005237// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5238SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5239 // This algorithm is not obvious. Here it is in C code, more or less:
5240 /*
5241 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5242 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5243 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005244
Bill Wendling8b8a6362009-01-17 03:56:04 +00005245 // Copy ints to xmm registers.
5246 __m128i xh = _mm_cvtsi32_si128( hi );
5247 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005248
Bill Wendling8b8a6362009-01-17 03:56:04 +00005249 // Combine into low half of a single xmm register.
5250 __m128i x = _mm_unpacklo_epi32( xh, xl );
5251 __m128d d;
5252 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005253
Bill Wendling8b8a6362009-01-17 03:56:04 +00005254 // Merge in appropriate exponents to give the integer bits the right
5255 // magnitude.
5256 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005257
Bill Wendling8b8a6362009-01-17 03:56:04 +00005258 // Subtract away the biases to deal with the IEEE-754 double precision
5259 // implicit 1.
5260 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005261
Bill Wendling8b8a6362009-01-17 03:56:04 +00005262 // All conversions up to here are exact. The correctly rounded result is
5263 // calculated using the current rounding mode using the following
5264 // horizontal add.
5265 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5266 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5267 // store doesn't really need to be here (except
5268 // maybe to zero the other double)
5269 return sd;
5270 }
5271 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005272
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005273 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005274 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005275
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005276 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005277 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005278 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5279 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5280 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5281 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005282 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005283 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005284
Bill Wendling8b8a6362009-01-17 03:56:04 +00005285 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005286 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005287 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005288 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005289 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005290 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005291 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005292
Owen Anderson825b72b2009-08-11 20:47:22 +00005293 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5294 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005295 Op.getOperand(0),
5296 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005297 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5298 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005299 Op.getOperand(0),
5300 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005301 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5302 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005303 PseudoSourceValue::getConstantPool(), 0,
5304 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005305 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5306 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5307 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005308 PseudoSourceValue::getConstantPool(), 0,
5309 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005310 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005311
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005312 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005313 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005314 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5315 DAG.getUNDEF(MVT::v2f64), ShufMask);
5316 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5317 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005318 DAG.getIntPtrConstant(0));
5319}
5320
Bill Wendling8b8a6362009-01-17 03:56:04 +00005321// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5322SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005323 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005324 // FP constant to bias correct the final result.
5325 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005326 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005327
5328 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005329 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5330 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005331 Op.getOperand(0),
5332 DAG.getIntPtrConstant(0)));
5333
Owen Anderson825b72b2009-08-11 20:47:22 +00005334 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5335 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005336 DAG.getIntPtrConstant(0));
5337
5338 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005339 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
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, Load)),
5343 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005344 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 MVT::v2f64, Bias)));
5346 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5347 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005348 DAG.getIntPtrConstant(0));
5349
5350 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005352
5353 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005354 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005355
Owen Anderson825b72b2009-08-11 20:47:22 +00005356 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005357 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005358 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005359 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005360 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005361 }
5362
5363 // Handle final rounding.
5364 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005365}
5366
5367SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005368 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005369 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005370
Evan Chenga06ec9e2009-01-19 08:08:22 +00005371 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5372 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5373 // the optimization here.
5374 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005375 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005376
Owen Andersone50ed302009-08-10 22:56:29 +00005377 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005378 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005379 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005380 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005381 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005382
Bill Wendling8b8a6362009-01-17 03:56:04 +00005383 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005384 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005385 return LowerUINT_TO_FP_i32(Op, DAG);
5386 }
5387
Owen Anderson825b72b2009-08-11 20:47:22 +00005388 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005389
5390 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005391 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005392 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5393 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5394 getPointerTy(), StackSlot, WordOff);
5395 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5396 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005397 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005398 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005399 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005400}
5401
Dan Gohman475871a2008-07-27 21:46:04 +00005402std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005403FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005404 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005405
Owen Andersone50ed302009-08-10 22:56:29 +00005406 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005407
5408 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005409 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5410 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005411 }
5412
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5414 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005415 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005416
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005417 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005418 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005419 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005420 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005421 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005422 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005423 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005424 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005425
Evan Cheng87c89352007-10-15 20:11:21 +00005426 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5427 // stack slot.
5428 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005429 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005430 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005431 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005432
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005434 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005435 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005436 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5437 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5438 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005440
Dan Gohman475871a2008-07-27 21:46:04 +00005441 SDValue Chain = DAG.getEntryNode();
5442 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005443 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005445 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005446 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005447 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005448 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005449 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5450 };
Dale Johannesenace16102009-02-03 19:33:06 +00005451 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005453 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5455 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005456
Evan Cheng0db9fe62006-04-25 20:13:52 +00005457 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005458 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005459 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005460
Chris Lattner27a6c732007-11-24 07:07:01 +00005461 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005462}
5463
Dan Gohman475871a2008-07-27 21:46:04 +00005464SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005465 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005466 if (Op.getValueType() == MVT::v2i32 &&
5467 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005468 return Op;
5469 }
5470 return SDValue();
5471 }
5472
Eli Friedman948e95a2009-05-23 09:59:16 +00005473 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005475 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5476 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005477
Chris Lattner27a6c732007-11-24 07:07:01 +00005478 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005479 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005480 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005481}
5482
Eli Friedman948e95a2009-05-23 09:59:16 +00005483SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5484 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5485 SDValue FIST = Vals.first, StackSlot = Vals.second;
5486 assert(FIST.getNode() && "Unexpected failure");
5487
5488 // Load the result.
5489 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5490 FIST, StackSlot, NULL, 0);
5491}
5492
Dan Gohman475871a2008-07-27 21:46:04 +00005493SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005494 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005495 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005496 EVT VT = Op.getValueType();
5497 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005498 if (VT.isVector())
5499 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005501 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005502 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005503 CV.push_back(C);
5504 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005505 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005506 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005507 CV.push_back(C);
5508 CV.push_back(C);
5509 CV.push_back(C);
5510 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005511 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005512 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005513 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005514 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005515 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005516 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005517 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005518}
5519
Dan Gohman475871a2008-07-27 21:46:04 +00005520SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005521 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005522 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005523 EVT VT = Op.getValueType();
5524 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005525 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005526 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005527 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005528 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005529 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005530 CV.push_back(C);
5531 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005532 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005533 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005534 CV.push_back(C);
5535 CV.push_back(C);
5536 CV.push_back(C);
5537 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005538 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005539 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005540 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005541 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005542 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005543 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005544 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005545 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5547 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005548 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005550 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005551 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005552 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005553}
5554
Dan Gohman475871a2008-07-27 21:46:04 +00005555SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005556 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005557 SDValue Op0 = Op.getOperand(0);
5558 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005559 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005560 EVT VT = Op.getValueType();
5561 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005562
5563 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005564 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005565 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005566 SrcVT = VT;
5567 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005568 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005569 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005570 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005571 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005572 }
5573
5574 // At this point the operands and the result should have the same
5575 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005576
Evan Cheng68c47cb2007-01-05 07:55:56 +00005577 // First get the sign bit of second operand.
5578 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005580 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5581 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005582 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005583 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5584 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5585 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5586 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005587 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005588 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005589 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005590 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005591 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005592 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005593 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005594
5595 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005596 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005597 // Op0 is MVT::f32, Op1 is MVT::f64.
5598 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5599 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5600 DAG.getConstant(32, MVT::i32));
5601 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5602 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005603 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005604 }
5605
Evan Cheng73d6cf12007-01-05 21:37:56 +00005606 // Clear first operand sign bit.
5607 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005608 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005609 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5610 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005611 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005612 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5613 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5614 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5615 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005616 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005617 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005618 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005619 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005620 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005621 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005622 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005623
5624 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005625 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005626}
5627
Dan Gohman076aee32009-03-04 19:44:21 +00005628/// Emit nodes that will be selected as "test Op0,Op0", or something
5629/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005630SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5631 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005632 DebugLoc dl = Op.getDebugLoc();
5633
Dan Gohman31125812009-03-07 01:58:32 +00005634 // CF and OF aren't always set the way we want. Determine which
5635 // of these we need.
5636 bool NeedCF = false;
5637 bool NeedOF = false;
5638 switch (X86CC) {
5639 case X86::COND_A: case X86::COND_AE:
5640 case X86::COND_B: case X86::COND_BE:
5641 NeedCF = true;
5642 break;
5643 case X86::COND_G: case X86::COND_GE:
5644 case X86::COND_L: case X86::COND_LE:
5645 case X86::COND_O: case X86::COND_NO:
5646 NeedOF = true;
5647 break;
5648 default: break;
5649 }
5650
Dan Gohman076aee32009-03-04 19:44:21 +00005651 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005652 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5653 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5654 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005655 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005656 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005657 switch (Op.getNode()->getOpcode()) {
5658 case ISD::ADD:
5659 // Due to an isel shortcoming, be conservative if this add is likely to
5660 // be selected as part of a load-modify-store instruction. When the root
5661 // node in a match is a store, isel doesn't know how to remap non-chain
5662 // non-flag uses of other nodes in the match, such as the ADD in this
5663 // case. This leads to the ADD being left around and reselected, with
5664 // the result being two adds in the output.
5665 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5666 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5667 if (UI->getOpcode() == ISD::STORE)
5668 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005669 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005670 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5671 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005672 if (C->getAPIntValue() == 1) {
5673 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005674 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005675 break;
5676 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005677 // An add of negative one (subtract of one) will be selected as a DEC.
5678 if (C->getAPIntValue().isAllOnesValue()) {
5679 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005680 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005681 break;
5682 }
5683 }
Dan Gohman076aee32009-03-04 19:44:21 +00005684 // Otherwise use a regular EFLAGS-setting add.
5685 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005686 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005687 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005688 case ISD::AND: {
5689 // If the primary and result isn't used, don't bother using X86ISD::AND,
5690 // because a TEST instruction will be better.
5691 bool NonFlagUse = false;
5692 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005693 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5694 SDNode *User = *UI;
5695 unsigned UOpNo = UI.getOperandNo();
5696 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5697 // Look pass truncate.
5698 UOpNo = User->use_begin().getOperandNo();
5699 User = *User->use_begin();
5700 }
5701 if (User->getOpcode() != ISD::BRCOND &&
5702 User->getOpcode() != ISD::SETCC &&
5703 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005704 NonFlagUse = true;
5705 break;
5706 }
Evan Cheng17751da2010-01-07 00:54:06 +00005707 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005708 if (!NonFlagUse)
5709 break;
5710 }
5711 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005712 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005713 case ISD::OR:
5714 case ISD::XOR:
5715 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005716 // likely to be selected as part of a load-modify-store instruction.
5717 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5718 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5719 if (UI->getOpcode() == ISD::STORE)
5720 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005721 // Otherwise use a regular EFLAGS-setting instruction.
5722 switch (Op.getNode()->getOpcode()) {
5723 case ISD::SUB: Opcode = X86ISD::SUB; break;
5724 case ISD::OR: Opcode = X86ISD::OR; break;
5725 case ISD::XOR: Opcode = X86ISD::XOR; break;
5726 case ISD::AND: Opcode = X86ISD::AND; break;
5727 default: llvm_unreachable("unexpected operator!");
5728 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005729 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005730 break;
5731 case X86ISD::ADD:
5732 case X86ISD::SUB:
5733 case X86ISD::INC:
5734 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005735 case X86ISD::OR:
5736 case X86ISD::XOR:
5737 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005738 return SDValue(Op.getNode(), 1);
5739 default:
5740 default_case:
5741 break;
5742 }
5743 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005745 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005746 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005747 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005748 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005749 DAG.ReplaceAllUsesWith(Op, New);
5750 return SDValue(New.getNode(), 1);
5751 }
5752 }
5753
5754 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005755 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005756 DAG.getConstant(0, Op.getValueType()));
5757}
5758
5759/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5760/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005761SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5762 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005763 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5764 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005765 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005766
5767 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005769}
5770
Evan Chengd40d03e2010-01-06 19:38:29 +00005771/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5772/// if it's possible.
5773static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005774 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005775 SDValue LHS, RHS;
5776 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5777 if (ConstantSDNode *Op010C =
5778 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5779 if (Op010C->getZExtValue() == 1) {
5780 LHS = Op0.getOperand(0);
5781 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005782 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005783 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5784 if (ConstantSDNode *Op000C =
5785 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5786 if (Op000C->getZExtValue() == 1) {
5787 LHS = Op0.getOperand(1);
5788 RHS = Op0.getOperand(0).getOperand(1);
5789 }
5790 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5791 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5792 SDValue AndLHS = Op0.getOperand(0);
5793 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5794 LHS = AndLHS.getOperand(0);
5795 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005796 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005797 }
Evan Cheng0488db92007-09-25 01:57:46 +00005798
Evan Chengd40d03e2010-01-06 19:38:29 +00005799 if (LHS.getNode()) {
5800 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5801 // instruction. Since the shift amount is in-range-or-undefined, we know
5802 // that doing a bittest on the i16 value is ok. We extend to i32 because
5803 // the encoding for the i16 version is larger than the i32 version.
5804 if (LHS.getValueType() == MVT::i8)
5805 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005806
Evan Chengd40d03e2010-01-06 19:38:29 +00005807 // If the operand types disagree, extend the shift amount to match. Since
5808 // BT ignores high bits (like shifts) we can use anyextend.
5809 if (LHS.getValueType() != RHS.getValueType())
5810 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005811
Evan Chengd40d03e2010-01-06 19:38:29 +00005812 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5813 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5814 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5815 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005816 }
5817
Evan Cheng54de3ea2010-01-05 06:52:31 +00005818 return SDValue();
5819}
5820
5821SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5822 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5823 SDValue Op0 = Op.getOperand(0);
5824 SDValue Op1 = Op.getOperand(1);
5825 DebugLoc dl = Op.getDebugLoc();
5826 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5827
5828 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005829 // Lower (X & (1 << N)) == 0 to BT(X, N).
5830 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5831 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5832 if (Op0.getOpcode() == ISD::AND &&
5833 Op0.hasOneUse() &&
5834 Op1.getOpcode() == ISD::Constant &&
5835 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5836 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5837 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5838 if (NewSetCC.getNode())
5839 return NewSetCC;
5840 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005841
Chris Lattnere55484e2008-12-25 05:34:37 +00005842 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5843 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005844 if (X86CC == X86::COND_INVALID)
5845 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005846
Dan Gohman31125812009-03-07 01:58:32 +00005847 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005848
5849 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005850 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005851 return DAG.getNode(ISD::AND, dl, MVT::i8,
5852 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5853 DAG.getConstant(X86CC, MVT::i8), Cond),
5854 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005855
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5857 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005858}
5859
Dan Gohman475871a2008-07-27 21:46:04 +00005860SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5861 SDValue Cond;
5862 SDValue Op0 = Op.getOperand(0);
5863 SDValue Op1 = Op.getOperand(1);
5864 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005865 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005866 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5867 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005868 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005869
5870 if (isFP) {
5871 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005872 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5874 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005875 bool Swap = false;
5876
5877 switch (SetCCOpcode) {
5878 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005879 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005880 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005881 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005882 case ISD::SETGT: Swap = true; // Fallthrough
5883 case ISD::SETLT:
5884 case ISD::SETOLT: SSECC = 1; break;
5885 case ISD::SETOGE:
5886 case ISD::SETGE: Swap = true; // Fallthrough
5887 case ISD::SETLE:
5888 case ISD::SETOLE: SSECC = 2; break;
5889 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005890 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005891 case ISD::SETNE: SSECC = 4; break;
5892 case ISD::SETULE: Swap = true;
5893 case ISD::SETUGE: SSECC = 5; break;
5894 case ISD::SETULT: Swap = true;
5895 case ISD::SETUGT: SSECC = 6; break;
5896 case ISD::SETO: SSECC = 7; break;
5897 }
5898 if (Swap)
5899 std::swap(Op0, Op1);
5900
Nate Begemanfb8ead02008-07-25 19:05:58 +00005901 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005902 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005903 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005904 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005905 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5906 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005907 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005908 }
5909 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005910 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005911 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5912 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005913 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005914 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005915 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005916 }
5917 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005918 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005919 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005920
Nate Begeman30a0de92008-07-17 16:51:19 +00005921 // We are handling one of the integer comparisons here. Since SSE only has
5922 // GT and EQ comparisons for integer, swapping operands and multiple
5923 // operations may be required for some comparisons.
5924 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5925 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005926
Owen Anderson825b72b2009-08-11 20:47:22 +00005927 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005928 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 case MVT::v8i8:
5930 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5931 case MVT::v4i16:
5932 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5933 case MVT::v2i32:
5934 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5935 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005937
Nate Begeman30a0de92008-07-17 16:51:19 +00005938 switch (SetCCOpcode) {
5939 default: break;
5940 case ISD::SETNE: Invert = true;
5941 case ISD::SETEQ: Opc = EQOpc; break;
5942 case ISD::SETLT: Swap = true;
5943 case ISD::SETGT: Opc = GTOpc; break;
5944 case ISD::SETGE: Swap = true;
5945 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5946 case ISD::SETULT: Swap = true;
5947 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5948 case ISD::SETUGE: Swap = true;
5949 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5950 }
5951 if (Swap)
5952 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005953
Nate Begeman30a0de92008-07-17 16:51:19 +00005954 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5955 // bits of the inputs before performing those operations.
5956 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005957 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005958 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5959 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005960 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005961 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5962 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005963 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5964 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005965 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005966
Dale Johannesenace16102009-02-03 19:33:06 +00005967 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005968
5969 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005970 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005971 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005972
Nate Begeman30a0de92008-07-17 16:51:19 +00005973 return Result;
5974}
Evan Cheng0488db92007-09-25 01:57:46 +00005975
Evan Cheng370e5342008-12-03 08:38:43 +00005976// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005977static bool isX86LogicalCmp(SDValue Op) {
5978 unsigned Opc = Op.getNode()->getOpcode();
5979 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5980 return true;
5981 if (Op.getResNo() == 1 &&
5982 (Opc == X86ISD::ADD ||
5983 Opc == X86ISD::SUB ||
5984 Opc == X86ISD::SMUL ||
5985 Opc == X86ISD::UMUL ||
5986 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005987 Opc == X86ISD::DEC ||
5988 Opc == X86ISD::OR ||
5989 Opc == X86ISD::XOR ||
5990 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005991 return true;
5992
5993 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005994}
5995
Dan Gohman475871a2008-07-27 21:46:04 +00005996SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005997 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005998 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005999 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006000 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006001
Dan Gohman1a492952009-10-20 16:22:37 +00006002 if (Cond.getOpcode() == ISD::SETCC) {
6003 SDValue NewCond = LowerSETCC(Cond, DAG);
6004 if (NewCond.getNode())
6005 Cond = NewCond;
6006 }
Evan Cheng734503b2006-09-11 02:19:56 +00006007
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006008 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6009 SDValue Op1 = Op.getOperand(1);
6010 SDValue Op2 = Op.getOperand(2);
6011 if (Cond.getOpcode() == X86ISD::SETCC &&
6012 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6013 SDValue Cmp = Cond.getOperand(1);
6014 if (Cmp.getOpcode() == X86ISD::CMP) {
6015 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6016 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6017 ConstantSDNode *RHSC =
6018 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6019 if (N1C && N1C->isAllOnesValue() &&
6020 N2C && N2C->isNullValue() &&
6021 RHSC && RHSC->isNullValue()) {
6022 SDValue CmpOp0 = Cmp.getOperand(0);
6023 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
6024 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6025 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6026 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6027 }
6028 }
6029 }
6030
Evan Chengad9c0a32009-12-15 00:53:42 +00006031 // Look pass (and (setcc_carry (cmp ...)), 1).
6032 if (Cond.getOpcode() == ISD::AND &&
6033 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6034 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6035 if (C && C->getAPIntValue() == 1)
6036 Cond = Cond.getOperand(0);
6037 }
6038
Evan Cheng3f41d662007-10-08 22:16:29 +00006039 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6040 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006041 if (Cond.getOpcode() == X86ISD::SETCC ||
6042 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006043 CC = Cond.getOperand(0);
6044
Dan Gohman475871a2008-07-27 21:46:04 +00006045 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006046 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006047 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006048
Evan Cheng3f41d662007-10-08 22:16:29 +00006049 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006050 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006051 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006052 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006053
Chris Lattnerd1980a52009-03-12 06:52:53 +00006054 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6055 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006056 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006057 addTest = false;
6058 }
6059 }
6060
6061 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006062 // Look pass the truncate.
6063 if (Cond.getOpcode() == ISD::TRUNCATE)
6064 Cond = Cond.getOperand(0);
6065
6066 // We know the result of AND is compared against zero. Try to match
6067 // it to BT.
6068 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6069 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6070 if (NewSetCC.getNode()) {
6071 CC = NewSetCC.getOperand(0);
6072 Cond = NewSetCC.getOperand(1);
6073 addTest = false;
6074 }
6075 }
6076 }
6077
6078 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006079 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006080 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006081 }
6082
Evan Cheng0488db92007-09-25 01:57:46 +00006083 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6084 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006085 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6086 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006087 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006088}
6089
Evan Cheng370e5342008-12-03 08:38:43 +00006090// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6091// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6092// from the AND / OR.
6093static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6094 Opc = Op.getOpcode();
6095 if (Opc != ISD::OR && Opc != ISD::AND)
6096 return false;
6097 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6098 Op.getOperand(0).hasOneUse() &&
6099 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6100 Op.getOperand(1).hasOneUse());
6101}
6102
Evan Cheng961d6d42009-02-02 08:19:07 +00006103// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6104// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006105static bool isXor1OfSetCC(SDValue Op) {
6106 if (Op.getOpcode() != ISD::XOR)
6107 return false;
6108 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6109 if (N1C && N1C->getAPIntValue() == 1) {
6110 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6111 Op.getOperand(0).hasOneUse();
6112 }
6113 return false;
6114}
6115
Dan Gohman475871a2008-07-27 21:46:04 +00006116SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006117 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006118 SDValue Chain = Op.getOperand(0);
6119 SDValue Cond = Op.getOperand(1);
6120 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006121 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006122 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006123
Dan Gohman1a492952009-10-20 16:22:37 +00006124 if (Cond.getOpcode() == ISD::SETCC) {
6125 SDValue NewCond = LowerSETCC(Cond, DAG);
6126 if (NewCond.getNode())
6127 Cond = NewCond;
6128 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006129#if 0
6130 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006131 else if (Cond.getOpcode() == X86ISD::ADD ||
6132 Cond.getOpcode() == X86ISD::SUB ||
6133 Cond.getOpcode() == X86ISD::SMUL ||
6134 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006135 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006136#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006137
Evan Chengad9c0a32009-12-15 00:53:42 +00006138 // Look pass (and (setcc_carry (cmp ...)), 1).
6139 if (Cond.getOpcode() == ISD::AND &&
6140 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6141 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6142 if (C && C->getAPIntValue() == 1)
6143 Cond = Cond.getOperand(0);
6144 }
6145
Evan Cheng3f41d662007-10-08 22:16:29 +00006146 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6147 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006148 if (Cond.getOpcode() == X86ISD::SETCC ||
6149 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006150 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006151
Dan Gohman475871a2008-07-27 21:46:04 +00006152 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006153 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006154 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006155 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006156 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006157 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006158 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006159 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006160 default: break;
6161 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006162 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006163 // These can only come from an arithmetic instruction with overflow,
6164 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006165 Cond = Cond.getNode()->getOperand(1);
6166 addTest = false;
6167 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006168 }
Evan Cheng0488db92007-09-25 01:57:46 +00006169 }
Evan Cheng370e5342008-12-03 08:38:43 +00006170 } else {
6171 unsigned CondOpc;
6172 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6173 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006174 if (CondOpc == ISD::OR) {
6175 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6176 // two branches instead of an explicit OR instruction with a
6177 // separate test.
6178 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006179 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006180 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006181 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006182 Chain, Dest, CC, Cmp);
6183 CC = Cond.getOperand(1).getOperand(0);
6184 Cond = Cmp;
6185 addTest = false;
6186 }
6187 } else { // ISD::AND
6188 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6189 // two branches instead of an explicit AND instruction with a
6190 // separate test. However, we only do this if this block doesn't
6191 // have a fall-through edge, because this requires an explicit
6192 // jmp when the condition is false.
6193 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006194 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006195 Op.getNode()->hasOneUse()) {
6196 X86::CondCode CCode =
6197 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6198 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006199 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006200 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6201 // Look for an unconditional branch following this conditional branch.
6202 // We need this because we need to reverse the successors in order
6203 // to implement FCMP_OEQ.
6204 if (User.getOpcode() == ISD::BR) {
6205 SDValue FalseBB = User.getOperand(1);
6206 SDValue NewBR =
6207 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6208 assert(NewBR == User);
6209 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006210
Dale Johannesene4d209d2009-02-03 20:21:25 +00006211 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006212 Chain, Dest, CC, Cmp);
6213 X86::CondCode CCode =
6214 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6215 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006216 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006217 Cond = Cmp;
6218 addTest = false;
6219 }
6220 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006221 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006222 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6223 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6224 // It should be transformed during dag combiner except when the condition
6225 // is set by a arithmetics with overflow node.
6226 X86::CondCode CCode =
6227 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6228 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006229 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006230 Cond = Cond.getOperand(0).getOperand(1);
6231 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006232 }
Evan Cheng0488db92007-09-25 01:57:46 +00006233 }
6234
6235 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006236 // Look pass the truncate.
6237 if (Cond.getOpcode() == ISD::TRUNCATE)
6238 Cond = Cond.getOperand(0);
6239
6240 // We know the result of AND is compared against zero. Try to match
6241 // it to BT.
6242 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6243 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6244 if (NewSetCC.getNode()) {
6245 CC = NewSetCC.getOperand(0);
6246 Cond = NewSetCC.getOperand(1);
6247 addTest = false;
6248 }
6249 }
6250 }
6251
6252 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006253 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006254 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006255 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006256 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006257 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006258}
6259
Anton Korobeynikove060b532007-04-17 19:34:00 +00006260
6261// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6262// Calls to _alloca is needed to probe the stack when allocating more than 4k
6263// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6264// that the guard pages used by the OS virtual memory manager are allocated in
6265// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006266SDValue
6267X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006268 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006269 assert(Subtarget->isTargetCygMing() &&
6270 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006271 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006272
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006273 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006274 SDValue Chain = Op.getOperand(0);
6275 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006276 // FIXME: Ensure alignment here
6277
Dan Gohman475871a2008-07-27 21:46:04 +00006278 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006279
Owen Andersone50ed302009-08-10 22:56:29 +00006280 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006281 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006282
Chris Lattnere563bbc2008-10-11 22:08:30 +00006283 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006284
Dale Johannesendd64c412009-02-04 00:33:20 +00006285 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006286 Flag = Chain.getValue(1);
6287
Owen Anderson825b72b2009-08-11 20:47:22 +00006288 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006289 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006290 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006291 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006292 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006293 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006294 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006295 Flag = Chain.getValue(1);
6296
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006297 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006298 DAG.getIntPtrConstant(0, true),
6299 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006300 Flag);
6301
Dale Johannesendd64c412009-02-04 00:33:20 +00006302 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006303
Dan Gohman475871a2008-07-27 21:46:04 +00006304 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006305 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006306}
6307
Dan Gohman475871a2008-07-27 21:46:04 +00006308SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006309X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006310 SDValue Chain,
6311 SDValue Dst, SDValue Src,
6312 SDValue Size, unsigned Align,
6313 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006314 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006315 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006316
Bill Wendling6f287b22008-09-30 21:22:07 +00006317 // If not DWORD aligned or size is more than the threshold, call the library.
6318 // The libc version is likely to be faster for these cases. It can use the
6319 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006320 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006321 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006322 ConstantSize->getZExtValue() >
6323 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006324 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006325
6326 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006327 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006328
Bill Wendling6158d842008-10-01 00:59:58 +00006329 if (const char *bzeroEntry = V &&
6330 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006331 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006332 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006333 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006334 TargetLowering::ArgListEntry Entry;
6335 Entry.Node = Dst;
6336 Entry.Ty = IntPtrTy;
6337 Args.push_back(Entry);
6338 Entry.Node = Size;
6339 Args.push_back(Entry);
6340 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006341 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6342 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006343 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006344 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6345 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006346 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006347 }
6348
Dan Gohman707e0182008-04-12 04:36:06 +00006349 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006350 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006351 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006352
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006353 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006355 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006356 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006357 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006358 unsigned BytesLeft = 0;
6359 bool TwoRepStos = false;
6360 if (ValC) {
6361 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006362 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006363
Evan Cheng0db9fe62006-04-25 20:13:52 +00006364 // If the value is a constant, then we can potentially use larger sets.
6365 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006366 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006367 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006368 ValReg = X86::AX;
6369 Val = (Val << 8) | Val;
6370 break;
6371 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006372 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006373 ValReg = X86::EAX;
6374 Val = (Val << 8) | Val;
6375 Val = (Val << 16) | Val;
6376 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006377 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006378 ValReg = X86::RAX;
6379 Val = (Val << 32) | Val;
6380 }
6381 break;
6382 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006383 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006384 ValReg = X86::AL;
6385 Count = DAG.getIntPtrConstant(SizeVal);
6386 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006387 }
6388
Owen Anderson825b72b2009-08-11 20:47:22 +00006389 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006390 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006391 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6392 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006393 }
6394
Dale Johannesen0f502f62009-02-03 22:26:09 +00006395 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006396 InFlag);
6397 InFlag = Chain.getValue(1);
6398 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006399 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006400 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006401 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006402 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006403 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006404
Scott Michelfdc40a02009-02-17 22:15:04 +00006405 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006406 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006407 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006408 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006409 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006410 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006411 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006412 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006413
Owen Anderson825b72b2009-08-11 20:47:22 +00006414 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006415 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6416 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006417
Evan Cheng0db9fe62006-04-25 20:13:52 +00006418 if (TwoRepStos) {
6419 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006420 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006421 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006422 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006423 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6424 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006425 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006426 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006427 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006428 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006429 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6430 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006431 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006432 // Handle the last 1 - 7 bytes.
6433 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006434 EVT AddrVT = Dst.getValueType();
6435 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006436
Dale Johannesen0f502f62009-02-03 22:26:09 +00006437 Chain = DAG.getMemset(Chain, dl,
6438 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006439 DAG.getConstant(Offset, AddrVT)),
6440 Src,
6441 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006442 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006443 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006444
Dan Gohman707e0182008-04-12 04:36:06 +00006445 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006446 return Chain;
6447}
Evan Cheng11e15b32006-04-03 20:53:28 +00006448
Dan Gohman475871a2008-07-27 21:46:04 +00006449SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006450X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006451 SDValue Chain, SDValue Dst, SDValue Src,
6452 SDValue Size, unsigned Align,
6453 bool AlwaysInline,
6454 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006455 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006456 // This requires the copy size to be a constant, preferrably
6457 // within a subtarget-specific limit.
6458 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6459 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006460 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006461 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006462 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006463 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006464
Evan Cheng1887c1c2008-08-21 21:00:15 +00006465 /// If not DWORD aligned, call the library.
6466 if ((Align & 3) != 0)
6467 return SDValue();
6468
6469 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006470 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006471 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006472 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006473
Duncan Sands83ec4b62008-06-06 12:08:01 +00006474 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006475 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006476 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006477 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006478
Dan Gohman475871a2008-07-27 21:46:04 +00006479 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006480 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006481 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006482 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006483 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006484 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006485 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006486 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006487 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006488 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006489 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006490 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491 InFlag = Chain.getValue(1);
6492
Owen Anderson825b72b2009-08-11 20:47:22 +00006493 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006494 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6495 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6496 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006497
Dan Gohman475871a2008-07-27 21:46:04 +00006498 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006499 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006500 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006501 // Handle the last 1 - 7 bytes.
6502 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006503 EVT DstVT = Dst.getValueType();
6504 EVT SrcVT = Src.getValueType();
6505 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006506 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006507 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006508 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006509 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006510 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006511 DAG.getConstant(BytesLeft, SizeVT),
6512 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006513 DstSV, DstSVOff + Offset,
6514 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006515 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006516
Owen Anderson825b72b2009-08-11 20:47:22 +00006517 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006518 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006519}
6520
Dan Gohman475871a2008-07-27 21:46:04 +00006521SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006522 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006523 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006524
Evan Cheng25ab6902006-09-08 06:48:29 +00006525 if (!Subtarget->is64Bit()) {
6526 // vastart just stores the address of the VarArgsFrameIndex slot into the
6527 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006528 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006529 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006530 }
6531
6532 // __va_list_tag:
6533 // gp_offset (0 - 6 * 8)
6534 // fp_offset (48 - 48 + 8 * 16)
6535 // overflow_arg_area (point to parameters coming in memory).
6536 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006537 SmallVector<SDValue, 8> MemOps;
6538 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006539 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006540 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006541 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006542 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006543 MemOps.push_back(Store);
6544
6545 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006546 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006547 FIN, DAG.getIntPtrConstant(4));
6548 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006549 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006550 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006551 MemOps.push_back(Store);
6552
6553 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006554 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006555 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006556 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006557 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006558 MemOps.push_back(Store);
6559
6560 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006561 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006562 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006563 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006564 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006565 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006566 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006567 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568}
6569
Dan Gohman475871a2008-07-27 21:46:04 +00006570SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006571 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6572 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006573 SDValue Chain = Op.getOperand(0);
6574 SDValue SrcPtr = Op.getOperand(1);
6575 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006576
Torok Edwindac237e2009-07-08 20:53:28 +00006577 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006578 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006579}
6580
Dan Gohman475871a2008-07-27 21:46:04 +00006581SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006582 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006583 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006584 SDValue Chain = Op.getOperand(0);
6585 SDValue DstPtr = Op.getOperand(1);
6586 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006587 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6588 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006589 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006590
Dale Johannesendd64c412009-02-04 00:33:20 +00006591 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006592 DAG.getIntPtrConstant(24), 8, false,
6593 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006594}
6595
Dan Gohman475871a2008-07-27 21:46:04 +00006596SDValue
6597X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006598 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006599 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006601 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006602 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 case Intrinsic::x86_sse_comieq_ss:
6604 case Intrinsic::x86_sse_comilt_ss:
6605 case Intrinsic::x86_sse_comile_ss:
6606 case Intrinsic::x86_sse_comigt_ss:
6607 case Intrinsic::x86_sse_comige_ss:
6608 case Intrinsic::x86_sse_comineq_ss:
6609 case Intrinsic::x86_sse_ucomieq_ss:
6610 case Intrinsic::x86_sse_ucomilt_ss:
6611 case Intrinsic::x86_sse_ucomile_ss:
6612 case Intrinsic::x86_sse_ucomigt_ss:
6613 case Intrinsic::x86_sse_ucomige_ss:
6614 case Intrinsic::x86_sse_ucomineq_ss:
6615 case Intrinsic::x86_sse2_comieq_sd:
6616 case Intrinsic::x86_sse2_comilt_sd:
6617 case Intrinsic::x86_sse2_comile_sd:
6618 case Intrinsic::x86_sse2_comigt_sd:
6619 case Intrinsic::x86_sse2_comige_sd:
6620 case Intrinsic::x86_sse2_comineq_sd:
6621 case Intrinsic::x86_sse2_ucomieq_sd:
6622 case Intrinsic::x86_sse2_ucomilt_sd:
6623 case Intrinsic::x86_sse2_ucomile_sd:
6624 case Intrinsic::x86_sse2_ucomigt_sd:
6625 case Intrinsic::x86_sse2_ucomige_sd:
6626 case Intrinsic::x86_sse2_ucomineq_sd: {
6627 unsigned Opc = 0;
6628 ISD::CondCode CC = ISD::SETCC_INVALID;
6629 switch (IntNo) {
6630 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006631 case Intrinsic::x86_sse_comieq_ss:
6632 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006633 Opc = X86ISD::COMI;
6634 CC = ISD::SETEQ;
6635 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006636 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006637 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006638 Opc = X86ISD::COMI;
6639 CC = ISD::SETLT;
6640 break;
6641 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006642 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006643 Opc = X86ISD::COMI;
6644 CC = ISD::SETLE;
6645 break;
6646 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006647 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648 Opc = X86ISD::COMI;
6649 CC = ISD::SETGT;
6650 break;
6651 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006652 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006653 Opc = X86ISD::COMI;
6654 CC = ISD::SETGE;
6655 break;
6656 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006657 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006658 Opc = X86ISD::COMI;
6659 CC = ISD::SETNE;
6660 break;
6661 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006662 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663 Opc = X86ISD::UCOMI;
6664 CC = ISD::SETEQ;
6665 break;
6666 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006667 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668 Opc = X86ISD::UCOMI;
6669 CC = ISD::SETLT;
6670 break;
6671 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006672 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006673 Opc = X86ISD::UCOMI;
6674 CC = ISD::SETLE;
6675 break;
6676 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006677 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678 Opc = X86ISD::UCOMI;
6679 CC = ISD::SETGT;
6680 break;
6681 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006682 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006683 Opc = X86ISD::UCOMI;
6684 CC = ISD::SETGE;
6685 break;
6686 case Intrinsic::x86_sse_ucomineq_ss:
6687 case Intrinsic::x86_sse2_ucomineq_sd:
6688 Opc = X86ISD::UCOMI;
6689 CC = ISD::SETNE;
6690 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006691 }
Evan Cheng734503b2006-09-11 02:19:56 +00006692
Dan Gohman475871a2008-07-27 21:46:04 +00006693 SDValue LHS = Op.getOperand(1);
6694 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006695 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006696 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006697 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6698 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6699 DAG.getConstant(X86CC, MVT::i8), Cond);
6700 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006701 }
Eric Christopher71c67532009-07-29 00:28:05 +00006702 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006703 // an integer value, not just an instruction so lower it to the ptest
6704 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006705 case Intrinsic::x86_sse41_ptestz:
6706 case Intrinsic::x86_sse41_ptestc:
6707 case Intrinsic::x86_sse41_ptestnzc:{
6708 unsigned X86CC = 0;
6709 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006710 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006711 case Intrinsic::x86_sse41_ptestz:
6712 // ZF = 1
6713 X86CC = X86::COND_E;
6714 break;
6715 case Intrinsic::x86_sse41_ptestc:
6716 // CF = 1
6717 X86CC = X86::COND_B;
6718 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006719 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006720 // ZF and CF = 0
6721 X86CC = X86::COND_A;
6722 break;
6723 }
Eric Christopherfd179292009-08-27 18:07:15 +00006724
Eric Christopher71c67532009-07-29 00:28:05 +00006725 SDValue LHS = Op.getOperand(1);
6726 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006727 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6728 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6729 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6730 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006731 }
Evan Cheng5759f972008-05-04 09:15:50 +00006732
6733 // Fix vector shift instructions where the last operand is a non-immediate
6734 // i32 value.
6735 case Intrinsic::x86_sse2_pslli_w:
6736 case Intrinsic::x86_sse2_pslli_d:
6737 case Intrinsic::x86_sse2_pslli_q:
6738 case Intrinsic::x86_sse2_psrli_w:
6739 case Intrinsic::x86_sse2_psrli_d:
6740 case Intrinsic::x86_sse2_psrli_q:
6741 case Intrinsic::x86_sse2_psrai_w:
6742 case Intrinsic::x86_sse2_psrai_d:
6743 case Intrinsic::x86_mmx_pslli_w:
6744 case Intrinsic::x86_mmx_pslli_d:
6745 case Intrinsic::x86_mmx_pslli_q:
6746 case Intrinsic::x86_mmx_psrli_w:
6747 case Intrinsic::x86_mmx_psrli_d:
6748 case Intrinsic::x86_mmx_psrli_q:
6749 case Intrinsic::x86_mmx_psrai_w:
6750 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006751 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006752 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006753 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006754
6755 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006756 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006757 switch (IntNo) {
6758 case Intrinsic::x86_sse2_pslli_w:
6759 NewIntNo = Intrinsic::x86_sse2_psll_w;
6760 break;
6761 case Intrinsic::x86_sse2_pslli_d:
6762 NewIntNo = Intrinsic::x86_sse2_psll_d;
6763 break;
6764 case Intrinsic::x86_sse2_pslli_q:
6765 NewIntNo = Intrinsic::x86_sse2_psll_q;
6766 break;
6767 case Intrinsic::x86_sse2_psrli_w:
6768 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6769 break;
6770 case Intrinsic::x86_sse2_psrli_d:
6771 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6772 break;
6773 case Intrinsic::x86_sse2_psrli_q:
6774 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6775 break;
6776 case Intrinsic::x86_sse2_psrai_w:
6777 NewIntNo = Intrinsic::x86_sse2_psra_w;
6778 break;
6779 case Intrinsic::x86_sse2_psrai_d:
6780 NewIntNo = Intrinsic::x86_sse2_psra_d;
6781 break;
6782 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006783 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006784 switch (IntNo) {
6785 case Intrinsic::x86_mmx_pslli_w:
6786 NewIntNo = Intrinsic::x86_mmx_psll_w;
6787 break;
6788 case Intrinsic::x86_mmx_pslli_d:
6789 NewIntNo = Intrinsic::x86_mmx_psll_d;
6790 break;
6791 case Intrinsic::x86_mmx_pslli_q:
6792 NewIntNo = Intrinsic::x86_mmx_psll_q;
6793 break;
6794 case Intrinsic::x86_mmx_psrli_w:
6795 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6796 break;
6797 case Intrinsic::x86_mmx_psrli_d:
6798 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6799 break;
6800 case Intrinsic::x86_mmx_psrli_q:
6801 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6802 break;
6803 case Intrinsic::x86_mmx_psrai_w:
6804 NewIntNo = Intrinsic::x86_mmx_psra_w;
6805 break;
6806 case Intrinsic::x86_mmx_psrai_d:
6807 NewIntNo = Intrinsic::x86_mmx_psra_d;
6808 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006809 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006810 }
6811 break;
6812 }
6813 }
Mon P Wangefa42202009-09-03 19:56:25 +00006814
6815 // The vector shift intrinsics with scalars uses 32b shift amounts but
6816 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6817 // to be zero.
6818 SDValue ShOps[4];
6819 ShOps[0] = ShAmt;
6820 ShOps[1] = DAG.getConstant(0, MVT::i32);
6821 if (ShAmtVT == MVT::v4i32) {
6822 ShOps[2] = DAG.getUNDEF(MVT::i32);
6823 ShOps[3] = DAG.getUNDEF(MVT::i32);
6824 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6825 } else {
6826 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6827 }
6828
Owen Andersone50ed302009-08-10 22:56:29 +00006829 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006830 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006831 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006832 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006833 Op.getOperand(1), ShAmt);
6834 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006835 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006836}
Evan Cheng72261582005-12-20 06:22:03 +00006837
Dan Gohman475871a2008-07-27 21:46:04 +00006838SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006839 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006840 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006841
6842 if (Depth > 0) {
6843 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6844 SDValue Offset =
6845 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006846 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006847 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006848 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006849 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006850 NULL, 0);
6851 }
6852
6853 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006854 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006855 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006856 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006857}
6858
Dan Gohman475871a2008-07-27 21:46:04 +00006859SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006860 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6861 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006862 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006863 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006864 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6865 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006866 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006867 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006868 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006869 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006870}
6871
Dan Gohman475871a2008-07-27 21:46:04 +00006872SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006873 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006874 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006875}
6876
Dan Gohman475871a2008-07-27 21:46:04 +00006877SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006878{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006879 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006880 SDValue Chain = Op.getOperand(0);
6881 SDValue Offset = Op.getOperand(1);
6882 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006883 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006884
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006885 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6886 getPointerTy());
6887 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006888
Dale Johannesene4d209d2009-02-03 20:21:25 +00006889 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006890 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006891 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6892 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006893 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006894 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006895
Dale Johannesene4d209d2009-02-03 20:21:25 +00006896 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006897 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006898 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006899}
6900
Dan Gohman475871a2008-07-27 21:46:04 +00006901SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006902 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006903 SDValue Root = Op.getOperand(0);
6904 SDValue Trmp = Op.getOperand(1); // trampoline
6905 SDValue FPtr = Op.getOperand(2); // nested function
6906 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006907 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006908
Dan Gohman69de1932008-02-06 22:27:42 +00006909 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006910
Duncan Sands339e14f2008-01-16 22:55:25 +00006911 const X86InstrInfo *TII =
6912 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6913
Duncan Sandsb116fac2007-07-27 20:02:49 +00006914 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006915 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006916
6917 // Large code-model.
6918
6919 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6920 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6921
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006922 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6923 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006924
6925 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6926
6927 // Load the pointer to the nested function into R11.
6928 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006929 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006931 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006932
Owen Anderson825b72b2009-08-11 20:47:22 +00006933 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6934 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006935 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006936
6937 // Load the 'nest' parameter value into R10.
6938 // R10 is specified in X86CallingConv.td
6939 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6941 DAG.getConstant(10, MVT::i64));
6942 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006943 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006944
Owen Anderson825b72b2009-08-11 20:47:22 +00006945 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6946 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006947 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006948
6949 // Jump to the nested function.
6950 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006951 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6952 DAG.getConstant(20, MVT::i64));
6953 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006954 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006955
6956 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006957 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6958 DAG.getConstant(22, MVT::i64));
6959 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006960 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006961
Dan Gohman475871a2008-07-27 21:46:04 +00006962 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006963 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006964 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006965 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006966 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006967 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006968 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006969 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006970
6971 switch (CC) {
6972 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006973 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006974 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006975 case CallingConv::X86_StdCall: {
6976 // Pass 'nest' parameter in ECX.
6977 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006978 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006979
6980 // Check that ECX wasn't needed by an 'inreg' parameter.
6981 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006982 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006983
Chris Lattner58d74912008-03-12 17:45:29 +00006984 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006985 unsigned InRegCount = 0;
6986 unsigned Idx = 1;
6987
6988 for (FunctionType::param_iterator I = FTy->param_begin(),
6989 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006990 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006991 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006992 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006993
6994 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006995 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006996 }
6997 }
6998 break;
6999 }
7000 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007001 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007002 // Pass 'nest' parameter in EAX.
7003 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007004 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007005 break;
7006 }
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008 SDValue OutChains[4];
7009 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007010
Owen Anderson825b72b2009-08-11 20:47:22 +00007011 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7012 DAG.getConstant(10, MVT::i32));
7013 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007014
Duncan Sands339e14f2008-01-16 22:55:25 +00007015 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007016 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007017 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007018 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00007019 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007020
Owen Anderson825b72b2009-08-11 20:47:22 +00007021 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7022 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007023 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007024
Duncan Sands339e14f2008-01-16 22:55:25 +00007025 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00007026 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7027 DAG.getConstant(5, MVT::i32));
7028 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00007029 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007030
Owen Anderson825b72b2009-08-11 20:47:22 +00007031 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7032 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007033 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007034
Dan Gohman475871a2008-07-27 21:46:04 +00007035 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007037 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007038 }
7039}
7040
Dan Gohman475871a2008-07-27 21:46:04 +00007041SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007042 /*
7043 The rounding mode is in bits 11:10 of FPSR, and has the following
7044 settings:
7045 00 Round to nearest
7046 01 Round to -inf
7047 10 Round to +inf
7048 11 Round to 0
7049
7050 FLT_ROUNDS, on the other hand, expects the following:
7051 -1 Undefined
7052 0 Round to 0
7053 1 Round to nearest
7054 2 Round to +inf
7055 3 Round to -inf
7056
7057 To perform the conversion, we do:
7058 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7059 */
7060
7061 MachineFunction &MF = DAG.getMachineFunction();
7062 const TargetMachine &TM = MF.getTarget();
7063 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7064 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007065 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007066 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007067
7068 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007069 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007070 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007071
Owen Anderson825b72b2009-08-11 20:47:22 +00007072 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007073 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007074
7075 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007076 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007077
7078 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007079 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 DAG.getNode(ISD::SRL, dl, MVT::i16,
7081 DAG.getNode(ISD::AND, dl, MVT::i16,
7082 CWD, DAG.getConstant(0x800, MVT::i16)),
7083 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007084 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007085 DAG.getNode(ISD::SRL, dl, MVT::i16,
7086 DAG.getNode(ISD::AND, dl, MVT::i16,
7087 CWD, DAG.getConstant(0x400, MVT::i16)),
7088 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007089
Dan Gohman475871a2008-07-27 21:46:04 +00007090 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007091 DAG.getNode(ISD::AND, dl, MVT::i16,
7092 DAG.getNode(ISD::ADD, dl, MVT::i16,
7093 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7094 DAG.getConstant(1, MVT::i16)),
7095 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007096
7097
Duncan Sands83ec4b62008-06-06 12:08:01 +00007098 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007099 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007100}
7101
Dan Gohman475871a2008-07-27 21:46:04 +00007102SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007103 EVT VT = Op.getValueType();
7104 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007105 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007106 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007107
7108 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007109 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007110 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007112 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007113 }
Evan Cheng18efe262007-12-14 02:13:44 +00007114
Evan Cheng152804e2007-12-14 08:30:15 +00007115 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007116 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007117 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007118
7119 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007120 SDValue Ops[] = {
7121 Op,
7122 DAG.getConstant(NumBits+NumBits-1, OpVT),
7123 DAG.getConstant(X86::COND_E, MVT::i8),
7124 Op.getValue(1)
7125 };
7126 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007127
7128 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007129 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007130
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 if (VT == MVT::i8)
7132 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007133 return Op;
7134}
7135
Dan Gohman475871a2008-07-27 21:46:04 +00007136SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007137 EVT VT = Op.getValueType();
7138 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007139 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007140 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007141
7142 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007143 if (VT == MVT::i8) {
7144 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007145 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007146 }
Evan Cheng152804e2007-12-14 08:30:15 +00007147
7148 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007149 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007150 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007151
7152 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007153 SDValue Ops[] = {
7154 Op,
7155 DAG.getConstant(NumBits, OpVT),
7156 DAG.getConstant(X86::COND_E, MVT::i8),
7157 Op.getValue(1)
7158 };
7159 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007160
Owen Anderson825b72b2009-08-11 20:47:22 +00007161 if (VT == MVT::i8)
7162 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007163 return Op;
7164}
7165
Mon P Wangaf9b9522008-12-18 21:42:19 +00007166SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007167 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007169 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007170
Mon P Wangaf9b9522008-12-18 21:42:19 +00007171 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7172 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7173 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7174 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7175 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7176 //
7177 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7178 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7179 // return AloBlo + AloBhi + AhiBlo;
7180
7181 SDValue A = Op.getOperand(0);
7182 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007183
Dale Johannesene4d209d2009-02-03 20:21:25 +00007184 SDValue Ahi = 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 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007187 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007188 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7189 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007190 SDValue AloBlo = 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, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007193 SDValue AloBhi = 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 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007196 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007197 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007198 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007199 AloBhi = 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 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007202 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007203 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7204 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007205 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7206 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007207 return Res;
7208}
7209
7210
Bill Wendling74c37652008-12-09 22:08:41 +00007211SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7212 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7213 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007214 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7215 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007216 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007217 SDValue LHS = N->getOperand(0);
7218 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007219 unsigned BaseOp = 0;
7220 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007221 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007222
7223 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007224 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007225 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007226 // A subtract of one will be selected as a INC. Note that INC doesn't
7227 // set CF, so we can't do this for UADDO.
7228 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7229 if (C->getAPIntValue() == 1) {
7230 BaseOp = X86ISD::INC;
7231 Cond = X86::COND_O;
7232 break;
7233 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007234 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007235 Cond = X86::COND_O;
7236 break;
7237 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007238 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007239 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007240 break;
7241 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007242 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7243 // set CF, so we can't do this for USUBO.
7244 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7245 if (C->getAPIntValue() == 1) {
7246 BaseOp = X86ISD::DEC;
7247 Cond = X86::COND_O;
7248 break;
7249 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007250 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007251 Cond = X86::COND_O;
7252 break;
7253 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007254 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007255 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007256 break;
7257 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007258 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007259 Cond = X86::COND_O;
7260 break;
7261 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007262 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007263 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007264 break;
7265 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007266
Bill Wendling61edeb52008-12-02 01:06:39 +00007267 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007268 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007269 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007270
Bill Wendling61edeb52008-12-02 01:06:39 +00007271 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007272 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007273 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007274
Bill Wendling61edeb52008-12-02 01:06:39 +00007275 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7276 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007277}
7278
Dan Gohman475871a2008-07-27 21:46:04 +00007279SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007280 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007281 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007282 unsigned Reg = 0;
7283 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007284 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007285 default:
7286 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 case MVT::i8: Reg = X86::AL; size = 1; break;
7288 case MVT::i16: Reg = X86::AX; size = 2; break;
7289 case MVT::i32: Reg = X86::EAX; size = 4; break;
7290 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007291 assert(Subtarget->is64Bit() && "Node not type legal!");
7292 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007293 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007294 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007295 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007296 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007297 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007298 Op.getOperand(1),
7299 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007300 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007301 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007303 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007304 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007305 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007306 return cpOut;
7307}
7308
Duncan Sands1607f052008-12-01 11:39:25 +00007309SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007310 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007311 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007313 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007314 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007315 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7317 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007318 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007319 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7320 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007321 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007322 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007323 rdx.getValue(1)
7324 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007325 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007326}
7327
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007328SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7329 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007330 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007331 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007332 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007333 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007334 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007335 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007336 Node->getOperand(0),
7337 Node->getOperand(1), negOp,
7338 cast<AtomicSDNode>(Node)->getSrcValue(),
7339 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007340}
7341
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342/// LowerOperation - Provide custom lowering hooks for some operations.
7343///
Dan Gohman475871a2008-07-27 21:46:04 +00007344SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007345 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007346 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007347 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7348 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007349 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007350 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007351 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7352 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7353 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7354 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7355 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7356 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007357 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007358 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007359 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007360 case ISD::SHL_PARTS:
7361 case ISD::SRA_PARTS:
7362 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7363 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007364 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007365 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007366 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007367 case ISD::FABS: return LowerFABS(Op, DAG);
7368 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007369 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007370 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007371 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007372 case ISD::SELECT: return LowerSELECT(Op, DAG);
7373 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007374 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007375 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007376 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007377 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007378 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007379 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7380 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007381 case ISD::FRAME_TO_ARGS_OFFSET:
7382 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007383 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007384 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007385 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007386 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007387 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7388 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007389 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007390 case ISD::SADDO:
7391 case ISD::UADDO:
7392 case ISD::SSUBO:
7393 case ISD::USUBO:
7394 case ISD::SMULO:
7395 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007396 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007397 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007398}
7399
Duncan Sands1607f052008-12-01 11:39:25 +00007400void X86TargetLowering::
7401ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7402 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007403 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007405 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007406
7407 SDValue Chain = Node->getOperand(0);
7408 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007409 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007410 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007412 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007413 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007414 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007415 SDValue Result =
7416 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7417 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007418 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007419 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007420 Results.push_back(Result.getValue(2));
7421}
7422
Duncan Sands126d9072008-07-04 11:47:58 +00007423/// ReplaceNodeResults - Replace a node with an illegal result type
7424/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007425void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7426 SmallVectorImpl<SDValue>&Results,
7427 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007428 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007429 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007430 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007431 assert(false && "Do not know how to custom type legalize this operation!");
7432 return;
7433 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007434 std::pair<SDValue,SDValue> Vals =
7435 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007436 SDValue FIST = Vals.first, StackSlot = Vals.second;
7437 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007438 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007439 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007441 }
7442 return;
7443 }
7444 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007446 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007447 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007448 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007449 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007450 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007451 eax.getValue(2));
7452 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7453 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007454 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007455 Results.push_back(edx.getValue(1));
7456 return;
7457 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007458 case ISD::SDIV:
7459 case ISD::UDIV:
7460 case ISD::SREM:
7461 case ISD::UREM: {
7462 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7463 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7464 return;
7465 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007466 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007467 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007468 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007469 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007470 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7471 DAG.getConstant(0, MVT::i32));
7472 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7473 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007474 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7475 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007476 cpInL.getValue(1));
7477 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7479 DAG.getConstant(0, MVT::i32));
7480 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7481 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007482 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007483 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007484 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007485 swapInL.getValue(1));
7486 SDValue Ops[] = { swapInH.getValue(0),
7487 N->getOperand(1),
7488 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007490 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007491 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007492 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007493 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007495 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007497 Results.push_back(cpOutH.getValue(1));
7498 return;
7499 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007500 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007501 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7502 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007503 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007504 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7505 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007506 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007507 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7508 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007509 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007510 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7511 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007512 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007513 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7514 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007515 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007516 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7517 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007518 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007519 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7520 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007521 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007522}
7523
Evan Cheng72261582005-12-20 06:22:03 +00007524const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7525 switch (Opcode) {
7526 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007527 case X86ISD::BSF: return "X86ISD::BSF";
7528 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007529 case X86ISD::SHLD: return "X86ISD::SHLD";
7530 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007531 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007532 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007533 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007534 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007535 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007536 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007537 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7538 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7539 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007540 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007541 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007542 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007543 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007544 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007545 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007546 case X86ISD::COMI: return "X86ISD::COMI";
7547 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007548 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007549 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007550 case X86ISD::CMOV: return "X86ISD::CMOV";
7551 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007552 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007553 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7554 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007555 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007556 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007557 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007558 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007559 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007560 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7561 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007562 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007563 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007564 case X86ISD::FMAX: return "X86ISD::FMAX";
7565 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007566 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7567 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007568 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007569 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007570 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007571 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007572 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007573 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7574 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007575 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7576 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7577 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7578 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7579 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7580 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007581 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7582 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007583 case X86ISD::VSHL: return "X86ISD::VSHL";
7584 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007585 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7586 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7587 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7588 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7589 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7590 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7591 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7592 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7593 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7594 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007595 case X86ISD::ADD: return "X86ISD::ADD";
7596 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007597 case X86ISD::SMUL: return "X86ISD::SMUL";
7598 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007599 case X86ISD::INC: return "X86ISD::INC";
7600 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007601 case X86ISD::OR: return "X86ISD::OR";
7602 case X86ISD::XOR: return "X86ISD::XOR";
7603 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007604 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007605 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007606 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007607 }
7608}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007609
Chris Lattnerc9addb72007-03-30 23:15:24 +00007610// isLegalAddressingMode - Return true if the addressing mode represented
7611// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007612bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007613 const Type *Ty) const {
7614 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007615 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007616
Chris Lattnerc9addb72007-03-30 23:15:24 +00007617 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007618 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007619 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007620
Chris Lattnerc9addb72007-03-30 23:15:24 +00007621 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007622 unsigned GVFlags =
7623 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007624
Chris Lattnerdfed4132009-07-10 07:38:24 +00007625 // If a reference to this global requires an extra load, we can't fold it.
7626 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007627 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007628
Chris Lattnerdfed4132009-07-10 07:38:24 +00007629 // If BaseGV requires a register for the PIC base, we cannot also have a
7630 // BaseReg specified.
7631 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007632 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007633
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007634 // If lower 4G is not available, then we must use rip-relative addressing.
7635 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7636 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007637 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007638
Chris Lattnerc9addb72007-03-30 23:15:24 +00007639 switch (AM.Scale) {
7640 case 0:
7641 case 1:
7642 case 2:
7643 case 4:
7644 case 8:
7645 // These scales always work.
7646 break;
7647 case 3:
7648 case 5:
7649 case 9:
7650 // These scales are formed with basereg+scalereg. Only accept if there is
7651 // no basereg yet.
7652 if (AM.HasBaseReg)
7653 return false;
7654 break;
7655 default: // Other stuff never works.
7656 return false;
7657 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007658
Chris Lattnerc9addb72007-03-30 23:15:24 +00007659 return true;
7660}
7661
7662
Evan Cheng2bd122c2007-10-26 01:56:11 +00007663bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7664 if (!Ty1->isInteger() || !Ty2->isInteger())
7665 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007666 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7667 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007668 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007669 return false;
7670 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007671}
7672
Owen Andersone50ed302009-08-10 22:56:29 +00007673bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007674 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007675 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007676 unsigned NumBits1 = VT1.getSizeInBits();
7677 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007678 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007679 return false;
7680 return Subtarget->is64Bit() || NumBits1 < 64;
7681}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007682
Dan Gohman97121ba2009-04-08 00:15:30 +00007683bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007684 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007685 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007686}
7687
Owen Andersone50ed302009-08-10 22:56:29 +00007688bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007689 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007690 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007691}
7692
Owen Andersone50ed302009-08-10 22:56:29 +00007693bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007694 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007695 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007696}
7697
Evan Cheng60c07e12006-07-05 22:17:51 +00007698/// isShuffleMaskLegal - Targets can use this to indicate that they only
7699/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7700/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7701/// are assumed to be legal.
7702bool
Eric Christopherfd179292009-08-27 18:07:15 +00007703X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007704 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007705 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007706 if (VT.getSizeInBits() == 64)
7707 return false;
7708
Nate Begemana09008b2009-10-19 02:17:23 +00007709 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007710 return (VT.getVectorNumElements() == 2 ||
7711 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7712 isMOVLMask(M, VT) ||
7713 isSHUFPMask(M, VT) ||
7714 isPSHUFDMask(M, VT) ||
7715 isPSHUFHWMask(M, VT) ||
7716 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007717 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007718 isUNPCKLMask(M, VT) ||
7719 isUNPCKHMask(M, VT) ||
7720 isUNPCKL_v_undef_Mask(M, VT) ||
7721 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007722}
7723
Dan Gohman7d8143f2008-04-09 20:09:42 +00007724bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007725X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007726 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007727 unsigned NumElts = VT.getVectorNumElements();
7728 // FIXME: This collection of masks seems suspect.
7729 if (NumElts == 2)
7730 return true;
7731 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7732 return (isMOVLMask(Mask, VT) ||
7733 isCommutedMOVLMask(Mask, VT, true) ||
7734 isSHUFPMask(Mask, VT) ||
7735 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007736 }
7737 return false;
7738}
7739
7740//===----------------------------------------------------------------------===//
7741// X86 Scheduler Hooks
7742//===----------------------------------------------------------------------===//
7743
Mon P Wang63307c32008-05-05 19:05:59 +00007744// private utility function
7745MachineBasicBlock *
7746X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7747 MachineBasicBlock *MBB,
7748 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007749 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007750 unsigned LoadOpc,
7751 unsigned CXchgOpc,
7752 unsigned copyOpc,
7753 unsigned notOpc,
7754 unsigned EAXreg,
7755 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007756 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007757 // For the atomic bitwise operator, we generate
7758 // thisMBB:
7759 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007760 // ld t1 = [bitinstr.addr]
7761 // op t2 = t1, [bitinstr.val]
7762 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007763 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7764 // bz newMBB
7765 // fallthrough -->nextMBB
7766 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7767 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007768 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007769 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007770
Mon P Wang63307c32008-05-05 19:05:59 +00007771 /// First build the CFG
7772 MachineFunction *F = MBB->getParent();
7773 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007774 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7775 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7776 F->insert(MBBIter, newMBB);
7777 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007778
Mon P Wang63307c32008-05-05 19:05:59 +00007779 // Move all successors to thisMBB to nextMBB
7780 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007781
Mon P Wang63307c32008-05-05 19:05:59 +00007782 // Update thisMBB to fall through to newMBB
7783 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007784
Mon P Wang63307c32008-05-05 19:05:59 +00007785 // newMBB jumps to itself and fall through to nextMBB
7786 newMBB->addSuccessor(nextMBB);
7787 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007788
Mon P Wang63307c32008-05-05 19:05:59 +00007789 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007790 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007791 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007792 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007793 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007794 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007795 int numArgs = bInstr->getNumOperands() - 1;
7796 for (int i=0; i < numArgs; ++i)
7797 argOpers[i] = &bInstr->getOperand(i+1);
7798
7799 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007800 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7801 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007802
Dale Johannesen140be2d2008-08-19 18:47:28 +00007803 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007804 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007805 for (int i=0; i <= lastAddrIndx; ++i)
7806 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007807
Dale Johannesen140be2d2008-08-19 18:47:28 +00007808 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007809 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007810 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007812 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007813 tt = t1;
7814
Dale Johannesen140be2d2008-08-19 18:47:28 +00007815 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007816 assert((argOpers[valArgIndx]->isReg() ||
7817 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007818 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007819 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007820 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007821 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007822 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007823 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007824 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007825
Dale Johannesene4d209d2009-02-03 20:21:25 +00007826 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007827 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007828
Dale Johannesene4d209d2009-02-03 20:21:25 +00007829 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007830 for (int i=0; i <= lastAddrIndx; ++i)
7831 (*MIB).addOperand(*argOpers[i]);
7832 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007833 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007834 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7835 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007836
Dale Johannesene4d209d2009-02-03 20:21:25 +00007837 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007838 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007839
Mon P Wang63307c32008-05-05 19:05:59 +00007840 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007841 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007842
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007843 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007844 return nextMBB;
7845}
7846
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007847// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007848MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007849X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7850 MachineBasicBlock *MBB,
7851 unsigned regOpcL,
7852 unsigned regOpcH,
7853 unsigned immOpcL,
7854 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007855 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007856 // For the atomic bitwise operator, we generate
7857 // thisMBB (instructions are in pairs, except cmpxchg8b)
7858 // ld t1,t2 = [bitinstr.addr]
7859 // newMBB:
7860 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7861 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007862 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007863 // mov ECX, EBX <- t5, t6
7864 // mov EAX, EDX <- t1, t2
7865 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7866 // mov t3, t4 <- EAX, EDX
7867 // bz newMBB
7868 // result in out1, out2
7869 // fallthrough -->nextMBB
7870
7871 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7872 const unsigned LoadOpc = X86::MOV32rm;
7873 const unsigned copyOpc = X86::MOV32rr;
7874 const unsigned NotOpc = X86::NOT32r;
7875 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7876 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7877 MachineFunction::iterator MBBIter = MBB;
7878 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007879
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007880 /// First build the CFG
7881 MachineFunction *F = MBB->getParent();
7882 MachineBasicBlock *thisMBB = MBB;
7883 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7884 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7885 F->insert(MBBIter, newMBB);
7886 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007887
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007888 // Move all successors to thisMBB to nextMBB
7889 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007890
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007891 // Update thisMBB to fall through to newMBB
7892 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007893
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007894 // newMBB jumps to itself and fall through to nextMBB
7895 newMBB->addSuccessor(nextMBB);
7896 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007897
Dale Johannesene4d209d2009-02-03 20:21:25 +00007898 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007899 // Insert instructions into newMBB based on incoming instruction
7900 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007901 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007902 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007903 MachineOperand& dest1Oper = bInstr->getOperand(0);
7904 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007905 MachineOperand* argOpers[2 + X86AddrNumOperands];
7906 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007907 argOpers[i] = &bInstr->getOperand(i+2);
7908
Evan Chengad5b52f2010-01-08 19:14:57 +00007909 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007910 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007911
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007912 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007913 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007914 for (int i=0; i <= lastAddrIndx; ++i)
7915 (*MIB).addOperand(*argOpers[i]);
7916 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007917 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007918 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007919 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007920 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007921 MachineOperand newOp3 = *(argOpers[3]);
7922 if (newOp3.isImm())
7923 newOp3.setImm(newOp3.getImm()+4);
7924 else
7925 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007926 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007927 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007928
7929 // t3/4 are defined later, at the bottom of the loop
7930 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7931 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007932 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007933 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007934 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007935 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7936
Evan Cheng306b4ca2010-01-08 23:41:50 +00007937 // The subsequent operations should be using the destination registers of
7938 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007939 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007940 t1 = F->getRegInfo().createVirtualRegister(RC);
7941 t2 = F->getRegInfo().createVirtualRegister(RC);
7942 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7943 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007944 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007945 t1 = dest1Oper.getReg();
7946 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007947 }
7948
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007949 int valArgIndx = lastAddrIndx + 1;
7950 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007951 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007952 "invalid operand");
7953 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7954 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007955 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007956 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007957 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007959 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007960 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007961 (*MIB).addOperand(*argOpers[valArgIndx]);
7962 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007963 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007964 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007965 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007966 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007967 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007968 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007970 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007971 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007972 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007973
Dale Johannesene4d209d2009-02-03 20:21:25 +00007974 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007975 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977 MIB.addReg(t2);
7978
Dale Johannesene4d209d2009-02-03 20:21:25 +00007979 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007980 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007982 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007983
Dale Johannesene4d209d2009-02-03 20:21:25 +00007984 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007985 for (int i=0; i <= lastAddrIndx; ++i)
7986 (*MIB).addOperand(*argOpers[i]);
7987
7988 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007989 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7990 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007991
Dale Johannesene4d209d2009-02-03 20:21:25 +00007992 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007993 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007995 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007996
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007997 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007998 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007999
8000 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8001 return nextMBB;
8002}
8003
8004// private utility function
8005MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008006X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8007 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008008 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008009 // For the atomic min/max operator, we generate
8010 // thisMBB:
8011 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008012 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008013 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008014 // cmp t1, t2
8015 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008016 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008017 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8018 // bz newMBB
8019 // fallthrough -->nextMBB
8020 //
8021 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8022 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008023 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008024 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008025
Mon P Wang63307c32008-05-05 19:05:59 +00008026 /// First build the CFG
8027 MachineFunction *F = MBB->getParent();
8028 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008029 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8030 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8031 F->insert(MBBIter, newMBB);
8032 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008033
Dan Gohmand6708ea2009-08-15 01:38:56 +00008034 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008035 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008036
Mon P Wang63307c32008-05-05 19:05:59 +00008037 // Update thisMBB to fall through to newMBB
8038 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008039
Mon P Wang63307c32008-05-05 19:05:59 +00008040 // newMBB jumps to newMBB and fall through to nextMBB
8041 newMBB->addSuccessor(nextMBB);
8042 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008043
Dale Johannesene4d209d2009-02-03 20:21:25 +00008044 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008045 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008046 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008047 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008048 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008049 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008050 int numArgs = mInstr->getNumOperands() - 1;
8051 for (int i=0; i < numArgs; ++i)
8052 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008053
Mon P Wang63307c32008-05-05 19:05:59 +00008054 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008055 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8056 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008057
Mon P Wangab3e7472008-05-05 22:56:23 +00008058 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008059 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008060 for (int i=0; i <= lastAddrIndx; ++i)
8061 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008062
Mon P Wang63307c32008-05-05 19:05:59 +00008063 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008064 assert((argOpers[valArgIndx]->isReg() ||
8065 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008066 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008067
8068 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008069 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008070 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008071 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008072 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008073 (*MIB).addOperand(*argOpers[valArgIndx]);
8074
Dale Johannesene4d209d2009-02-03 20:21:25 +00008075 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008076 MIB.addReg(t1);
8077
Dale Johannesene4d209d2009-02-03 20:21:25 +00008078 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008079 MIB.addReg(t1);
8080 MIB.addReg(t2);
8081
8082 // Generate movc
8083 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008085 MIB.addReg(t2);
8086 MIB.addReg(t1);
8087
8088 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008089 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008090 for (int i=0; i <= lastAddrIndx; ++i)
8091 (*MIB).addOperand(*argOpers[i]);
8092 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008093 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008094 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8095 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008096
Dale Johannesene4d209d2009-02-03 20:21:25 +00008097 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008098 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008099
Mon P Wang63307c32008-05-05 19:05:59 +00008100 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008101 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008102
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008103 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008104 return nextMBB;
8105}
8106
Eric Christopherf83a5de2009-08-27 18:08:16 +00008107// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8108// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008109MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008110X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008111 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008112
8113 MachineFunction *F = BB->getParent();
8114 DebugLoc dl = MI->getDebugLoc();
8115 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8116
8117 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008118 if (memArg)
8119 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8120 else
8121 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008122
8123 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8124
8125 for (unsigned i = 0; i < numArgs; ++i) {
8126 MachineOperand &Op = MI->getOperand(i+1);
8127
8128 if (!(Op.isReg() && Op.isImplicit()))
8129 MIB.addOperand(Op);
8130 }
8131
8132 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8133 .addReg(X86::XMM0);
8134
8135 F->DeleteMachineInstr(MI);
8136
8137 return BB;
8138}
8139
8140MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008141X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8142 MachineInstr *MI,
8143 MachineBasicBlock *MBB) const {
8144 // Emit code to save XMM registers to the stack. The ABI says that the
8145 // number of registers to save is given in %al, so it's theoretically
8146 // possible to do an indirect jump trick to avoid saving all of them,
8147 // however this code takes a simpler approach and just executes all
8148 // of the stores if %al is non-zero. It's less code, and it's probably
8149 // easier on the hardware branch predictor, and stores aren't all that
8150 // expensive anyway.
8151
8152 // Create the new basic blocks. One block contains all the XMM stores,
8153 // and one block is the final destination regardless of whether any
8154 // stores were performed.
8155 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8156 MachineFunction *F = MBB->getParent();
8157 MachineFunction::iterator MBBIter = MBB;
8158 ++MBBIter;
8159 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8160 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8161 F->insert(MBBIter, XMMSaveMBB);
8162 F->insert(MBBIter, EndMBB);
8163
8164 // Set up the CFG.
8165 // Move any original successors of MBB to the end block.
8166 EndMBB->transferSuccessors(MBB);
8167 // The original block will now fall through to the XMM save block.
8168 MBB->addSuccessor(XMMSaveMBB);
8169 // The XMMSaveMBB will fall through to the end block.
8170 XMMSaveMBB->addSuccessor(EndMBB);
8171
8172 // Now add the instructions.
8173 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8174 DebugLoc DL = MI->getDebugLoc();
8175
8176 unsigned CountReg = MI->getOperand(0).getReg();
8177 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8178 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8179
8180 if (!Subtarget->isTargetWin64()) {
8181 // If %al is 0, branch around the XMM save block.
8182 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8183 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8184 MBB->addSuccessor(EndMBB);
8185 }
8186
8187 // In the XMM save block, save all the XMM argument registers.
8188 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8189 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008190 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008191 F->getMachineMemOperand(
8192 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8193 MachineMemOperand::MOStore, Offset,
8194 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008195 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8196 .addFrameIndex(RegSaveFrameIndex)
8197 .addImm(/*Scale=*/1)
8198 .addReg(/*IndexReg=*/0)
8199 .addImm(/*Disp=*/Offset)
8200 .addReg(/*Segment=*/0)
8201 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008202 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008203 }
8204
8205 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8206
8207 return EndMBB;
8208}
Mon P Wang63307c32008-05-05 19:05:59 +00008209
Evan Cheng60c07e12006-07-05 22:17:51 +00008210MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008211X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008212 MachineBasicBlock *BB,
8213 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008214 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8215 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008216
Chris Lattner52600972009-09-02 05:57:00 +00008217 // To "insert" a SELECT_CC instruction, we actually have to insert the
8218 // diamond control-flow pattern. The incoming instruction knows the
8219 // destination vreg to set, the condition code register to branch on, the
8220 // true/false values to select between, and a branch opcode to use.
8221 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8222 MachineFunction::iterator It = BB;
8223 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008224
Chris Lattner52600972009-09-02 05:57:00 +00008225 // thisMBB:
8226 // ...
8227 // TrueVal = ...
8228 // cmpTY ccX, r1, r2
8229 // bCC copy1MBB
8230 // fallthrough --> copy0MBB
8231 MachineBasicBlock *thisMBB = BB;
8232 MachineFunction *F = BB->getParent();
8233 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8234 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8235 unsigned Opc =
8236 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8237 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8238 F->insert(It, copy0MBB);
8239 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008240 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008241 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008242 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008243 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008244 E = BB->succ_end(); I != E; ++I) {
8245 EM->insert(std::make_pair(*I, sinkMBB));
8246 sinkMBB->addSuccessor(*I);
8247 }
8248 // Next, remove all successors of the current block, and add the true
8249 // and fallthrough blocks as its successors.
8250 while (!BB->succ_empty())
8251 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008252 // Add the true and fallthrough blocks as its successors.
8253 BB->addSuccessor(copy0MBB);
8254 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008255
Chris Lattner52600972009-09-02 05:57:00 +00008256 // copy0MBB:
8257 // %FalseValue = ...
8258 // # fallthrough to sinkMBB
8259 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008260
Chris Lattner52600972009-09-02 05:57:00 +00008261 // Update machine-CFG edges
8262 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008263
Chris Lattner52600972009-09-02 05:57:00 +00008264 // sinkMBB:
8265 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8266 // ...
8267 BB = sinkMBB;
8268 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8269 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8270 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8271
8272 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8273 return BB;
8274}
8275
8276
8277MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008278X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008279 MachineBasicBlock *BB,
8280 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008281 switch (MI->getOpcode()) {
8282 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008283 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008284 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008285 case X86::CMOV_FR32:
8286 case X86::CMOV_FR64:
8287 case X86::CMOV_V4F32:
8288 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008289 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008290 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008291
Dale Johannesen849f2142007-07-03 00:53:03 +00008292 case X86::FP32_TO_INT16_IN_MEM:
8293 case X86::FP32_TO_INT32_IN_MEM:
8294 case X86::FP32_TO_INT64_IN_MEM:
8295 case X86::FP64_TO_INT16_IN_MEM:
8296 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008297 case X86::FP64_TO_INT64_IN_MEM:
8298 case X86::FP80_TO_INT16_IN_MEM:
8299 case X86::FP80_TO_INT32_IN_MEM:
8300 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8302 DebugLoc DL = MI->getDebugLoc();
8303
Evan Cheng60c07e12006-07-05 22:17:51 +00008304 // Change the floating point control register to use "round towards zero"
8305 // mode when truncating to an integer value.
8306 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008307 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008308 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008309
8310 // Load the old value of the high byte of the control word...
8311 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008312 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008313 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008314 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008315
8316 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008317 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008318 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008319
8320 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008321 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008322
8323 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008324 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008325 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008326
8327 // Get the X86 opcode to use.
8328 unsigned Opc;
8329 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008330 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008331 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8332 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8333 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8334 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8335 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8336 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008337 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8338 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8339 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008340 }
8341
8342 X86AddressMode AM;
8343 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008344 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008345 AM.BaseType = X86AddressMode::RegBase;
8346 AM.Base.Reg = Op.getReg();
8347 } else {
8348 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008349 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008350 }
8351 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008352 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008353 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008354 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008355 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008356 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008357 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008358 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008359 AM.GV = Op.getGlobal();
8360 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008361 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008362 }
Chris Lattner52600972009-09-02 05:57:00 +00008363 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008364 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008365
8366 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008367 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008368
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008369 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008370 return BB;
8371 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008372 // String/text processing lowering.
8373 case X86::PCMPISTRM128REG:
8374 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8375 case X86::PCMPISTRM128MEM:
8376 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8377 case X86::PCMPESTRM128REG:
8378 return EmitPCMP(MI, BB, 5, false /* in mem */);
8379 case X86::PCMPESTRM128MEM:
8380 return EmitPCMP(MI, BB, 5, true /* in mem */);
8381
8382 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008383 case X86::ATOMAND32:
8384 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008385 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008386 X86::LCMPXCHG32, X86::MOV32rr,
8387 X86::NOT32r, X86::EAX,
8388 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008389 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008390 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8391 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008392 X86::LCMPXCHG32, X86::MOV32rr,
8393 X86::NOT32r, X86::EAX,
8394 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008395 case X86::ATOMXOR32:
8396 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008397 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008398 X86::LCMPXCHG32, X86::MOV32rr,
8399 X86::NOT32r, X86::EAX,
8400 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008401 case X86::ATOMNAND32:
8402 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008403 X86::AND32ri, X86::MOV32rm,
8404 X86::LCMPXCHG32, X86::MOV32rr,
8405 X86::NOT32r, X86::EAX,
8406 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008407 case X86::ATOMMIN32:
8408 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8409 case X86::ATOMMAX32:
8410 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8411 case X86::ATOMUMIN32:
8412 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8413 case X86::ATOMUMAX32:
8414 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008415
8416 case X86::ATOMAND16:
8417 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8418 X86::AND16ri, X86::MOV16rm,
8419 X86::LCMPXCHG16, X86::MOV16rr,
8420 X86::NOT16r, X86::AX,
8421 X86::GR16RegisterClass);
8422 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008423 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008424 X86::OR16ri, X86::MOV16rm,
8425 X86::LCMPXCHG16, X86::MOV16rr,
8426 X86::NOT16r, X86::AX,
8427 X86::GR16RegisterClass);
8428 case X86::ATOMXOR16:
8429 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8430 X86::XOR16ri, X86::MOV16rm,
8431 X86::LCMPXCHG16, X86::MOV16rr,
8432 X86::NOT16r, X86::AX,
8433 X86::GR16RegisterClass);
8434 case X86::ATOMNAND16:
8435 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8436 X86::AND16ri, X86::MOV16rm,
8437 X86::LCMPXCHG16, X86::MOV16rr,
8438 X86::NOT16r, X86::AX,
8439 X86::GR16RegisterClass, true);
8440 case X86::ATOMMIN16:
8441 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8442 case X86::ATOMMAX16:
8443 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8444 case X86::ATOMUMIN16:
8445 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8446 case X86::ATOMUMAX16:
8447 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8448
8449 case X86::ATOMAND8:
8450 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8451 X86::AND8ri, X86::MOV8rm,
8452 X86::LCMPXCHG8, X86::MOV8rr,
8453 X86::NOT8r, X86::AL,
8454 X86::GR8RegisterClass);
8455 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008456 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008457 X86::OR8ri, X86::MOV8rm,
8458 X86::LCMPXCHG8, X86::MOV8rr,
8459 X86::NOT8r, X86::AL,
8460 X86::GR8RegisterClass);
8461 case X86::ATOMXOR8:
8462 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8463 X86::XOR8ri, X86::MOV8rm,
8464 X86::LCMPXCHG8, X86::MOV8rr,
8465 X86::NOT8r, X86::AL,
8466 X86::GR8RegisterClass);
8467 case X86::ATOMNAND8:
8468 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8469 X86::AND8ri, X86::MOV8rm,
8470 X86::LCMPXCHG8, X86::MOV8rr,
8471 X86::NOT8r, X86::AL,
8472 X86::GR8RegisterClass, true);
8473 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008474 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008475 case X86::ATOMAND64:
8476 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008477 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008478 X86::LCMPXCHG64, X86::MOV64rr,
8479 X86::NOT64r, X86::RAX,
8480 X86::GR64RegisterClass);
8481 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008482 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8483 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008484 X86::LCMPXCHG64, X86::MOV64rr,
8485 X86::NOT64r, X86::RAX,
8486 X86::GR64RegisterClass);
8487 case X86::ATOMXOR64:
8488 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008489 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008490 X86::LCMPXCHG64, X86::MOV64rr,
8491 X86::NOT64r, X86::RAX,
8492 X86::GR64RegisterClass);
8493 case X86::ATOMNAND64:
8494 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8495 X86::AND64ri32, X86::MOV64rm,
8496 X86::LCMPXCHG64, X86::MOV64rr,
8497 X86::NOT64r, X86::RAX,
8498 X86::GR64RegisterClass, true);
8499 case X86::ATOMMIN64:
8500 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8501 case X86::ATOMMAX64:
8502 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8503 case X86::ATOMUMIN64:
8504 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8505 case X86::ATOMUMAX64:
8506 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008507
8508 // This group does 64-bit operations on a 32-bit host.
8509 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008510 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008511 X86::AND32rr, X86::AND32rr,
8512 X86::AND32ri, X86::AND32ri,
8513 false);
8514 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008515 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008516 X86::OR32rr, X86::OR32rr,
8517 X86::OR32ri, X86::OR32ri,
8518 false);
8519 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008520 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008521 X86::XOR32rr, X86::XOR32rr,
8522 X86::XOR32ri, X86::XOR32ri,
8523 false);
8524 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008525 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008526 X86::AND32rr, X86::AND32rr,
8527 X86::AND32ri, X86::AND32ri,
8528 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008529 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008530 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008531 X86::ADD32rr, X86::ADC32rr,
8532 X86::ADD32ri, X86::ADC32ri,
8533 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008534 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008535 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008536 X86::SUB32rr, X86::SBB32rr,
8537 X86::SUB32ri, X86::SBB32ri,
8538 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008539 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008540 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008541 X86::MOV32rr, X86::MOV32rr,
8542 X86::MOV32ri, X86::MOV32ri,
8543 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008544 case X86::VASTART_SAVE_XMM_REGS:
8545 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008546 }
8547}
8548
8549//===----------------------------------------------------------------------===//
8550// X86 Optimization Hooks
8551//===----------------------------------------------------------------------===//
8552
Dan Gohman475871a2008-07-27 21:46:04 +00008553void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008554 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008555 APInt &KnownZero,
8556 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008557 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008558 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008559 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008560 assert((Opc >= ISD::BUILTIN_OP_END ||
8561 Opc == ISD::INTRINSIC_WO_CHAIN ||
8562 Opc == ISD::INTRINSIC_W_CHAIN ||
8563 Opc == ISD::INTRINSIC_VOID) &&
8564 "Should use MaskedValueIsZero if you don't know whether Op"
8565 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008566
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008567 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008568 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008569 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008570 case X86ISD::ADD:
8571 case X86ISD::SUB:
8572 case X86ISD::SMUL:
8573 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008574 case X86ISD::INC:
8575 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008576 case X86ISD::OR:
8577 case X86ISD::XOR:
8578 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008579 // These nodes' second result is a boolean.
8580 if (Op.getResNo() == 0)
8581 break;
8582 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008583 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008584 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8585 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008586 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008587 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008588}
Chris Lattner259e97c2006-01-31 19:43:35 +00008589
Evan Cheng206ee9d2006-07-07 08:33:52 +00008590/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008591/// node is a GlobalAddress + offset.
8592bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8593 GlobalValue* &GA, int64_t &Offset) const{
8594 if (N->getOpcode() == X86ISD::Wrapper) {
8595 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008596 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008597 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008598 return true;
8599 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008600 }
Evan Chengad4196b2008-05-12 19:56:52 +00008601 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008602}
8603
Nate Begeman9008ca62009-04-27 18:41:29 +00008604static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008605 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008606 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008607 SelectionDAG &DAG, MachineFrameInfo *MFI,
8608 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008609 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008610 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008611 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008612 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008613 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008614 return false;
8615 continue;
8616 }
8617
Dan Gohman475871a2008-07-27 21:46:04 +00008618 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008619 if (!Elt.getNode() ||
8620 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008621 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008622 if (!LDBase) {
8623 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008624 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008625 LDBase = cast<LoadSDNode>(Elt.getNode());
8626 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008627 continue;
8628 }
8629 if (Elt.getOpcode() == ISD::UNDEF)
8630 continue;
8631
Nate Begemanabc01992009-06-05 21:37:30 +00008632 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008633 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008634 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008635 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008636 }
8637 return true;
8638}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008639
8640/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8641/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8642/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008643/// order. In the case of v2i64, it will see if it can rewrite the
8644/// shuffle to be an appropriate build vector so it can take advantage of
8645// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008646static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008647 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008648 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008649 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008650 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008651 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8652 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008653
Eli Friedman7a5e5552009-06-07 06:52:44 +00008654 if (VT.getSizeInBits() != 128)
8655 return SDValue();
8656
Mon P Wang1e955802009-04-03 02:43:30 +00008657 // Try to combine a vector_shuffle into a 128-bit load.
8658 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008659 LoadSDNode *LD = NULL;
8660 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008661 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008662 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008663 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008664
Eli Friedman7a5e5552009-06-07 06:52:44 +00008665 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008666 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008667 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8668 LD->getSrcValue(), LD->getSrcValueOffset(),
8669 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008670 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008671 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008672 LD->isVolatile(), LD->getAlignment());
8673 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008675 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8676 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008677 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8678 }
8679 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008680}
Evan Chengd880b972008-05-09 21:53:03 +00008681
Chris Lattner83e6c992006-10-04 06:57:07 +00008682/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008683static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008684 const X86Subtarget *Subtarget) {
8685 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008686 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008687 // Get the LHS/RHS of the select.
8688 SDValue LHS = N->getOperand(1);
8689 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008690
Dan Gohman670e5392009-09-21 18:03:22 +00008691 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8692 // instructions have the peculiarity that if either operand is a NaN,
8693 // they chose what we call the RHS operand (and as such are not symmetric).
8694 // It happens that this matches the semantics of the common C idiom
8695 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008696 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008697 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008698 Cond.getOpcode() == ISD::SETCC) {
8699 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008700
Chris Lattner47b4ce82009-03-11 05:48:52 +00008701 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008702 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008703 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8704 switch (CC) {
8705 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008706 case ISD::SETULT:
8707 // This can be a min if we can prove that at least one of the operands
8708 // is not a nan.
8709 if (!FiniteOnlyFPMath()) {
8710 if (DAG.isKnownNeverNaN(RHS)) {
8711 // Put the potential NaN in the RHS so that SSE will preserve it.
8712 std::swap(LHS, RHS);
8713 } else if (!DAG.isKnownNeverNaN(LHS))
8714 break;
8715 }
8716 Opcode = X86ISD::FMIN;
8717 break;
8718 case ISD::SETOLE:
8719 // This can be a min if we can prove that at least one of the operands
8720 // is not a nan.
8721 if (!FiniteOnlyFPMath()) {
8722 if (DAG.isKnownNeverNaN(LHS)) {
8723 // Put the potential NaN in the RHS so that SSE will preserve it.
8724 std::swap(LHS, RHS);
8725 } else if (!DAG.isKnownNeverNaN(RHS))
8726 break;
8727 }
8728 Opcode = X86ISD::FMIN;
8729 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008730 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008731 // This can be a min, but if either operand is a NaN we need it to
8732 // preserve the original LHS.
8733 std::swap(LHS, RHS);
8734 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008735 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008736 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008737 Opcode = X86ISD::FMIN;
8738 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008739
Dan Gohman670e5392009-09-21 18:03:22 +00008740 case ISD::SETOGE:
8741 // This can be a max if we can prove that at least one of the operands
8742 // is not a nan.
8743 if (!FiniteOnlyFPMath()) {
8744 if (DAG.isKnownNeverNaN(LHS)) {
8745 // Put the potential NaN in the RHS so that SSE will preserve it.
8746 std::swap(LHS, RHS);
8747 } else if (!DAG.isKnownNeverNaN(RHS))
8748 break;
8749 }
8750 Opcode = X86ISD::FMAX;
8751 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008752 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008753 // This can be a max if we can prove that at least one of the operands
8754 // is not a nan.
8755 if (!FiniteOnlyFPMath()) {
8756 if (DAG.isKnownNeverNaN(RHS)) {
8757 // Put the potential NaN in the RHS so that SSE will preserve it.
8758 std::swap(LHS, RHS);
8759 } else if (!DAG.isKnownNeverNaN(LHS))
8760 break;
8761 }
8762 Opcode = X86ISD::FMAX;
8763 break;
8764 case ISD::SETUGE:
8765 // This can be a max, but if either operand is a NaN we need it to
8766 // preserve the original LHS.
8767 std::swap(LHS, RHS);
8768 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008769 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008770 case ISD::SETGE:
8771 Opcode = X86ISD::FMAX;
8772 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008773 }
Dan Gohman670e5392009-09-21 18:03:22 +00008774 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008775 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8776 switch (CC) {
8777 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008778 case ISD::SETOGE:
8779 // This can be a min if we can prove that at least one of the operands
8780 // is not a nan.
8781 if (!FiniteOnlyFPMath()) {
8782 if (DAG.isKnownNeverNaN(RHS)) {
8783 // Put the potential NaN in the RHS so that SSE will preserve it.
8784 std::swap(LHS, RHS);
8785 } else if (!DAG.isKnownNeverNaN(LHS))
8786 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008787 }
Dan Gohman670e5392009-09-21 18:03:22 +00008788 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008789 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008790 case ISD::SETUGT:
8791 // This can be a min if we can prove that at least one of the operands
8792 // is not a nan.
8793 if (!FiniteOnlyFPMath()) {
8794 if (DAG.isKnownNeverNaN(LHS)) {
8795 // Put the potential NaN in the RHS so that SSE will preserve it.
8796 std::swap(LHS, RHS);
8797 } else if (!DAG.isKnownNeverNaN(RHS))
8798 break;
8799 }
8800 Opcode = X86ISD::FMIN;
8801 break;
8802 case ISD::SETUGE:
8803 // This can be a min, but if either operand is a NaN we need it to
8804 // preserve the original LHS.
8805 std::swap(LHS, RHS);
8806 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008807 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008808 case ISD::SETGE:
8809 Opcode = X86ISD::FMIN;
8810 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008811
Dan Gohman670e5392009-09-21 18:03:22 +00008812 case ISD::SETULT:
8813 // This can be a max if we can prove that at least one of the operands
8814 // is not a nan.
8815 if (!FiniteOnlyFPMath()) {
8816 if (DAG.isKnownNeverNaN(LHS)) {
8817 // Put the potential NaN in the RHS so that SSE will preserve it.
8818 std::swap(LHS, RHS);
8819 } else if (!DAG.isKnownNeverNaN(RHS))
8820 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008821 }
Dan Gohman670e5392009-09-21 18:03:22 +00008822 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008823 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008824 case ISD::SETOLE:
8825 // This can be a max if we can prove that at least one of the operands
8826 // is not a nan.
8827 if (!FiniteOnlyFPMath()) {
8828 if (DAG.isKnownNeverNaN(RHS)) {
8829 // Put the potential NaN in the RHS so that SSE will preserve it.
8830 std::swap(LHS, RHS);
8831 } else if (!DAG.isKnownNeverNaN(LHS))
8832 break;
8833 }
8834 Opcode = X86ISD::FMAX;
8835 break;
8836 case ISD::SETULE:
8837 // This can be a max, but if either operand is a NaN we need it to
8838 // preserve the original LHS.
8839 std::swap(LHS, RHS);
8840 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008841 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008842 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008843 Opcode = X86ISD::FMAX;
8844 break;
8845 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008846 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008847
Chris Lattner47b4ce82009-03-11 05:48:52 +00008848 if (Opcode)
8849 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008850 }
Eric Christopherfd179292009-08-27 18:07:15 +00008851
Chris Lattnerd1980a52009-03-12 06:52:53 +00008852 // If this is a select between two integer constants, try to do some
8853 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008854 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8855 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008856 // Don't do this for crazy integer types.
8857 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8858 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008859 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008860 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008861
Chris Lattnercee56e72009-03-13 05:53:31 +00008862 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008863 // Efficiently invertible.
8864 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8865 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8866 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8867 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008868 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008869 }
Eric Christopherfd179292009-08-27 18:07:15 +00008870
Chris Lattnerd1980a52009-03-12 06:52:53 +00008871 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008872 if (FalseC->getAPIntValue() == 0 &&
8873 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008874 if (NeedsCondInvert) // Invert the condition if needed.
8875 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8876 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008877
Chris Lattnerd1980a52009-03-12 06:52:53 +00008878 // Zero extend the condition if needed.
8879 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008880
Chris Lattnercee56e72009-03-13 05:53:31 +00008881 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008882 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008883 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008884 }
Eric Christopherfd179292009-08-27 18:07:15 +00008885
Chris Lattner97a29a52009-03-13 05:22:11 +00008886 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008887 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008888 if (NeedsCondInvert) // Invert the condition if needed.
8889 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8890 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008891
Chris Lattner97a29a52009-03-13 05:22:11 +00008892 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008893 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8894 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008895 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008896 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008897 }
Eric Christopherfd179292009-08-27 18:07:15 +00008898
Chris Lattnercee56e72009-03-13 05:53:31 +00008899 // Optimize cases that will turn into an LEA instruction. This requires
8900 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008901 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008902 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008903 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008904
Chris Lattnercee56e72009-03-13 05:53:31 +00008905 bool isFastMultiplier = false;
8906 if (Diff < 10) {
8907 switch ((unsigned char)Diff) {
8908 default: break;
8909 case 1: // result = add base, cond
8910 case 2: // result = lea base( , cond*2)
8911 case 3: // result = lea base(cond, cond*2)
8912 case 4: // result = lea base( , cond*4)
8913 case 5: // result = lea base(cond, cond*4)
8914 case 8: // result = lea base( , cond*8)
8915 case 9: // result = lea base(cond, cond*8)
8916 isFastMultiplier = true;
8917 break;
8918 }
8919 }
Eric Christopherfd179292009-08-27 18:07:15 +00008920
Chris Lattnercee56e72009-03-13 05:53:31 +00008921 if (isFastMultiplier) {
8922 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8923 if (NeedsCondInvert) // Invert the condition if needed.
8924 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8925 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008926
Chris Lattnercee56e72009-03-13 05:53:31 +00008927 // Zero extend the condition if needed.
8928 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8929 Cond);
8930 // Scale the condition by the difference.
8931 if (Diff != 1)
8932 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8933 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008934
Chris Lattnercee56e72009-03-13 05:53:31 +00008935 // Add the base if non-zero.
8936 if (FalseC->getAPIntValue() != 0)
8937 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8938 SDValue(FalseC, 0));
8939 return Cond;
8940 }
Eric Christopherfd179292009-08-27 18:07:15 +00008941 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008942 }
8943 }
Eric Christopherfd179292009-08-27 18:07:15 +00008944
Dan Gohman475871a2008-07-27 21:46:04 +00008945 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008946}
8947
Chris Lattnerd1980a52009-03-12 06:52:53 +00008948/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8949static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8950 TargetLowering::DAGCombinerInfo &DCI) {
8951 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008952
Chris Lattnerd1980a52009-03-12 06:52:53 +00008953 // If the flag operand isn't dead, don't touch this CMOV.
8954 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8955 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008956
Chris Lattnerd1980a52009-03-12 06:52:53 +00008957 // If this is a select between two integer constants, try to do some
8958 // optimizations. Note that the operands are ordered the opposite of SELECT
8959 // operands.
8960 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8961 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8962 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8963 // larger than FalseC (the false value).
8964 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008965
Chris Lattnerd1980a52009-03-12 06:52:53 +00008966 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8967 CC = X86::GetOppositeBranchCondition(CC);
8968 std::swap(TrueC, FalseC);
8969 }
Eric Christopherfd179292009-08-27 18:07:15 +00008970
Chris Lattnerd1980a52009-03-12 06:52:53 +00008971 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008972 // This is efficient for any integer data type (including i8/i16) and
8973 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008974 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8975 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008976 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8977 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008978
Chris Lattnerd1980a52009-03-12 06:52:53 +00008979 // Zero extend the condition if needed.
8980 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008981
Chris Lattnerd1980a52009-03-12 06:52:53 +00008982 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8983 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008984 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008985 if (N->getNumValues() == 2) // Dead flag value?
8986 return DCI.CombineTo(N, Cond, SDValue());
8987 return Cond;
8988 }
Eric Christopherfd179292009-08-27 18:07:15 +00008989
Chris Lattnercee56e72009-03-13 05:53:31 +00008990 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8991 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008992 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8993 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008994 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8995 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008996
Chris Lattner97a29a52009-03-13 05:22:11 +00008997 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008998 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8999 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009000 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9001 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009002
Chris Lattner97a29a52009-03-13 05:22:11 +00009003 if (N->getNumValues() == 2) // Dead flag value?
9004 return DCI.CombineTo(N, Cond, SDValue());
9005 return Cond;
9006 }
Eric Christopherfd179292009-08-27 18:07:15 +00009007
Chris Lattnercee56e72009-03-13 05:53:31 +00009008 // Optimize cases that will turn into an LEA instruction. This requires
9009 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009010 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009011 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009013
Chris Lattnercee56e72009-03-13 05:53:31 +00009014 bool isFastMultiplier = false;
9015 if (Diff < 10) {
9016 switch ((unsigned char)Diff) {
9017 default: break;
9018 case 1: // result = add base, cond
9019 case 2: // result = lea base( , cond*2)
9020 case 3: // result = lea base(cond, cond*2)
9021 case 4: // result = lea base( , cond*4)
9022 case 5: // result = lea base(cond, cond*4)
9023 case 8: // result = lea base( , cond*8)
9024 case 9: // result = lea base(cond, cond*8)
9025 isFastMultiplier = true;
9026 break;
9027 }
9028 }
Eric Christopherfd179292009-08-27 18:07:15 +00009029
Chris Lattnercee56e72009-03-13 05:53:31 +00009030 if (isFastMultiplier) {
9031 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9032 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009033 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9034 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009035 // Zero extend the condition if needed.
9036 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9037 Cond);
9038 // Scale the condition by the difference.
9039 if (Diff != 1)
9040 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9041 DAG.getConstant(Diff, Cond.getValueType()));
9042
9043 // Add the base if non-zero.
9044 if (FalseC->getAPIntValue() != 0)
9045 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9046 SDValue(FalseC, 0));
9047 if (N->getNumValues() == 2) // Dead flag value?
9048 return DCI.CombineTo(N, Cond, SDValue());
9049 return Cond;
9050 }
Eric Christopherfd179292009-08-27 18:07:15 +00009051 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009052 }
9053 }
9054 return SDValue();
9055}
9056
9057
Evan Cheng0b0cd912009-03-28 05:57:29 +00009058/// PerformMulCombine - Optimize a single multiply with constant into two
9059/// in order to implement it with two cheaper instructions, e.g.
9060/// LEA + SHL, LEA + LEA.
9061static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9062 TargetLowering::DAGCombinerInfo &DCI) {
9063 if (DAG.getMachineFunction().
9064 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9065 return SDValue();
9066
9067 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9068 return SDValue();
9069
Owen Andersone50ed302009-08-10 22:56:29 +00009070 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009071 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009072 return SDValue();
9073
9074 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9075 if (!C)
9076 return SDValue();
9077 uint64_t MulAmt = C->getZExtValue();
9078 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9079 return SDValue();
9080
9081 uint64_t MulAmt1 = 0;
9082 uint64_t MulAmt2 = 0;
9083 if ((MulAmt % 9) == 0) {
9084 MulAmt1 = 9;
9085 MulAmt2 = MulAmt / 9;
9086 } else if ((MulAmt % 5) == 0) {
9087 MulAmt1 = 5;
9088 MulAmt2 = MulAmt / 5;
9089 } else if ((MulAmt % 3) == 0) {
9090 MulAmt1 = 3;
9091 MulAmt2 = MulAmt / 3;
9092 }
9093 if (MulAmt2 &&
9094 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9095 DebugLoc DL = N->getDebugLoc();
9096
9097 if (isPowerOf2_64(MulAmt2) &&
9098 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9099 // If second multiplifer is pow2, issue it first. We want the multiply by
9100 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9101 // is an add.
9102 std::swap(MulAmt1, MulAmt2);
9103
9104 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009105 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009106 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009107 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009108 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009109 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009110 DAG.getConstant(MulAmt1, VT));
9111
Eric Christopherfd179292009-08-27 18:07:15 +00009112 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009113 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009114 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009115 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009116 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009117 DAG.getConstant(MulAmt2, VT));
9118
9119 // Do not add new nodes to DAG combiner worklist.
9120 DCI.CombineTo(N, NewMul, false);
9121 }
9122 return SDValue();
9123}
9124
Evan Chengad9c0a32009-12-15 00:53:42 +00009125static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9126 SDValue N0 = N->getOperand(0);
9127 SDValue N1 = N->getOperand(1);
9128 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9129 EVT VT = N0.getValueType();
9130
9131 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9132 // since the result of setcc_c is all zero's or all ones.
9133 if (N1C && N0.getOpcode() == ISD::AND &&
9134 N0.getOperand(1).getOpcode() == ISD::Constant) {
9135 SDValue N00 = N0.getOperand(0);
9136 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9137 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9138 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9139 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9140 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9141 APInt ShAmt = N1C->getAPIntValue();
9142 Mask = Mask.shl(ShAmt);
9143 if (Mask != 0)
9144 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9145 N00, DAG.getConstant(Mask, VT));
9146 }
9147 }
9148
9149 return SDValue();
9150}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009151
Nate Begeman740ab032009-01-26 00:52:55 +00009152/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9153/// when possible.
9154static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9155 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009156 EVT VT = N->getValueType(0);
9157 if (!VT.isVector() && VT.isInteger() &&
9158 N->getOpcode() == ISD::SHL)
9159 return PerformSHLCombine(N, DAG);
9160
Nate Begeman740ab032009-01-26 00:52:55 +00009161 // On X86 with SSE2 support, we can transform this to a vector shift if
9162 // all elements are shifted by the same amount. We can't do this in legalize
9163 // because the a constant vector is typically transformed to a constant pool
9164 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009165 if (!Subtarget->hasSSE2())
9166 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009167
Owen Anderson825b72b2009-08-11 20:47:22 +00009168 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009169 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009170
Mon P Wang3becd092009-01-28 08:12:05 +00009171 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009172 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009173 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009174 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009175 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9176 unsigned NumElts = VT.getVectorNumElements();
9177 unsigned i = 0;
9178 for (; i != NumElts; ++i) {
9179 SDValue Arg = ShAmtOp.getOperand(i);
9180 if (Arg.getOpcode() == ISD::UNDEF) continue;
9181 BaseShAmt = Arg;
9182 break;
9183 }
9184 for (; i != NumElts; ++i) {
9185 SDValue Arg = ShAmtOp.getOperand(i);
9186 if (Arg.getOpcode() == ISD::UNDEF) continue;
9187 if (Arg != BaseShAmt) {
9188 return SDValue();
9189 }
9190 }
9191 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009192 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009193 SDValue InVec = ShAmtOp.getOperand(0);
9194 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9195 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9196 unsigned i = 0;
9197 for (; i != NumElts; ++i) {
9198 SDValue Arg = InVec.getOperand(i);
9199 if (Arg.getOpcode() == ISD::UNDEF) continue;
9200 BaseShAmt = Arg;
9201 break;
9202 }
9203 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9204 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9205 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9206 if (C->getZExtValue() == SplatIdx)
9207 BaseShAmt = InVec.getOperand(1);
9208 }
9209 }
9210 if (BaseShAmt.getNode() == 0)
9211 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9212 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009213 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009214 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009215
Mon P Wangefa42202009-09-03 19:56:25 +00009216 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009217 if (EltVT.bitsGT(MVT::i32))
9218 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9219 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009220 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009221
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009222 // The shift amount is identical so we can do a vector shift.
9223 SDValue ValOp = N->getOperand(0);
9224 switch (N->getOpcode()) {
9225 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009226 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009227 break;
9228 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009229 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009230 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009231 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009232 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009233 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009234 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009235 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009236 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009237 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009238 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009239 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009240 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009241 break;
9242 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009243 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009244 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009245 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009246 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009247 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009248 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009249 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009250 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009251 break;
9252 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009253 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009254 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009256 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009257 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009258 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009259 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009260 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009261 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009262 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009263 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009264 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009265 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009266 }
9267 return SDValue();
9268}
9269
Evan Cheng760d1942010-01-04 21:22:48 +00009270static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9271 const X86Subtarget *Subtarget) {
9272 EVT VT = N->getValueType(0);
9273 if (VT != MVT::i64 || !Subtarget->is64Bit())
9274 return SDValue();
9275
9276 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9277 SDValue N0 = N->getOperand(0);
9278 SDValue N1 = N->getOperand(1);
9279 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9280 std::swap(N0, N1);
9281 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9282 return SDValue();
9283
9284 SDValue ShAmt0 = N0.getOperand(1);
9285 if (ShAmt0.getValueType() != MVT::i8)
9286 return SDValue();
9287 SDValue ShAmt1 = N1.getOperand(1);
9288 if (ShAmt1.getValueType() != MVT::i8)
9289 return SDValue();
9290 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9291 ShAmt0 = ShAmt0.getOperand(0);
9292 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9293 ShAmt1 = ShAmt1.getOperand(0);
9294
9295 DebugLoc DL = N->getDebugLoc();
9296 unsigned Opc = X86ISD::SHLD;
9297 SDValue Op0 = N0.getOperand(0);
9298 SDValue Op1 = N1.getOperand(0);
9299 if (ShAmt0.getOpcode() == ISD::SUB) {
9300 Opc = X86ISD::SHRD;
9301 std::swap(Op0, Op1);
9302 std::swap(ShAmt0, ShAmt1);
9303 }
9304
9305 if (ShAmt1.getOpcode() == ISD::SUB) {
9306 SDValue Sum = ShAmt1.getOperand(0);
9307 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9308 if (SumC->getSExtValue() == 64 &&
9309 ShAmt1.getOperand(1) == ShAmt0)
9310 return DAG.getNode(Opc, DL, VT,
9311 Op0, Op1,
9312 DAG.getNode(ISD::TRUNCATE, DL,
9313 MVT::i8, ShAmt0));
9314 }
9315 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9316 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9317 if (ShAmt0C &&
9318 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9319 return DAG.getNode(Opc, DL, VT,
9320 N0.getOperand(0), N1.getOperand(0),
9321 DAG.getNode(ISD::TRUNCATE, DL,
9322 MVT::i8, ShAmt0));
9323 }
9324
9325 return SDValue();
9326}
9327
Chris Lattner149a4e52008-02-22 02:09:43 +00009328/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009329static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009330 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009331 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9332 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009333 // A preferable solution to the general problem is to figure out the right
9334 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009335
9336 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009337 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009338 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009339 if (VT.getSizeInBits() != 64)
9340 return SDValue();
9341
Devang Patel578efa92009-06-05 21:57:13 +00009342 const Function *F = DAG.getMachineFunction().getFunction();
9343 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009344 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009345 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009346 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009348 isa<LoadSDNode>(St->getValue()) &&
9349 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9350 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009351 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009352 LoadSDNode *Ld = 0;
9353 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009354 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009355 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009356 // Must be a store of a load. We currently handle two cases: the load
9357 // is a direct child, and it's under an intervening TokenFactor. It is
9358 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009359 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009360 Ld = cast<LoadSDNode>(St->getChain());
9361 else if (St->getValue().hasOneUse() &&
9362 ChainVal->getOpcode() == ISD::TokenFactor) {
9363 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009364 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009365 TokenFactorIndex = i;
9366 Ld = cast<LoadSDNode>(St->getValue());
9367 } else
9368 Ops.push_back(ChainVal->getOperand(i));
9369 }
9370 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009371
Evan Cheng536e6672009-03-12 05:59:15 +00009372 if (!Ld || !ISD::isNormalLoad(Ld))
9373 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009374
Evan Cheng536e6672009-03-12 05:59:15 +00009375 // If this is not the MMX case, i.e. we are just turning i64 load/store
9376 // into f64 load/store, avoid the transformation if there are multiple
9377 // uses of the loaded value.
9378 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9379 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009380
Evan Cheng536e6672009-03-12 05:59:15 +00009381 DebugLoc LdDL = Ld->getDebugLoc();
9382 DebugLoc StDL = N->getDebugLoc();
9383 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9384 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9385 // pair instead.
9386 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009387 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009388 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9389 Ld->getBasePtr(), Ld->getSrcValue(),
9390 Ld->getSrcValueOffset(), Ld->isVolatile(),
9391 Ld->getAlignment());
9392 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009393 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009394 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009395 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009396 Ops.size());
9397 }
Evan Cheng536e6672009-03-12 05:59:15 +00009398 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009399 St->getSrcValue(), St->getSrcValueOffset(),
9400 St->isVolatile(), St->getAlignment());
9401 }
Evan Cheng536e6672009-03-12 05:59:15 +00009402
9403 // Otherwise, lower to two pairs of 32-bit loads / stores.
9404 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009405 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9406 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009407
Owen Anderson825b72b2009-08-11 20:47:22 +00009408 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009409 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9410 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009411 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009412 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9413 Ld->isVolatile(),
9414 MinAlign(Ld->getAlignment(), 4));
9415
9416 SDValue NewChain = LoLd.getValue(1);
9417 if (TokenFactorIndex != -1) {
9418 Ops.push_back(LoLd);
9419 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009421 Ops.size());
9422 }
9423
9424 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009425 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9426 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009427
9428 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9429 St->getSrcValue(), St->getSrcValueOffset(),
9430 St->isVolatile(), St->getAlignment());
9431 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9432 St->getSrcValue(),
9433 St->getSrcValueOffset() + 4,
9434 St->isVolatile(),
9435 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009436 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009437 }
Dan Gohman475871a2008-07-27 21:46:04 +00009438 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009439}
9440
Chris Lattner6cf73262008-01-25 06:14:17 +00009441/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9442/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009443static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009444 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9445 // F[X]OR(0.0, x) -> x
9446 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009447 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9448 if (C->getValueAPF().isPosZero())
9449 return N->getOperand(1);
9450 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9451 if (C->getValueAPF().isPosZero())
9452 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009453 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009454}
9455
9456/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009457static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009458 // FAND(0.0, x) -> 0.0
9459 // FAND(x, 0.0) -> 0.0
9460 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9461 if (C->getValueAPF().isPosZero())
9462 return N->getOperand(0);
9463 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9464 if (C->getValueAPF().isPosZero())
9465 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009466 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009467}
9468
Dan Gohmane5af2d32009-01-29 01:59:02 +00009469static SDValue PerformBTCombine(SDNode *N,
9470 SelectionDAG &DAG,
9471 TargetLowering::DAGCombinerInfo &DCI) {
9472 // BT ignores high bits in the bit index operand.
9473 SDValue Op1 = N->getOperand(1);
9474 if (Op1.hasOneUse()) {
9475 unsigned BitWidth = Op1.getValueSizeInBits();
9476 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9477 APInt KnownZero, KnownOne;
9478 TargetLowering::TargetLoweringOpt TLO(DAG);
9479 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9480 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9481 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9482 DCI.CommitTargetLoweringOpt(TLO);
9483 }
9484 return SDValue();
9485}
Chris Lattner83e6c992006-10-04 06:57:07 +00009486
Eli Friedman7a5e5552009-06-07 06:52:44 +00009487static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9488 SDValue Op = N->getOperand(0);
9489 if (Op.getOpcode() == ISD::BIT_CONVERT)
9490 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009491 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009492 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009493 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009494 OpVT.getVectorElementType().getSizeInBits()) {
9495 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9496 }
9497 return SDValue();
9498}
9499
Owen Anderson99177002009-06-29 18:04:45 +00009500// On X86 and X86-64, atomic operations are lowered to locked instructions.
9501// Locked instructions, in turn, have implicit fence semantics (all memory
9502// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009503// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009504// fence-atomic-fence.
9505static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9506 SDValue atomic = N->getOperand(0);
9507 switch (atomic.getOpcode()) {
9508 case ISD::ATOMIC_CMP_SWAP:
9509 case ISD::ATOMIC_SWAP:
9510 case ISD::ATOMIC_LOAD_ADD:
9511 case ISD::ATOMIC_LOAD_SUB:
9512 case ISD::ATOMIC_LOAD_AND:
9513 case ISD::ATOMIC_LOAD_OR:
9514 case ISD::ATOMIC_LOAD_XOR:
9515 case ISD::ATOMIC_LOAD_NAND:
9516 case ISD::ATOMIC_LOAD_MIN:
9517 case ISD::ATOMIC_LOAD_MAX:
9518 case ISD::ATOMIC_LOAD_UMIN:
9519 case ISD::ATOMIC_LOAD_UMAX:
9520 break;
9521 default:
9522 return SDValue();
9523 }
Eric Christopherfd179292009-08-27 18:07:15 +00009524
Owen Anderson99177002009-06-29 18:04:45 +00009525 SDValue fence = atomic.getOperand(0);
9526 if (fence.getOpcode() != ISD::MEMBARRIER)
9527 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009528
Owen Anderson99177002009-06-29 18:04:45 +00009529 switch (atomic.getOpcode()) {
9530 case ISD::ATOMIC_CMP_SWAP:
9531 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9532 atomic.getOperand(1), atomic.getOperand(2),
9533 atomic.getOperand(3));
9534 case ISD::ATOMIC_SWAP:
9535 case ISD::ATOMIC_LOAD_ADD:
9536 case ISD::ATOMIC_LOAD_SUB:
9537 case ISD::ATOMIC_LOAD_AND:
9538 case ISD::ATOMIC_LOAD_OR:
9539 case ISD::ATOMIC_LOAD_XOR:
9540 case ISD::ATOMIC_LOAD_NAND:
9541 case ISD::ATOMIC_LOAD_MIN:
9542 case ISD::ATOMIC_LOAD_MAX:
9543 case ISD::ATOMIC_LOAD_UMIN:
9544 case ISD::ATOMIC_LOAD_UMAX:
9545 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9546 atomic.getOperand(1), atomic.getOperand(2));
9547 default:
9548 return SDValue();
9549 }
9550}
9551
Evan Cheng2e489c42009-12-16 00:53:11 +00009552static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9553 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9554 // (and (i32 x86isd::setcc_carry), 1)
9555 // This eliminates the zext. This transformation is necessary because
9556 // ISD::SETCC is always legalized to i8.
9557 DebugLoc dl = N->getDebugLoc();
9558 SDValue N0 = N->getOperand(0);
9559 EVT VT = N->getValueType(0);
9560 if (N0.getOpcode() == ISD::AND &&
9561 N0.hasOneUse() &&
9562 N0.getOperand(0).hasOneUse()) {
9563 SDValue N00 = N0.getOperand(0);
9564 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9565 return SDValue();
9566 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9567 if (!C || C->getZExtValue() != 1)
9568 return SDValue();
9569 return DAG.getNode(ISD::AND, dl, VT,
9570 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9571 N00.getOperand(0), N00.getOperand(1)),
9572 DAG.getConstant(1, VT));
9573 }
9574
9575 return SDValue();
9576}
9577
Dan Gohman475871a2008-07-27 21:46:04 +00009578SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009579 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009580 SelectionDAG &DAG = DCI.DAG;
9581 switch (N->getOpcode()) {
9582 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009583 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009584 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009585 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009586 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009587 case ISD::SHL:
9588 case ISD::SRA:
9589 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009590 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009591 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009592 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009593 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9594 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009595 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009596 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009597 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009598 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009599 }
9600
Dan Gohman475871a2008-07-27 21:46:04 +00009601 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009602}
9603
Evan Cheng60c07e12006-07-05 22:17:51 +00009604//===----------------------------------------------------------------------===//
9605// X86 Inline Assembly Support
9606//===----------------------------------------------------------------------===//
9607
Chris Lattnerb8105652009-07-20 17:51:36 +00009608static bool LowerToBSwap(CallInst *CI) {
9609 // FIXME: this should verify that we are targetting a 486 or better. If not,
9610 // we will turn this bswap into something that will be lowered to logical ops
9611 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9612 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009613
Chris Lattnerb8105652009-07-20 17:51:36 +00009614 // Verify this is a simple bswap.
9615 if (CI->getNumOperands() != 2 ||
9616 CI->getType() != CI->getOperand(1)->getType() ||
9617 !CI->getType()->isInteger())
9618 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009619
Chris Lattnerb8105652009-07-20 17:51:36 +00009620 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9621 if (!Ty || Ty->getBitWidth() % 16 != 0)
9622 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009623
Chris Lattnerb8105652009-07-20 17:51:36 +00009624 // Okay, we can do this xform, do so now.
9625 const Type *Tys[] = { Ty };
9626 Module *M = CI->getParent()->getParent()->getParent();
9627 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009628
Chris Lattnerb8105652009-07-20 17:51:36 +00009629 Value *Op = CI->getOperand(1);
9630 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009631
Chris Lattnerb8105652009-07-20 17:51:36 +00009632 CI->replaceAllUsesWith(Op);
9633 CI->eraseFromParent();
9634 return true;
9635}
9636
9637bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9638 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9639 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9640
9641 std::string AsmStr = IA->getAsmString();
9642
9643 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009644 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009645 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9646
9647 switch (AsmPieces.size()) {
9648 default: return false;
9649 case 1:
9650 AsmStr = AsmPieces[0];
9651 AsmPieces.clear();
9652 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9653
9654 // bswap $0
9655 if (AsmPieces.size() == 2 &&
9656 (AsmPieces[0] == "bswap" ||
9657 AsmPieces[0] == "bswapq" ||
9658 AsmPieces[0] == "bswapl") &&
9659 (AsmPieces[1] == "$0" ||
9660 AsmPieces[1] == "${0:q}")) {
9661 // No need to check constraints, nothing other than the equivalent of
9662 // "=r,0" would be valid here.
9663 return LowerToBSwap(CI);
9664 }
9665 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009666 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009667 AsmPieces.size() == 3 &&
9668 AsmPieces[0] == "rorw" &&
9669 AsmPieces[1] == "$$8," &&
9670 AsmPieces[2] == "${0:w}" &&
9671 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9672 return LowerToBSwap(CI);
9673 }
9674 break;
9675 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009676 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009677 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009678 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9679 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9680 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009681 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009682 SplitString(AsmPieces[0], Words, " \t");
9683 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9684 Words.clear();
9685 SplitString(AsmPieces[1], Words, " \t");
9686 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9687 Words.clear();
9688 SplitString(AsmPieces[2], Words, " \t,");
9689 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9690 Words[2] == "%edx") {
9691 return LowerToBSwap(CI);
9692 }
9693 }
9694 }
9695 }
9696 break;
9697 }
9698 return false;
9699}
9700
9701
9702
Chris Lattnerf4dff842006-07-11 02:54:03 +00009703/// getConstraintType - Given a constraint letter, return the type of
9704/// constraint it is for this target.
9705X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009706X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9707 if (Constraint.size() == 1) {
9708 switch (Constraint[0]) {
9709 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009710 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009711 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009712 case 'r':
9713 case 'R':
9714 case 'l':
9715 case 'q':
9716 case 'Q':
9717 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009718 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009719 case 'Y':
9720 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009721 case 'e':
9722 case 'Z':
9723 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009724 default:
9725 break;
9726 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009727 }
Chris Lattner4234f572007-03-25 02:14:49 +00009728 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009729}
9730
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009731/// LowerXConstraint - try to replace an X constraint, which matches anything,
9732/// with another that has more specific requirements based on the type of the
9733/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009734const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009735LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009736 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9737 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009738 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009739 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009740 return "Y";
9741 if (Subtarget->hasSSE1())
9742 return "x";
9743 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009744
Chris Lattner5e764232008-04-26 23:02:14 +00009745 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009746}
9747
Chris Lattner48884cd2007-08-25 00:47:38 +00009748/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9749/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009750void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009751 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009752 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009753 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009754 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009755 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009756
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009757 switch (Constraint) {
9758 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009759 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009760 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009761 if (C->getZExtValue() <= 31) {
9762 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009763 break;
9764 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009765 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009766 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009767 case 'J':
9768 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009769 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009770 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9771 break;
9772 }
9773 }
9774 return;
9775 case 'K':
9776 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009777 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009778 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9779 break;
9780 }
9781 }
9782 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009783 case 'N':
9784 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009785 if (C->getZExtValue() <= 255) {
9786 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009787 break;
9788 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009789 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009790 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009791 case 'e': {
9792 // 32-bit signed value
9793 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9794 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009795 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9796 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009797 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009798 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009799 break;
9800 }
9801 // FIXME gcc accepts some relocatable values here too, but only in certain
9802 // memory models; it's complicated.
9803 }
9804 return;
9805 }
9806 case 'Z': {
9807 // 32-bit unsigned value
9808 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9809 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009810 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9811 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009812 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9813 break;
9814 }
9815 }
9816 // FIXME gcc accepts some relocatable values here too, but only in certain
9817 // memory models; it's complicated.
9818 return;
9819 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009820 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009821 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009822 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009823 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009824 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009825 break;
9826 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009827
Chris Lattnerdc43a882007-05-03 16:52:29 +00009828 // If we are in non-pic codegen mode, we allow the address of a global (with
9829 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009830 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009831 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009832
Chris Lattner49921962009-05-08 18:23:14 +00009833 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9834 while (1) {
9835 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9836 Offset += GA->getOffset();
9837 break;
9838 } else if (Op.getOpcode() == ISD::ADD) {
9839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9840 Offset += C->getZExtValue();
9841 Op = Op.getOperand(0);
9842 continue;
9843 }
9844 } else if (Op.getOpcode() == ISD::SUB) {
9845 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9846 Offset += -C->getZExtValue();
9847 Op = Op.getOperand(0);
9848 continue;
9849 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009850 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009851
Chris Lattner49921962009-05-08 18:23:14 +00009852 // Otherwise, this isn't something we can handle, reject it.
9853 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009854 }
Eric Christopherfd179292009-08-27 18:07:15 +00009855
Chris Lattner36c25012009-07-10 07:34:39 +00009856 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009857 // If we require an extra load to get this address, as in PIC mode, we
9858 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009859 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9860 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009861 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009862
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009863 if (hasMemory)
9864 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9865 else
9866 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009867 Result = Op;
9868 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009869 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009870 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009871
Gabor Greifba36cb52008-08-28 21:40:38 +00009872 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009873 Ops.push_back(Result);
9874 return;
9875 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009876 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9877 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009878}
9879
Chris Lattner259e97c2006-01-31 19:43:35 +00009880std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009881getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009882 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009883 if (Constraint.size() == 1) {
9884 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009885 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009886 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009887 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9888 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009889 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009890 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9891 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9892 X86::R10D,X86::R11D,X86::R12D,
9893 X86::R13D,X86::R14D,X86::R15D,
9894 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009895 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009896 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9897 X86::SI, X86::DI, X86::R8W,X86::R9W,
9898 X86::R10W,X86::R11W,X86::R12W,
9899 X86::R13W,X86::R14W,X86::R15W,
9900 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009901 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009902 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9903 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9904 X86::R10B,X86::R11B,X86::R12B,
9905 X86::R13B,X86::R14B,X86::R15B,
9906 X86::BPL, X86::SPL, 0);
9907
Owen Anderson825b72b2009-08-11 20:47:22 +00009908 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009909 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9910 X86::RSI, X86::RDI, X86::R8, X86::R9,
9911 X86::R10, X86::R11, X86::R12,
9912 X86::R13, X86::R14, X86::R15,
9913 X86::RBP, X86::RSP, 0);
9914
9915 break;
9916 }
Eric Christopherfd179292009-08-27 18:07:15 +00009917 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009918 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009919 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009920 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009921 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009922 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009923 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009924 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009925 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009926 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9927 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009928 }
9929 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009930
Chris Lattner1efa40f2006-02-22 00:56:39 +00009931 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009932}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009933
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009934std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009935X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009936 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009937 // First, see if this is a constraint that directly corresponds to an LLVM
9938 // register class.
9939 if (Constraint.size() == 1) {
9940 // GCC Constraint Letters
9941 switch (Constraint[0]) {
9942 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009943 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009944 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009945 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009946 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009947 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009948 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009949 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009950 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009951 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009952 case 'R': // LEGACY_REGS
9953 if (VT == MVT::i8)
9954 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9955 if (VT == MVT::i16)
9956 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9957 if (VT == MVT::i32 || !Subtarget->is64Bit())
9958 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9959 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009960 case 'f': // FP Stack registers.
9961 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9962 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009963 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009964 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009965 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009966 return std::make_pair(0U, X86::RFP64RegisterClass);
9967 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009968 case 'y': // MMX_REGS if MMX allowed.
9969 if (!Subtarget->hasMMX()) break;
9970 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009971 case 'Y': // SSE_REGS if SSE2 allowed
9972 if (!Subtarget->hasSSE2()) break;
9973 // FALL THROUGH.
9974 case 'x': // SSE_REGS if SSE1 allowed
9975 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009976
Owen Anderson825b72b2009-08-11 20:47:22 +00009977 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009978 default: break;
9979 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009980 case MVT::f32:
9981 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009982 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009983 case MVT::f64:
9984 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009985 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009986 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009987 case MVT::v16i8:
9988 case MVT::v8i16:
9989 case MVT::v4i32:
9990 case MVT::v2i64:
9991 case MVT::v4f32:
9992 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009993 return std::make_pair(0U, X86::VR128RegisterClass);
9994 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009995 break;
9996 }
9997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009998
Chris Lattnerf76d1802006-07-31 23:26:50 +00009999 // Use the default implementation in TargetLowering to convert the register
10000 // constraint into a member of a register class.
10001 std::pair<unsigned, const TargetRegisterClass*> Res;
10002 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010003
10004 // Not found as a standard register?
10005 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010006 // Map st(0) -> st(7) -> ST0
10007 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10008 tolower(Constraint[1]) == 's' &&
10009 tolower(Constraint[2]) == 't' &&
10010 Constraint[3] == '(' &&
10011 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10012 Constraint[5] == ')' &&
10013 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010014
Chris Lattner56d77c72009-09-13 22:41:48 +000010015 Res.first = X86::ST0+Constraint[4]-'0';
10016 Res.second = X86::RFP80RegisterClass;
10017 return Res;
10018 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010019
Chris Lattner56d77c72009-09-13 22:41:48 +000010020 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010021 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010022 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010023 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010024 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010025 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010026
10027 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010028 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010029 Res.first = X86::EFLAGS;
10030 Res.second = X86::CCRRegisterClass;
10031 return Res;
10032 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010033
Dale Johannesen330169f2008-11-13 21:52:36 +000010034 // 'A' means EAX + EDX.
10035 if (Constraint == "A") {
10036 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010037 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010038 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010039 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010040 return Res;
10041 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010042
Chris Lattnerf76d1802006-07-31 23:26:50 +000010043 // Otherwise, check to see if this is a register class of the wrong value
10044 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10045 // turn into {ax},{dx}.
10046 if (Res.second->hasType(VT))
10047 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010048
Chris Lattnerf76d1802006-07-31 23:26:50 +000010049 // All of the single-register GCC register classes map their values onto
10050 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10051 // really want an 8-bit or 32-bit register, map to the appropriate register
10052 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010053 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010054 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010055 unsigned DestReg = 0;
10056 switch (Res.first) {
10057 default: break;
10058 case X86::AX: DestReg = X86::AL; break;
10059 case X86::DX: DestReg = X86::DL; break;
10060 case X86::CX: DestReg = X86::CL; break;
10061 case X86::BX: DestReg = X86::BL; break;
10062 }
10063 if (DestReg) {
10064 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010065 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010066 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010067 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010068 unsigned DestReg = 0;
10069 switch (Res.first) {
10070 default: break;
10071 case X86::AX: DestReg = X86::EAX; break;
10072 case X86::DX: DestReg = X86::EDX; break;
10073 case X86::CX: DestReg = X86::ECX; break;
10074 case X86::BX: DestReg = X86::EBX; break;
10075 case X86::SI: DestReg = X86::ESI; break;
10076 case X86::DI: DestReg = X86::EDI; break;
10077 case X86::BP: DestReg = X86::EBP; break;
10078 case X86::SP: DestReg = X86::ESP; break;
10079 }
10080 if (DestReg) {
10081 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010082 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010083 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010084 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010085 unsigned DestReg = 0;
10086 switch (Res.first) {
10087 default: break;
10088 case X86::AX: DestReg = X86::RAX; break;
10089 case X86::DX: DestReg = X86::RDX; break;
10090 case X86::CX: DestReg = X86::RCX; break;
10091 case X86::BX: DestReg = X86::RBX; break;
10092 case X86::SI: DestReg = X86::RSI; break;
10093 case X86::DI: DestReg = X86::RDI; break;
10094 case X86::BP: DestReg = X86::RBP; break;
10095 case X86::SP: DestReg = X86::RSP; break;
10096 }
10097 if (DestReg) {
10098 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010099 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010100 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010101 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010102 } else if (Res.second == X86::FR32RegisterClass ||
10103 Res.second == X86::FR64RegisterClass ||
10104 Res.second == X86::VR128RegisterClass) {
10105 // Handle references to XMM physical registers that got mapped into the
10106 // wrong class. This can happen with constraints like {xmm0} where the
10107 // target independent register mapper will just pick the first match it can
10108 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010109 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010110 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010111 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010112 Res.second = X86::FR64RegisterClass;
10113 else if (X86::VR128RegisterClass->hasType(VT))
10114 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010115 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010116
Chris Lattnerf76d1802006-07-31 23:26:50 +000010117 return Res;
10118}
Mon P Wang0c397192008-10-30 08:01:45 +000010119
10120//===----------------------------------------------------------------------===//
10121// X86 Widen vector type
10122//===----------------------------------------------------------------------===//
10123
10124/// getWidenVectorType: given a vector type, returns the type to widen
10125/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010126/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010127/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010128/// scalarizing vs using the wider vector type.
10129
Owen Andersone50ed302009-08-10 22:56:29 +000010130EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010131 assert(VT.isVector());
10132 if (isTypeLegal(VT))
10133 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010134
Mon P Wang0c397192008-10-30 08:01:45 +000010135 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10136 // type based on element type. This would speed up our search (though
10137 // it may not be worth it since the size of the list is relatively
10138 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010139 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010140 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010141
Mon P Wang0c397192008-10-30 08:01:45 +000010142 // On X86, it make sense to widen any vector wider than 1
10143 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010144 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010145
Owen Anderson825b72b2009-08-11 20:47:22 +000010146 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10147 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10148 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010149
10150 if (isTypeLegal(SVT) &&
10151 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010152 SVT.getVectorNumElements() > NElts)
10153 return SVT;
10154 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010155 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010156}