blob: 95df0defcb519f9e9253a86f5f0ebb5ce431d7b5 [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 Lattnerc64daab2010-01-26 05:02:42 +000036#include "llvm/MC/MCContext.h"
37#include "llvm/MC/MCExpr.h"
38#include "llvm/MC/MCSymbol.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039#include "llvm/Target/TargetOptions.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 Lattnerc64daab2010-01-26 05:02:42 +00001111const MCExpr *
1112X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1113 const MachineBasicBlock *MBB,
1114 unsigned uid,MCContext &Ctx) const{
1115 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1116 Subtarget->isPICStyleGOT());
1117 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1118 // entries.
1119
1120 // FIXME: @GOTOFF should be a property of MCSymbolRefExpr not in the MCSymbol.
1121 std::string Name = MBB->getSymbol(Ctx)->getName() + "@GOTOFF";
1122 return MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(StringRef(Name)), Ctx);
1123}
1124
Evan Chengcc415862007-11-09 01:32:10 +00001125/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1126/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001127SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001128 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001129 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001130 // This doesn't have DebugLoc associated with it, but is not really the
1131 // same as a Register.
1132 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1133 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001134 return Table;
1135}
1136
Bill Wendlingb4202b82009-07-01 18:50:55 +00001137/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001138unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001139 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001140}
1141
Chris Lattner2b02a442007-02-25 08:29:00 +00001142//===----------------------------------------------------------------------===//
1143// Return Value Calling Convention Implementation
1144//===----------------------------------------------------------------------===//
1145
Chris Lattner59ed56b2007-02-28 04:55:35 +00001146#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001147
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001148bool
1149X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1150 const SmallVectorImpl<EVT> &OutTys,
1151 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1152 SelectionDAG &DAG) {
1153 SmallVector<CCValAssign, 16> RVLocs;
1154 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1155 RVLocs, *DAG.getContext());
1156 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1157}
1158
Dan Gohman98ca4f22009-08-05 01:29:28 +00001159SDValue
1160X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001161 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001162 const SmallVectorImpl<ISD::OutputArg> &Outs,
1163 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001164
Chris Lattner9774c912007-02-27 05:28:59 +00001165 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001166 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1167 RVLocs, *DAG.getContext());
1168 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001169
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001170 // If this is the first return lowered for this function, add the regs to the
1171 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001172 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001173 for (unsigned i = 0; i != RVLocs.size(); ++i)
1174 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001175 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001176 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001177
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001179
Dan Gohman475871a2008-07-27 21:46:04 +00001180 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001181 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1182 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001183 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001184
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001185 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001186 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1187 CCValAssign &VA = RVLocs[i];
1188 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001189 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001190
Chris Lattner447ff682008-03-11 03:23:40 +00001191 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1192 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001193 if (VA.getLocReg() == X86::ST0 ||
1194 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001195 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1196 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001197 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001198 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001199 RetOps.push_back(ValToCopy);
1200 // Don't emit a copytoreg.
1201 continue;
1202 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001203
Evan Cheng242b38b2009-02-23 09:03:22 +00001204 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1205 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001206 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001207 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001208 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001209 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001210 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001211 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001212 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001213 }
1214
Dale Johannesendd64c412009-02-04 00:33:20 +00001215 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001216 Flag = Chain.getValue(1);
1217 }
Dan Gohman61a92132008-04-21 23:59:07 +00001218
1219 // The x86-64 ABI for returning structs by value requires that we copy
1220 // the sret argument into %rax for the return. We saved the argument into
1221 // a virtual register in the entry block, so now we copy the value out
1222 // and into %rax.
1223 if (Subtarget->is64Bit() &&
1224 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1225 MachineFunction &MF = DAG.getMachineFunction();
1226 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1227 unsigned Reg = FuncInfo->getSRetReturnReg();
1228 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001230 FuncInfo->setSRetReturnReg(Reg);
1231 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001232 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001233
Dale Johannesendd64c412009-02-04 00:33:20 +00001234 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001235 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001236
1237 // RAX now acts like a return value.
1238 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001239 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001240
Chris Lattner447ff682008-03-11 03:23:40 +00001241 RetOps[0] = Chain; // Update chain.
1242
1243 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001245 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001246
1247 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001249}
1250
Dan Gohman98ca4f22009-08-05 01:29:28 +00001251/// LowerCallResult - Lower the result values of a call into the
1252/// appropriate copies out of appropriate physical registers.
1253///
1254SDValue
1255X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001256 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001257 const SmallVectorImpl<ISD::InputArg> &Ins,
1258 DebugLoc dl, SelectionDAG &DAG,
1259 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001260
Chris Lattnere32bbf62007-02-28 07:09:55 +00001261 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001262 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001263 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001264 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001265 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001266 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001267
Chris Lattner3085e152007-02-25 08:59:22 +00001268 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001269 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001270 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001271 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001272
Torok Edwin3f142c32009-02-01 18:15:56 +00001273 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001274 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001275 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001276 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001277 }
1278
Chris Lattner8e6da152008-03-10 21:08:41 +00001279 // If this is a call to a function that returns an fp value on the floating
1280 // point stack, but where we prefer to use the value in xmm registers, copy
1281 // 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 +00001282 if ((VA.getLocReg() == X86::ST0 ||
1283 VA.getLocReg() == X86::ST1) &&
1284 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001285 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001286 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001287
Evan Cheng79fb3b42009-02-20 20:43:02 +00001288 SDValue Val;
1289 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001290 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1291 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1292 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001294 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001295 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1296 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001297 } else {
1298 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001300 Val = Chain.getValue(0);
1301 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001302 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1303 } else {
1304 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1305 CopyVT, InFlag).getValue(1);
1306 Val = Chain.getValue(0);
1307 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001308 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001309
Dan Gohman37eed792009-02-04 17:28:58 +00001310 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001311 // Round the F80 the right size, which also moves to the appropriate xmm
1312 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001313 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001314 // This truncation won't change the value.
1315 DAG.getIntPtrConstant(1));
1316 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001317
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001319 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001320
Dan Gohman98ca4f22009-08-05 01:29:28 +00001321 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001322}
1323
1324
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001325//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001326// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001327//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001328// StdCall calling convention seems to be standard for many Windows' API
1329// routines and around. It differs from C calling convention just a little:
1330// callee should clean up the stack, not caller. Symbols should be also
1331// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001332// For info on fast calling convention see Fast Calling Convention (tail call)
1333// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001334
Dan Gohman98ca4f22009-08-05 01:29:28 +00001335/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001336/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1338 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001339 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001340
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001342}
1343
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001344/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001345/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346static bool
1347ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1348 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001349 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001350
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001352}
1353
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001354/// IsCalleePop - Determines whether the callee is required to pop its
1355/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001356bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001357 if (IsVarArg)
1358 return false;
1359
Dan Gohman095cc292008-09-13 01:54:27 +00001360 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001361 default:
1362 return false;
1363 case CallingConv::X86_StdCall:
1364 return !Subtarget->is64Bit();
1365 case CallingConv::X86_FastCall:
1366 return !Subtarget->is64Bit();
1367 case CallingConv::Fast:
1368 return PerformTailCallOpt;
1369 }
1370}
1371
Dan Gohman095cc292008-09-13 01:54:27 +00001372/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1373/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001374CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001375 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001376 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001377 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001378 else
1379 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001380 }
1381
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 if (CC == CallingConv::X86_FastCall)
1383 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001384 else if (CC == CallingConv::Fast)
1385 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001386 else
1387 return CC_X86_32_C;
1388}
1389
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390/// NameDecorationForCallConv - Selects the appropriate decoration to
1391/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001392NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001393X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001395 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001397 return StdCall;
1398 return None;
1399}
1400
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001401
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001402/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1403/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001404/// the specific parameter attribute. The copy will be passed as a byval
1405/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001406static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001407CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001408 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1409 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001410 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001411 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001412 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001413}
1414
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415SDValue
1416X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001417 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001418 const SmallVectorImpl<ISD::InputArg> &Ins,
1419 DebugLoc dl, SelectionDAG &DAG,
1420 const CCValAssign &VA,
1421 MachineFrameInfo *MFI,
1422 unsigned i) {
1423
Rafael Espindola7effac52007-09-14 15:48:13 +00001424 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001425 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1426 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001427 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001428 EVT ValVT;
1429
1430 // If value is passed by pointer we have address passed instead of the value
1431 // itself.
1432 if (VA.getLocInfo() == CCValAssign::Indirect)
1433 ValVT = VA.getLocVT();
1434 else
1435 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001436
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001437 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001438 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001439 // In case of tail call optimization mark all arguments mutable. Since they
1440 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001441 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001442 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001444 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001445 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001446 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001447 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001448}
1449
Dan Gohman475871a2008-07-27 21:46:04 +00001450SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001451X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001452 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001453 bool isVarArg,
1454 const SmallVectorImpl<ISD::InputArg> &Ins,
1455 DebugLoc dl,
1456 SelectionDAG &DAG,
1457 SmallVectorImpl<SDValue> &InVals) {
1458
Evan Cheng1bc78042006-04-26 01:20:17 +00001459 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001461
Gordon Henriksen86737662008-01-05 16:56:59 +00001462 const Function* Fn = MF.getFunction();
1463 if (Fn->hasExternalLinkage() &&
1464 Subtarget->isTargetCygMing() &&
1465 Fn->getName() == "main")
1466 FuncInfo->setForceFramePointer(true);
1467
1468 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001470
Evan Cheng1bc78042006-04-26 01:20:17 +00001471 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001472 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001473 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001474
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001476 "Var args not supported with calling convention fastcc");
1477
Chris Lattner638402b2007-02-28 07:00:42 +00001478 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001479 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1481 ArgLocs, *DAG.getContext());
1482 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001483
Chris Lattnerf39f7712007-02-28 05:46:49 +00001484 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001485 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001486 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1487 CCValAssign &VA = ArgLocs[i];
1488 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1489 // places.
1490 assert(VA.getValNo() != LastVal &&
1491 "Don't support value assigned to multiple locs yet");
1492 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001493
Chris Lattnerf39f7712007-02-28 05:46:49 +00001494 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001496 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001497 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001498 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001499 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001500 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001502 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001503 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001504 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001505 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001506 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001507 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1508 RC = X86::VR64RegisterClass;
1509 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001510 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001511
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001512 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Chris Lattnerf39f7712007-02-28 05:46:49 +00001515 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1516 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1517 // right size.
1518 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001519 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001520 DAG.getValueType(VA.getValVT()));
1521 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001522 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001523 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001524 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001525 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001526
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001527 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001528 // Handle MMX values passed in XMM regs.
1529 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1531 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001532 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1533 } else
1534 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001535 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001536 } else {
1537 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001539 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001540
1541 // If value is passed via pointer - do a load.
1542 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001543 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001544
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001546 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001547
Dan Gohman61a92132008-04-21 23:59:07 +00001548 // The x86-64 ABI for returning structs by value requires that we copy
1549 // the sret argument into %rax for the return. Save the argument into
1550 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001551 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001552 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1553 unsigned Reg = FuncInfo->getSRetReturnReg();
1554 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001556 FuncInfo->setSRetReturnReg(Reg);
1557 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001559 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001560 }
1561
Chris Lattnerf39f7712007-02-28 05:46:49 +00001562 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001563 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001565 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001566
Evan Cheng1bc78042006-04-26 01:20:17 +00001567 // If the function takes variable number of arguments, make a frame index for
1568 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001569 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001571 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001572 }
1573 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001574 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1575
1576 // FIXME: We should really autogenerate these arrays
1577 static const unsigned GPR64ArgRegsWin64[] = {
1578 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001579 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001580 static const unsigned XMMArgRegsWin64[] = {
1581 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1582 };
1583 static const unsigned GPR64ArgRegs64Bit[] = {
1584 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1585 };
1586 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001587 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1588 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1589 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001590 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1591
1592 if (IsWin64) {
1593 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1594 GPR64ArgRegs = GPR64ArgRegsWin64;
1595 XMMArgRegs = XMMArgRegsWin64;
1596 } else {
1597 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1598 GPR64ArgRegs = GPR64ArgRegs64Bit;
1599 XMMArgRegs = XMMArgRegs64Bit;
1600 }
1601 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1602 TotalNumIntRegs);
1603 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1604 TotalNumXMMRegs);
1605
Devang Patel578efa92009-06-05 21:57:13 +00001606 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001607 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001608 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001609 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001610 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001611 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001612 // Kernel mode asks for SSE to be disabled, so don't push them
1613 // on the stack.
1614 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001615
Gordon Henriksen86737662008-01-05 16:56:59 +00001616 // For X86-64, if there are vararg parameters that are passed via
1617 // registers, then we must store them to their spots on the stack so they
1618 // may be loaded by deferencing the result of va_next.
1619 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001620 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1621 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001622 TotalNumXMMRegs * 16, 16,
1623 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001624
Gordon Henriksen86737662008-01-05 16:56:59 +00001625 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SmallVector<SDValue, 8> MemOps;
1627 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001628 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001629 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001630 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1631 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001632 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1633 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001635 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001636 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001637 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001638 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001640 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001641 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642
Dan Gohmanface41a2009-08-16 21:24:25 +00001643 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1644 // Now store the XMM (fp + vector) parameter registers.
1645 SmallVector<SDValue, 11> SaveXMMOps;
1646 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001647
Dan Gohmanface41a2009-08-16 21:24:25 +00001648 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1649 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1650 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001651
Dan Gohmanface41a2009-08-16 21:24:25 +00001652 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1653 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001654
Dan Gohmanface41a2009-08-16 21:24:25 +00001655 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1656 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1657 X86::VR128RegisterClass);
1658 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1659 SaveXMMOps.push_back(Val);
1660 }
1661 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1662 MVT::Other,
1663 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001665
1666 if (!MemOps.empty())
1667 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1668 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001669 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001670 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001671
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001673 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001674 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001675 BytesCallerReserves = 0;
1676 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001677 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001678 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001679 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001680 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001681 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001682 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001683
Gordon Henriksen86737662008-01-05 16:56:59 +00001684 if (!Is64Bit) {
1685 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001686 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001687 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1688 }
Evan Cheng25caf632006-05-23 21:06:34 +00001689
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001690 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001691
Dan Gohman98ca4f22009-08-05 01:29:28 +00001692 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001693}
1694
Dan Gohman475871a2008-07-27 21:46:04 +00001695SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001696X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1697 SDValue StackPtr, SDValue Arg,
1698 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001699 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001701 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001702 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001704 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001705 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001706 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001707 }
Dale Johannesenace16102009-02-03 19:33:06 +00001708 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001709 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001710}
1711
Bill Wendling64e87322009-01-16 19:25:27 +00001712/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001713/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001714SDValue
1715X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001717 SDValue Chain,
1718 bool IsTailCall,
1719 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001720 int FPDiff,
1721 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001722 if (!IsTailCall || FPDiff==0) return Chain;
1723
1724 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001725 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001726 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001727
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001728 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001729 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001730 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001731}
1732
1733/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1734/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001735static SDValue
1736EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001737 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001738 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001739 // Store the return address to the appropriate stack slot.
1740 if (!FPDiff) return Chain;
1741 // Calculate the new stack slot for the return address.
1742 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001743 int NewReturnAddrFI =
David Greene3f2bf852009-11-12 20:49:22 +00001744 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1745 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001746 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001747 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001748 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001749 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001750 return Chain;
1751}
1752
Dan Gohman98ca4f22009-08-05 01:29:28 +00001753SDValue
1754X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001755 CallingConv::ID CallConv, bool isVarArg,
1756 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 const SmallVectorImpl<ISD::OutputArg> &Outs,
1758 const SmallVectorImpl<ISD::InputArg> &Ins,
1759 DebugLoc dl, SelectionDAG &DAG,
1760 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001761
Dan Gohman98ca4f22009-08-05 01:29:28 +00001762 MachineFunction &MF = DAG.getMachineFunction();
1763 bool Is64Bit = Subtarget->is64Bit();
1764 bool IsStructRet = CallIsStructReturn(Outs);
1765
1766 assert((!isTailCall ||
1767 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1768 "IsEligibleForTailCallOptimization missed a case!");
1769 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001770 "Var args not supported with calling convention fastcc");
1771
Chris Lattner638402b2007-02-28 07:00:42 +00001772 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001773 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1775 ArgLocs, *DAG.getContext());
1776 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001777
Chris Lattner423c5f42007-02-28 05:31:48 +00001778 // Get a count of how many bytes are to be pushed on the stack.
1779 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001781 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001782
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001784 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001785 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001786 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1788 FPDiff = NumBytesCallerPushed - NumBytes;
1789
1790 // Set the delta of movement of the returnaddr stackslot.
1791 // But only set if delta is greater than previous delta.
1792 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1793 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1794 }
1795
Chris Lattnere563bbc2008-10-11 22:08:30 +00001796 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001797
Dan Gohman475871a2008-07-27 21:46:04 +00001798 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001799 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001801 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001802
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1804 SmallVector<SDValue, 8> MemOpChains;
1805 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001806
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001807 // Walk the register/memloc assignments, inserting copies/loads. In the case
1808 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001809 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1810 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001811 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 SDValue Arg = Outs[i].Val;
1813 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001814 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001815
Chris Lattner423c5f42007-02-28 05:31:48 +00001816 // Promote the value if needed.
1817 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001818 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001819 case CCValAssign::Full: break;
1820 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001821 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001822 break;
1823 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001824 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001825 break;
1826 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001827 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1828 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001829 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1830 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1831 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001832 } else
1833 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1834 break;
1835 case CCValAssign::BCvt:
1836 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001837 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001838 case CCValAssign::Indirect: {
1839 // Store the argument.
1840 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001841 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001842 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001843 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001844 Arg = SpillSlot;
1845 break;
1846 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001848
Chris Lattner423c5f42007-02-28 05:31:48 +00001849 if (VA.isRegLoc()) {
1850 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1851 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001852 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001853 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001854 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001855 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001856
Dan Gohman98ca4f22009-08-05 01:29:28 +00001857 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1858 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001859 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001860 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Evan Cheng32fe1032006-05-25 00:59:30 +00001863 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001864 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001865 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001866
Evan Cheng347d5f72006-04-28 21:29:37 +00001867 // Build a sequence of copy-to-reg nodes chained together with token chain
1868 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001869 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001870 // Tail call byval lowering might overwrite argument registers so in case of
1871 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001872 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001873 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001874 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001875 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001876 InFlag = Chain.getValue(1);
1877 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001878
Eric Christopherfd179292009-08-27 18:07:15 +00001879
Chris Lattner88e1fd52009-07-09 04:24:46 +00001880 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001881 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1882 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001883 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001884 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1885 DAG.getNode(X86ISD::GlobalBaseReg,
1886 DebugLoc::getUnknownLoc(),
1887 getPointerTy()),
1888 InFlag);
1889 InFlag = Chain.getValue(1);
1890 } else {
1891 // If we are tail calling and generating PIC/GOT style code load the
1892 // address of the callee into ECX. The value in ecx is used as target of
1893 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1894 // for tail calls on PIC/GOT architectures. Normally we would just put the
1895 // address of GOT into ebx and then call target@PLT. But for tail calls
1896 // ebx would be restored (since ebx is callee saved) before jumping to the
1897 // target@PLT.
1898
1899 // Note: The actual moving to ECX is done further down.
1900 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1901 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1902 !G->getGlobal()->hasProtectedVisibility())
1903 Callee = LowerGlobalAddress(Callee, DAG);
1904 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001905 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001906 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001907 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001908
Gordon Henriksen86737662008-01-05 16:56:59 +00001909 if (Is64Bit && isVarArg) {
1910 // From AMD64 ABI document:
1911 // For calls that may call functions that use varargs or stdargs
1912 // (prototype-less calls or calls to functions containing ellipsis (...) in
1913 // the declaration) %al is used as hidden argument to specify the number
1914 // of SSE registers used. The contents of %al do not need to match exactly
1915 // the number of registers, but must be an ubound on the number of SSE
1916 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001917
1918 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001919 // Count the number of XMM registers allocated.
1920 static const unsigned XMMArgRegs[] = {
1921 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1922 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1923 };
1924 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001926 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Dale Johannesendd64c412009-02-04 00:33:20 +00001928 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001930 InFlag = Chain.getValue(1);
1931 }
1932
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001933
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001934 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001935 if (isTailCall) {
1936 // Force all the incoming stack arguments to be loaded from the stack
1937 // before any new outgoing arguments are stored to the stack, because the
1938 // outgoing stack slots may alias the incoming argument stack slots, and
1939 // the alias isn't otherwise explicit. This is slightly more conservative
1940 // than necessary, because it means that each store effectively depends
1941 // on every argument instead of just those arguments it would clobber.
1942 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1943
Dan Gohman475871a2008-07-27 21:46:04 +00001944 SmallVector<SDValue, 8> MemOpChains2;
1945 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001946 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001947 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001948 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1950 CCValAssign &VA = ArgLocs[i];
1951 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001952 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001953 SDValue Arg = Outs[i].Val;
1954 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 // Create frame index.
1956 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001957 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001958 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001959 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001960
Duncan Sands276dcbd2008-03-21 09:14:45 +00001961 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001962 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001964 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001965 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001966 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001967 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001968
Dan Gohman98ca4f22009-08-05 01:29:28 +00001969 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1970 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001971 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001972 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001973 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001974 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001975 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001976 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001977 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001978 }
1979 }
1980
1981 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001982 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001983 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001984
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001985 // Copy arguments to their registers.
1986 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001987 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001988 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001989 InFlag = Chain.getValue(1);
1990 }
Dan Gohman475871a2008-07-27 21:46:04 +00001991 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001992
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001994 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001995 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001996 }
1997
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00001998 bool WasGlobalOrExternal = false;
1999 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2000 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2001 // In the 64-bit large code model, we have to make all calls
2002 // through a register, since the call instruction's 32-bit
2003 // pc-relative offset may not be large enough to hold the whole
2004 // address.
2005 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2006 WasGlobalOrExternal = true;
2007 // If the callee is a GlobalAddress node (quite common, every direct call
2008 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2009 // it.
2010
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002011 // We should use extra load for direct calls to dllimported functions in
2012 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002013 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002014 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002015 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002016
Chris Lattner48a7d022009-07-09 05:02:21 +00002017 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2018 // external symbols most go through the PLT in PIC mode. If the symbol
2019 // has hidden or protected visibility, or if it is static or local, then
2020 // we don't need to use the PLT - we can directly call it.
2021 if (Subtarget->isTargetELF() &&
2022 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002023 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002024 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002025 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002026 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2027 Subtarget->getDarwinVers() < 9) {
2028 // PC-relative references to external symbols should go through $stub,
2029 // unless we're building with the leopard linker or later, which
2030 // automatically synthesizes these stubs.
2031 OpFlags = X86II::MO_DARWIN_STUB;
2032 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002033
Chris Lattner74e726e2009-07-09 05:27:35 +00002034 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002035 G->getOffset(), OpFlags);
2036 }
Bill Wendling056292f2008-09-16 21:48:12 +00002037 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002038 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002039 unsigned char OpFlags = 0;
2040
2041 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2042 // symbols should go through the PLT.
2043 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002044 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002045 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002046 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002047 Subtarget->getDarwinVers() < 9) {
2048 // PC-relative references to external symbols should go through $stub,
2049 // unless we're building with the leopard linker or later, which
2050 // automatically synthesizes these stubs.
2051 OpFlags = X86II::MO_DARWIN_STUB;
2052 }
Eric Christopherfd179292009-08-27 18:07:15 +00002053
Chris Lattner48a7d022009-07-09 05:02:21 +00002054 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2055 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002056 }
2057
2058 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002059 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002060
Dale Johannesendd64c412009-02-04 00:33:20 +00002061 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002062 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002063 Callee,InFlag);
2064 Callee = DAG.getRegister(Opc, getPointerTy());
2065 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002066 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002068
Chris Lattnerd96d0722007-02-25 06:40:16 +00002069 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002070 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002071 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002072
Dan Gohman98ca4f22009-08-05 01:29:28 +00002073 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002074 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2075 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002076 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002077 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002078
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002079 Ops.push_back(Chain);
2080 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002081
Dan Gohman98ca4f22009-08-05 01:29:28 +00002082 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002083 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002084
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 // Add argument registers to the end of the list so that they are known live
2086 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002087 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2088 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2089 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002090
Evan Cheng586ccac2008-03-18 23:36:35 +00002091 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002092 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002093 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2094
2095 // Add an implicit use of AL for x86 vararg functions.
2096 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002097 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002098
Gabor Greifba36cb52008-08-28 21:40:38 +00002099 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002100 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002101
Dan Gohman98ca4f22009-08-05 01:29:28 +00002102 if (isTailCall) {
2103 // If this is the first return lowered for this function, add the regs
2104 // to the liveout set for the function.
2105 if (MF.getRegInfo().liveout_empty()) {
2106 SmallVector<CCValAssign, 16> RVLocs;
2107 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2108 *DAG.getContext());
2109 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2110 for (unsigned i = 0; i != RVLocs.size(); ++i)
2111 if (RVLocs[i].isRegLoc())
2112 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2113 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002114
Dan Gohman98ca4f22009-08-05 01:29:28 +00002115 assert(((Callee.getOpcode() == ISD::Register &&
2116 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002117 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002118 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2119 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002120 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002121
2122 return DAG.getNode(X86ISD::TC_RETURN, dl,
2123 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002124 }
2125
Dale Johannesenace16102009-02-03 19:33:06 +00002126 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002127 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002128
Chris Lattner2d297092006-05-23 18:50:38 +00002129 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002130 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002131 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002134 // If this is is a call to a struct-return function, the callee
2135 // pops the hidden struct pointer, so we have to push it back.
2136 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002137 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002138 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002139 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002140
Gordon Henriksenae636f82008-01-03 16:47:34 +00002141 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002142 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002143 DAG.getIntPtrConstant(NumBytes, true),
2144 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2145 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002146 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002147 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002148
Chris Lattner3085e152007-02-25 08:59:22 +00002149 // Handle result values, copying them out of physregs into vregs that we
2150 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002151 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2152 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002153}
2154
Evan Cheng25ab6902006-09-08 06:48:29 +00002155
2156//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002157// Fast Calling Convention (tail call) implementation
2158//===----------------------------------------------------------------------===//
2159
2160// Like std call, callee cleans arguments, convention except that ECX is
2161// reserved for storing the tail called function address. Only 2 registers are
2162// free for argument passing (inreg). Tail call optimization is performed
2163// provided:
2164// * tailcallopt is enabled
2165// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002166// On X86_64 architecture with GOT-style position independent code only local
2167// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002168// To keep the stack aligned according to platform abi the function
2169// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2170// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002171// If a tail called function callee has more arguments than the caller the
2172// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002173// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002174// original REtADDR, but before the saved framepointer or the spilled registers
2175// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2176// stack layout:
2177// arg1
2178// arg2
2179// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002180// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002181// move area ]
2182// (possible EBP)
2183// ESI
2184// EDI
2185// local1 ..
2186
2187/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2188/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002189unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002190 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002191 MachineFunction &MF = DAG.getMachineFunction();
2192 const TargetMachine &TM = MF.getTarget();
2193 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2194 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002195 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002196 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002197 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002198 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2199 // Number smaller than 12 so just add the difference.
2200 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2201 } else {
2202 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002203 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002204 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002205 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002206 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002207}
2208
Dan Gohman98ca4f22009-08-05 01:29:28 +00002209/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2210/// for tail call optimization. Targets which want to do tail call
2211/// optimization should implement this function.
2212bool
2213X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002214 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002215 bool isVarArg,
2216 const SmallVectorImpl<ISD::InputArg> &Ins,
2217 SelectionDAG& DAG) const {
2218 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002219 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002220 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002221}
2222
Dan Gohman3df24e62008-09-03 23:12:08 +00002223FastISel *
2224X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002225 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002226 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002227 DenseMap<const Value *, unsigned> &vm,
2228 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002229 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002230 DenseMap<const AllocaInst *, int> &am
2231#ifndef NDEBUG
2232 , SmallSet<Instruction*, 8> &cil
2233#endif
2234 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002235 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002236#ifndef NDEBUG
2237 , cil
2238#endif
2239 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002240}
2241
2242
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002243//===----------------------------------------------------------------------===//
2244// Other Lowering Hooks
2245//===----------------------------------------------------------------------===//
2246
2247
Dan Gohman475871a2008-07-27 21:46:04 +00002248SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002249 MachineFunction &MF = DAG.getMachineFunction();
2250 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2251 int ReturnAddrIndex = FuncInfo->getRAIndex();
2252
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002253 if (ReturnAddrIndex == 0) {
2254 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002255 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002256 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2257 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002258 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002259 }
2260
Evan Cheng25ab6902006-09-08 06:48:29 +00002261 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002262}
2263
2264
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002265bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2266 bool hasSymbolicDisplacement) {
2267 // Offset should fit into 32 bit immediate field.
2268 if (!isInt32(Offset))
2269 return false;
2270
2271 // If we don't have a symbolic displacement - we don't have any extra
2272 // restrictions.
2273 if (!hasSymbolicDisplacement)
2274 return true;
2275
2276 // FIXME: Some tweaks might be needed for medium code model.
2277 if (M != CodeModel::Small && M != CodeModel::Kernel)
2278 return false;
2279
2280 // For small code model we assume that latest object is 16MB before end of 31
2281 // bits boundary. We may also accept pretty large negative constants knowing
2282 // that all objects are in the positive half of address space.
2283 if (M == CodeModel::Small && Offset < 16*1024*1024)
2284 return true;
2285
2286 // For kernel code model we know that all object resist in the negative half
2287 // of 32bits address space. We may not accept negative offsets, since they may
2288 // be just off and we may accept pretty large positive ones.
2289 if (M == CodeModel::Kernel && Offset > 0)
2290 return true;
2291
2292 return false;
2293}
2294
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002295/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2296/// specific condition code, returning the condition code and the LHS/RHS of the
2297/// comparison to make.
2298static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2299 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002300 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002301 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2302 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2303 // X > -1 -> X == 0, jump !sign.
2304 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002305 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002306 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2307 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002308 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002309 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002310 // X < 1 -> X <= 0
2311 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002312 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002313 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002314 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002315
Evan Chengd9558e02006-01-06 00:43:03 +00002316 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002317 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002318 case ISD::SETEQ: return X86::COND_E;
2319 case ISD::SETGT: return X86::COND_G;
2320 case ISD::SETGE: return X86::COND_GE;
2321 case ISD::SETLT: return X86::COND_L;
2322 case ISD::SETLE: return X86::COND_LE;
2323 case ISD::SETNE: return X86::COND_NE;
2324 case ISD::SETULT: return X86::COND_B;
2325 case ISD::SETUGT: return X86::COND_A;
2326 case ISD::SETULE: return X86::COND_BE;
2327 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002328 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002330
Chris Lattner4c78e022008-12-23 23:42:27 +00002331 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002332
Chris Lattner4c78e022008-12-23 23:42:27 +00002333 // If LHS is a foldable load, but RHS is not, flip the condition.
2334 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2335 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2336 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2337 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002338 }
2339
Chris Lattner4c78e022008-12-23 23:42:27 +00002340 switch (SetCCOpcode) {
2341 default: break;
2342 case ISD::SETOLT:
2343 case ISD::SETOLE:
2344 case ISD::SETUGT:
2345 case ISD::SETUGE:
2346 std::swap(LHS, RHS);
2347 break;
2348 }
2349
2350 // On a floating point condition, the flags are set as follows:
2351 // ZF PF CF op
2352 // 0 | 0 | 0 | X > Y
2353 // 0 | 0 | 1 | X < Y
2354 // 1 | 0 | 0 | X == Y
2355 // 1 | 1 | 1 | unordered
2356 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002357 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002358 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002359 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002360 case ISD::SETOLT: // flipped
2361 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002362 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002363 case ISD::SETOLE: // flipped
2364 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002365 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002366 case ISD::SETUGT: // flipped
2367 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002368 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002369 case ISD::SETUGE: // flipped
2370 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002371 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002372 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002373 case ISD::SETNE: return X86::COND_NE;
2374 case ISD::SETUO: return X86::COND_P;
2375 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002376 case ISD::SETOEQ:
2377 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002378 }
Evan Chengd9558e02006-01-06 00:43:03 +00002379}
2380
Evan Cheng4a460802006-01-11 00:33:36 +00002381/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2382/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002383/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002384static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002385 switch (X86CC) {
2386 default:
2387 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002388 case X86::COND_B:
2389 case X86::COND_BE:
2390 case X86::COND_E:
2391 case X86::COND_P:
2392 case X86::COND_A:
2393 case X86::COND_AE:
2394 case X86::COND_NE:
2395 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002396 return true;
2397 }
2398}
2399
Evan Chengeb2f9692009-10-27 19:56:55 +00002400/// isFPImmLegal - Returns true if the target can instruction select the
2401/// specified FP immediate natively. If false, the legalizer will
2402/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002403bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002404 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2405 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2406 return true;
2407 }
2408 return false;
2409}
2410
Nate Begeman9008ca62009-04-27 18:41:29 +00002411/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2412/// the specified range (L, H].
2413static bool isUndefOrInRange(int Val, int Low, int Hi) {
2414 return (Val < 0) || (Val >= Low && Val < Hi);
2415}
2416
2417/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2418/// specified value.
2419static bool isUndefOrEqual(int Val, int CmpVal) {
2420 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002421 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002422 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002423}
2424
Nate Begeman9008ca62009-04-27 18:41:29 +00002425/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2426/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2427/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002428static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002429 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002430 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002431 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002432 return (Mask[0] < 2 && Mask[1] < 2);
2433 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002434}
2435
Nate Begeman9008ca62009-04-27 18:41:29 +00002436bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002437 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002438 N->getMask(M);
2439 return ::isPSHUFDMask(M, N->getValueType(0));
2440}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002441
Nate Begeman9008ca62009-04-27 18:41:29 +00002442/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2443/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002444static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002445 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002446 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002447
Nate Begeman9008ca62009-04-27 18:41:29 +00002448 // Lower quadword copied in order or undef.
2449 for (int i = 0; i != 4; ++i)
2450 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002451 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002452
Evan Cheng506d3df2006-03-29 23:07:14 +00002453 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002454 for (int i = 4; i != 8; ++i)
2455 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002456 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002457
Evan Cheng506d3df2006-03-29 23:07:14 +00002458 return true;
2459}
2460
Nate Begeman9008ca62009-04-27 18:41:29 +00002461bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002462 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002463 N->getMask(M);
2464 return ::isPSHUFHWMask(M, N->getValueType(0));
2465}
Evan Cheng506d3df2006-03-29 23:07:14 +00002466
Nate Begeman9008ca62009-04-27 18:41:29 +00002467/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2468/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002469static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002472
Rafael Espindola15684b22009-04-24 12:40:33 +00002473 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002474 for (int i = 4; i != 8; ++i)
2475 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002476 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002477
Rafael Espindola15684b22009-04-24 12:40:33 +00002478 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002479 for (int i = 0; i != 4; ++i)
2480 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002481 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002482
Rafael Espindola15684b22009-04-24 12:40:33 +00002483 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002484}
2485
Nate Begeman9008ca62009-04-27 18:41:29 +00002486bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002487 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002488 N->getMask(M);
2489 return ::isPSHUFLWMask(M, N->getValueType(0));
2490}
2491
Nate Begemana09008b2009-10-19 02:17:23 +00002492/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2493/// is suitable for input to PALIGNR.
2494static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2495 bool hasSSSE3) {
2496 int i, e = VT.getVectorNumElements();
2497
2498 // Do not handle v2i64 / v2f64 shuffles with palignr.
2499 if (e < 4 || !hasSSSE3)
2500 return false;
2501
2502 for (i = 0; i != e; ++i)
2503 if (Mask[i] >= 0)
2504 break;
2505
2506 // All undef, not a palignr.
2507 if (i == e)
2508 return false;
2509
2510 // Determine if it's ok to perform a palignr with only the LHS, since we
2511 // don't have access to the actual shuffle elements to see if RHS is undef.
2512 bool Unary = Mask[i] < (int)e;
2513 bool NeedsUnary = false;
2514
2515 int s = Mask[i] - i;
2516
2517 // Check the rest of the elements to see if they are consecutive.
2518 for (++i; i != e; ++i) {
2519 int m = Mask[i];
2520 if (m < 0)
2521 continue;
2522
2523 Unary = Unary && (m < (int)e);
2524 NeedsUnary = NeedsUnary || (m < s);
2525
2526 if (NeedsUnary && !Unary)
2527 return false;
2528 if (Unary && m != ((s+i) & (e-1)))
2529 return false;
2530 if (!Unary && m != (s+i))
2531 return false;
2532 }
2533 return true;
2534}
2535
2536bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2537 SmallVector<int, 8> M;
2538 N->getMask(M);
2539 return ::isPALIGNRMask(M, N->getValueType(0), true);
2540}
2541
Evan Cheng14aed5e2006-03-24 01:18:28 +00002542/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2543/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002544static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 int NumElems = VT.getVectorNumElements();
2546 if (NumElems != 2 && NumElems != 4)
2547 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002548
Nate Begeman9008ca62009-04-27 18:41:29 +00002549 int Half = NumElems / 2;
2550 for (int i = 0; i < Half; ++i)
2551 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002552 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002553 for (int i = Half; i < NumElems; ++i)
2554 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002555 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002556
Evan Cheng14aed5e2006-03-24 01:18:28 +00002557 return true;
2558}
2559
Nate Begeman9008ca62009-04-27 18:41:29 +00002560bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2561 SmallVector<int, 8> M;
2562 N->getMask(M);
2563 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002564}
2565
Evan Cheng213d2cf2007-05-17 18:45:50 +00002566/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002567/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2568/// half elements to come from vector 1 (which would equal the dest.) and
2569/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002570static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002571 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002572
2573 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002574 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002575
Nate Begeman9008ca62009-04-27 18:41:29 +00002576 int Half = NumElems / 2;
2577 for (int i = 0; i < Half; ++i)
2578 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002579 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002580 for (int i = Half; i < NumElems; ++i)
2581 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002582 return false;
2583 return true;
2584}
2585
Nate Begeman9008ca62009-04-27 18:41:29 +00002586static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2587 SmallVector<int, 8> M;
2588 N->getMask(M);
2589 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002590}
2591
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002592/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2593/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002594bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2595 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002596 return false;
2597
Evan Cheng2064a2b2006-03-28 06:50:32 +00002598 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2600 isUndefOrEqual(N->getMaskElt(1), 7) &&
2601 isUndefOrEqual(N->getMaskElt(2), 2) &&
2602 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002603}
2604
Nate Begeman0b10b912009-11-07 23:17:15 +00002605/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2606/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2607/// <2, 3, 2, 3>
2608bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2609 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2610
2611 if (NumElems != 4)
2612 return false;
2613
2614 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2615 isUndefOrEqual(N->getMaskElt(1), 3) &&
2616 isUndefOrEqual(N->getMaskElt(2), 2) &&
2617 isUndefOrEqual(N->getMaskElt(3), 3);
2618}
2619
Evan Cheng5ced1d82006-04-06 23:23:56 +00002620/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2621/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002622bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2623 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002624
Evan Cheng5ced1d82006-04-06 23:23:56 +00002625 if (NumElems != 2 && NumElems != 4)
2626 return false;
2627
Evan Chengc5cdff22006-04-07 21:53:05 +00002628 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002629 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002630 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002631
Evan Chengc5cdff22006-04-07 21:53:05 +00002632 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002634 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002635
2636 return true;
2637}
2638
Nate Begeman0b10b912009-11-07 23:17:15 +00002639/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2640/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2641bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002642 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002643
Evan Cheng5ced1d82006-04-06 23:23:56 +00002644 if (NumElems != 2 && NumElems != 4)
2645 return false;
2646
Evan Chengc5cdff22006-04-07 21:53:05 +00002647 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002648 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002649 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002650
Nate Begeman9008ca62009-04-27 18:41:29 +00002651 for (unsigned i = 0; i < NumElems/2; ++i)
2652 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002653 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002654
2655 return true;
2656}
2657
Evan Cheng0038e592006-03-28 00:39:58 +00002658/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2659/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002660static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002661 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002663 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002664 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002665
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2667 int BitI = Mask[i];
2668 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002669 if (!isUndefOrEqual(BitI, j))
2670 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002671 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002672 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002673 return false;
2674 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002675 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002676 return false;
2677 }
Evan Cheng0038e592006-03-28 00:39:58 +00002678 }
Evan Cheng0038e592006-03-28 00:39:58 +00002679 return true;
2680}
2681
Nate Begeman9008ca62009-04-27 18:41:29 +00002682bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2683 SmallVector<int, 8> M;
2684 N->getMask(M);
2685 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002686}
2687
Evan Cheng4fcb9222006-03-28 02:43:26 +00002688/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2689/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002690static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002691 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002692 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002693 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002694 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002695
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2697 int BitI = Mask[i];
2698 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002699 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002700 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002701 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002702 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002703 return false;
2704 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002705 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002706 return false;
2707 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002708 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002709 return true;
2710}
2711
Nate Begeman9008ca62009-04-27 18:41:29 +00002712bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2713 SmallVector<int, 8> M;
2714 N->getMask(M);
2715 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002716}
2717
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002718/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2719/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2720/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002721static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002722 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002723 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002724 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002725
Nate Begeman9008ca62009-04-27 18:41:29 +00002726 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2727 int BitI = Mask[i];
2728 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002729 if (!isUndefOrEqual(BitI, j))
2730 return false;
2731 if (!isUndefOrEqual(BitI1, j))
2732 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002733 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002734 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002735}
2736
Nate Begeman9008ca62009-04-27 18:41:29 +00002737bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2738 SmallVector<int, 8> M;
2739 N->getMask(M);
2740 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2741}
2742
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002743/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2744/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2745/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002746static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002747 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002748 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2749 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002750
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2752 int BitI = Mask[i];
2753 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002754 if (!isUndefOrEqual(BitI, j))
2755 return false;
2756 if (!isUndefOrEqual(BitI1, j))
2757 return false;
2758 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002759 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002760}
2761
Nate Begeman9008ca62009-04-27 18:41:29 +00002762bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2763 SmallVector<int, 8> M;
2764 N->getMask(M);
2765 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2766}
2767
Evan Cheng017dcc62006-04-21 01:05:10 +00002768/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2769/// specifies a shuffle of elements that is suitable for input to MOVSS,
2770/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002771static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002772 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002773 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002774
2775 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002776
Nate Begeman9008ca62009-04-27 18:41:29 +00002777 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002778 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002779
Nate Begeman9008ca62009-04-27 18:41:29 +00002780 for (int i = 1; i < NumElts; ++i)
2781 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002782 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002783
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002784 return true;
2785}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002786
Nate Begeman9008ca62009-04-27 18:41:29 +00002787bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2788 SmallVector<int, 8> M;
2789 N->getMask(M);
2790 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002791}
2792
Evan Cheng017dcc62006-04-21 01:05:10 +00002793/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2794/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002795/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002796static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 bool V2IsSplat = false, bool V2IsUndef = false) {
2798 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002799 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002800 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002801
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002803 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002804
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 for (int i = 1; i < NumOps; ++i)
2806 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2807 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2808 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002809 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002810
Evan Cheng39623da2006-04-20 08:58:49 +00002811 return true;
2812}
2813
Nate Begeman9008ca62009-04-27 18:41:29 +00002814static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002815 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 SmallVector<int, 8> M;
2817 N->getMask(M);
2818 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002819}
2820
Evan Chengd9539472006-04-14 21:59:03 +00002821/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2822/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002823bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2824 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002825 return false;
2826
2827 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002828 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 int Elt = N->getMaskElt(i);
2830 if (Elt >= 0 && Elt != 1)
2831 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002832 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002833
2834 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002835 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 int Elt = N->getMaskElt(i);
2837 if (Elt >= 0 && Elt != 3)
2838 return false;
2839 if (Elt == 3)
2840 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002841 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002842 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002844 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002845}
2846
2847/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2848/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002849bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2850 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002851 return false;
2852
2853 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 for (unsigned i = 0; i < 2; ++i)
2855 if (N->getMaskElt(i) > 0)
2856 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002857
2858 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002859 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 int Elt = N->getMaskElt(i);
2861 if (Elt >= 0 && Elt != 2)
2862 return false;
2863 if (Elt == 2)
2864 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002865 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002866 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002867 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002868}
2869
Evan Cheng0b457f02008-09-25 20:50:48 +00002870/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2871/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002872bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2873 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002874
Nate Begeman9008ca62009-04-27 18:41:29 +00002875 for (int i = 0; i < e; ++i)
2876 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002877 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002878 for (int i = 0; i < e; ++i)
2879 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002880 return false;
2881 return true;
2882}
2883
Evan Cheng63d33002006-03-22 08:01:21 +00002884/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002885/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002886unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2888 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2889
Evan Chengb9df0ca2006-03-22 02:53:00 +00002890 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2891 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002892 for (int i = 0; i < NumOperands; ++i) {
2893 int Val = SVOp->getMaskElt(NumOperands-i-1);
2894 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002895 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002896 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002897 if (i != NumOperands - 1)
2898 Mask <<= Shift;
2899 }
Evan Cheng63d33002006-03-22 08:01:21 +00002900 return Mask;
2901}
2902
Evan Cheng506d3df2006-03-29 23:07:14 +00002903/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002904/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002905unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002906 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002907 unsigned Mask = 0;
2908 // 8 nodes, but we only care about the last 4.
2909 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002910 int Val = SVOp->getMaskElt(i);
2911 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002912 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002913 if (i != 4)
2914 Mask <<= 2;
2915 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002916 return Mask;
2917}
2918
2919/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002920/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002921unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002923 unsigned Mask = 0;
2924 // 8 nodes, but we only care about the first 4.
2925 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 int Val = SVOp->getMaskElt(i);
2927 if (Val >= 0)
2928 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002929 if (i != 0)
2930 Mask <<= 2;
2931 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002932 return Mask;
2933}
2934
Nate Begemana09008b2009-10-19 02:17:23 +00002935/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2936/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2937unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2938 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2939 EVT VVT = N->getValueType(0);
2940 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2941 int Val = 0;
2942
2943 unsigned i, e;
2944 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2945 Val = SVOp->getMaskElt(i);
2946 if (Val >= 0)
2947 break;
2948 }
2949 return (Val - i) * EltSize;
2950}
2951
Evan Cheng37b73872009-07-30 08:33:02 +00002952/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2953/// constant +0.0.
2954bool X86::isZeroNode(SDValue Elt) {
2955 return ((isa<ConstantSDNode>(Elt) &&
2956 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2957 (isa<ConstantFPSDNode>(Elt) &&
2958 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2959}
2960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2962/// their permute mask.
2963static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2964 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002965 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002966 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Nate Begeman5a5ca152009-04-29 05:20:52 +00002969 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002970 int idx = SVOp->getMaskElt(i);
2971 if (idx < 0)
2972 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002973 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002974 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002975 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002977 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2979 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002980}
2981
Evan Cheng779ccea2007-12-07 21:30:01 +00002982/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2983/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00002984static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002985 unsigned NumElems = VT.getVectorNumElements();
2986 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 int idx = Mask[i];
2988 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002989 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002990 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002991 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002992 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002993 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002994 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002995}
2996
Evan Cheng533a0aa2006-04-19 20:35:22 +00002997/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2998/// match movhlps. The lower half elements should come from upper half of
2999/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003000/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003001static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3002 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003003 return false;
3004 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003006 return false;
3007 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003009 return false;
3010 return true;
3011}
3012
Evan Cheng5ced1d82006-04-06 23:23:56 +00003013/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003014/// is promoted to a vector. It also returns the LoadSDNode by reference if
3015/// required.
3016static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003017 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3018 return false;
3019 N = N->getOperand(0).getNode();
3020 if (!ISD::isNON_EXTLoad(N))
3021 return false;
3022 if (LD)
3023 *LD = cast<LoadSDNode>(N);
3024 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025}
3026
Evan Cheng533a0aa2006-04-19 20:35:22 +00003027/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3028/// match movlp{s|d}. The lower half elements should come from lower half of
3029/// V1 (and in order), and the upper half elements should come from the upper
3030/// half of V2 (and in order). And since V1 will become the source of the
3031/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003032static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3033 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003034 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003035 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003036 // Is V2 is a vector load, don't do this transformation. We will try to use
3037 // load folding shufps op.
3038 if (ISD::isNON_EXTLoad(V2))
3039 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003040
Nate Begeman5a5ca152009-04-29 05:20:52 +00003041 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003042
Evan Cheng533a0aa2006-04-19 20:35:22 +00003043 if (NumElems != 2 && NumElems != 4)
3044 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003045 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003047 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003048 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003050 return false;
3051 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003052}
3053
Evan Cheng39623da2006-04-20 08:58:49 +00003054/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3055/// all the same.
3056static bool isSplatVector(SDNode *N) {
3057 if (N->getOpcode() != ISD::BUILD_VECTOR)
3058 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003059
Dan Gohman475871a2008-07-27 21:46:04 +00003060 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003061 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3062 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003063 return false;
3064 return true;
3065}
3066
Evan Cheng213d2cf2007-05-17 18:45:50 +00003067/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003068/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003069/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003070static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003071 SDValue V1 = N->getOperand(0);
3072 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003073 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3074 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003075 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003076 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003078 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3079 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003080 if (Opc != ISD::BUILD_VECTOR ||
3081 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 return false;
3083 } else if (Idx >= 0) {
3084 unsigned Opc = V1.getOpcode();
3085 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3086 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003087 if (Opc != ISD::BUILD_VECTOR ||
3088 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003089 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003090 }
3091 }
3092 return true;
3093}
3094
3095/// getZeroVector - Returns a vector of specified type with all zero elements.
3096///
Owen Andersone50ed302009-08-10 22:56:29 +00003097static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003098 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003099 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003100
Chris Lattner8a594482007-11-25 00:24:49 +00003101 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3102 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003103 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3106 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003107 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3109 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003110 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3112 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003113 }
Dale Johannesenace16102009-02-03 19:33:06 +00003114 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003115}
3116
Chris Lattner8a594482007-11-25 00:24:49 +00003117/// getOnesVector - Returns a vector of specified type with all bits set.
3118///
Owen Andersone50ed302009-08-10 22:56:29 +00003119static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003120 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003121
Chris Lattner8a594482007-11-25 00:24:49 +00003122 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3123 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003125 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003126 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003128 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003130 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003131}
3132
3133
Evan Cheng39623da2006-04-20 08:58:49 +00003134/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3135/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003136static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003137 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003138 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003139
Evan Cheng39623da2006-04-20 08:58:49 +00003140 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 SmallVector<int, 8> MaskVec;
3142 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003143
Nate Begeman5a5ca152009-04-29 05:20:52 +00003144 for (unsigned i = 0; i != NumElems; ++i) {
3145 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003146 MaskVec[i] = NumElems;
3147 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003148 }
Evan Cheng39623da2006-04-20 08:58:49 +00003149 }
Evan Cheng39623da2006-04-20 08:58:49 +00003150 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3152 SVOp->getOperand(1), &MaskVec[0]);
3153 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003154}
3155
Evan Cheng017dcc62006-04-21 01:05:10 +00003156/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3157/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003158static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 SDValue V2) {
3160 unsigned NumElems = VT.getVectorNumElements();
3161 SmallVector<int, 8> Mask;
3162 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003163 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003164 Mask.push_back(i);
3165 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003166}
3167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003169static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 SDValue V2) {
3171 unsigned NumElems = VT.getVectorNumElements();
3172 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003173 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 Mask.push_back(i);
3175 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003176 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003177 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003178}
3179
Nate Begeman9008ca62009-04-27 18:41:29 +00003180/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003181static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 SDValue V2) {
3183 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003184 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003186 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003187 Mask.push_back(i + Half);
3188 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003189 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003190 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003191}
3192
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003193/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003194static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 bool HasSSE2) {
3196 if (SV->getValueType(0).getVectorNumElements() <= 4)
3197 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003198
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003200 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 DebugLoc dl = SV->getDebugLoc();
3202 SDValue V1 = SV->getOperand(0);
3203 int NumElems = VT.getVectorNumElements();
3204 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003205
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 // unpack elements to the correct location
3207 while (NumElems > 4) {
3208 if (EltNo < NumElems/2) {
3209 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3210 } else {
3211 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3212 EltNo -= NumElems/2;
3213 }
3214 NumElems >>= 1;
3215 }
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 // Perform the splat.
3218 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003219 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3221 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003222}
3223
Evan Chengba05f722006-04-21 23:03:30 +00003224/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003225/// vector of zero or undef vector. This produces a shuffle where the low
3226/// element of V2 is swizzled into the zero/undef vector, landing at element
3227/// 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 +00003228static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003229 bool isZero, bool HasSSE2,
3230 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003231 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003232 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3234 unsigned NumElems = VT.getVectorNumElements();
3235 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003236 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003237 // If this is the insertion idx, put the low elt of V2 here.
3238 MaskVec.push_back(i == Idx ? NumElems : i);
3239 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003240}
3241
Evan Chengf26ffe92008-05-29 08:22:04 +00003242/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3243/// a shuffle that is zero.
3244static
Nate Begeman9008ca62009-04-27 18:41:29 +00003245unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3246 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003247 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003248 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003249 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003250 int Idx = SVOp->getMaskElt(Index);
3251 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003252 ++NumZeros;
3253 continue;
3254 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003255 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003256 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003257 ++NumZeros;
3258 else
3259 break;
3260 }
3261 return NumZeros;
3262}
3263
3264/// isVectorShift - Returns true if the shuffle can be implemented as a
3265/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003266/// FIXME: split into pslldqi, psrldqi, palignr variants.
3267static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003268 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003270
3271 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003272 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003273 if (!NumZeros) {
3274 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003276 if (!NumZeros)
3277 return false;
3278 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003279 bool SeenV1 = false;
3280 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003281 for (int i = NumZeros; i < NumElems; ++i) {
3282 int Val = isLeft ? (i - NumZeros) : i;
3283 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3284 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003285 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003286 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003287 SeenV1 = true;
3288 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003290 SeenV2 = true;
3291 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003293 return false;
3294 }
3295 if (SeenV1 && SeenV2)
3296 return false;
3297
Nate Begeman9008ca62009-04-27 18:41:29 +00003298 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003299 ShAmt = NumZeros;
3300 return true;
3301}
3302
3303
Evan Chengc78d3b42006-04-24 18:01:45 +00003304/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3305///
Dan Gohman475871a2008-07-27 21:46:04 +00003306static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003307 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003308 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003309 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003310 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003311
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003312 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003313 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003314 bool First = true;
3315 for (unsigned i = 0; i < 16; ++i) {
3316 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3317 if (ThisIsNonZero && First) {
3318 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003320 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003322 First = false;
3323 }
3324
3325 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003326 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003327 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3328 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003329 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003331 }
3332 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3334 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3335 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003336 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003338 } else
3339 ThisElt = LastElt;
3340
Gabor Greifba36cb52008-08-28 21:40:38 +00003341 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003343 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003344 }
3345 }
3346
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003348}
3349
Bill Wendlinga348c562007-03-22 18:42:45 +00003350/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003351///
Dan Gohman475871a2008-07-27 21:46:04 +00003352static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003353 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003354 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003355 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003356 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003357
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003358 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003359 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003360 bool First = true;
3361 for (unsigned i = 0; i < 8; ++i) {
3362 bool isNonZero = (NonZeros & (1 << i)) != 0;
3363 if (isNonZero) {
3364 if (First) {
3365 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003367 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003369 First = false;
3370 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003371 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003373 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003374 }
3375 }
3376
3377 return V;
3378}
3379
Evan Chengf26ffe92008-05-29 08:22:04 +00003380/// getVShift - Return a vector logical shift node.
3381///
Owen Andersone50ed302009-08-10 22:56:29 +00003382static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 unsigned NumBits, SelectionDAG &DAG,
3384 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003385 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003387 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003388 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3389 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3390 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003391 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003392}
3393
Dan Gohman475871a2008-07-27 21:46:04 +00003394SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003395X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3396 SelectionDAG &DAG) {
3397
3398 // Check if the scalar load can be widened into a vector load. And if
3399 // the address is "base + cst" see if the cst can be "absorbed" into
3400 // the shuffle mask.
3401 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3402 SDValue Ptr = LD->getBasePtr();
3403 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3404 return SDValue();
3405 EVT PVT = LD->getValueType(0);
3406 if (PVT != MVT::i32 && PVT != MVT::f32)
3407 return SDValue();
3408
3409 int FI = -1;
3410 int64_t Offset = 0;
3411 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3412 FI = FINode->getIndex();
3413 Offset = 0;
3414 } else if (Ptr.getOpcode() == ISD::ADD &&
3415 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3416 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3417 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3418 Offset = Ptr.getConstantOperandVal(1);
3419 Ptr = Ptr.getOperand(0);
3420 } else {
3421 return SDValue();
3422 }
3423
3424 SDValue Chain = LD->getChain();
3425 // Make sure the stack object alignment is at least 16.
3426 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3427 if (DAG.InferPtrAlignment(Ptr) < 16) {
3428 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003429 // Can't change the alignment. FIXME: It's possible to compute
3430 // the exact stack offset and reference FI + adjust offset instead.
3431 // If someone *really* cares about this. That's the way to implement it.
3432 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003433 } else {
3434 MFI->setObjectAlignment(FI, 16);
3435 }
3436 }
3437
3438 // (Offset % 16) must be multiple of 4. Then address is then
3439 // Ptr + (Offset & ~15).
3440 if (Offset < 0)
3441 return SDValue();
3442 if ((Offset % 16) & 3)
3443 return SDValue();
3444 int64_t StartOffset = Offset & ~15;
3445 if (StartOffset)
3446 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3447 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3448
3449 int EltNo = (Offset - StartOffset) >> 2;
3450 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3451 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3452 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3453 // Canonicalize it to a v4i32 shuffle.
3454 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3455 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3456 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3457 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3458 }
3459
3460 return SDValue();
3461}
3462
3463SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003464X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003465 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003466 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003467 if (ISD::isBuildVectorAllZeros(Op.getNode())
3468 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003469 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3470 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3471 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003472 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003473 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003474
Gabor Greifba36cb52008-08-28 21:40:38 +00003475 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003476 return getOnesVector(Op.getValueType(), DAG, dl);
3477 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003478 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003479
Owen Andersone50ed302009-08-10 22:56:29 +00003480 EVT VT = Op.getValueType();
3481 EVT ExtVT = VT.getVectorElementType();
3482 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003483
3484 unsigned NumElems = Op.getNumOperands();
3485 unsigned NumZero = 0;
3486 unsigned NumNonZero = 0;
3487 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003488 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003489 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003490 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003492 if (Elt.getOpcode() == ISD::UNDEF)
3493 continue;
3494 Values.insert(Elt);
3495 if (Elt.getOpcode() != ISD::Constant &&
3496 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003497 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003498 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003499 NumZero++;
3500 else {
3501 NonZeros |= (1 << i);
3502 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003503 }
3504 }
3505
Dan Gohman7f321562007-06-25 16:23:39 +00003506 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003507 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003508 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003509 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003510
Chris Lattner67f453a2008-03-09 05:42:06 +00003511 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003512 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003513 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003514 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003515
Chris Lattner62098042008-03-09 01:05:04 +00003516 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3517 // the value are obviously zero, truncate the value to i32 and do the
3518 // insertion that way. Only do this if the value is non-constant or if the
3519 // value is a constant being inserted into element 0. It is cheaper to do
3520 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003522 (!IsAllConstants || Idx == 0)) {
3523 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3524 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3526 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003527
Chris Lattner62098042008-03-09 01:05:04 +00003528 // Truncate the value (which may itself be a constant) to i32, and
3529 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003531 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003532 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3533 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003534
Chris Lattner62098042008-03-09 01:05:04 +00003535 // Now we have our 32-bit value zero extended in the low element of
3536 // a vector. If Idx != 0, swizzle it into place.
3537 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003538 SmallVector<int, 4> Mask;
3539 Mask.push_back(Idx);
3540 for (unsigned i = 1; i != VecElts; ++i)
3541 Mask.push_back(i);
3542 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003543 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003544 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003545 }
Dale Johannesenace16102009-02-03 19:33:06 +00003546 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003547 }
3548 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003549
Chris Lattner19f79692008-03-08 22:59:52 +00003550 // If we have a constant or non-constant insertion into the low element of
3551 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3552 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003553 // depending on what the source datatype is.
3554 if (Idx == 0) {
3555 if (NumZero == 0) {
3556 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3558 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003559 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3560 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3561 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3562 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3564 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3565 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003566 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3567 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3568 Subtarget->hasSSE2(), DAG);
3569 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3570 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003571 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003572
3573 // Is it a vector logical left shift?
3574 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003575 X86::isZeroNode(Op.getOperand(0)) &&
3576 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003577 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003578 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003579 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003580 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003581 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003582 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003583
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003584 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003585 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003586
Chris Lattner19f79692008-03-08 22:59:52 +00003587 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3588 // is a non-constant being inserted into an element other than the low one,
3589 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3590 // movd/movss) to move this into the low element, then shuffle it into
3591 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003592 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003593 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003594
Evan Cheng0db9fe62006-04-25 20:13:52 +00003595 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003596 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3597 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003599 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 MaskVec.push_back(i == Idx ? 0 : 1);
3601 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003602 }
3603 }
3604
Chris Lattner67f453a2008-03-09 05:42:06 +00003605 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003606 if (Values.size() == 1) {
3607 if (EVTBits == 32) {
3608 // Instead of a shuffle like this:
3609 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3610 // Check if it's possible to issue this instead.
3611 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3612 unsigned Idx = CountTrailingZeros_32(NonZeros);
3613 SDValue Item = Op.getOperand(Idx);
3614 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3615 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3616 }
Dan Gohman475871a2008-07-27 21:46:04 +00003617 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003618 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003619
Dan Gohmana3941172007-07-24 22:55:08 +00003620 // A vector full of immediates; various special cases are already
3621 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003622 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003623 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003624
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003625 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003626 if (EVTBits == 64) {
3627 if (NumNonZero == 1) {
3628 // One half is zero or undef.
3629 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003630 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003631 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003632 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3633 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003634 }
Dan Gohman475871a2008-07-27 21:46:04 +00003635 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003636 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003637
3638 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003639 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003640 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003641 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003642 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003643 }
3644
Bill Wendling826f36f2007-03-28 00:57:11 +00003645 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003646 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003647 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003648 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003649 }
3650
3651 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003652 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003653 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003654 if (NumElems == 4 && NumZero > 0) {
3655 for (unsigned i = 0; i < 4; ++i) {
3656 bool isZero = !(NonZeros & (1 << i));
3657 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003658 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003659 else
Dale Johannesenace16102009-02-03 19:33:06 +00003660 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003661 }
3662
3663 for (unsigned i = 0; i < 2; ++i) {
3664 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3665 default: break;
3666 case 0:
3667 V[i] = V[i*2]; // Must be a zero vector.
3668 break;
3669 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003670 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003671 break;
3672 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003673 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 break;
3675 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003676 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003677 break;
3678 }
3679 }
3680
Nate Begeman9008ca62009-04-27 18:41:29 +00003681 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003682 bool Reverse = (NonZeros & 0x3) == 2;
3683 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003684 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003685 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3686 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003687 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3688 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003689 }
3690
3691 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003692 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3693 // values to be inserted is equal to the number of elements, in which case
3694 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003695 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003696 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003697 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 getSubtarget()->hasSSE41()) {
3699 V[0] = DAG.getUNDEF(VT);
3700 for (unsigned i = 0; i < NumElems; ++i)
3701 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3702 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3703 Op.getOperand(i), DAG.getIntPtrConstant(i));
3704 return V[0];
3705 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003706 // Expand into a number of unpckl*.
3707 // e.g. for v4f32
3708 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3709 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3710 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003711 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003712 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003713 NumElems >>= 1;
3714 while (NumElems != 0) {
3715 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003716 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003717 NumElems >>= 1;
3718 }
3719 return V[0];
3720 }
3721
Dan Gohman475871a2008-07-27 21:46:04 +00003722 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003723}
3724
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003725SDValue
3726X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3727 // We support concatenate two MMX registers and place them in a MMX
3728 // register. This is better than doing a stack convert.
3729 DebugLoc dl = Op.getDebugLoc();
3730 EVT ResVT = Op.getValueType();
3731 assert(Op.getNumOperands() == 2);
3732 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3733 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3734 int Mask[2];
3735 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3736 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3737 InVec = Op.getOperand(1);
3738 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3739 unsigned NumElts = ResVT.getVectorNumElements();
3740 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3741 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3742 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3743 } else {
3744 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3745 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3746 Mask[0] = 0; Mask[1] = 2;
3747 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3748 }
3749 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3750}
3751
Nate Begemanb9a47b82009-02-23 08:49:38 +00003752// v8i16 shuffles - Prefer shuffles in the following order:
3753// 1. [all] pshuflw, pshufhw, optional move
3754// 2. [ssse3] 1 x pshufb
3755// 3. [ssse3] 2 x pshufb + 1 x por
3756// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003757static
Nate Begeman9008ca62009-04-27 18:41:29 +00003758SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3759 SelectionDAG &DAG, X86TargetLowering &TLI) {
3760 SDValue V1 = SVOp->getOperand(0);
3761 SDValue V2 = SVOp->getOperand(1);
3762 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003763 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003764
Nate Begemanb9a47b82009-02-23 08:49:38 +00003765 // Determine if more than 1 of the words in each of the low and high quadwords
3766 // of the result come from the same quadword of one of the two inputs. Undef
3767 // mask values count as coming from any quadword, for better codegen.
3768 SmallVector<unsigned, 4> LoQuad(4);
3769 SmallVector<unsigned, 4> HiQuad(4);
3770 BitVector InputQuads(4);
3771 for (unsigned i = 0; i < 8; ++i) {
3772 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003774 MaskVals.push_back(EltIdx);
3775 if (EltIdx < 0) {
3776 ++Quad[0];
3777 ++Quad[1];
3778 ++Quad[2];
3779 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003780 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003781 }
3782 ++Quad[EltIdx / 4];
3783 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003784 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003785
Nate Begemanb9a47b82009-02-23 08:49:38 +00003786 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003787 unsigned MaxQuad = 1;
3788 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003789 if (LoQuad[i] > MaxQuad) {
3790 BestLoQuad = i;
3791 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003792 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003793 }
3794
Nate Begemanb9a47b82009-02-23 08:49:38 +00003795 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003796 MaxQuad = 1;
3797 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003798 if (HiQuad[i] > MaxQuad) {
3799 BestHiQuad = i;
3800 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003801 }
3802 }
3803
Nate Begemanb9a47b82009-02-23 08:49:38 +00003804 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003805 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003806 // single pshufb instruction is necessary. If There are more than 2 input
3807 // quads, disable the next transformation since it does not help SSSE3.
3808 bool V1Used = InputQuads[0] || InputQuads[1];
3809 bool V2Used = InputQuads[2] || InputQuads[3];
3810 if (TLI.getSubtarget()->hasSSSE3()) {
3811 if (InputQuads.count() == 2 && V1Used && V2Used) {
3812 BestLoQuad = InputQuads.find_first();
3813 BestHiQuad = InputQuads.find_next(BestLoQuad);
3814 }
3815 if (InputQuads.count() > 2) {
3816 BestLoQuad = -1;
3817 BestHiQuad = -1;
3818 }
3819 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003820
Nate Begemanb9a47b82009-02-23 08:49:38 +00003821 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3822 // the shuffle mask. If a quad is scored as -1, that means that it contains
3823 // words from all 4 input quadwords.
3824 SDValue NewV;
3825 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 SmallVector<int, 8> MaskV;
3827 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3828 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003829 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3831 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3832 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003833
Nate Begemanb9a47b82009-02-23 08:49:38 +00003834 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3835 // source words for the shuffle, to aid later transformations.
3836 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003837 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003838 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003839 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003840 if (idx != (int)i)
3841 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003842 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003843 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003844 AllWordsInNewV = false;
3845 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003846 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003847
Nate Begemanb9a47b82009-02-23 08:49:38 +00003848 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3849 if (AllWordsInNewV) {
3850 for (int i = 0; i != 8; ++i) {
3851 int idx = MaskVals[i];
3852 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003853 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003854 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003855 if ((idx != i) && idx < 4)
3856 pshufhw = false;
3857 if ((idx != i) && idx > 3)
3858 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003859 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003860 V1 = NewV;
3861 V2Used = false;
3862 BestLoQuad = 0;
3863 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003864 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003865
Nate Begemanb9a47b82009-02-23 08:49:38 +00003866 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3867 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003868 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003869 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003871 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003872 }
Eric Christopherfd179292009-08-27 18:07:15 +00003873
Nate Begemanb9a47b82009-02-23 08:49:38 +00003874 // If we have SSSE3, and all words of the result are from 1 input vector,
3875 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3876 // is present, fall back to case 4.
3877 if (TLI.getSubtarget()->hasSSSE3()) {
3878 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003879
Nate Begemanb9a47b82009-02-23 08:49:38 +00003880 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003881 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003882 // mask, and elements that come from V1 in the V2 mask, so that the two
3883 // results can be OR'd together.
3884 bool TwoInputs = V1Used && V2Used;
3885 for (unsigned i = 0; i != 8; ++i) {
3886 int EltIdx = MaskVals[i] * 2;
3887 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003888 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3889 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003890 continue;
3891 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003892 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3893 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003894 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003896 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003897 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003899 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003901
Nate Begemanb9a47b82009-02-23 08:49:38 +00003902 // Calculate the shuffle mask for the second input, shuffle it, and
3903 // OR it with the first shuffled input.
3904 pshufbMask.clear();
3905 for (unsigned i = 0; i != 8; ++i) {
3906 int EltIdx = MaskVals[i] * 2;
3907 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3909 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003910 continue;
3911 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3913 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003914 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003916 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003917 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 MVT::v16i8, &pshufbMask[0], 16));
3919 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3920 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003921 }
3922
3923 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3924 // and update MaskVals with new element order.
3925 BitVector InOrder(8);
3926 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003928 for (int i = 0; i != 4; ++i) {
3929 int idx = MaskVals[i];
3930 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003932 InOrder.set(i);
3933 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003934 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003935 InOrder.set(i);
3936 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003937 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003938 }
3939 }
3940 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003944 }
Eric Christopherfd179292009-08-27 18:07:15 +00003945
Nate Begemanb9a47b82009-02-23 08:49:38 +00003946 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3947 // and update MaskVals with the new element order.
3948 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003950 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003951 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003952 for (unsigned i = 4; i != 8; ++i) {
3953 int idx = MaskVals[i];
3954 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003955 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003956 InOrder.set(i);
3957 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003958 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003959 InOrder.set(i);
3960 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003962 }
3963 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003966 }
Eric Christopherfd179292009-08-27 18:07:15 +00003967
Nate Begemanb9a47b82009-02-23 08:49:38 +00003968 // In case BestHi & BestLo were both -1, which means each quadword has a word
3969 // from each of the four input quadwords, calculate the InOrder bitvector now
3970 // before falling through to the insert/extract cleanup.
3971 if (BestLoQuad == -1 && BestHiQuad == -1) {
3972 NewV = V1;
3973 for (int i = 0; i != 8; ++i)
3974 if (MaskVals[i] < 0 || MaskVals[i] == i)
3975 InOrder.set(i);
3976 }
Eric Christopherfd179292009-08-27 18:07:15 +00003977
Nate Begemanb9a47b82009-02-23 08:49:38 +00003978 // The other elements are put in the right place using pextrw and pinsrw.
3979 for (unsigned i = 0; i != 8; ++i) {
3980 if (InOrder[i])
3981 continue;
3982 int EltIdx = MaskVals[i];
3983 if (EltIdx < 0)
3984 continue;
3985 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00003986 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003987 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003989 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003990 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00003991 DAG.getIntPtrConstant(i));
3992 }
3993 return NewV;
3994}
3995
3996// v16i8 shuffles - Prefer shuffles in the following order:
3997// 1. [ssse3] 1 x pshufb
3998// 2. [ssse3] 2 x pshufb + 1 x por
3999// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4000static
Nate Begeman9008ca62009-04-27 18:41:29 +00004001SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4002 SelectionDAG &DAG, X86TargetLowering &TLI) {
4003 SDValue V1 = SVOp->getOperand(0);
4004 SDValue V2 = SVOp->getOperand(1);
4005 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004006 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004007 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004008
Nate Begemanb9a47b82009-02-23 08:49:38 +00004009 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004010 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004011 // present, fall back to case 3.
4012 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4013 bool V1Only = true;
4014 bool V2Only = true;
4015 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004016 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004017 if (EltIdx < 0)
4018 continue;
4019 if (EltIdx < 16)
4020 V2Only = false;
4021 else
4022 V1Only = false;
4023 }
Eric Christopherfd179292009-08-27 18:07:15 +00004024
Nate Begemanb9a47b82009-02-23 08:49:38 +00004025 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4026 if (TLI.getSubtarget()->hasSSSE3()) {
4027 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004028
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004030 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004031 //
4032 // Otherwise, we have elements from both input vectors, and must zero out
4033 // elements that come from V2 in the first mask, and V1 in the second mask
4034 // so that we can OR them together.
4035 bool TwoInputs = !(V1Only || V2Only);
4036 for (unsigned i = 0; i != 16; ++i) {
4037 int EltIdx = MaskVals[i];
4038 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004040 continue;
4041 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004043 }
4044 // If all the elements are from V2, assign it to V1 and return after
4045 // building the first pshufb.
4046 if (V2Only)
4047 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004049 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004051 if (!TwoInputs)
4052 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004053
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 // Calculate the shuffle mask for the second input, shuffle it, and
4055 // OR it with the first shuffled input.
4056 pshufbMask.clear();
4057 for (unsigned i = 0; i != 16; ++i) {
4058 int EltIdx = MaskVals[i];
4059 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004060 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004061 continue;
4062 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004064 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004066 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 MVT::v16i8, &pshufbMask[0], 16));
4068 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004069 }
Eric Christopherfd179292009-08-27 18:07:15 +00004070
Nate Begemanb9a47b82009-02-23 08:49:38 +00004071 // No SSSE3 - Calculate in place words and then fix all out of place words
4072 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4073 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004074 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4075 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004076 SDValue NewV = V2Only ? V2 : V1;
4077 for (int i = 0; i != 8; ++i) {
4078 int Elt0 = MaskVals[i*2];
4079 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004080
Nate Begemanb9a47b82009-02-23 08:49:38 +00004081 // This word of the result is all undef, skip it.
4082 if (Elt0 < 0 && Elt1 < 0)
4083 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004084
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 // This word of the result is already in the correct place, skip it.
4086 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4087 continue;
4088 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4089 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004090
Nate Begemanb9a47b82009-02-23 08:49:38 +00004091 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4092 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4093 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004094
4095 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4096 // using a single extract together, load it and store it.
4097 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004099 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004100 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004101 DAG.getIntPtrConstant(i));
4102 continue;
4103 }
4104
Nate Begemanb9a47b82009-02-23 08:49:38 +00004105 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004106 // source byte is not also odd, shift the extracted word left 8 bits
4107 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004108 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004110 DAG.getIntPtrConstant(Elt1 / 2));
4111 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004113 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004114 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4116 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004117 }
4118 // If Elt0 is defined, extract it from the appropriate source. If the
4119 // source byte is not also even, shift the extracted word right 8 bits. If
4120 // Elt1 was also defined, OR the extracted values together before
4121 // inserting them in the result.
4122 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004124 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4125 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004127 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004128 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4130 DAG.getConstant(0x00FF, MVT::i16));
4131 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004132 : InsElt0;
4133 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 DAG.getIntPtrConstant(i));
4136 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004138}
4139
Evan Cheng7a831ce2007-12-15 03:00:47 +00004140/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4141/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4142/// done when every pair / quad of shuffle mask elements point to elements in
4143/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004144/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4145static
Nate Begeman9008ca62009-04-27 18:41:29 +00004146SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4147 SelectionDAG &DAG,
4148 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004149 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004150 SDValue V1 = SVOp->getOperand(0);
4151 SDValue V2 = SVOp->getOperand(1);
4152 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004153 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004155 EVT MaskEltVT = MaskVT.getVectorElementType();
4156 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 case MVT::v4f32: NewVT = MVT::v2f64; break;
4160 case MVT::v4i32: NewVT = MVT::v2i64; break;
4161 case MVT::v8i16: NewVT = MVT::v4i32; break;
4162 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004163 }
4164
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004165 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004166 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004168 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004170 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004171 int Scale = NumElems / NewWidth;
4172 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004173 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 int StartIdx = -1;
4175 for (int j = 0; j < Scale; ++j) {
4176 int EltIdx = SVOp->getMaskElt(i+j);
4177 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004178 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004179 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004180 StartIdx = EltIdx - (EltIdx % Scale);
4181 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004182 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004183 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004184 if (StartIdx == -1)
4185 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004186 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004188 }
4189
Dale Johannesenace16102009-02-03 19:33:06 +00004190 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4191 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004193}
4194
Evan Chengd880b972008-05-09 21:53:03 +00004195/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004196///
Owen Andersone50ed302009-08-10 22:56:29 +00004197static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 SDValue SrcOp, SelectionDAG &DAG,
4199 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004201 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004202 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004203 LD = dyn_cast<LoadSDNode>(SrcOp);
4204 if (!LD) {
4205 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4206 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004207 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4208 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004209 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4210 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004211 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004212 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004214 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4215 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4216 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4217 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004218 SrcOp.getOperand(0)
4219 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004220 }
4221 }
4222 }
4223
Dale Johannesenace16102009-02-03 19:33:06 +00004224 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4225 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004226 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004227 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004228}
4229
Evan Chengace3c172008-07-22 21:13:36 +00004230/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4231/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004232static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004233LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4234 SDValue V1 = SVOp->getOperand(0);
4235 SDValue V2 = SVOp->getOperand(1);
4236 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004237 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Evan Chengace3c172008-07-22 21:13:36 +00004239 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004240 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 SmallVector<int, 8> Mask1(4U, -1);
4242 SmallVector<int, 8> PermMask;
4243 SVOp->getMask(PermMask);
4244
Evan Chengace3c172008-07-22 21:13:36 +00004245 unsigned NumHi = 0;
4246 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004247 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 int Idx = PermMask[i];
4249 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004250 Locs[i] = std::make_pair(-1, -1);
4251 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004252 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4253 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004254 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004256 NumLo++;
4257 } else {
4258 Locs[i] = std::make_pair(1, NumHi);
4259 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004260 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004261 NumHi++;
4262 }
4263 }
4264 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004265
Evan Chengace3c172008-07-22 21:13:36 +00004266 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004267 // If no more than two elements come from either vector. This can be
4268 // implemented with two shuffles. First shuffle gather the elements.
4269 // The second shuffle, which takes the first shuffle as both of its
4270 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004271 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004272
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004274
Evan Chengace3c172008-07-22 21:13:36 +00004275 for (unsigned i = 0; i != 4; ++i) {
4276 if (Locs[i].first == -1)
4277 continue;
4278 else {
4279 unsigned Idx = (i < 2) ? 0 : 4;
4280 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004281 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004282 }
4283 }
4284
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004286 } else if (NumLo == 3 || NumHi == 3) {
4287 // Otherwise, we must have three elements from one vector, call it X, and
4288 // one element from the other, call it Y. First, use a shufps to build an
4289 // intermediate vector with the one element from Y and the element from X
4290 // that will be in the same half in the final destination (the indexes don't
4291 // matter). Then, use a shufps to build the final vector, taking the half
4292 // containing the element from Y from the intermediate, and the other half
4293 // from X.
4294 if (NumHi == 3) {
4295 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004297 std::swap(V1, V2);
4298 }
4299
4300 // Find the element from V2.
4301 unsigned HiIndex;
4302 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004303 int Val = PermMask[HiIndex];
4304 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004305 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004306 if (Val >= 4)
4307 break;
4308 }
4309
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 Mask1[0] = PermMask[HiIndex];
4311 Mask1[1] = -1;
4312 Mask1[2] = PermMask[HiIndex^1];
4313 Mask1[3] = -1;
4314 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004315
4316 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 Mask1[0] = PermMask[0];
4318 Mask1[1] = PermMask[1];
4319 Mask1[2] = HiIndex & 1 ? 6 : 4;
4320 Mask1[3] = HiIndex & 1 ? 4 : 6;
4321 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004322 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004323 Mask1[0] = HiIndex & 1 ? 2 : 0;
4324 Mask1[1] = HiIndex & 1 ? 0 : 2;
4325 Mask1[2] = PermMask[2];
4326 Mask1[3] = PermMask[3];
4327 if (Mask1[2] >= 0)
4328 Mask1[2] += 4;
4329 if (Mask1[3] >= 0)
4330 Mask1[3] += 4;
4331 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004332 }
Evan Chengace3c172008-07-22 21:13:36 +00004333 }
4334
4335 // Break it into (shuffle shuffle_hi, shuffle_lo).
4336 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004337 SmallVector<int,8> LoMask(4U, -1);
4338 SmallVector<int,8> HiMask(4U, -1);
4339
4340 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004341 unsigned MaskIdx = 0;
4342 unsigned LoIdx = 0;
4343 unsigned HiIdx = 2;
4344 for (unsigned i = 0; i != 4; ++i) {
4345 if (i == 2) {
4346 MaskPtr = &HiMask;
4347 MaskIdx = 1;
4348 LoIdx = 0;
4349 HiIdx = 2;
4350 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004351 int Idx = PermMask[i];
4352 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004353 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004355 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004356 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004357 LoIdx++;
4358 } else {
4359 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004361 HiIdx++;
4362 }
4363 }
4364
Nate Begeman9008ca62009-04-27 18:41:29 +00004365 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4366 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4367 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004368 for (unsigned i = 0; i != 4; ++i) {
4369 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004370 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004371 } else {
4372 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004373 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004374 }
4375 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004377}
4378
Dan Gohman475871a2008-07-27 21:46:04 +00004379SDValue
4380X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004382 SDValue V1 = Op.getOperand(0);
4383 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004384 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004385 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004387 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4389 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004390 bool V1IsSplat = false;
4391 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004392
Nate Begeman9008ca62009-04-27 18:41:29 +00004393 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004394 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004395
Nate Begeman9008ca62009-04-27 18:41:29 +00004396 // Promote splats to v4f32.
4397 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004398 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004399 return Op;
4400 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401 }
4402
Evan Cheng7a831ce2007-12-15 03:00:47 +00004403 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4404 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004405 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004407 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004408 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004409 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004411 // FIXME: Figure out a cleaner way to do this.
4412 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004413 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004414 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004415 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4417 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4418 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004419 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004420 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4422 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004423 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004425 }
4426 }
Eric Christopherfd179292009-08-27 18:07:15 +00004427
Nate Begeman9008ca62009-04-27 18:41:29 +00004428 if (X86::isPSHUFDMask(SVOp))
4429 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004430
Evan Chengf26ffe92008-05-29 08:22:04 +00004431 // Check if this can be converted into a logical shift.
4432 bool isLeft = false;
4433 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004436 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004437 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004438 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004439 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004440 EVT EltVT = VT.getVectorElementType();
4441 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004442 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004443 }
Eric Christopherfd179292009-08-27 18:07:15 +00004444
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004446 if (V1IsUndef)
4447 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004448 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004449 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004450 if (!isMMX)
4451 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004452 }
Eric Christopherfd179292009-08-27 18:07:15 +00004453
Nate Begeman9008ca62009-04-27 18:41:29 +00004454 // FIXME: fold these into legal mask.
4455 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4456 X86::isMOVSLDUPMask(SVOp) ||
4457 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004458 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004460 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004461
Nate Begeman9008ca62009-04-27 18:41:29 +00004462 if (ShouldXformToMOVHLPS(SVOp) ||
4463 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4464 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004465
Evan Chengf26ffe92008-05-29 08:22:04 +00004466 if (isShift) {
4467 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004468 EVT EltVT = VT.getVectorElementType();
4469 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004470 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004471 }
Eric Christopherfd179292009-08-27 18:07:15 +00004472
Evan Cheng9eca5e82006-10-25 21:49:50 +00004473 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004474 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4475 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004476 V1IsSplat = isSplatVector(V1.getNode());
4477 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004478
Chris Lattner8a594482007-11-25 00:24:49 +00004479 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004480 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 Op = CommuteVectorShuffle(SVOp, DAG);
4482 SVOp = cast<ShuffleVectorSDNode>(Op);
4483 V1 = SVOp->getOperand(0);
4484 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004485 std::swap(V1IsSplat, V2IsSplat);
4486 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004487 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004488 }
4489
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4491 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004492 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 return V1;
4494 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4495 // the instruction selector will not match, so get a canonical MOVL with
4496 // swapped operands to undo the commute.
4497 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004499
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4501 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4502 X86::isUNPCKLMask(SVOp) ||
4503 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004504 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004505
Evan Cheng9bbbb982006-10-25 20:48:19 +00004506 if (V2IsSplat) {
4507 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004508 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004509 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004510 SDValue NewMask = NormalizeMask(SVOp, DAG);
4511 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4512 if (NSVOp != SVOp) {
4513 if (X86::isUNPCKLMask(NSVOp, true)) {
4514 return NewMask;
4515 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4516 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517 }
4518 }
4519 }
4520
Evan Cheng9eca5e82006-10-25 21:49:50 +00004521 if (Commuted) {
4522 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 // FIXME: this seems wrong.
4524 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4525 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4526 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4527 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4528 X86::isUNPCKLMask(NewSVOp) ||
4529 X86::isUNPCKHMask(NewSVOp))
4530 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004531 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004532
Nate Begemanb9a47b82009-02-23 08:49:38 +00004533 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004534
4535 // Normalize the node to match x86 shuffle ops if needed
4536 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4537 return CommuteVectorShuffle(SVOp, DAG);
4538
4539 // Check for legal shuffle and return?
4540 SmallVector<int, 16> PermMask;
4541 SVOp->getMask(PermMask);
4542 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004543 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004544
Evan Cheng14b32e12007-12-11 01:46:18 +00004545 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004546 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004547 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004548 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004549 return NewOp;
4550 }
4551
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004554 if (NewOp.getNode())
4555 return NewOp;
4556 }
Eric Christopherfd179292009-08-27 18:07:15 +00004557
Evan Chengace3c172008-07-22 21:13:36 +00004558 // Handle all 4 wide cases with a number of shuffles except for MMX.
4559 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004560 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561
Dan Gohman475871a2008-07-27 21:46:04 +00004562 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563}
4564
Dan Gohman475871a2008-07-27 21:46:04 +00004565SDValue
4566X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004567 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004568 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004569 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004570 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004572 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004573 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004574 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004575 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004576 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004577 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4578 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4579 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4581 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004582 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004583 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004584 Op.getOperand(0)),
4585 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004586 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004587 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004588 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004589 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004590 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004591 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004592 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4593 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004594 // result has a single use which is a store or a bitcast to i32. And in
4595 // the case of a store, it's not worth it if the index is a constant 0,
4596 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004597 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004598 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004599 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004600 if ((User->getOpcode() != ISD::STORE ||
4601 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4602 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004603 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004604 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004605 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004606 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4607 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004608 Op.getOperand(0)),
4609 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004610 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4611 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004612 // ExtractPS works with constant index.
4613 if (isa<ConstantSDNode>(Op.getOperand(1)))
4614 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004615 }
Dan Gohman475871a2008-07-27 21:46:04 +00004616 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004617}
4618
4619
Dan Gohman475871a2008-07-27 21:46:04 +00004620SDValue
4621X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004622 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004623 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004624
Evan Cheng62a3f152008-03-24 21:52:23 +00004625 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004626 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004627 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004628 return Res;
4629 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004630
Owen Andersone50ed302009-08-10 22:56:29 +00004631 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004632 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004634 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004635 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004636 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004637 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004638 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4639 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004640 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004641 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004642 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004643 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004644 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004645 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004647 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004648 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004649 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004650 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004651 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 if (Idx == 0)
4653 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004654
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004657 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004658 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004659 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004660 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004661 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004662 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004663 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4664 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4665 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004666 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 if (Idx == 0)
4668 return Op;
4669
4670 // UNPCKHPD the element to the lowest double word, then movsd.
4671 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4672 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004673 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004674 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004675 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004676 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004677 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004678 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004679 }
4680
Dan Gohman475871a2008-07-27 21:46:04 +00004681 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004682}
4683
Dan Gohman475871a2008-07-27 21:46:04 +00004684SDValue
4685X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004686 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004687 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004688 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004689
Dan Gohman475871a2008-07-27 21:46:04 +00004690 SDValue N0 = Op.getOperand(0);
4691 SDValue N1 = Op.getOperand(1);
4692 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004693
Dan Gohman8a55ce42009-09-23 21:02:20 +00004694 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004695 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004696 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4697 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004698 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4699 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004700 if (N1.getValueType() != MVT::i32)
4701 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4702 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004703 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004704 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004705 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004706 // Bits [7:6] of the constant are the source select. This will always be
4707 // zero here. The DAG Combiner may combine an extract_elt index into these
4708 // bits. For example (insert (extract, 3), 2) could be matched by putting
4709 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004710 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004711 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004712 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004713 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004714 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004715 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004717 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004718 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004719 // PINSR* works with constant index.
4720 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004721 }
Dan Gohman475871a2008-07-27 21:46:04 +00004722 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004723}
4724
Dan Gohman475871a2008-07-27 21:46:04 +00004725SDValue
4726X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004727 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004728 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004729
4730 if (Subtarget->hasSSE41())
4731 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4732
Dan Gohman8a55ce42009-09-23 21:02:20 +00004733 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004734 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004735
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004736 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004737 SDValue N0 = Op.getOperand(0);
4738 SDValue N1 = Op.getOperand(1);
4739 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004740
Dan Gohman8a55ce42009-09-23 21:02:20 +00004741 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004742 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4743 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004744 if (N1.getValueType() != MVT::i32)
4745 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4746 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004747 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004748 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004749 }
Dan Gohman475871a2008-07-27 21:46:04 +00004750 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004751}
4752
Dan Gohman475871a2008-07-27 21:46:04 +00004753SDValue
4754X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004755 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004756 if (Op.getValueType() == MVT::v2f32)
4757 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4758 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4759 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004760 Op.getOperand(0))));
4761
Owen Anderson825b72b2009-08-11 20:47:22 +00004762 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4763 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004764
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4766 EVT VT = MVT::v2i32;
4767 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004768 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 case MVT::v16i8:
4770 case MVT::v8i16:
4771 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004772 break;
4773 }
Dale Johannesenace16102009-02-03 19:33:06 +00004774 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4775 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004776}
4777
Bill Wendling056292f2008-09-16 21:48:12 +00004778// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4779// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4780// one of the above mentioned nodes. It has to be wrapped because otherwise
4781// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4782// be used to form addressing mode. These wrapped nodes will be selected
4783// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004784SDValue
4785X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004786 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004787
Chris Lattner41621a22009-06-26 19:22:52 +00004788 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4789 // global base reg.
4790 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004791 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004792 CodeModel::Model M = getTargetMachine().getCodeModel();
4793
Chris Lattner4f066492009-07-11 20:29:19 +00004794 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004795 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004796 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004797 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004798 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004799 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004800 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004801
Evan Cheng1606e8e2009-03-13 07:51:59 +00004802 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004803 CP->getAlignment(),
4804 CP->getOffset(), OpFlag);
4805 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004806 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004807 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004808 if (OpFlag) {
4809 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004810 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004811 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004812 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004813 }
4814
4815 return Result;
4816}
4817
Chris Lattner18c59872009-06-27 04:16:01 +00004818SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4819 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004820
Chris Lattner18c59872009-06-27 04:16:01 +00004821 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4822 // global base reg.
4823 unsigned char OpFlag = 0;
4824 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004825 CodeModel::Model M = getTargetMachine().getCodeModel();
4826
Chris Lattner4f066492009-07-11 20:29:19 +00004827 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004828 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004829 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004830 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004831 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004832 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004833 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004834
Chris Lattner18c59872009-06-27 04:16:01 +00004835 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4836 OpFlag);
4837 DebugLoc DL = JT->getDebugLoc();
4838 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004839
Chris Lattner18c59872009-06-27 04:16:01 +00004840 // With PIC, the address is actually $g + Offset.
4841 if (OpFlag) {
4842 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4843 DAG.getNode(X86ISD::GlobalBaseReg,
4844 DebugLoc::getUnknownLoc(), getPointerTy()),
4845 Result);
4846 }
Eric Christopherfd179292009-08-27 18:07:15 +00004847
Chris Lattner18c59872009-06-27 04:16:01 +00004848 return Result;
4849}
4850
4851SDValue
4852X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4853 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004854
Chris Lattner18c59872009-06-27 04:16:01 +00004855 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4856 // global base reg.
4857 unsigned char OpFlag = 0;
4858 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004859 CodeModel::Model M = getTargetMachine().getCodeModel();
4860
Chris Lattner4f066492009-07-11 20:29:19 +00004861 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004862 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004863 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004864 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004865 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004866 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004867 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004868
Chris Lattner18c59872009-06-27 04:16:01 +00004869 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004870
Chris Lattner18c59872009-06-27 04:16:01 +00004871 DebugLoc DL = Op.getDebugLoc();
4872 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004873
4874
Chris Lattner18c59872009-06-27 04:16:01 +00004875 // With PIC, the address is actually $g + Offset.
4876 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004877 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004878 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4879 DAG.getNode(X86ISD::GlobalBaseReg,
4880 DebugLoc::getUnknownLoc(),
4881 getPointerTy()),
4882 Result);
4883 }
Eric Christopherfd179292009-08-27 18:07:15 +00004884
Chris Lattner18c59872009-06-27 04:16:01 +00004885 return Result;
4886}
4887
Dan Gohman475871a2008-07-27 21:46:04 +00004888SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004889X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004890 // Create the TargetBlockAddressAddress node.
4891 unsigned char OpFlags =
4892 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004893 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004894 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4895 DebugLoc dl = Op.getDebugLoc();
4896 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4897 /*isTarget=*/true, OpFlags);
4898
Dan Gohmanf705adb2009-10-30 01:28:02 +00004899 if (Subtarget->isPICStyleRIPRel() &&
4900 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004901 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4902 else
4903 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004904
Dan Gohman29cbade2009-11-20 23:18:13 +00004905 // With PIC, the address is actually $g + Offset.
4906 if (isGlobalRelativeToPICBase(OpFlags)) {
4907 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4908 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4909 Result);
4910 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004911
4912 return Result;
4913}
4914
4915SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004916X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004917 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004918 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004919 // Create the TargetGlobalAddress node, folding in the constant
4920 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004921 unsigned char OpFlags =
4922 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004923 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004924 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004925 if (OpFlags == X86II::MO_NO_FLAG &&
4926 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004927 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004928 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004929 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004930 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004931 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004932 }
Eric Christopherfd179292009-08-27 18:07:15 +00004933
Chris Lattner4f066492009-07-11 20:29:19 +00004934 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004935 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004936 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4937 else
4938 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004939
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004940 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004941 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004942 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4943 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004944 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004946
Chris Lattner36c25012009-07-10 07:34:39 +00004947 // For globals that require a load from a stub to get the address, emit the
4948 // load.
4949 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004950 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004951 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004952
Dan Gohman6520e202008-10-18 02:06:02 +00004953 // If there was a non-zero offset that we didn't fold, create an explicit
4954 // addition for it.
4955 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004956 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004957 DAG.getConstant(Offset, getPointerTy()));
4958
Evan Cheng0db9fe62006-04-25 20:13:52 +00004959 return Result;
4960}
4961
Evan Chengda43bcf2008-09-24 00:05:32 +00004962SDValue
4963X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4964 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004965 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004966 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004967}
4968
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004969static SDValue
4970GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004971 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004972 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004973 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004974 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004975 DebugLoc dl = GA->getDebugLoc();
4976 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4977 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004978 GA->getOffset(),
4979 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004980 if (InFlag) {
4981 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004982 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004983 } else {
4984 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004985 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004986 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004987
4988 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
4989 MFI->setHasCalls(true);
4990
Rafael Espindola15f1b662009-04-24 12:59:40 +00004991 SDValue Flag = Chain.getValue(1);
4992 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004993}
4994
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004995// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004996static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004997LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00004998 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004999 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005000 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5001 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005002 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005003 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005004 PtrVT), InFlag);
5005 InFlag = Chain.getValue(1);
5006
Chris Lattnerb903bed2009-06-26 21:20:29 +00005007 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005008}
5009
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005010// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005011static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005012LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005013 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005014 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5015 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005016}
5017
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005018// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5019// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005020static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005021 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005022 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005023 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005024 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005025 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5026 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005027 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005028 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005029
5030 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5031 NULL, 0);
5032
Chris Lattnerb903bed2009-06-26 21:20:29 +00005033 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005034 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5035 // initialexec.
5036 unsigned WrapperKind = X86ISD::Wrapper;
5037 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005038 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005039 } else if (is64Bit) {
5040 assert(model == TLSModel::InitialExec);
5041 OperandFlags = X86II::MO_GOTTPOFF;
5042 WrapperKind = X86ISD::WrapperRIP;
5043 } else {
5044 assert(model == TLSModel::InitialExec);
5045 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005046 }
Eric Christopherfd179292009-08-27 18:07:15 +00005047
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005048 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5049 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005050 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005051 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005052 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005053
Rafael Espindola9a580232009-02-27 13:37:18 +00005054 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005055 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005056 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005057
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005058 // The address of the thread local variable is the add of the thread
5059 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005060 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005061}
5062
Dan Gohman475871a2008-07-27 21:46:04 +00005063SDValue
5064X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005065 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005066 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005067 assert(Subtarget->isTargetELF() &&
5068 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005069 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005070 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005071
Chris Lattnerb903bed2009-06-26 21:20:29 +00005072 // If GV is an alias then use the aliasee for determining
5073 // thread-localness.
5074 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5075 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005076
Chris Lattnerb903bed2009-06-26 21:20:29 +00005077 TLSModel::Model model = getTLSModel(GV,
5078 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005079
Chris Lattnerb903bed2009-06-26 21:20:29 +00005080 switch (model) {
5081 case TLSModel::GeneralDynamic:
5082 case TLSModel::LocalDynamic: // not implemented
5083 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005084 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005085 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005086
Chris Lattnerb903bed2009-06-26 21:20:29 +00005087 case TLSModel::InitialExec:
5088 case TLSModel::LocalExec:
5089 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5090 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005091 }
Eric Christopherfd179292009-08-27 18:07:15 +00005092
Torok Edwinc23197a2009-07-14 16:55:14 +00005093 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005094 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005095}
5096
Evan Cheng0db9fe62006-04-25 20:13:52 +00005097
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005098/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005099/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005100SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005101 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005102 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005103 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005104 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005105 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005106 SDValue ShOpLo = Op.getOperand(0);
5107 SDValue ShOpHi = Op.getOperand(1);
5108 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005109 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005111 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005112
Dan Gohman475871a2008-07-27 21:46:04 +00005113 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005114 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005115 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5116 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005117 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005118 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5119 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005120 }
Evan Chenge3413162006-01-09 18:33:28 +00005121
Owen Anderson825b72b2009-08-11 20:47:22 +00005122 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5123 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005124 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005125 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005126
Dan Gohman475871a2008-07-27 21:46:04 +00005127 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005128 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005129 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5130 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005131
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005132 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005133 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5134 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005135 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005136 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5137 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005138 }
5139
Dan Gohman475871a2008-07-27 21:46:04 +00005140 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005141 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142}
Evan Chenga3195e82006-01-12 22:54:21 +00005143
Dan Gohman475871a2008-07-27 21:46:04 +00005144SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005145 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005146
5147 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005149 return Op;
5150 }
5151 return SDValue();
5152 }
5153
Owen Anderson825b72b2009-08-11 20:47:22 +00005154 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005155 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005156
Eli Friedman36df4992009-05-27 00:47:34 +00005157 // These are really Legal; return the operand so the caller accepts it as
5158 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005159 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005160 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005161 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005162 Subtarget->is64Bit()) {
5163 return Op;
5164 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005165
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005166 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005167 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005168 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005169 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005170 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005171 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005172 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005173 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005174 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5175}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005176
Owen Andersone50ed302009-08-10 22:56:29 +00005177SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005178 SDValue StackSlot,
5179 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005180 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005181 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005182 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005183 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005184 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005185 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005186 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005187 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005188 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005189 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005190 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005192 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005193 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005194 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005195
5196 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5197 // shouldn't be necessary except that RFP cannot be live across
5198 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005199 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005200 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005201 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005202 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005203 SDValue Ops[] = {
5204 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5205 };
5206 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005207 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005208 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005209 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005210
Evan Cheng0db9fe62006-04-25 20:13:52 +00005211 return Result;
5212}
5213
Bill Wendling8b8a6362009-01-17 03:56:04 +00005214// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5215SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5216 // This algorithm is not obvious. Here it is in C code, more or less:
5217 /*
5218 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5219 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5220 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005221
Bill Wendling8b8a6362009-01-17 03:56:04 +00005222 // Copy ints to xmm registers.
5223 __m128i xh = _mm_cvtsi32_si128( hi );
5224 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005225
Bill Wendling8b8a6362009-01-17 03:56:04 +00005226 // Combine into low half of a single xmm register.
5227 __m128i x = _mm_unpacklo_epi32( xh, xl );
5228 __m128d d;
5229 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005230
Bill Wendling8b8a6362009-01-17 03:56:04 +00005231 // Merge in appropriate exponents to give the integer bits the right
5232 // magnitude.
5233 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005234
Bill Wendling8b8a6362009-01-17 03:56:04 +00005235 // Subtract away the biases to deal with the IEEE-754 double precision
5236 // implicit 1.
5237 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005238
Bill Wendling8b8a6362009-01-17 03:56:04 +00005239 // All conversions up to here are exact. The correctly rounded result is
5240 // calculated using the current rounding mode using the following
5241 // horizontal add.
5242 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5243 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5244 // store doesn't really need to be here (except
5245 // maybe to zero the other double)
5246 return sd;
5247 }
5248 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005249
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005250 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005251 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005252
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005253 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005254 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005255 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5256 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5257 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5258 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005259 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005260 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005261
Bill Wendling8b8a6362009-01-17 03:56:04 +00005262 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005263 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005264 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005265 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005266 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005267 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005268 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005269
Owen Anderson825b72b2009-08-11 20:47:22 +00005270 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5271 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005272 Op.getOperand(0),
5273 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005274 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5275 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005276 Op.getOperand(0),
5277 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005278 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5279 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005280 PseudoSourceValue::getConstantPool(), 0,
5281 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005282 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5283 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5284 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005285 PseudoSourceValue::getConstantPool(), 0,
5286 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005288
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005289 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005290 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005291 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5292 DAG.getUNDEF(MVT::v2f64), ShufMask);
5293 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5294 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005295 DAG.getIntPtrConstant(0));
5296}
5297
Bill Wendling8b8a6362009-01-17 03:56:04 +00005298// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5299SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005300 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005301 // FP constant to bias correct the final result.
5302 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005304
5305 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005306 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5307 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005308 Op.getOperand(0),
5309 DAG.getIntPtrConstant(0)));
5310
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5312 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005313 DAG.getIntPtrConstant(0));
5314
5315 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005316 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5317 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005318 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 MVT::v2f64, Load)),
5320 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005321 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005322 MVT::v2f64, Bias)));
5323 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5324 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005325 DAG.getIntPtrConstant(0));
5326
5327 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005328 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005329
5330 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005331 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005332
Owen Anderson825b72b2009-08-11 20:47:22 +00005333 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005334 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005335 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005336 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005337 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005338 }
5339
5340 // Handle final rounding.
5341 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005342}
5343
5344SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005345 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005346 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005347
Evan Chenga06ec9e2009-01-19 08:08:22 +00005348 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5349 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5350 // the optimization here.
5351 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005352 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005353
Owen Andersone50ed302009-08-10 22:56:29 +00005354 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005355 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005356 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005357 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005358 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005359
Bill Wendling8b8a6362009-01-17 03:56:04 +00005360 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005362 return LowerUINT_TO_FP_i32(Op, DAG);
5363 }
5364
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005366
5367 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005368 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005369 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5370 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5371 getPointerTy(), StackSlot, WordOff);
5372 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5373 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005374 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005375 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005376 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005377}
5378
Dan Gohman475871a2008-07-27 21:46:04 +00005379std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005380FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005381 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005382
Owen Andersone50ed302009-08-10 22:56:29 +00005383 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005384
5385 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005386 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5387 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005388 }
5389
Owen Anderson825b72b2009-08-11 20:47:22 +00005390 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5391 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005392 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005393
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005394 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005395 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005396 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005397 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005398 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005399 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005400 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005401 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005402
Evan Cheng87c89352007-10-15 20:11:21 +00005403 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5404 // stack slot.
5405 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005406 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005407 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005409
Evan Cheng0db9fe62006-04-25 20:13:52 +00005410 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005412 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5414 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5415 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005416 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005417
Dan Gohman475871a2008-07-27 21:46:04 +00005418 SDValue Chain = DAG.getEntryNode();
5419 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005420 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005421 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005422 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005423 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005424 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005425 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005426 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5427 };
Dale Johannesenace16102009-02-03 19:33:06 +00005428 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005429 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005430 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005431 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5432 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005433
Evan Cheng0db9fe62006-04-25 20:13:52 +00005434 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005435 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005436 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005437
Chris Lattner27a6c732007-11-24 07:07:01 +00005438 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439}
5440
Dan Gohman475871a2008-07-27 21:46:04 +00005441SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005442 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005443 if (Op.getValueType() == MVT::v2i32 &&
5444 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005445 return Op;
5446 }
5447 return SDValue();
5448 }
5449
Eli Friedman948e95a2009-05-23 09:59:16 +00005450 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005451 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005452 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5453 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005454
Chris Lattner27a6c732007-11-24 07:07:01 +00005455 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005456 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005457 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005458}
5459
Eli Friedman948e95a2009-05-23 09:59:16 +00005460SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5461 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5462 SDValue FIST = Vals.first, StackSlot = Vals.second;
5463 assert(FIST.getNode() && "Unexpected failure");
5464
5465 // Load the result.
5466 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5467 FIST, StackSlot, NULL, 0);
5468}
5469
Dan Gohman475871a2008-07-27 21:46:04 +00005470SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005471 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005472 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005473 EVT VT = Op.getValueType();
5474 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005475 if (VT.isVector())
5476 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005477 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005478 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005479 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005480 CV.push_back(C);
5481 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005482 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005483 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005484 CV.push_back(C);
5485 CV.push_back(C);
5486 CV.push_back(C);
5487 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005489 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005490 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005491 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005492 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005493 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005494 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005495}
5496
Dan Gohman475871a2008-07-27 21:46:04 +00005497SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005498 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005499 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005500 EVT VT = Op.getValueType();
5501 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005502 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005503 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005506 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005507 CV.push_back(C);
5508 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005509 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005510 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005511 CV.push_back(C);
5512 CV.push_back(C);
5513 CV.push_back(C);
5514 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005515 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005516 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005517 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005518 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005519 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005520 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005521 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005522 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005523 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5524 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005525 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005526 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005527 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005528 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005529 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005530}
5531
Dan Gohman475871a2008-07-27 21:46:04 +00005532SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005533 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue Op0 = Op.getOperand(0);
5535 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005536 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005537 EVT VT = Op.getValueType();
5538 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005539
5540 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005541 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005542 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005543 SrcVT = VT;
5544 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005545 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005546 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005547 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005548 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005549 }
5550
5551 // At this point the operands and the result should have the same
5552 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005553
Evan Cheng68c47cb2007-01-05 07:55:56 +00005554 // First get the sign bit of second operand.
5555 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005557 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5558 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005559 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005560 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5561 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5562 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5563 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005564 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005565 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005566 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005567 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005568 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005569 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005570 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005571
5572 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005573 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005574 // Op0 is MVT::f32, Op1 is MVT::f64.
5575 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5576 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5577 DAG.getConstant(32, MVT::i32));
5578 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5579 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005580 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005581 }
5582
Evan Cheng73d6cf12007-01-05 21:37:56 +00005583 // Clear first operand sign bit.
5584 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005586 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5587 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005588 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005589 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5590 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5591 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5592 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005593 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005594 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005595 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005596 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005597 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005598 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005599 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005600
5601 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005602 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005603}
5604
Dan Gohman076aee32009-03-04 19:44:21 +00005605/// Emit nodes that will be selected as "test Op0,Op0", or something
5606/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005607SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5608 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005609 DebugLoc dl = Op.getDebugLoc();
5610
Dan Gohman31125812009-03-07 01:58:32 +00005611 // CF and OF aren't always set the way we want. Determine which
5612 // of these we need.
5613 bool NeedCF = false;
5614 bool NeedOF = false;
5615 switch (X86CC) {
5616 case X86::COND_A: case X86::COND_AE:
5617 case X86::COND_B: case X86::COND_BE:
5618 NeedCF = true;
5619 break;
5620 case X86::COND_G: case X86::COND_GE:
5621 case X86::COND_L: case X86::COND_LE:
5622 case X86::COND_O: case X86::COND_NO:
5623 NeedOF = true;
5624 break;
5625 default: break;
5626 }
5627
Dan Gohman076aee32009-03-04 19:44:21 +00005628 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005629 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5630 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5631 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005632 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005633 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005634 switch (Op.getNode()->getOpcode()) {
5635 case ISD::ADD:
5636 // Due to an isel shortcoming, be conservative if this add is likely to
5637 // be selected as part of a load-modify-store instruction. When the root
5638 // node in a match is a store, isel doesn't know how to remap non-chain
5639 // non-flag uses of other nodes in the match, such as the ADD in this
5640 // case. This leads to the ADD being left around and reselected, with
5641 // the result being two adds in the output.
5642 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5643 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5644 if (UI->getOpcode() == ISD::STORE)
5645 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005646 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005647 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5648 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005649 if (C->getAPIntValue() == 1) {
5650 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005651 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005652 break;
5653 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005654 // An add of negative one (subtract of one) will be selected as a DEC.
5655 if (C->getAPIntValue().isAllOnesValue()) {
5656 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005657 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005658 break;
5659 }
5660 }
Dan Gohman076aee32009-03-04 19:44:21 +00005661 // Otherwise use a regular EFLAGS-setting add.
5662 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005663 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005664 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005665 case ISD::AND: {
5666 // If the primary and result isn't used, don't bother using X86ISD::AND,
5667 // because a TEST instruction will be better.
5668 bool NonFlagUse = false;
5669 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005670 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5671 SDNode *User = *UI;
5672 unsigned UOpNo = UI.getOperandNo();
5673 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5674 // Look pass truncate.
5675 UOpNo = User->use_begin().getOperandNo();
5676 User = *User->use_begin();
5677 }
5678 if (User->getOpcode() != ISD::BRCOND &&
5679 User->getOpcode() != ISD::SETCC &&
5680 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005681 NonFlagUse = true;
5682 break;
5683 }
Evan Cheng17751da2010-01-07 00:54:06 +00005684 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005685 if (!NonFlagUse)
5686 break;
5687 }
5688 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005689 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005690 case ISD::OR:
5691 case ISD::XOR:
5692 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005693 // likely to be selected as part of a load-modify-store instruction.
5694 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5695 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5696 if (UI->getOpcode() == ISD::STORE)
5697 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005698 // Otherwise use a regular EFLAGS-setting instruction.
5699 switch (Op.getNode()->getOpcode()) {
5700 case ISD::SUB: Opcode = X86ISD::SUB; break;
5701 case ISD::OR: Opcode = X86ISD::OR; break;
5702 case ISD::XOR: Opcode = X86ISD::XOR; break;
5703 case ISD::AND: Opcode = X86ISD::AND; break;
5704 default: llvm_unreachable("unexpected operator!");
5705 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005706 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005707 break;
5708 case X86ISD::ADD:
5709 case X86ISD::SUB:
5710 case X86ISD::INC:
5711 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005712 case X86ISD::OR:
5713 case X86ISD::XOR:
5714 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005715 return SDValue(Op.getNode(), 1);
5716 default:
5717 default_case:
5718 break;
5719 }
5720 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005721 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005722 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005723 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005724 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005725 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005726 DAG.ReplaceAllUsesWith(Op, New);
5727 return SDValue(New.getNode(), 1);
5728 }
5729 }
5730
5731 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005732 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005733 DAG.getConstant(0, Op.getValueType()));
5734}
5735
5736/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5737/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005738SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5739 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005740 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5741 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005742 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005743
5744 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005745 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005746}
5747
Evan Chengd40d03e2010-01-06 19:38:29 +00005748/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5749/// if it's possible.
5750static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005751 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005752 SDValue LHS, RHS;
5753 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5754 if (ConstantSDNode *Op010C =
5755 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5756 if (Op010C->getZExtValue() == 1) {
5757 LHS = Op0.getOperand(0);
5758 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005759 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005760 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5761 if (ConstantSDNode *Op000C =
5762 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5763 if (Op000C->getZExtValue() == 1) {
5764 LHS = Op0.getOperand(1);
5765 RHS = Op0.getOperand(0).getOperand(1);
5766 }
5767 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5768 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5769 SDValue AndLHS = Op0.getOperand(0);
5770 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5771 LHS = AndLHS.getOperand(0);
5772 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005773 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005774 }
Evan Cheng0488db92007-09-25 01:57:46 +00005775
Evan Chengd40d03e2010-01-06 19:38:29 +00005776 if (LHS.getNode()) {
5777 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5778 // instruction. Since the shift amount is in-range-or-undefined, we know
5779 // that doing a bittest on the i16 value is ok. We extend to i32 because
5780 // the encoding for the i16 version is larger than the i32 version.
5781 if (LHS.getValueType() == MVT::i8)
5782 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005783
Evan Chengd40d03e2010-01-06 19:38:29 +00005784 // If the operand types disagree, extend the shift amount to match. Since
5785 // BT ignores high bits (like shifts) we can use anyextend.
5786 if (LHS.getValueType() != RHS.getValueType())
5787 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005788
Evan Chengd40d03e2010-01-06 19:38:29 +00005789 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5790 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5791 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5792 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005793 }
5794
Evan Cheng54de3ea2010-01-05 06:52:31 +00005795 return SDValue();
5796}
5797
5798SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5799 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5800 SDValue Op0 = Op.getOperand(0);
5801 SDValue Op1 = Op.getOperand(1);
5802 DebugLoc dl = Op.getDebugLoc();
5803 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5804
5805 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005806 // Lower (X & (1 << N)) == 0 to BT(X, N).
5807 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5808 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5809 if (Op0.getOpcode() == ISD::AND &&
5810 Op0.hasOneUse() &&
5811 Op1.getOpcode() == ISD::Constant &&
5812 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5813 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5814 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5815 if (NewSetCC.getNode())
5816 return NewSetCC;
5817 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005818
Chris Lattnere55484e2008-12-25 05:34:37 +00005819 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5820 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005821 if (X86CC == X86::COND_INVALID)
5822 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005823
Dan Gohman31125812009-03-07 01:58:32 +00005824 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005825
5826 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005827 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005828 return DAG.getNode(ISD::AND, dl, MVT::i8,
5829 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5830 DAG.getConstant(X86CC, MVT::i8), Cond),
5831 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005832
Owen Anderson825b72b2009-08-11 20:47:22 +00005833 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5834 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005835}
5836
Dan Gohman475871a2008-07-27 21:46:04 +00005837SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5838 SDValue Cond;
5839 SDValue Op0 = Op.getOperand(0);
5840 SDValue Op1 = Op.getOperand(1);
5841 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005842 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005843 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5844 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005845 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005846
5847 if (isFP) {
5848 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005849 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005850 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5851 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005852 bool Swap = false;
5853
5854 switch (SetCCOpcode) {
5855 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005856 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005857 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005858 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005859 case ISD::SETGT: Swap = true; // Fallthrough
5860 case ISD::SETLT:
5861 case ISD::SETOLT: SSECC = 1; break;
5862 case ISD::SETOGE:
5863 case ISD::SETGE: Swap = true; // Fallthrough
5864 case ISD::SETLE:
5865 case ISD::SETOLE: SSECC = 2; break;
5866 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005867 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005868 case ISD::SETNE: SSECC = 4; break;
5869 case ISD::SETULE: Swap = true;
5870 case ISD::SETUGE: SSECC = 5; break;
5871 case ISD::SETULT: Swap = true;
5872 case ISD::SETUGT: SSECC = 6; break;
5873 case ISD::SETO: SSECC = 7; break;
5874 }
5875 if (Swap)
5876 std::swap(Op0, Op1);
5877
Nate Begemanfb8ead02008-07-25 19:05:58 +00005878 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005879 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005880 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005881 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005882 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5883 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005884 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005885 }
5886 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005887 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005888 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5889 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005890 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005891 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005892 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005893 }
5894 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005895 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005896 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005897
Nate Begeman30a0de92008-07-17 16:51:19 +00005898 // We are handling one of the integer comparisons here. Since SSE only has
5899 // GT and EQ comparisons for integer, swapping operands and multiple
5900 // operations may be required for some comparisons.
5901 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5902 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005903
Owen Anderson825b72b2009-08-11 20:47:22 +00005904 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005905 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005906 case MVT::v8i8:
5907 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5908 case MVT::v4i16:
5909 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5910 case MVT::v2i32:
5911 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5912 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005914
Nate Begeman30a0de92008-07-17 16:51:19 +00005915 switch (SetCCOpcode) {
5916 default: break;
5917 case ISD::SETNE: Invert = true;
5918 case ISD::SETEQ: Opc = EQOpc; break;
5919 case ISD::SETLT: Swap = true;
5920 case ISD::SETGT: Opc = GTOpc; break;
5921 case ISD::SETGE: Swap = true;
5922 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5923 case ISD::SETULT: Swap = true;
5924 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5925 case ISD::SETUGE: Swap = true;
5926 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5927 }
5928 if (Swap)
5929 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005930
Nate Begeman30a0de92008-07-17 16:51:19 +00005931 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5932 // bits of the inputs before performing those operations.
5933 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005934 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005935 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5936 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005937 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005938 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5939 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005940 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5941 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005943
Dale Johannesenace16102009-02-03 19:33:06 +00005944 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005945
5946 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005947 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005948 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005949
Nate Begeman30a0de92008-07-17 16:51:19 +00005950 return Result;
5951}
Evan Cheng0488db92007-09-25 01:57:46 +00005952
Evan Cheng370e5342008-12-03 08:38:43 +00005953// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005954static bool isX86LogicalCmp(SDValue Op) {
5955 unsigned Opc = Op.getNode()->getOpcode();
5956 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5957 return true;
5958 if (Op.getResNo() == 1 &&
5959 (Opc == X86ISD::ADD ||
5960 Opc == X86ISD::SUB ||
5961 Opc == X86ISD::SMUL ||
5962 Opc == X86ISD::UMUL ||
5963 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005964 Opc == X86ISD::DEC ||
5965 Opc == X86ISD::OR ||
5966 Opc == X86ISD::XOR ||
5967 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005968 return true;
5969
5970 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005971}
5972
Dan Gohman475871a2008-07-27 21:46:04 +00005973SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005974 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005975 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005976 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005977 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005978
Dan Gohman1a492952009-10-20 16:22:37 +00005979 if (Cond.getOpcode() == ISD::SETCC) {
5980 SDValue NewCond = LowerSETCC(Cond, DAG);
5981 if (NewCond.getNode())
5982 Cond = NewCond;
5983 }
Evan Cheng734503b2006-09-11 02:19:56 +00005984
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00005985 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
5986 SDValue Op1 = Op.getOperand(1);
5987 SDValue Op2 = Op.getOperand(2);
5988 if (Cond.getOpcode() == X86ISD::SETCC &&
5989 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
5990 SDValue Cmp = Cond.getOperand(1);
5991 if (Cmp.getOpcode() == X86ISD::CMP) {
5992 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
5993 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
5994 ConstantSDNode *RHSC =
5995 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
5996 if (N1C && N1C->isAllOnesValue() &&
5997 N2C && N2C->isNullValue() &&
5998 RHSC && RHSC->isNullValue()) {
5999 SDValue CmpOp0 = Cmp.getOperand(0);
6000 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
6001 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6002 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6003 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6004 }
6005 }
6006 }
6007
Evan Chengad9c0a32009-12-15 00:53:42 +00006008 // Look pass (and (setcc_carry (cmp ...)), 1).
6009 if (Cond.getOpcode() == ISD::AND &&
6010 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6011 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6012 if (C && C->getAPIntValue() == 1)
6013 Cond = Cond.getOperand(0);
6014 }
6015
Evan Cheng3f41d662007-10-08 22:16:29 +00006016 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6017 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006018 if (Cond.getOpcode() == X86ISD::SETCC ||
6019 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006020 CC = Cond.getOperand(0);
6021
Dan Gohman475871a2008-07-27 21:46:04 +00006022 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006023 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006024 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006025
Evan Cheng3f41d662007-10-08 22:16:29 +00006026 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006027 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006028 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006029 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006030
Chris Lattnerd1980a52009-03-12 06:52:53 +00006031 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6032 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006033 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006034 addTest = false;
6035 }
6036 }
6037
6038 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006039 // Look pass the truncate.
6040 if (Cond.getOpcode() == ISD::TRUNCATE)
6041 Cond = Cond.getOperand(0);
6042
6043 // We know the result of AND is compared against zero. Try to match
6044 // it to BT.
6045 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6046 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6047 if (NewSetCC.getNode()) {
6048 CC = NewSetCC.getOperand(0);
6049 Cond = NewSetCC.getOperand(1);
6050 addTest = false;
6051 }
6052 }
6053 }
6054
6055 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006056 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006057 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006058 }
6059
Evan Cheng0488db92007-09-25 01:57:46 +00006060 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6061 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006062 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6063 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006064 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006065}
6066
Evan Cheng370e5342008-12-03 08:38:43 +00006067// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6068// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6069// from the AND / OR.
6070static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6071 Opc = Op.getOpcode();
6072 if (Opc != ISD::OR && Opc != ISD::AND)
6073 return false;
6074 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6075 Op.getOperand(0).hasOneUse() &&
6076 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6077 Op.getOperand(1).hasOneUse());
6078}
6079
Evan Cheng961d6d42009-02-02 08:19:07 +00006080// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6081// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006082static bool isXor1OfSetCC(SDValue Op) {
6083 if (Op.getOpcode() != ISD::XOR)
6084 return false;
6085 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6086 if (N1C && N1C->getAPIntValue() == 1) {
6087 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6088 Op.getOperand(0).hasOneUse();
6089 }
6090 return false;
6091}
6092
Dan Gohman475871a2008-07-27 21:46:04 +00006093SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006094 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006095 SDValue Chain = Op.getOperand(0);
6096 SDValue Cond = Op.getOperand(1);
6097 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006098 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006099 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006100
Dan Gohman1a492952009-10-20 16:22:37 +00006101 if (Cond.getOpcode() == ISD::SETCC) {
6102 SDValue NewCond = LowerSETCC(Cond, DAG);
6103 if (NewCond.getNode())
6104 Cond = NewCond;
6105 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006106#if 0
6107 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006108 else if (Cond.getOpcode() == X86ISD::ADD ||
6109 Cond.getOpcode() == X86ISD::SUB ||
6110 Cond.getOpcode() == X86ISD::SMUL ||
6111 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006112 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006113#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006114
Evan Chengad9c0a32009-12-15 00:53:42 +00006115 // Look pass (and (setcc_carry (cmp ...)), 1).
6116 if (Cond.getOpcode() == ISD::AND &&
6117 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6118 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6119 if (C && C->getAPIntValue() == 1)
6120 Cond = Cond.getOperand(0);
6121 }
6122
Evan Cheng3f41d662007-10-08 22:16:29 +00006123 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6124 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006125 if (Cond.getOpcode() == X86ISD::SETCC ||
6126 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006127 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006128
Dan Gohman475871a2008-07-27 21:46:04 +00006129 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006130 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006131 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006132 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006133 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006134 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006135 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006136 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006137 default: break;
6138 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006139 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006140 // These can only come from an arithmetic instruction with overflow,
6141 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006142 Cond = Cond.getNode()->getOperand(1);
6143 addTest = false;
6144 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006145 }
Evan Cheng0488db92007-09-25 01:57:46 +00006146 }
Evan Cheng370e5342008-12-03 08:38:43 +00006147 } else {
6148 unsigned CondOpc;
6149 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6150 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006151 if (CondOpc == ISD::OR) {
6152 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6153 // two branches instead of an explicit OR instruction with a
6154 // separate test.
6155 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006156 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006157 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006158 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006159 Chain, Dest, CC, Cmp);
6160 CC = Cond.getOperand(1).getOperand(0);
6161 Cond = Cmp;
6162 addTest = false;
6163 }
6164 } else { // ISD::AND
6165 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6166 // two branches instead of an explicit AND instruction with a
6167 // separate test. However, we only do this if this block doesn't
6168 // have a fall-through edge, because this requires an explicit
6169 // jmp when the condition is false.
6170 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006171 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006172 Op.getNode()->hasOneUse()) {
6173 X86::CondCode CCode =
6174 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6175 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006176 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006177 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6178 // Look for an unconditional branch following this conditional branch.
6179 // We need this because we need to reverse the successors in order
6180 // to implement FCMP_OEQ.
6181 if (User.getOpcode() == ISD::BR) {
6182 SDValue FalseBB = User.getOperand(1);
6183 SDValue NewBR =
6184 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6185 assert(NewBR == User);
6186 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006187
Dale Johannesene4d209d2009-02-03 20:21:25 +00006188 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006189 Chain, Dest, CC, Cmp);
6190 X86::CondCode CCode =
6191 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6192 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006193 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006194 Cond = Cmp;
6195 addTest = false;
6196 }
6197 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006198 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006199 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6200 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6201 // It should be transformed during dag combiner except when the condition
6202 // is set by a arithmetics with overflow node.
6203 X86::CondCode CCode =
6204 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6205 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006206 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006207 Cond = Cond.getOperand(0).getOperand(1);
6208 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006209 }
Evan Cheng0488db92007-09-25 01:57:46 +00006210 }
6211
6212 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006213 // Look pass the truncate.
6214 if (Cond.getOpcode() == ISD::TRUNCATE)
6215 Cond = Cond.getOperand(0);
6216
6217 // We know the result of AND is compared against zero. Try to match
6218 // it to BT.
6219 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6220 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6221 if (NewSetCC.getNode()) {
6222 CC = NewSetCC.getOperand(0);
6223 Cond = NewSetCC.getOperand(1);
6224 addTest = false;
6225 }
6226 }
6227 }
6228
6229 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006230 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006231 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006232 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006233 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006234 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006235}
6236
Anton Korobeynikove060b532007-04-17 19:34:00 +00006237
6238// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6239// Calls to _alloca is needed to probe the stack when allocating more than 4k
6240// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6241// that the guard pages used by the OS virtual memory manager are allocated in
6242// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006243SDValue
6244X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006245 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006246 assert(Subtarget->isTargetCygMing() &&
6247 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006248 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006249
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006250 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006251 SDValue Chain = Op.getOperand(0);
6252 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006253 // FIXME: Ensure alignment here
6254
Dan Gohman475871a2008-07-27 21:46:04 +00006255 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006256
Owen Andersone50ed302009-08-10 22:56:29 +00006257 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006258 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006259
Chris Lattnere563bbc2008-10-11 22:08:30 +00006260 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006261
Dale Johannesendd64c412009-02-04 00:33:20 +00006262 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006263 Flag = Chain.getValue(1);
6264
Owen Anderson825b72b2009-08-11 20:47:22 +00006265 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006266 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006267 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006268 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006269 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006270 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006271 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006272 Flag = Chain.getValue(1);
6273
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006274 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006275 DAG.getIntPtrConstant(0, true),
6276 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006277 Flag);
6278
Dale Johannesendd64c412009-02-04 00:33:20 +00006279 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006280
Dan Gohman475871a2008-07-27 21:46:04 +00006281 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006282 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006283}
6284
Dan Gohman475871a2008-07-27 21:46:04 +00006285SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006286X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006287 SDValue Chain,
6288 SDValue Dst, SDValue Src,
6289 SDValue Size, unsigned Align,
6290 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006291 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006292 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006293
Bill Wendling6f287b22008-09-30 21:22:07 +00006294 // If not DWORD aligned or size is more than the threshold, call the library.
6295 // The libc version is likely to be faster for these cases. It can use the
6296 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006297 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006298 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006299 ConstantSize->getZExtValue() >
6300 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006301 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006302
6303 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006304 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006305
Bill Wendling6158d842008-10-01 00:59:58 +00006306 if (const char *bzeroEntry = V &&
6307 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006308 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006309 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006310 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006311 TargetLowering::ArgListEntry Entry;
6312 Entry.Node = Dst;
6313 Entry.Ty = IntPtrTy;
6314 Args.push_back(Entry);
6315 Entry.Node = Size;
6316 Args.push_back(Entry);
6317 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006318 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6319 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006320 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006321 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6322 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006323 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006324 }
6325
Dan Gohman707e0182008-04-12 04:36:06 +00006326 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006327 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006328 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006329
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006330 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006331 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006332 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006333 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006334 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006335 unsigned BytesLeft = 0;
6336 bool TwoRepStos = false;
6337 if (ValC) {
6338 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006339 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006340
Evan Cheng0db9fe62006-04-25 20:13:52 +00006341 // If the value is a constant, then we can potentially use larger sets.
6342 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006343 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006344 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006345 ValReg = X86::AX;
6346 Val = (Val << 8) | Val;
6347 break;
6348 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006349 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006350 ValReg = X86::EAX;
6351 Val = (Val << 8) | Val;
6352 Val = (Val << 16) | Val;
6353 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006354 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006355 ValReg = X86::RAX;
6356 Val = (Val << 32) | Val;
6357 }
6358 break;
6359 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006360 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006361 ValReg = X86::AL;
6362 Count = DAG.getIntPtrConstant(SizeVal);
6363 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006364 }
6365
Owen Anderson825b72b2009-08-11 20:47:22 +00006366 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006367 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006368 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6369 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006370 }
6371
Dale Johannesen0f502f62009-02-03 22:26:09 +00006372 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006373 InFlag);
6374 InFlag = Chain.getValue(1);
6375 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006376 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006377 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006378 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006379 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006380 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006381
Scott Michelfdc40a02009-02-17 22:15:04 +00006382 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006383 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006384 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006385 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006386 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006387 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006388 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006389 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006390
Owen Anderson825b72b2009-08-11 20:47:22 +00006391 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006392 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6393 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006394
Evan Cheng0db9fe62006-04-25 20:13:52 +00006395 if (TwoRepStos) {
6396 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006397 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006398 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006399 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6401 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006402 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006403 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006404 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006405 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006406 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6407 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006408 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006409 // Handle the last 1 - 7 bytes.
6410 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006411 EVT AddrVT = Dst.getValueType();
6412 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006413
Dale Johannesen0f502f62009-02-03 22:26:09 +00006414 Chain = DAG.getMemset(Chain, dl,
6415 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006416 DAG.getConstant(Offset, AddrVT)),
6417 Src,
6418 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006419 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006420 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006421
Dan Gohman707e0182008-04-12 04:36:06 +00006422 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006423 return Chain;
6424}
Evan Cheng11e15b32006-04-03 20:53:28 +00006425
Dan Gohman475871a2008-07-27 21:46:04 +00006426SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006427X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006428 SDValue Chain, SDValue Dst, SDValue Src,
6429 SDValue Size, unsigned Align,
6430 bool AlwaysInline,
6431 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006432 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006433 // This requires the copy size to be a constant, preferrably
6434 // within a subtarget-specific limit.
6435 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6436 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006437 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006438 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006439 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006440 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006441
Evan Cheng1887c1c2008-08-21 21:00:15 +00006442 /// If not DWORD aligned, call the library.
6443 if ((Align & 3) != 0)
6444 return SDValue();
6445
6446 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006447 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006448 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006449 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006450
Duncan Sands83ec4b62008-06-06 12:08:01 +00006451 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006452 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006453 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006454 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006455
Dan Gohman475871a2008-07-27 21:46:04 +00006456 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006457 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006458 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006459 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006460 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006461 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006462 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006463 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006464 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006465 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006466 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006467 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006468 InFlag = Chain.getValue(1);
6469
Owen Anderson825b72b2009-08-11 20:47:22 +00006470 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006471 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6472 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6473 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006474
Dan Gohman475871a2008-07-27 21:46:04 +00006475 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006476 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006477 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006478 // Handle the last 1 - 7 bytes.
6479 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006480 EVT DstVT = Dst.getValueType();
6481 EVT SrcVT = Src.getValueType();
6482 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006483 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006484 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006485 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006486 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006487 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006488 DAG.getConstant(BytesLeft, SizeVT),
6489 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006490 DstSV, DstSVOff + Offset,
6491 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006492 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006493
Owen Anderson825b72b2009-08-11 20:47:22 +00006494 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006495 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006496}
6497
Dan Gohman475871a2008-07-27 21:46:04 +00006498SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006499 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006500 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006501
Evan Cheng25ab6902006-09-08 06:48:29 +00006502 if (!Subtarget->is64Bit()) {
6503 // vastart just stores the address of the VarArgsFrameIndex slot into the
6504 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006505 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006506 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006507 }
6508
6509 // __va_list_tag:
6510 // gp_offset (0 - 6 * 8)
6511 // fp_offset (48 - 48 + 8 * 16)
6512 // overflow_arg_area (point to parameters coming in memory).
6513 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006514 SmallVector<SDValue, 8> MemOps;
6515 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006516 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006517 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006518 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006519 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006520 MemOps.push_back(Store);
6521
6522 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006523 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006524 FIN, DAG.getIntPtrConstant(4));
6525 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006526 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006527 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006528 MemOps.push_back(Store);
6529
6530 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006531 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006532 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006533 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006534 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006535 MemOps.push_back(Store);
6536
6537 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006538 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006539 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006540 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006541 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006542 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006543 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006544 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006545}
6546
Dan Gohman475871a2008-07-27 21:46:04 +00006547SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006548 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6549 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006550 SDValue Chain = Op.getOperand(0);
6551 SDValue SrcPtr = Op.getOperand(1);
6552 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006553
Torok Edwindac237e2009-07-08 20:53:28 +00006554 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006555 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006556}
6557
Dan Gohman475871a2008-07-27 21:46:04 +00006558SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006559 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006560 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006561 SDValue Chain = Op.getOperand(0);
6562 SDValue DstPtr = Op.getOperand(1);
6563 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006564 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6565 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006566 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006567
Dale Johannesendd64c412009-02-04 00:33:20 +00006568 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006569 DAG.getIntPtrConstant(24), 8, false,
6570 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006571}
6572
Dan Gohman475871a2008-07-27 21:46:04 +00006573SDValue
6574X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006575 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006576 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006577 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006578 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006579 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 case Intrinsic::x86_sse_comieq_ss:
6581 case Intrinsic::x86_sse_comilt_ss:
6582 case Intrinsic::x86_sse_comile_ss:
6583 case Intrinsic::x86_sse_comigt_ss:
6584 case Intrinsic::x86_sse_comige_ss:
6585 case Intrinsic::x86_sse_comineq_ss:
6586 case Intrinsic::x86_sse_ucomieq_ss:
6587 case Intrinsic::x86_sse_ucomilt_ss:
6588 case Intrinsic::x86_sse_ucomile_ss:
6589 case Intrinsic::x86_sse_ucomigt_ss:
6590 case Intrinsic::x86_sse_ucomige_ss:
6591 case Intrinsic::x86_sse_ucomineq_ss:
6592 case Intrinsic::x86_sse2_comieq_sd:
6593 case Intrinsic::x86_sse2_comilt_sd:
6594 case Intrinsic::x86_sse2_comile_sd:
6595 case Intrinsic::x86_sse2_comigt_sd:
6596 case Intrinsic::x86_sse2_comige_sd:
6597 case Intrinsic::x86_sse2_comineq_sd:
6598 case Intrinsic::x86_sse2_ucomieq_sd:
6599 case Intrinsic::x86_sse2_ucomilt_sd:
6600 case Intrinsic::x86_sse2_ucomile_sd:
6601 case Intrinsic::x86_sse2_ucomigt_sd:
6602 case Intrinsic::x86_sse2_ucomige_sd:
6603 case Intrinsic::x86_sse2_ucomineq_sd: {
6604 unsigned Opc = 0;
6605 ISD::CondCode CC = ISD::SETCC_INVALID;
6606 switch (IntNo) {
6607 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006608 case Intrinsic::x86_sse_comieq_ss:
6609 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 Opc = X86ISD::COMI;
6611 CC = ISD::SETEQ;
6612 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006613 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006614 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006615 Opc = X86ISD::COMI;
6616 CC = ISD::SETLT;
6617 break;
6618 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006619 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006620 Opc = X86ISD::COMI;
6621 CC = ISD::SETLE;
6622 break;
6623 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006624 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006625 Opc = X86ISD::COMI;
6626 CC = ISD::SETGT;
6627 break;
6628 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006629 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006630 Opc = X86ISD::COMI;
6631 CC = ISD::SETGE;
6632 break;
6633 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006634 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635 Opc = X86ISD::COMI;
6636 CC = ISD::SETNE;
6637 break;
6638 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006639 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006640 Opc = X86ISD::UCOMI;
6641 CC = ISD::SETEQ;
6642 break;
6643 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006644 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645 Opc = X86ISD::UCOMI;
6646 CC = ISD::SETLT;
6647 break;
6648 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006649 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006650 Opc = X86ISD::UCOMI;
6651 CC = ISD::SETLE;
6652 break;
6653 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006654 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655 Opc = X86ISD::UCOMI;
6656 CC = ISD::SETGT;
6657 break;
6658 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006659 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660 Opc = X86ISD::UCOMI;
6661 CC = ISD::SETGE;
6662 break;
6663 case Intrinsic::x86_sse_ucomineq_ss:
6664 case Intrinsic::x86_sse2_ucomineq_sd:
6665 Opc = X86ISD::UCOMI;
6666 CC = ISD::SETNE;
6667 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006668 }
Evan Cheng734503b2006-09-11 02:19:56 +00006669
Dan Gohman475871a2008-07-27 21:46:04 +00006670 SDValue LHS = Op.getOperand(1);
6671 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006672 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006673 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006674 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6675 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6676 DAG.getConstant(X86CC, MVT::i8), Cond);
6677 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006678 }
Eric Christopher71c67532009-07-29 00:28:05 +00006679 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006680 // an integer value, not just an instruction so lower it to the ptest
6681 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006682 case Intrinsic::x86_sse41_ptestz:
6683 case Intrinsic::x86_sse41_ptestc:
6684 case Intrinsic::x86_sse41_ptestnzc:{
6685 unsigned X86CC = 0;
6686 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006687 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006688 case Intrinsic::x86_sse41_ptestz:
6689 // ZF = 1
6690 X86CC = X86::COND_E;
6691 break;
6692 case Intrinsic::x86_sse41_ptestc:
6693 // CF = 1
6694 X86CC = X86::COND_B;
6695 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006696 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006697 // ZF and CF = 0
6698 X86CC = X86::COND_A;
6699 break;
6700 }
Eric Christopherfd179292009-08-27 18:07:15 +00006701
Eric Christopher71c67532009-07-29 00:28:05 +00006702 SDValue LHS = Op.getOperand(1);
6703 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006704 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6705 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6706 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6707 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006708 }
Evan Cheng5759f972008-05-04 09:15:50 +00006709
6710 // Fix vector shift instructions where the last operand is a non-immediate
6711 // i32 value.
6712 case Intrinsic::x86_sse2_pslli_w:
6713 case Intrinsic::x86_sse2_pslli_d:
6714 case Intrinsic::x86_sse2_pslli_q:
6715 case Intrinsic::x86_sse2_psrli_w:
6716 case Intrinsic::x86_sse2_psrli_d:
6717 case Intrinsic::x86_sse2_psrli_q:
6718 case Intrinsic::x86_sse2_psrai_w:
6719 case Intrinsic::x86_sse2_psrai_d:
6720 case Intrinsic::x86_mmx_pslli_w:
6721 case Intrinsic::x86_mmx_pslli_d:
6722 case Intrinsic::x86_mmx_pslli_q:
6723 case Intrinsic::x86_mmx_psrli_w:
6724 case Intrinsic::x86_mmx_psrli_d:
6725 case Intrinsic::x86_mmx_psrli_q:
6726 case Intrinsic::x86_mmx_psrai_w:
6727 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006728 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006729 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006730 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006731
6732 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006734 switch (IntNo) {
6735 case Intrinsic::x86_sse2_pslli_w:
6736 NewIntNo = Intrinsic::x86_sse2_psll_w;
6737 break;
6738 case Intrinsic::x86_sse2_pslli_d:
6739 NewIntNo = Intrinsic::x86_sse2_psll_d;
6740 break;
6741 case Intrinsic::x86_sse2_pslli_q:
6742 NewIntNo = Intrinsic::x86_sse2_psll_q;
6743 break;
6744 case Intrinsic::x86_sse2_psrli_w:
6745 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6746 break;
6747 case Intrinsic::x86_sse2_psrli_d:
6748 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6749 break;
6750 case Intrinsic::x86_sse2_psrli_q:
6751 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6752 break;
6753 case Intrinsic::x86_sse2_psrai_w:
6754 NewIntNo = Intrinsic::x86_sse2_psra_w;
6755 break;
6756 case Intrinsic::x86_sse2_psrai_d:
6757 NewIntNo = Intrinsic::x86_sse2_psra_d;
6758 break;
6759 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006760 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006761 switch (IntNo) {
6762 case Intrinsic::x86_mmx_pslli_w:
6763 NewIntNo = Intrinsic::x86_mmx_psll_w;
6764 break;
6765 case Intrinsic::x86_mmx_pslli_d:
6766 NewIntNo = Intrinsic::x86_mmx_psll_d;
6767 break;
6768 case Intrinsic::x86_mmx_pslli_q:
6769 NewIntNo = Intrinsic::x86_mmx_psll_q;
6770 break;
6771 case Intrinsic::x86_mmx_psrli_w:
6772 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6773 break;
6774 case Intrinsic::x86_mmx_psrli_d:
6775 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6776 break;
6777 case Intrinsic::x86_mmx_psrli_q:
6778 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6779 break;
6780 case Intrinsic::x86_mmx_psrai_w:
6781 NewIntNo = Intrinsic::x86_mmx_psra_w;
6782 break;
6783 case Intrinsic::x86_mmx_psrai_d:
6784 NewIntNo = Intrinsic::x86_mmx_psra_d;
6785 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006786 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006787 }
6788 break;
6789 }
6790 }
Mon P Wangefa42202009-09-03 19:56:25 +00006791
6792 // The vector shift intrinsics with scalars uses 32b shift amounts but
6793 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6794 // to be zero.
6795 SDValue ShOps[4];
6796 ShOps[0] = ShAmt;
6797 ShOps[1] = DAG.getConstant(0, MVT::i32);
6798 if (ShAmtVT == MVT::v4i32) {
6799 ShOps[2] = DAG.getUNDEF(MVT::i32);
6800 ShOps[3] = DAG.getUNDEF(MVT::i32);
6801 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6802 } else {
6803 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6804 }
6805
Owen Andersone50ed302009-08-10 22:56:29 +00006806 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006807 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006808 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006809 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006810 Op.getOperand(1), ShAmt);
6811 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006812 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006813}
Evan Cheng72261582005-12-20 06:22:03 +00006814
Dan Gohman475871a2008-07-27 21:46:04 +00006815SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006816 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006817 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006818
6819 if (Depth > 0) {
6820 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6821 SDValue Offset =
6822 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006823 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006824 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006825 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006826 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006827 NULL, 0);
6828 }
6829
6830 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006831 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006832 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006833 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006834}
6835
Dan Gohman475871a2008-07-27 21:46:04 +00006836SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006837 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6838 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006839 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006840 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006841 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6842 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006843 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006844 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006845 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006846 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006847}
6848
Dan Gohman475871a2008-07-27 21:46:04 +00006849SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006850 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006851 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006852}
6853
Dan Gohman475871a2008-07-27 21:46:04 +00006854SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006855{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006856 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006857 SDValue Chain = Op.getOperand(0);
6858 SDValue Offset = Op.getOperand(1);
6859 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006860 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006861
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006862 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6863 getPointerTy());
6864 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006865
Dale Johannesene4d209d2009-02-03 20:21:25 +00006866 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006867 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006868 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6869 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006870 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006871 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006872
Dale Johannesene4d209d2009-02-03 20:21:25 +00006873 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006874 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006875 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006876}
6877
Dan Gohman475871a2008-07-27 21:46:04 +00006878SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006879 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006880 SDValue Root = Op.getOperand(0);
6881 SDValue Trmp = Op.getOperand(1); // trampoline
6882 SDValue FPtr = Op.getOperand(2); // nested function
6883 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006884 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006885
Dan Gohman69de1932008-02-06 22:27:42 +00006886 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006887
Duncan Sands339e14f2008-01-16 22:55:25 +00006888 const X86InstrInfo *TII =
6889 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6890
Duncan Sandsb116fac2007-07-27 20:02:49 +00006891 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006892 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006893
6894 // Large code-model.
6895
6896 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6897 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6898
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006899 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6900 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006901
6902 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6903
6904 // Load the pointer to the nested function into R11.
6905 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006906 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006907 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006908 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006909
Owen Anderson825b72b2009-08-11 20:47:22 +00006910 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6911 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006912 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006913
6914 // Load the 'nest' parameter value into R10.
6915 // R10 is specified in X86CallingConv.td
6916 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006917 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6918 DAG.getConstant(10, MVT::i64));
6919 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006920 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006921
Owen Anderson825b72b2009-08-11 20:47:22 +00006922 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6923 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006924 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006925
6926 // Jump to the nested function.
6927 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006928 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6929 DAG.getConstant(20, MVT::i64));
6930 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006931 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006932
6933 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006934 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6935 DAG.getConstant(22, MVT::i64));
6936 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006937 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006938
Dan Gohman475871a2008-07-27 21:46:04 +00006939 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006940 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006941 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006942 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006943 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006944 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006945 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006946 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006947
6948 switch (CC) {
6949 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006950 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006951 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006952 case CallingConv::X86_StdCall: {
6953 // Pass 'nest' parameter in ECX.
6954 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006955 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006956
6957 // Check that ECX wasn't needed by an 'inreg' parameter.
6958 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006959 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006960
Chris Lattner58d74912008-03-12 17:45:29 +00006961 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006962 unsigned InRegCount = 0;
6963 unsigned Idx = 1;
6964
6965 for (FunctionType::param_iterator I = FTy->param_begin(),
6966 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006967 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006968 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006969 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006970
6971 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006972 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006973 }
6974 }
6975 break;
6976 }
6977 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006978 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006979 // Pass 'nest' parameter in EAX.
6980 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006981 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006982 break;
6983 }
6984
Dan Gohman475871a2008-07-27 21:46:04 +00006985 SDValue OutChains[4];
6986 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006987
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6989 DAG.getConstant(10, MVT::i32));
6990 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006991
Duncan Sands339e14f2008-01-16 22:55:25 +00006992 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006993 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006994 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006995 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006996 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006997
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
6999 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007000 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007001
Duncan Sands339e14f2008-01-16 22:55:25 +00007002 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7004 DAG.getConstant(5, MVT::i32));
7005 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00007006 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007007
Owen Anderson825b72b2009-08-11 20:47:22 +00007008 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7009 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007010 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007011
Dan Gohman475871a2008-07-27 21:46:04 +00007012 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007014 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007015 }
7016}
7017
Dan Gohman475871a2008-07-27 21:46:04 +00007018SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007019 /*
7020 The rounding mode is in bits 11:10 of FPSR, and has the following
7021 settings:
7022 00 Round to nearest
7023 01 Round to -inf
7024 10 Round to +inf
7025 11 Round to 0
7026
7027 FLT_ROUNDS, on the other hand, expects the following:
7028 -1 Undefined
7029 0 Round to 0
7030 1 Round to nearest
7031 2 Round to +inf
7032 3 Round to -inf
7033
7034 To perform the conversion, we do:
7035 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7036 */
7037
7038 MachineFunction &MF = DAG.getMachineFunction();
7039 const TargetMachine &TM = MF.getTarget();
7040 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7041 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007042 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007043 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007044
7045 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007046 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007047 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007048
Owen Anderson825b72b2009-08-11 20:47:22 +00007049 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007050 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007051
7052 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007053 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007054
7055 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007056 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 DAG.getNode(ISD::SRL, dl, MVT::i16,
7058 DAG.getNode(ISD::AND, dl, MVT::i16,
7059 CWD, DAG.getConstant(0x800, MVT::i16)),
7060 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007061 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007062 DAG.getNode(ISD::SRL, dl, MVT::i16,
7063 DAG.getNode(ISD::AND, dl, MVT::i16,
7064 CWD, DAG.getConstant(0x400, MVT::i16)),
7065 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007066
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 DAG.getNode(ISD::AND, dl, MVT::i16,
7069 DAG.getNode(ISD::ADD, dl, MVT::i16,
7070 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7071 DAG.getConstant(1, MVT::i16)),
7072 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007073
7074
Duncan Sands83ec4b62008-06-06 12:08:01 +00007075 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007076 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007077}
7078
Dan Gohman475871a2008-07-27 21:46:04 +00007079SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007080 EVT VT = Op.getValueType();
7081 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007082 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007083 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007084
7085 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007087 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007088 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007089 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007090 }
Evan Cheng18efe262007-12-14 02:13:44 +00007091
Evan Cheng152804e2007-12-14 08:30:15 +00007092 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007094 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007095
7096 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007097 SDValue Ops[] = {
7098 Op,
7099 DAG.getConstant(NumBits+NumBits-1, OpVT),
7100 DAG.getConstant(X86::COND_E, MVT::i8),
7101 Op.getValue(1)
7102 };
7103 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007104
7105 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007106 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007107
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 if (VT == MVT::i8)
7109 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007110 return Op;
7111}
7112
Dan Gohman475871a2008-07-27 21:46:04 +00007113SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007114 EVT VT = Op.getValueType();
7115 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007116 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007117 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007118
7119 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007120 if (VT == MVT::i8) {
7121 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007122 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007123 }
Evan Cheng152804e2007-12-14 08:30:15 +00007124
7125 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007126 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007127 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007128
7129 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007130 SDValue Ops[] = {
7131 Op,
7132 DAG.getConstant(NumBits, OpVT),
7133 DAG.getConstant(X86::COND_E, MVT::i8),
7134 Op.getValue(1)
7135 };
7136 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007137
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 if (VT == MVT::i8)
7139 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007140 return Op;
7141}
7142
Mon P Wangaf9b9522008-12-18 21:42:19 +00007143SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007144 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007146 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007147
Mon P Wangaf9b9522008-12-18 21:42:19 +00007148 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7149 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7150 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7151 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7152 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7153 //
7154 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7155 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7156 // return AloBlo + AloBhi + AhiBlo;
7157
7158 SDValue A = Op.getOperand(0);
7159 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007160
Dale Johannesene4d209d2009-02-03 20:21:25 +00007161 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007162 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7163 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007164 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7166 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007167 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007169 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007170 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007171 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007172 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007175 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007176 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007177 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7178 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007179 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7181 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007182 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7183 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007184 return Res;
7185}
7186
7187
Bill Wendling74c37652008-12-09 22:08:41 +00007188SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7189 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7190 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007191 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7192 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007193 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007194 SDValue LHS = N->getOperand(0);
7195 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007196 unsigned BaseOp = 0;
7197 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007198 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007199
7200 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007201 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007202 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007203 // A subtract of one will be selected as a INC. Note that INC doesn't
7204 // set CF, so we can't do this for UADDO.
7205 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7206 if (C->getAPIntValue() == 1) {
7207 BaseOp = X86ISD::INC;
7208 Cond = X86::COND_O;
7209 break;
7210 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007211 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007212 Cond = X86::COND_O;
7213 break;
7214 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007215 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007216 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007217 break;
7218 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007219 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7220 // set CF, so we can't do this for USUBO.
7221 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7222 if (C->getAPIntValue() == 1) {
7223 BaseOp = X86ISD::DEC;
7224 Cond = X86::COND_O;
7225 break;
7226 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007227 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007228 Cond = X86::COND_O;
7229 break;
7230 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007231 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007232 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007233 break;
7234 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007235 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007236 Cond = X86::COND_O;
7237 break;
7238 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007239 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007240 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007241 break;
7242 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007243
Bill Wendling61edeb52008-12-02 01:06:39 +00007244 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007245 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007246 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007247
Bill Wendling61edeb52008-12-02 01:06:39 +00007248 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007249 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007250 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007251
Bill Wendling61edeb52008-12-02 01:06:39 +00007252 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7253 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007254}
7255
Dan Gohman475871a2008-07-27 21:46:04 +00007256SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007257 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007258 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007259 unsigned Reg = 0;
7260 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007262 default:
7263 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 case MVT::i8: Reg = X86::AL; size = 1; break;
7265 case MVT::i16: Reg = X86::AX; size = 2; break;
7266 case MVT::i32: Reg = X86::EAX; size = 4; break;
7267 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007268 assert(Subtarget->is64Bit() && "Node not type legal!");
7269 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007270 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007271 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007272 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007273 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007274 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007275 Op.getOperand(1),
7276 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007277 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007278 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007279 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007280 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007281 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007282 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007283 return cpOut;
7284}
7285
Duncan Sands1607f052008-12-01 11:39:25 +00007286SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007287 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007288 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007289 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007290 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007291 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007292 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007293 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7294 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007295 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007296 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7297 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007298 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007299 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007300 rdx.getValue(1)
7301 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007302 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007303}
7304
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007305SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7306 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007307 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007308 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007309 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007310 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007311 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007312 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007313 Node->getOperand(0),
7314 Node->getOperand(1), negOp,
7315 cast<AtomicSDNode>(Node)->getSrcValue(),
7316 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007317}
7318
Evan Cheng0db9fe62006-04-25 20:13:52 +00007319/// LowerOperation - Provide custom lowering hooks for some operations.
7320///
Dan Gohman475871a2008-07-27 21:46:04 +00007321SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007322 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007323 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007324 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7325 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007326 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007327 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007328 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7329 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7330 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7331 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7332 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7333 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007334 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007335 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007336 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007337 case ISD::SHL_PARTS:
7338 case ISD::SRA_PARTS:
7339 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7340 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007341 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007342 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007343 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007344 case ISD::FABS: return LowerFABS(Op, DAG);
7345 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007346 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007347 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007348 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007349 case ISD::SELECT: return LowerSELECT(Op, DAG);
7350 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007351 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007352 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007353 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007354 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007355 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007356 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7357 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007358 case ISD::FRAME_TO_ARGS_OFFSET:
7359 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007360 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007361 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007362 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007363 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007364 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7365 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007366 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007367 case ISD::SADDO:
7368 case ISD::UADDO:
7369 case ISD::SSUBO:
7370 case ISD::USUBO:
7371 case ISD::SMULO:
7372 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007373 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007374 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007375}
7376
Duncan Sands1607f052008-12-01 11:39:25 +00007377void X86TargetLowering::
7378ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7379 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007380 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007381 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007382 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007383
7384 SDValue Chain = Node->getOperand(0);
7385 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007387 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007389 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007390 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007391 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007392 SDValue Result =
7393 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7394 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007395 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007396 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007397 Results.push_back(Result.getValue(2));
7398}
7399
Duncan Sands126d9072008-07-04 11:47:58 +00007400/// ReplaceNodeResults - Replace a node with an illegal result type
7401/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007402void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7403 SmallVectorImpl<SDValue>&Results,
7404 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007405 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007406 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007407 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007408 assert(false && "Do not know how to custom type legalize this operation!");
7409 return;
7410 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007411 std::pair<SDValue,SDValue> Vals =
7412 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007413 SDValue FIST = Vals.first, StackSlot = Vals.second;
7414 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007415 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007416 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007417 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007418 }
7419 return;
7420 }
7421 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007422 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007423 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007424 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007425 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007426 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007427 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007428 eax.getValue(2));
7429 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7430 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007431 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007432 Results.push_back(edx.getValue(1));
7433 return;
7434 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007435 case ISD::SDIV:
7436 case ISD::UDIV:
7437 case ISD::SREM:
7438 case ISD::UREM: {
7439 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7440 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7441 return;
7442 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007443 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007444 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007446 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7448 DAG.getConstant(0, MVT::i32));
7449 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7450 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007451 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7452 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007453 cpInL.getValue(1));
7454 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007455 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7456 DAG.getConstant(0, MVT::i32));
7457 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7458 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007459 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007460 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007461 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007462 swapInL.getValue(1));
7463 SDValue Ops[] = { swapInH.getValue(0),
7464 N->getOperand(1),
7465 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007466 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007467 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007468 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007469 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007470 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007471 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007472 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007474 Results.push_back(cpOutH.getValue(1));
7475 return;
7476 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007477 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007478 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7479 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007480 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007481 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7482 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007483 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007484 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7485 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007486 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007487 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7488 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007489 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007490 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7491 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007492 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007493 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7494 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007495 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007496 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7497 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007499}
7500
Evan Cheng72261582005-12-20 06:22:03 +00007501const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7502 switch (Opcode) {
7503 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007504 case X86ISD::BSF: return "X86ISD::BSF";
7505 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007506 case X86ISD::SHLD: return "X86ISD::SHLD";
7507 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007508 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007509 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007510 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007511 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007512 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007513 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007514 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7515 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7516 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007517 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007518 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007519 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007520 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007521 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007522 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007523 case X86ISD::COMI: return "X86ISD::COMI";
7524 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007525 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007526 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007527 case X86ISD::CMOV: return "X86ISD::CMOV";
7528 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007529 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007530 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7531 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007532 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007533 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007534 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007535 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007536 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007537 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7538 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007539 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007540 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007541 case X86ISD::FMAX: return "X86ISD::FMAX";
7542 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007543 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7544 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007545 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007546 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007547 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007548 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007549 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007550 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7551 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007552 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7553 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7554 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7555 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7556 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7557 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007558 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7559 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007560 case X86ISD::VSHL: return "X86ISD::VSHL";
7561 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007562 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7563 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7564 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7565 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7566 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7567 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7568 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7569 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7570 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7571 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007572 case X86ISD::ADD: return "X86ISD::ADD";
7573 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007574 case X86ISD::SMUL: return "X86ISD::SMUL";
7575 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007576 case X86ISD::INC: return "X86ISD::INC";
7577 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007578 case X86ISD::OR: return "X86ISD::OR";
7579 case X86ISD::XOR: return "X86ISD::XOR";
7580 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007581 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007582 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007583 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007584 }
7585}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007586
Chris Lattnerc9addb72007-03-30 23:15:24 +00007587// isLegalAddressingMode - Return true if the addressing mode represented
7588// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007589bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007590 const Type *Ty) const {
7591 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007592 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007593
Chris Lattnerc9addb72007-03-30 23:15:24 +00007594 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007595 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007596 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007597
Chris Lattnerc9addb72007-03-30 23:15:24 +00007598 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007599 unsigned GVFlags =
7600 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007601
Chris Lattnerdfed4132009-07-10 07:38:24 +00007602 // If a reference to this global requires an extra load, we can't fold it.
7603 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007604 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007605
Chris Lattnerdfed4132009-07-10 07:38:24 +00007606 // If BaseGV requires a register for the PIC base, we cannot also have a
7607 // BaseReg specified.
7608 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007609 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007610
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007611 // If lower 4G is not available, then we must use rip-relative addressing.
7612 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7613 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007614 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007615
Chris Lattnerc9addb72007-03-30 23:15:24 +00007616 switch (AM.Scale) {
7617 case 0:
7618 case 1:
7619 case 2:
7620 case 4:
7621 case 8:
7622 // These scales always work.
7623 break;
7624 case 3:
7625 case 5:
7626 case 9:
7627 // These scales are formed with basereg+scalereg. Only accept if there is
7628 // no basereg yet.
7629 if (AM.HasBaseReg)
7630 return false;
7631 break;
7632 default: // Other stuff never works.
7633 return false;
7634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007635
Chris Lattnerc9addb72007-03-30 23:15:24 +00007636 return true;
7637}
7638
7639
Evan Cheng2bd122c2007-10-26 01:56:11 +00007640bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7641 if (!Ty1->isInteger() || !Ty2->isInteger())
7642 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007643 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7644 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007645 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007646 return false;
7647 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007648}
7649
Owen Andersone50ed302009-08-10 22:56:29 +00007650bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007651 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007652 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007653 unsigned NumBits1 = VT1.getSizeInBits();
7654 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007655 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007656 return false;
7657 return Subtarget->is64Bit() || NumBits1 < 64;
7658}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007659
Dan Gohman97121ba2009-04-08 00:15:30 +00007660bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007661 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007662 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007663}
7664
Owen Andersone50ed302009-08-10 22:56:29 +00007665bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007666 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007667 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007668}
7669
Owen Andersone50ed302009-08-10 22:56:29 +00007670bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007671 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007672 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007673}
7674
Evan Cheng60c07e12006-07-05 22:17:51 +00007675/// isShuffleMaskLegal - Targets can use this to indicate that they only
7676/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7677/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7678/// are assumed to be legal.
7679bool
Eric Christopherfd179292009-08-27 18:07:15 +00007680X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007681 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007682 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007683 if (VT.getSizeInBits() == 64)
7684 return false;
7685
Nate Begemana09008b2009-10-19 02:17:23 +00007686 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007687 return (VT.getVectorNumElements() == 2 ||
7688 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7689 isMOVLMask(M, VT) ||
7690 isSHUFPMask(M, VT) ||
7691 isPSHUFDMask(M, VT) ||
7692 isPSHUFHWMask(M, VT) ||
7693 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007694 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007695 isUNPCKLMask(M, VT) ||
7696 isUNPCKHMask(M, VT) ||
7697 isUNPCKL_v_undef_Mask(M, VT) ||
7698 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007699}
7700
Dan Gohman7d8143f2008-04-09 20:09:42 +00007701bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007702X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007703 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007704 unsigned NumElts = VT.getVectorNumElements();
7705 // FIXME: This collection of masks seems suspect.
7706 if (NumElts == 2)
7707 return true;
7708 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7709 return (isMOVLMask(Mask, VT) ||
7710 isCommutedMOVLMask(Mask, VT, true) ||
7711 isSHUFPMask(Mask, VT) ||
7712 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007713 }
7714 return false;
7715}
7716
7717//===----------------------------------------------------------------------===//
7718// X86 Scheduler Hooks
7719//===----------------------------------------------------------------------===//
7720
Mon P Wang63307c32008-05-05 19:05:59 +00007721// private utility function
7722MachineBasicBlock *
7723X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7724 MachineBasicBlock *MBB,
7725 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007726 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007727 unsigned LoadOpc,
7728 unsigned CXchgOpc,
7729 unsigned copyOpc,
7730 unsigned notOpc,
7731 unsigned EAXreg,
7732 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007733 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007734 // For the atomic bitwise operator, we generate
7735 // thisMBB:
7736 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007737 // ld t1 = [bitinstr.addr]
7738 // op t2 = t1, [bitinstr.val]
7739 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007740 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7741 // bz newMBB
7742 // fallthrough -->nextMBB
7743 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7744 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007745 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007746 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007747
Mon P Wang63307c32008-05-05 19:05:59 +00007748 /// First build the CFG
7749 MachineFunction *F = MBB->getParent();
7750 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007751 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7752 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7753 F->insert(MBBIter, newMBB);
7754 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007755
Mon P Wang63307c32008-05-05 19:05:59 +00007756 // Move all successors to thisMBB to nextMBB
7757 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007758
Mon P Wang63307c32008-05-05 19:05:59 +00007759 // Update thisMBB to fall through to newMBB
7760 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007761
Mon P Wang63307c32008-05-05 19:05:59 +00007762 // newMBB jumps to itself and fall through to nextMBB
7763 newMBB->addSuccessor(nextMBB);
7764 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007765
Mon P Wang63307c32008-05-05 19:05:59 +00007766 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007767 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007768 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007769 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007770 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007771 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007772 int numArgs = bInstr->getNumOperands() - 1;
7773 for (int i=0; i < numArgs; ++i)
7774 argOpers[i] = &bInstr->getOperand(i+1);
7775
7776 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007777 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7778 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007779
Dale Johannesen140be2d2008-08-19 18:47:28 +00007780 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007781 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007782 for (int i=0; i <= lastAddrIndx; ++i)
7783 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007784
Dale Johannesen140be2d2008-08-19 18:47:28 +00007785 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007786 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007787 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007788 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007789 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007790 tt = t1;
7791
Dale Johannesen140be2d2008-08-19 18:47:28 +00007792 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007793 assert((argOpers[valArgIndx]->isReg() ||
7794 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007795 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007796 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007797 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007798 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007799 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007800 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007801 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007802
Dale Johannesene4d209d2009-02-03 20:21:25 +00007803 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007804 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007805
Dale Johannesene4d209d2009-02-03 20:21:25 +00007806 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007807 for (int i=0; i <= lastAddrIndx; ++i)
7808 (*MIB).addOperand(*argOpers[i]);
7809 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007810 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007811 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7812 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007813
Dale Johannesene4d209d2009-02-03 20:21:25 +00007814 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007815 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007816
Mon P Wang63307c32008-05-05 19:05:59 +00007817 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007818 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007819
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007820 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007821 return nextMBB;
7822}
7823
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007824// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007825MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007826X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7827 MachineBasicBlock *MBB,
7828 unsigned regOpcL,
7829 unsigned regOpcH,
7830 unsigned immOpcL,
7831 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007832 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007833 // For the atomic bitwise operator, we generate
7834 // thisMBB (instructions are in pairs, except cmpxchg8b)
7835 // ld t1,t2 = [bitinstr.addr]
7836 // newMBB:
7837 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7838 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007839 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007840 // mov ECX, EBX <- t5, t6
7841 // mov EAX, EDX <- t1, t2
7842 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7843 // mov t3, t4 <- EAX, EDX
7844 // bz newMBB
7845 // result in out1, out2
7846 // fallthrough -->nextMBB
7847
7848 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7849 const unsigned LoadOpc = X86::MOV32rm;
7850 const unsigned copyOpc = X86::MOV32rr;
7851 const unsigned NotOpc = X86::NOT32r;
7852 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7853 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7854 MachineFunction::iterator MBBIter = MBB;
7855 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007856
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007857 /// First build the CFG
7858 MachineFunction *F = MBB->getParent();
7859 MachineBasicBlock *thisMBB = MBB;
7860 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7861 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7862 F->insert(MBBIter, newMBB);
7863 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007864
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007865 // Move all successors to thisMBB to nextMBB
7866 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007867
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007868 // Update thisMBB to fall through to newMBB
7869 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007870
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007871 // newMBB jumps to itself and fall through to nextMBB
7872 newMBB->addSuccessor(nextMBB);
7873 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007874
Dale Johannesene4d209d2009-02-03 20:21:25 +00007875 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007876 // Insert instructions into newMBB based on incoming instruction
7877 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007878 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007879 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007880 MachineOperand& dest1Oper = bInstr->getOperand(0);
7881 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007882 MachineOperand* argOpers[2 + X86AddrNumOperands];
7883 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007884 argOpers[i] = &bInstr->getOperand(i+2);
7885
Evan Chengad5b52f2010-01-08 19:14:57 +00007886 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007887 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007888
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007889 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007890 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007891 for (int i=0; i <= lastAddrIndx; ++i)
7892 (*MIB).addOperand(*argOpers[i]);
7893 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007894 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007895 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007896 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007897 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007898 MachineOperand newOp3 = *(argOpers[3]);
7899 if (newOp3.isImm())
7900 newOp3.setImm(newOp3.getImm()+4);
7901 else
7902 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007903 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007904 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007905
7906 // t3/4 are defined later, at the bottom of the loop
7907 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7908 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007909 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007910 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007911 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007912 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7913
Evan Cheng306b4ca2010-01-08 23:41:50 +00007914 // The subsequent operations should be using the destination registers of
7915 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007916 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007917 t1 = F->getRegInfo().createVirtualRegister(RC);
7918 t2 = F->getRegInfo().createVirtualRegister(RC);
7919 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7920 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007921 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007922 t1 = dest1Oper.getReg();
7923 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007924 }
7925
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007926 int valArgIndx = lastAddrIndx + 1;
7927 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007928 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007929 "invalid operand");
7930 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7931 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007932 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007933 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007934 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007935 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007936 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007937 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007938 (*MIB).addOperand(*argOpers[valArgIndx]);
7939 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007940 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007941 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007942 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007943 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007944 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007945 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007946 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007947 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007948 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007949 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007950
Dale Johannesene4d209d2009-02-03 20:21:25 +00007951 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007952 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007953 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007954 MIB.addReg(t2);
7955
Dale Johannesene4d209d2009-02-03 20:21:25 +00007956 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007957 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007959 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Dale Johannesene4d209d2009-02-03 20:21:25 +00007961 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007962 for (int i=0; i <= lastAddrIndx; ++i)
7963 (*MIB).addOperand(*argOpers[i]);
7964
7965 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007966 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7967 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007968
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007970 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007971 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007972 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007973
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007974 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007975 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007976
7977 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7978 return nextMBB;
7979}
7980
7981// private utility function
7982MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007983X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7984 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007985 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007986 // For the atomic min/max operator, we generate
7987 // thisMBB:
7988 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007989 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007990 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007991 // cmp t1, t2
7992 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007993 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007994 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7995 // bz newMBB
7996 // fallthrough -->nextMBB
7997 //
7998 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7999 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008000 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008001 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008002
Mon P Wang63307c32008-05-05 19:05:59 +00008003 /// First build the CFG
8004 MachineFunction *F = MBB->getParent();
8005 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008006 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8007 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8008 F->insert(MBBIter, newMBB);
8009 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008010
Dan Gohmand6708ea2009-08-15 01:38:56 +00008011 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008012 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008013
Mon P Wang63307c32008-05-05 19:05:59 +00008014 // Update thisMBB to fall through to newMBB
8015 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008016
Mon P Wang63307c32008-05-05 19:05:59 +00008017 // newMBB jumps to newMBB and fall through to nextMBB
8018 newMBB->addSuccessor(nextMBB);
8019 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008020
Dale Johannesene4d209d2009-02-03 20:21:25 +00008021 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008022 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008023 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008024 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008025 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008026 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008027 int numArgs = mInstr->getNumOperands() - 1;
8028 for (int i=0; i < numArgs; ++i)
8029 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008030
Mon P Wang63307c32008-05-05 19:05:59 +00008031 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008032 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8033 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008034
Mon P Wangab3e7472008-05-05 22:56:23 +00008035 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008036 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008037 for (int i=0; i <= lastAddrIndx; ++i)
8038 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008039
Mon P Wang63307c32008-05-05 19:05:59 +00008040 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008041 assert((argOpers[valArgIndx]->isReg() ||
8042 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008043 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008044
8045 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008046 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008047 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008048 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008049 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008050 (*MIB).addOperand(*argOpers[valArgIndx]);
8051
Dale Johannesene4d209d2009-02-03 20:21:25 +00008052 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008053 MIB.addReg(t1);
8054
Dale Johannesene4d209d2009-02-03 20:21:25 +00008055 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008056 MIB.addReg(t1);
8057 MIB.addReg(t2);
8058
8059 // Generate movc
8060 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008061 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008062 MIB.addReg(t2);
8063 MIB.addReg(t1);
8064
8065 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008066 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008067 for (int i=0; i <= lastAddrIndx; ++i)
8068 (*MIB).addOperand(*argOpers[i]);
8069 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008070 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008071 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8072 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008073
Dale Johannesene4d209d2009-02-03 20:21:25 +00008074 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008075 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008076
Mon P Wang63307c32008-05-05 19:05:59 +00008077 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008078 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008079
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008080 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008081 return nextMBB;
8082}
8083
Eric Christopherf83a5de2009-08-27 18:08:16 +00008084// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8085// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008086MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008087X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008088 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008089
8090 MachineFunction *F = BB->getParent();
8091 DebugLoc dl = MI->getDebugLoc();
8092 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8093
8094 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008095 if (memArg)
8096 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8097 else
8098 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008099
8100 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8101
8102 for (unsigned i = 0; i < numArgs; ++i) {
8103 MachineOperand &Op = MI->getOperand(i+1);
8104
8105 if (!(Op.isReg() && Op.isImplicit()))
8106 MIB.addOperand(Op);
8107 }
8108
8109 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8110 .addReg(X86::XMM0);
8111
8112 F->DeleteMachineInstr(MI);
8113
8114 return BB;
8115}
8116
8117MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008118X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8119 MachineInstr *MI,
8120 MachineBasicBlock *MBB) const {
8121 // Emit code to save XMM registers to the stack. The ABI says that the
8122 // number of registers to save is given in %al, so it's theoretically
8123 // possible to do an indirect jump trick to avoid saving all of them,
8124 // however this code takes a simpler approach and just executes all
8125 // of the stores if %al is non-zero. It's less code, and it's probably
8126 // easier on the hardware branch predictor, and stores aren't all that
8127 // expensive anyway.
8128
8129 // Create the new basic blocks. One block contains all the XMM stores,
8130 // and one block is the final destination regardless of whether any
8131 // stores were performed.
8132 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8133 MachineFunction *F = MBB->getParent();
8134 MachineFunction::iterator MBBIter = MBB;
8135 ++MBBIter;
8136 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8137 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8138 F->insert(MBBIter, XMMSaveMBB);
8139 F->insert(MBBIter, EndMBB);
8140
8141 // Set up the CFG.
8142 // Move any original successors of MBB to the end block.
8143 EndMBB->transferSuccessors(MBB);
8144 // The original block will now fall through to the XMM save block.
8145 MBB->addSuccessor(XMMSaveMBB);
8146 // The XMMSaveMBB will fall through to the end block.
8147 XMMSaveMBB->addSuccessor(EndMBB);
8148
8149 // Now add the instructions.
8150 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8151 DebugLoc DL = MI->getDebugLoc();
8152
8153 unsigned CountReg = MI->getOperand(0).getReg();
8154 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8155 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8156
8157 if (!Subtarget->isTargetWin64()) {
8158 // If %al is 0, branch around the XMM save block.
8159 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8160 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8161 MBB->addSuccessor(EndMBB);
8162 }
8163
8164 // In the XMM save block, save all the XMM argument registers.
8165 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8166 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008167 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008168 F->getMachineMemOperand(
8169 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8170 MachineMemOperand::MOStore, Offset,
8171 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008172 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8173 .addFrameIndex(RegSaveFrameIndex)
8174 .addImm(/*Scale=*/1)
8175 .addReg(/*IndexReg=*/0)
8176 .addImm(/*Disp=*/Offset)
8177 .addReg(/*Segment=*/0)
8178 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008179 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008180 }
8181
8182 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8183
8184 return EndMBB;
8185}
Mon P Wang63307c32008-05-05 19:05:59 +00008186
Evan Cheng60c07e12006-07-05 22:17:51 +00008187MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008188X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008189 MachineBasicBlock *BB,
8190 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008191 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8192 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008193
Chris Lattner52600972009-09-02 05:57:00 +00008194 // To "insert" a SELECT_CC instruction, we actually have to insert the
8195 // diamond control-flow pattern. The incoming instruction knows the
8196 // destination vreg to set, the condition code register to branch on, the
8197 // true/false values to select between, and a branch opcode to use.
8198 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8199 MachineFunction::iterator It = BB;
8200 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008201
Chris Lattner52600972009-09-02 05:57:00 +00008202 // thisMBB:
8203 // ...
8204 // TrueVal = ...
8205 // cmpTY ccX, r1, r2
8206 // bCC copy1MBB
8207 // fallthrough --> copy0MBB
8208 MachineBasicBlock *thisMBB = BB;
8209 MachineFunction *F = BB->getParent();
8210 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8211 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8212 unsigned Opc =
8213 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8214 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8215 F->insert(It, copy0MBB);
8216 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008217 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008218 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008219 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008220 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008221 E = BB->succ_end(); I != E; ++I) {
8222 EM->insert(std::make_pair(*I, sinkMBB));
8223 sinkMBB->addSuccessor(*I);
8224 }
8225 // Next, remove all successors of the current block, and add the true
8226 // and fallthrough blocks as its successors.
8227 while (!BB->succ_empty())
8228 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008229 // Add the true and fallthrough blocks as its successors.
8230 BB->addSuccessor(copy0MBB);
8231 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008232
Chris Lattner52600972009-09-02 05:57:00 +00008233 // copy0MBB:
8234 // %FalseValue = ...
8235 // # fallthrough to sinkMBB
8236 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008237
Chris Lattner52600972009-09-02 05:57:00 +00008238 // Update machine-CFG edges
8239 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008240
Chris Lattner52600972009-09-02 05:57:00 +00008241 // sinkMBB:
8242 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8243 // ...
8244 BB = sinkMBB;
8245 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8246 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8247 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8248
8249 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8250 return BB;
8251}
8252
8253
8254MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008255X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008256 MachineBasicBlock *BB,
8257 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008258 switch (MI->getOpcode()) {
8259 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008260 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008261 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008262 case X86::CMOV_FR32:
8263 case X86::CMOV_FR64:
8264 case X86::CMOV_V4F32:
8265 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008266 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008267 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008268
Dale Johannesen849f2142007-07-03 00:53:03 +00008269 case X86::FP32_TO_INT16_IN_MEM:
8270 case X86::FP32_TO_INT32_IN_MEM:
8271 case X86::FP32_TO_INT64_IN_MEM:
8272 case X86::FP64_TO_INT16_IN_MEM:
8273 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008274 case X86::FP64_TO_INT64_IN_MEM:
8275 case X86::FP80_TO_INT16_IN_MEM:
8276 case X86::FP80_TO_INT32_IN_MEM:
8277 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008278 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8279 DebugLoc DL = MI->getDebugLoc();
8280
Evan Cheng60c07e12006-07-05 22:17:51 +00008281 // Change the floating point control register to use "round towards zero"
8282 // mode when truncating to an integer value.
8283 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008284 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008285 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008286
8287 // Load the old value of the high byte of the control word...
8288 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008289 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008290 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008291 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008292
8293 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008294 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008295 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008296
8297 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008298 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008299
8300 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008301 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008302 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008303
8304 // Get the X86 opcode to use.
8305 unsigned Opc;
8306 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008307 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008308 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8309 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8310 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8311 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8312 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8313 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008314 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8315 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8316 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008317 }
8318
8319 X86AddressMode AM;
8320 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008321 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008322 AM.BaseType = X86AddressMode::RegBase;
8323 AM.Base.Reg = Op.getReg();
8324 } else {
8325 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008326 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008327 }
8328 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008329 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008330 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008331 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008332 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008333 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008334 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008335 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008336 AM.GV = Op.getGlobal();
8337 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008338 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008339 }
Chris Lattner52600972009-09-02 05:57:00 +00008340 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008341 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008342
8343 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008344 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008345
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008346 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008347 return BB;
8348 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008349 // String/text processing lowering.
8350 case X86::PCMPISTRM128REG:
8351 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8352 case X86::PCMPISTRM128MEM:
8353 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8354 case X86::PCMPESTRM128REG:
8355 return EmitPCMP(MI, BB, 5, false /* in mem */);
8356 case X86::PCMPESTRM128MEM:
8357 return EmitPCMP(MI, BB, 5, true /* in mem */);
8358
8359 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008360 case X86::ATOMAND32:
8361 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008362 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008363 X86::LCMPXCHG32, X86::MOV32rr,
8364 X86::NOT32r, X86::EAX,
8365 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008366 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008367 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8368 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008369 X86::LCMPXCHG32, X86::MOV32rr,
8370 X86::NOT32r, X86::EAX,
8371 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008372 case X86::ATOMXOR32:
8373 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008374 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008375 X86::LCMPXCHG32, X86::MOV32rr,
8376 X86::NOT32r, X86::EAX,
8377 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008378 case X86::ATOMNAND32:
8379 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008380 X86::AND32ri, X86::MOV32rm,
8381 X86::LCMPXCHG32, X86::MOV32rr,
8382 X86::NOT32r, X86::EAX,
8383 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008384 case X86::ATOMMIN32:
8385 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8386 case X86::ATOMMAX32:
8387 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8388 case X86::ATOMUMIN32:
8389 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8390 case X86::ATOMUMAX32:
8391 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008392
8393 case X86::ATOMAND16:
8394 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8395 X86::AND16ri, X86::MOV16rm,
8396 X86::LCMPXCHG16, X86::MOV16rr,
8397 X86::NOT16r, X86::AX,
8398 X86::GR16RegisterClass);
8399 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008400 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008401 X86::OR16ri, X86::MOV16rm,
8402 X86::LCMPXCHG16, X86::MOV16rr,
8403 X86::NOT16r, X86::AX,
8404 X86::GR16RegisterClass);
8405 case X86::ATOMXOR16:
8406 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8407 X86::XOR16ri, X86::MOV16rm,
8408 X86::LCMPXCHG16, X86::MOV16rr,
8409 X86::NOT16r, X86::AX,
8410 X86::GR16RegisterClass);
8411 case X86::ATOMNAND16:
8412 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8413 X86::AND16ri, X86::MOV16rm,
8414 X86::LCMPXCHG16, X86::MOV16rr,
8415 X86::NOT16r, X86::AX,
8416 X86::GR16RegisterClass, true);
8417 case X86::ATOMMIN16:
8418 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8419 case X86::ATOMMAX16:
8420 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8421 case X86::ATOMUMIN16:
8422 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8423 case X86::ATOMUMAX16:
8424 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8425
8426 case X86::ATOMAND8:
8427 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8428 X86::AND8ri, X86::MOV8rm,
8429 X86::LCMPXCHG8, X86::MOV8rr,
8430 X86::NOT8r, X86::AL,
8431 X86::GR8RegisterClass);
8432 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008433 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008434 X86::OR8ri, X86::MOV8rm,
8435 X86::LCMPXCHG8, X86::MOV8rr,
8436 X86::NOT8r, X86::AL,
8437 X86::GR8RegisterClass);
8438 case X86::ATOMXOR8:
8439 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8440 X86::XOR8ri, X86::MOV8rm,
8441 X86::LCMPXCHG8, X86::MOV8rr,
8442 X86::NOT8r, X86::AL,
8443 X86::GR8RegisterClass);
8444 case X86::ATOMNAND8:
8445 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8446 X86::AND8ri, X86::MOV8rm,
8447 X86::LCMPXCHG8, X86::MOV8rr,
8448 X86::NOT8r, X86::AL,
8449 X86::GR8RegisterClass, true);
8450 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008451 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008452 case X86::ATOMAND64:
8453 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008454 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008455 X86::LCMPXCHG64, X86::MOV64rr,
8456 X86::NOT64r, X86::RAX,
8457 X86::GR64RegisterClass);
8458 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008459 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8460 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008461 X86::LCMPXCHG64, X86::MOV64rr,
8462 X86::NOT64r, X86::RAX,
8463 X86::GR64RegisterClass);
8464 case X86::ATOMXOR64:
8465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008466 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008467 X86::LCMPXCHG64, X86::MOV64rr,
8468 X86::NOT64r, X86::RAX,
8469 X86::GR64RegisterClass);
8470 case X86::ATOMNAND64:
8471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8472 X86::AND64ri32, X86::MOV64rm,
8473 X86::LCMPXCHG64, X86::MOV64rr,
8474 X86::NOT64r, X86::RAX,
8475 X86::GR64RegisterClass, true);
8476 case X86::ATOMMIN64:
8477 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8478 case X86::ATOMMAX64:
8479 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8480 case X86::ATOMUMIN64:
8481 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8482 case X86::ATOMUMAX64:
8483 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008484
8485 // This group does 64-bit operations on a 32-bit host.
8486 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008487 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008488 X86::AND32rr, X86::AND32rr,
8489 X86::AND32ri, X86::AND32ri,
8490 false);
8491 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008492 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008493 X86::OR32rr, X86::OR32rr,
8494 X86::OR32ri, X86::OR32ri,
8495 false);
8496 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008497 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008498 X86::XOR32rr, X86::XOR32rr,
8499 X86::XOR32ri, X86::XOR32ri,
8500 false);
8501 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008502 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008503 X86::AND32rr, X86::AND32rr,
8504 X86::AND32ri, X86::AND32ri,
8505 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008506 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008507 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008508 X86::ADD32rr, X86::ADC32rr,
8509 X86::ADD32ri, X86::ADC32ri,
8510 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008511 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008512 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008513 X86::SUB32rr, X86::SBB32rr,
8514 X86::SUB32ri, X86::SBB32ri,
8515 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008516 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008517 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008518 X86::MOV32rr, X86::MOV32rr,
8519 X86::MOV32ri, X86::MOV32ri,
8520 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008521 case X86::VASTART_SAVE_XMM_REGS:
8522 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008523 }
8524}
8525
8526//===----------------------------------------------------------------------===//
8527// X86 Optimization Hooks
8528//===----------------------------------------------------------------------===//
8529
Dan Gohman475871a2008-07-27 21:46:04 +00008530void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008531 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008532 APInt &KnownZero,
8533 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008534 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008535 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008536 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008537 assert((Opc >= ISD::BUILTIN_OP_END ||
8538 Opc == ISD::INTRINSIC_WO_CHAIN ||
8539 Opc == ISD::INTRINSIC_W_CHAIN ||
8540 Opc == ISD::INTRINSIC_VOID) &&
8541 "Should use MaskedValueIsZero if you don't know whether Op"
8542 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008543
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008544 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008545 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008546 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008547 case X86ISD::ADD:
8548 case X86ISD::SUB:
8549 case X86ISD::SMUL:
8550 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008551 case X86ISD::INC:
8552 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008553 case X86ISD::OR:
8554 case X86ISD::XOR:
8555 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008556 // These nodes' second result is a boolean.
8557 if (Op.getResNo() == 0)
8558 break;
8559 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008560 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008561 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8562 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008563 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008564 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008565}
Chris Lattner259e97c2006-01-31 19:43:35 +00008566
Evan Cheng206ee9d2006-07-07 08:33:52 +00008567/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008568/// node is a GlobalAddress + offset.
8569bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8570 GlobalValue* &GA, int64_t &Offset) const{
8571 if (N->getOpcode() == X86ISD::Wrapper) {
8572 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008573 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008574 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008575 return true;
8576 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008577 }
Evan Chengad4196b2008-05-12 19:56:52 +00008578 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008579}
8580
Nate Begeman9008ca62009-04-27 18:41:29 +00008581static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008582 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008583 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008584 SelectionDAG &DAG, MachineFrameInfo *MFI,
8585 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008586 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008587 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008588 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008589 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008590 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008591 return false;
8592 continue;
8593 }
8594
Dan Gohman475871a2008-07-27 21:46:04 +00008595 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008596 if (!Elt.getNode() ||
8597 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008598 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008599 if (!LDBase) {
8600 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008601 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008602 LDBase = cast<LoadSDNode>(Elt.getNode());
8603 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008604 continue;
8605 }
8606 if (Elt.getOpcode() == ISD::UNDEF)
8607 continue;
8608
Nate Begemanabc01992009-06-05 21:37:30 +00008609 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008610 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008611 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008612 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008613 }
8614 return true;
8615}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008616
8617/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8618/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8619/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008620/// order. In the case of v2i64, it will see if it can rewrite the
8621/// shuffle to be an appropriate build vector so it can take advantage of
8622// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008623static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008624 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008625 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008626 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008627 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008628 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8629 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008630
Eli Friedman7a5e5552009-06-07 06:52:44 +00008631 if (VT.getSizeInBits() != 128)
8632 return SDValue();
8633
Mon P Wang1e955802009-04-03 02:43:30 +00008634 // Try to combine a vector_shuffle into a 128-bit load.
8635 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008636 LoadSDNode *LD = NULL;
8637 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008638 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008639 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008640 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008641
Eli Friedman7a5e5552009-06-07 06:52:44 +00008642 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008643 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008644 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8645 LD->getSrcValue(), LD->getSrcValueOffset(),
8646 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008647 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008648 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008649 LD->isVolatile(), LD->getAlignment());
8650 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008651 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008652 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8653 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008654 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8655 }
8656 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008657}
Evan Chengd880b972008-05-09 21:53:03 +00008658
Chris Lattner83e6c992006-10-04 06:57:07 +00008659/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008660static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008661 const X86Subtarget *Subtarget) {
8662 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008663 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008664 // Get the LHS/RHS of the select.
8665 SDValue LHS = N->getOperand(1);
8666 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008667
Dan Gohman670e5392009-09-21 18:03:22 +00008668 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8669 // instructions have the peculiarity that if either operand is a NaN,
8670 // they chose what we call the RHS operand (and as such are not symmetric).
8671 // It happens that this matches the semantics of the common C idiom
8672 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008673 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008674 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008675 Cond.getOpcode() == ISD::SETCC) {
8676 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008677
Chris Lattner47b4ce82009-03-11 05:48:52 +00008678 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008679 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008680 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8681 switch (CC) {
8682 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008683 case ISD::SETULT:
8684 // This can be a min if we can prove that at least one of the operands
8685 // is not a nan.
8686 if (!FiniteOnlyFPMath()) {
8687 if (DAG.isKnownNeverNaN(RHS)) {
8688 // Put the potential NaN in the RHS so that SSE will preserve it.
8689 std::swap(LHS, RHS);
8690 } else if (!DAG.isKnownNeverNaN(LHS))
8691 break;
8692 }
8693 Opcode = X86ISD::FMIN;
8694 break;
8695 case ISD::SETOLE:
8696 // This can be a min if we can prove that at least one of the operands
8697 // is not a nan.
8698 if (!FiniteOnlyFPMath()) {
8699 if (DAG.isKnownNeverNaN(LHS)) {
8700 // Put the potential NaN in the RHS so that SSE will preserve it.
8701 std::swap(LHS, RHS);
8702 } else if (!DAG.isKnownNeverNaN(RHS))
8703 break;
8704 }
8705 Opcode = X86ISD::FMIN;
8706 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008707 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008708 // This can be a min, but if either operand is a NaN we need it to
8709 // preserve the original LHS.
8710 std::swap(LHS, RHS);
8711 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008712 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008713 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008714 Opcode = X86ISD::FMIN;
8715 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008716
Dan Gohman670e5392009-09-21 18:03:22 +00008717 case ISD::SETOGE:
8718 // This can be a max if we can prove that at least one of the operands
8719 // is not a nan.
8720 if (!FiniteOnlyFPMath()) {
8721 if (DAG.isKnownNeverNaN(LHS)) {
8722 // Put the potential NaN in the RHS so that SSE will preserve it.
8723 std::swap(LHS, RHS);
8724 } else if (!DAG.isKnownNeverNaN(RHS))
8725 break;
8726 }
8727 Opcode = X86ISD::FMAX;
8728 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008729 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008730 // This can be a max if we can prove that at least one of the operands
8731 // is not a nan.
8732 if (!FiniteOnlyFPMath()) {
8733 if (DAG.isKnownNeverNaN(RHS)) {
8734 // Put the potential NaN in the RHS so that SSE will preserve it.
8735 std::swap(LHS, RHS);
8736 } else if (!DAG.isKnownNeverNaN(LHS))
8737 break;
8738 }
8739 Opcode = X86ISD::FMAX;
8740 break;
8741 case ISD::SETUGE:
8742 // This can be a max, but if either operand is a NaN we need it to
8743 // preserve the original LHS.
8744 std::swap(LHS, RHS);
8745 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008746 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008747 case ISD::SETGE:
8748 Opcode = X86ISD::FMAX;
8749 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008750 }
Dan Gohman670e5392009-09-21 18:03:22 +00008751 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008752 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8753 switch (CC) {
8754 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008755 case ISD::SETOGE:
8756 // This can be a min if we can prove that at least one of the operands
8757 // is not a nan.
8758 if (!FiniteOnlyFPMath()) {
8759 if (DAG.isKnownNeverNaN(RHS)) {
8760 // Put the potential NaN in the RHS so that SSE will preserve it.
8761 std::swap(LHS, RHS);
8762 } else if (!DAG.isKnownNeverNaN(LHS))
8763 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008764 }
Dan Gohman670e5392009-09-21 18:03:22 +00008765 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008766 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008767 case ISD::SETUGT:
8768 // This can be a min if we can prove that at least one of the operands
8769 // is not a nan.
8770 if (!FiniteOnlyFPMath()) {
8771 if (DAG.isKnownNeverNaN(LHS)) {
8772 // Put the potential NaN in the RHS so that SSE will preserve it.
8773 std::swap(LHS, RHS);
8774 } else if (!DAG.isKnownNeverNaN(RHS))
8775 break;
8776 }
8777 Opcode = X86ISD::FMIN;
8778 break;
8779 case ISD::SETUGE:
8780 // This can be a min, but if either operand is a NaN we need it to
8781 // preserve the original LHS.
8782 std::swap(LHS, RHS);
8783 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008784 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008785 case ISD::SETGE:
8786 Opcode = X86ISD::FMIN;
8787 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008788
Dan Gohman670e5392009-09-21 18:03:22 +00008789 case ISD::SETULT:
8790 // This can be a max if we can prove that at least one of the operands
8791 // is not a nan.
8792 if (!FiniteOnlyFPMath()) {
8793 if (DAG.isKnownNeverNaN(LHS)) {
8794 // Put the potential NaN in the RHS so that SSE will preserve it.
8795 std::swap(LHS, RHS);
8796 } else if (!DAG.isKnownNeverNaN(RHS))
8797 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008798 }
Dan Gohman670e5392009-09-21 18:03:22 +00008799 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008800 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008801 case ISD::SETOLE:
8802 // This can be a max if we can prove that at least one of the operands
8803 // is not a nan.
8804 if (!FiniteOnlyFPMath()) {
8805 if (DAG.isKnownNeverNaN(RHS)) {
8806 // Put the potential NaN in the RHS so that SSE will preserve it.
8807 std::swap(LHS, RHS);
8808 } else if (!DAG.isKnownNeverNaN(LHS))
8809 break;
8810 }
8811 Opcode = X86ISD::FMAX;
8812 break;
8813 case ISD::SETULE:
8814 // This can be a max, but if either operand is a NaN we need it to
8815 // preserve the original LHS.
8816 std::swap(LHS, RHS);
8817 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008818 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008819 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008820 Opcode = X86ISD::FMAX;
8821 break;
8822 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008823 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008824
Chris Lattner47b4ce82009-03-11 05:48:52 +00008825 if (Opcode)
8826 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008827 }
Eric Christopherfd179292009-08-27 18:07:15 +00008828
Chris Lattnerd1980a52009-03-12 06:52:53 +00008829 // If this is a select between two integer constants, try to do some
8830 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008831 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8832 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008833 // Don't do this for crazy integer types.
8834 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8835 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008836 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008837 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008838
Chris Lattnercee56e72009-03-13 05:53:31 +00008839 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008840 // Efficiently invertible.
8841 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8842 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8843 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8844 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008845 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008846 }
Eric Christopherfd179292009-08-27 18:07:15 +00008847
Chris Lattnerd1980a52009-03-12 06:52:53 +00008848 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008849 if (FalseC->getAPIntValue() == 0 &&
8850 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008851 if (NeedsCondInvert) // Invert the condition if needed.
8852 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8853 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008854
Chris Lattnerd1980a52009-03-12 06:52:53 +00008855 // Zero extend the condition if needed.
8856 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008857
Chris Lattnercee56e72009-03-13 05:53:31 +00008858 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008859 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008860 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008861 }
Eric Christopherfd179292009-08-27 18:07:15 +00008862
Chris Lattner97a29a52009-03-13 05:22:11 +00008863 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008864 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008865 if (NeedsCondInvert) // Invert the condition if needed.
8866 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8867 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008868
Chris Lattner97a29a52009-03-13 05:22:11 +00008869 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008870 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8871 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008872 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008873 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008874 }
Eric Christopherfd179292009-08-27 18:07:15 +00008875
Chris Lattnercee56e72009-03-13 05:53:31 +00008876 // Optimize cases that will turn into an LEA instruction. This requires
8877 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008878 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008879 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008880 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008881
Chris Lattnercee56e72009-03-13 05:53:31 +00008882 bool isFastMultiplier = false;
8883 if (Diff < 10) {
8884 switch ((unsigned char)Diff) {
8885 default: break;
8886 case 1: // result = add base, cond
8887 case 2: // result = lea base( , cond*2)
8888 case 3: // result = lea base(cond, cond*2)
8889 case 4: // result = lea base( , cond*4)
8890 case 5: // result = lea base(cond, cond*4)
8891 case 8: // result = lea base( , cond*8)
8892 case 9: // result = lea base(cond, cond*8)
8893 isFastMultiplier = true;
8894 break;
8895 }
8896 }
Eric Christopherfd179292009-08-27 18:07:15 +00008897
Chris Lattnercee56e72009-03-13 05:53:31 +00008898 if (isFastMultiplier) {
8899 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8900 if (NeedsCondInvert) // Invert the condition if needed.
8901 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8902 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008903
Chris Lattnercee56e72009-03-13 05:53:31 +00008904 // Zero extend the condition if needed.
8905 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8906 Cond);
8907 // Scale the condition by the difference.
8908 if (Diff != 1)
8909 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8910 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008911
Chris Lattnercee56e72009-03-13 05:53:31 +00008912 // Add the base if non-zero.
8913 if (FalseC->getAPIntValue() != 0)
8914 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8915 SDValue(FalseC, 0));
8916 return Cond;
8917 }
Eric Christopherfd179292009-08-27 18:07:15 +00008918 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008919 }
8920 }
Eric Christopherfd179292009-08-27 18:07:15 +00008921
Dan Gohman475871a2008-07-27 21:46:04 +00008922 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008923}
8924
Chris Lattnerd1980a52009-03-12 06:52:53 +00008925/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8926static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8927 TargetLowering::DAGCombinerInfo &DCI) {
8928 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008929
Chris Lattnerd1980a52009-03-12 06:52:53 +00008930 // If the flag operand isn't dead, don't touch this CMOV.
8931 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8932 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008933
Chris Lattnerd1980a52009-03-12 06:52:53 +00008934 // If this is a select between two integer constants, try to do some
8935 // optimizations. Note that the operands are ordered the opposite of SELECT
8936 // operands.
8937 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8938 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8939 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8940 // larger than FalseC (the false value).
8941 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008942
Chris Lattnerd1980a52009-03-12 06:52:53 +00008943 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8944 CC = X86::GetOppositeBranchCondition(CC);
8945 std::swap(TrueC, FalseC);
8946 }
Eric Christopherfd179292009-08-27 18:07:15 +00008947
Chris Lattnerd1980a52009-03-12 06:52:53 +00008948 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008949 // This is efficient for any integer data type (including i8/i16) and
8950 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008951 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8952 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008953 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8954 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008955
Chris Lattnerd1980a52009-03-12 06:52:53 +00008956 // Zero extend the condition if needed.
8957 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008958
Chris Lattnerd1980a52009-03-12 06:52:53 +00008959 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8960 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008961 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008962 if (N->getNumValues() == 2) // Dead flag value?
8963 return DCI.CombineTo(N, Cond, SDValue());
8964 return Cond;
8965 }
Eric Christopherfd179292009-08-27 18:07:15 +00008966
Chris Lattnercee56e72009-03-13 05:53:31 +00008967 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8968 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008969 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8970 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008971 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8972 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008973
Chris Lattner97a29a52009-03-13 05:22:11 +00008974 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008975 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8976 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008977 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8978 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00008979
Chris Lattner97a29a52009-03-13 05:22:11 +00008980 if (N->getNumValues() == 2) // Dead flag value?
8981 return DCI.CombineTo(N, Cond, SDValue());
8982 return Cond;
8983 }
Eric Christopherfd179292009-08-27 18:07:15 +00008984
Chris Lattnercee56e72009-03-13 05:53:31 +00008985 // Optimize cases that will turn into an LEA instruction. This requires
8986 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008987 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008988 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008989 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008990
Chris Lattnercee56e72009-03-13 05:53:31 +00008991 bool isFastMultiplier = false;
8992 if (Diff < 10) {
8993 switch ((unsigned char)Diff) {
8994 default: break;
8995 case 1: // result = add base, cond
8996 case 2: // result = lea base( , cond*2)
8997 case 3: // result = lea base(cond, cond*2)
8998 case 4: // result = lea base( , cond*4)
8999 case 5: // result = lea base(cond, cond*4)
9000 case 8: // result = lea base( , cond*8)
9001 case 9: // result = lea base(cond, cond*8)
9002 isFastMultiplier = true;
9003 break;
9004 }
9005 }
Eric Christopherfd179292009-08-27 18:07:15 +00009006
Chris Lattnercee56e72009-03-13 05:53:31 +00009007 if (isFastMultiplier) {
9008 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9009 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009010 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9011 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009012 // Zero extend the condition if needed.
9013 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9014 Cond);
9015 // Scale the condition by the difference.
9016 if (Diff != 1)
9017 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9018 DAG.getConstant(Diff, Cond.getValueType()));
9019
9020 // Add the base if non-zero.
9021 if (FalseC->getAPIntValue() != 0)
9022 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9023 SDValue(FalseC, 0));
9024 if (N->getNumValues() == 2) // Dead flag value?
9025 return DCI.CombineTo(N, Cond, SDValue());
9026 return Cond;
9027 }
Eric Christopherfd179292009-08-27 18:07:15 +00009028 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009029 }
9030 }
9031 return SDValue();
9032}
9033
9034
Evan Cheng0b0cd912009-03-28 05:57:29 +00009035/// PerformMulCombine - Optimize a single multiply with constant into two
9036/// in order to implement it with two cheaper instructions, e.g.
9037/// LEA + SHL, LEA + LEA.
9038static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9039 TargetLowering::DAGCombinerInfo &DCI) {
9040 if (DAG.getMachineFunction().
9041 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9042 return SDValue();
9043
9044 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9045 return SDValue();
9046
Owen Andersone50ed302009-08-10 22:56:29 +00009047 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009048 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009049 return SDValue();
9050
9051 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9052 if (!C)
9053 return SDValue();
9054 uint64_t MulAmt = C->getZExtValue();
9055 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9056 return SDValue();
9057
9058 uint64_t MulAmt1 = 0;
9059 uint64_t MulAmt2 = 0;
9060 if ((MulAmt % 9) == 0) {
9061 MulAmt1 = 9;
9062 MulAmt2 = MulAmt / 9;
9063 } else if ((MulAmt % 5) == 0) {
9064 MulAmt1 = 5;
9065 MulAmt2 = MulAmt / 5;
9066 } else if ((MulAmt % 3) == 0) {
9067 MulAmt1 = 3;
9068 MulAmt2 = MulAmt / 3;
9069 }
9070 if (MulAmt2 &&
9071 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9072 DebugLoc DL = N->getDebugLoc();
9073
9074 if (isPowerOf2_64(MulAmt2) &&
9075 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9076 // If second multiplifer is pow2, issue it first. We want the multiply by
9077 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9078 // is an add.
9079 std::swap(MulAmt1, MulAmt2);
9080
9081 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009082 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009083 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009084 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009085 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009086 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009087 DAG.getConstant(MulAmt1, VT));
9088
Eric Christopherfd179292009-08-27 18:07:15 +00009089 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009090 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009091 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009092 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009093 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009094 DAG.getConstant(MulAmt2, VT));
9095
9096 // Do not add new nodes to DAG combiner worklist.
9097 DCI.CombineTo(N, NewMul, false);
9098 }
9099 return SDValue();
9100}
9101
Evan Chengad9c0a32009-12-15 00:53:42 +00009102static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9103 SDValue N0 = N->getOperand(0);
9104 SDValue N1 = N->getOperand(1);
9105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9106 EVT VT = N0.getValueType();
9107
9108 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9109 // since the result of setcc_c is all zero's or all ones.
9110 if (N1C && N0.getOpcode() == ISD::AND &&
9111 N0.getOperand(1).getOpcode() == ISD::Constant) {
9112 SDValue N00 = N0.getOperand(0);
9113 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9114 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9115 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9116 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9117 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9118 APInt ShAmt = N1C->getAPIntValue();
9119 Mask = Mask.shl(ShAmt);
9120 if (Mask != 0)
9121 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9122 N00, DAG.getConstant(Mask, VT));
9123 }
9124 }
9125
9126 return SDValue();
9127}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009128
Nate Begeman740ab032009-01-26 00:52:55 +00009129/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9130/// when possible.
9131static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9132 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009133 EVT VT = N->getValueType(0);
9134 if (!VT.isVector() && VT.isInteger() &&
9135 N->getOpcode() == ISD::SHL)
9136 return PerformSHLCombine(N, DAG);
9137
Nate Begeman740ab032009-01-26 00:52:55 +00009138 // On X86 with SSE2 support, we can transform this to a vector shift if
9139 // all elements are shifted by the same amount. We can't do this in legalize
9140 // because the a constant vector is typically transformed to a constant pool
9141 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009142 if (!Subtarget->hasSSE2())
9143 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009144
Owen Anderson825b72b2009-08-11 20:47:22 +00009145 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009146 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009147
Mon P Wang3becd092009-01-28 08:12:05 +00009148 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009149 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009150 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009151 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009152 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9153 unsigned NumElts = VT.getVectorNumElements();
9154 unsigned i = 0;
9155 for (; i != NumElts; ++i) {
9156 SDValue Arg = ShAmtOp.getOperand(i);
9157 if (Arg.getOpcode() == ISD::UNDEF) continue;
9158 BaseShAmt = Arg;
9159 break;
9160 }
9161 for (; i != NumElts; ++i) {
9162 SDValue Arg = ShAmtOp.getOperand(i);
9163 if (Arg.getOpcode() == ISD::UNDEF) continue;
9164 if (Arg != BaseShAmt) {
9165 return SDValue();
9166 }
9167 }
9168 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009169 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009170 SDValue InVec = ShAmtOp.getOperand(0);
9171 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9172 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9173 unsigned i = 0;
9174 for (; i != NumElts; ++i) {
9175 SDValue Arg = InVec.getOperand(i);
9176 if (Arg.getOpcode() == ISD::UNDEF) continue;
9177 BaseShAmt = Arg;
9178 break;
9179 }
9180 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9182 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9183 if (C->getZExtValue() == SplatIdx)
9184 BaseShAmt = InVec.getOperand(1);
9185 }
9186 }
9187 if (BaseShAmt.getNode() == 0)
9188 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9189 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009190 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009191 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009192
Mon P Wangefa42202009-09-03 19:56:25 +00009193 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009194 if (EltVT.bitsGT(MVT::i32))
9195 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9196 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009197 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009198
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009199 // The shift amount is identical so we can do a vector shift.
9200 SDValue ValOp = N->getOperand(0);
9201 switch (N->getOpcode()) {
9202 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009203 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009204 break;
9205 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009206 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009207 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009208 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009209 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009210 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009211 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009212 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009213 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009214 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009215 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009216 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009217 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009218 break;
9219 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009220 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009221 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009222 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009223 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009224 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009225 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009227 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009228 break;
9229 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009230 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009231 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009232 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009233 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009234 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009235 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009236 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009237 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009238 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009239 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009240 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009241 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009242 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009243 }
9244 return SDValue();
9245}
9246
Evan Cheng760d1942010-01-04 21:22:48 +00009247static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9248 const X86Subtarget *Subtarget) {
9249 EVT VT = N->getValueType(0);
9250 if (VT != MVT::i64 || !Subtarget->is64Bit())
9251 return SDValue();
9252
9253 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9254 SDValue N0 = N->getOperand(0);
9255 SDValue N1 = N->getOperand(1);
9256 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9257 std::swap(N0, N1);
9258 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9259 return SDValue();
9260
9261 SDValue ShAmt0 = N0.getOperand(1);
9262 if (ShAmt0.getValueType() != MVT::i8)
9263 return SDValue();
9264 SDValue ShAmt1 = N1.getOperand(1);
9265 if (ShAmt1.getValueType() != MVT::i8)
9266 return SDValue();
9267 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9268 ShAmt0 = ShAmt0.getOperand(0);
9269 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9270 ShAmt1 = ShAmt1.getOperand(0);
9271
9272 DebugLoc DL = N->getDebugLoc();
9273 unsigned Opc = X86ISD::SHLD;
9274 SDValue Op0 = N0.getOperand(0);
9275 SDValue Op1 = N1.getOperand(0);
9276 if (ShAmt0.getOpcode() == ISD::SUB) {
9277 Opc = X86ISD::SHRD;
9278 std::swap(Op0, Op1);
9279 std::swap(ShAmt0, ShAmt1);
9280 }
9281
9282 if (ShAmt1.getOpcode() == ISD::SUB) {
9283 SDValue Sum = ShAmt1.getOperand(0);
9284 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9285 if (SumC->getSExtValue() == 64 &&
9286 ShAmt1.getOperand(1) == ShAmt0)
9287 return DAG.getNode(Opc, DL, VT,
9288 Op0, Op1,
9289 DAG.getNode(ISD::TRUNCATE, DL,
9290 MVT::i8, ShAmt0));
9291 }
9292 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9293 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9294 if (ShAmt0C &&
9295 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9296 return DAG.getNode(Opc, DL, VT,
9297 N0.getOperand(0), N1.getOperand(0),
9298 DAG.getNode(ISD::TRUNCATE, DL,
9299 MVT::i8, ShAmt0));
9300 }
9301
9302 return SDValue();
9303}
9304
Chris Lattner149a4e52008-02-22 02:09:43 +00009305/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009306static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009307 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009308 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9309 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009310 // A preferable solution to the general problem is to figure out the right
9311 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009312
9313 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009314 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009315 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009316 if (VT.getSizeInBits() != 64)
9317 return SDValue();
9318
Devang Patel578efa92009-06-05 21:57:13 +00009319 const Function *F = DAG.getMachineFunction().getFunction();
9320 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009321 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009322 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009323 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009324 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009325 isa<LoadSDNode>(St->getValue()) &&
9326 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9327 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009328 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009329 LoadSDNode *Ld = 0;
9330 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009331 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009332 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009333 // Must be a store of a load. We currently handle two cases: the load
9334 // is a direct child, and it's under an intervening TokenFactor. It is
9335 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009336 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009337 Ld = cast<LoadSDNode>(St->getChain());
9338 else if (St->getValue().hasOneUse() &&
9339 ChainVal->getOpcode() == ISD::TokenFactor) {
9340 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009341 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009342 TokenFactorIndex = i;
9343 Ld = cast<LoadSDNode>(St->getValue());
9344 } else
9345 Ops.push_back(ChainVal->getOperand(i));
9346 }
9347 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009348
Evan Cheng536e6672009-03-12 05:59:15 +00009349 if (!Ld || !ISD::isNormalLoad(Ld))
9350 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009351
Evan Cheng536e6672009-03-12 05:59:15 +00009352 // If this is not the MMX case, i.e. we are just turning i64 load/store
9353 // into f64 load/store, avoid the transformation if there are multiple
9354 // uses of the loaded value.
9355 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9356 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009357
Evan Cheng536e6672009-03-12 05:59:15 +00009358 DebugLoc LdDL = Ld->getDebugLoc();
9359 DebugLoc StDL = N->getDebugLoc();
9360 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9361 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9362 // pair instead.
9363 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009364 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009365 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9366 Ld->getBasePtr(), Ld->getSrcValue(),
9367 Ld->getSrcValueOffset(), Ld->isVolatile(),
9368 Ld->getAlignment());
9369 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009370 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009371 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009372 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009373 Ops.size());
9374 }
Evan Cheng536e6672009-03-12 05:59:15 +00009375 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009376 St->getSrcValue(), St->getSrcValueOffset(),
9377 St->isVolatile(), St->getAlignment());
9378 }
Evan Cheng536e6672009-03-12 05:59:15 +00009379
9380 // Otherwise, lower to two pairs of 32-bit loads / stores.
9381 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009382 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9383 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009384
Owen Anderson825b72b2009-08-11 20:47:22 +00009385 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009386 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9387 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009389 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9390 Ld->isVolatile(),
9391 MinAlign(Ld->getAlignment(), 4));
9392
9393 SDValue NewChain = LoLd.getValue(1);
9394 if (TokenFactorIndex != -1) {
9395 Ops.push_back(LoLd);
9396 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009398 Ops.size());
9399 }
9400
9401 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009402 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9403 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009404
9405 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9406 St->getSrcValue(), St->getSrcValueOffset(),
9407 St->isVolatile(), St->getAlignment());
9408 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9409 St->getSrcValue(),
9410 St->getSrcValueOffset() + 4,
9411 St->isVolatile(),
9412 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009414 }
Dan Gohman475871a2008-07-27 21:46:04 +00009415 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009416}
9417
Chris Lattner6cf73262008-01-25 06:14:17 +00009418/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9419/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009420static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009421 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9422 // F[X]OR(0.0, x) -> x
9423 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009424 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9425 if (C->getValueAPF().isPosZero())
9426 return N->getOperand(1);
9427 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9428 if (C->getValueAPF().isPosZero())
9429 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009430 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009431}
9432
9433/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009434static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009435 // FAND(0.0, x) -> 0.0
9436 // FAND(x, 0.0) -> 0.0
9437 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9438 if (C->getValueAPF().isPosZero())
9439 return N->getOperand(0);
9440 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9441 if (C->getValueAPF().isPosZero())
9442 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009443 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009444}
9445
Dan Gohmane5af2d32009-01-29 01:59:02 +00009446static SDValue PerformBTCombine(SDNode *N,
9447 SelectionDAG &DAG,
9448 TargetLowering::DAGCombinerInfo &DCI) {
9449 // BT ignores high bits in the bit index operand.
9450 SDValue Op1 = N->getOperand(1);
9451 if (Op1.hasOneUse()) {
9452 unsigned BitWidth = Op1.getValueSizeInBits();
9453 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9454 APInt KnownZero, KnownOne;
9455 TargetLowering::TargetLoweringOpt TLO(DAG);
9456 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9457 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9458 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9459 DCI.CommitTargetLoweringOpt(TLO);
9460 }
9461 return SDValue();
9462}
Chris Lattner83e6c992006-10-04 06:57:07 +00009463
Eli Friedman7a5e5552009-06-07 06:52:44 +00009464static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9465 SDValue Op = N->getOperand(0);
9466 if (Op.getOpcode() == ISD::BIT_CONVERT)
9467 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009468 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009469 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009470 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009471 OpVT.getVectorElementType().getSizeInBits()) {
9472 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9473 }
9474 return SDValue();
9475}
9476
Owen Anderson99177002009-06-29 18:04:45 +00009477// On X86 and X86-64, atomic operations are lowered to locked instructions.
9478// Locked instructions, in turn, have implicit fence semantics (all memory
9479// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009480// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009481// fence-atomic-fence.
9482static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9483 SDValue atomic = N->getOperand(0);
9484 switch (atomic.getOpcode()) {
9485 case ISD::ATOMIC_CMP_SWAP:
9486 case ISD::ATOMIC_SWAP:
9487 case ISD::ATOMIC_LOAD_ADD:
9488 case ISD::ATOMIC_LOAD_SUB:
9489 case ISD::ATOMIC_LOAD_AND:
9490 case ISD::ATOMIC_LOAD_OR:
9491 case ISD::ATOMIC_LOAD_XOR:
9492 case ISD::ATOMIC_LOAD_NAND:
9493 case ISD::ATOMIC_LOAD_MIN:
9494 case ISD::ATOMIC_LOAD_MAX:
9495 case ISD::ATOMIC_LOAD_UMIN:
9496 case ISD::ATOMIC_LOAD_UMAX:
9497 break;
9498 default:
9499 return SDValue();
9500 }
Eric Christopherfd179292009-08-27 18:07:15 +00009501
Owen Anderson99177002009-06-29 18:04:45 +00009502 SDValue fence = atomic.getOperand(0);
9503 if (fence.getOpcode() != ISD::MEMBARRIER)
9504 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009505
Owen Anderson99177002009-06-29 18:04:45 +00009506 switch (atomic.getOpcode()) {
9507 case ISD::ATOMIC_CMP_SWAP:
9508 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9509 atomic.getOperand(1), atomic.getOperand(2),
9510 atomic.getOperand(3));
9511 case ISD::ATOMIC_SWAP:
9512 case ISD::ATOMIC_LOAD_ADD:
9513 case ISD::ATOMIC_LOAD_SUB:
9514 case ISD::ATOMIC_LOAD_AND:
9515 case ISD::ATOMIC_LOAD_OR:
9516 case ISD::ATOMIC_LOAD_XOR:
9517 case ISD::ATOMIC_LOAD_NAND:
9518 case ISD::ATOMIC_LOAD_MIN:
9519 case ISD::ATOMIC_LOAD_MAX:
9520 case ISD::ATOMIC_LOAD_UMIN:
9521 case ISD::ATOMIC_LOAD_UMAX:
9522 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9523 atomic.getOperand(1), atomic.getOperand(2));
9524 default:
9525 return SDValue();
9526 }
9527}
9528
Evan Cheng2e489c42009-12-16 00:53:11 +00009529static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9530 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9531 // (and (i32 x86isd::setcc_carry), 1)
9532 // This eliminates the zext. This transformation is necessary because
9533 // ISD::SETCC is always legalized to i8.
9534 DebugLoc dl = N->getDebugLoc();
9535 SDValue N0 = N->getOperand(0);
9536 EVT VT = N->getValueType(0);
9537 if (N0.getOpcode() == ISD::AND &&
9538 N0.hasOneUse() &&
9539 N0.getOperand(0).hasOneUse()) {
9540 SDValue N00 = N0.getOperand(0);
9541 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9542 return SDValue();
9543 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9544 if (!C || C->getZExtValue() != 1)
9545 return SDValue();
9546 return DAG.getNode(ISD::AND, dl, VT,
9547 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9548 N00.getOperand(0), N00.getOperand(1)),
9549 DAG.getConstant(1, VT));
9550 }
9551
9552 return SDValue();
9553}
9554
Dan Gohman475871a2008-07-27 21:46:04 +00009555SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009556 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009557 SelectionDAG &DAG = DCI.DAG;
9558 switch (N->getOpcode()) {
9559 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009560 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009561 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009562 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009563 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009564 case ISD::SHL:
9565 case ISD::SRA:
9566 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009567 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009568 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009569 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009570 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9571 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009572 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009573 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009574 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009575 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009576 }
9577
Dan Gohman475871a2008-07-27 21:46:04 +00009578 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009579}
9580
Evan Cheng60c07e12006-07-05 22:17:51 +00009581//===----------------------------------------------------------------------===//
9582// X86 Inline Assembly Support
9583//===----------------------------------------------------------------------===//
9584
Chris Lattnerb8105652009-07-20 17:51:36 +00009585static bool LowerToBSwap(CallInst *CI) {
9586 // FIXME: this should verify that we are targetting a 486 or better. If not,
9587 // we will turn this bswap into something that will be lowered to logical ops
9588 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9589 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009590
Chris Lattnerb8105652009-07-20 17:51:36 +00009591 // Verify this is a simple bswap.
9592 if (CI->getNumOperands() != 2 ||
9593 CI->getType() != CI->getOperand(1)->getType() ||
9594 !CI->getType()->isInteger())
9595 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009596
Chris Lattnerb8105652009-07-20 17:51:36 +00009597 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9598 if (!Ty || Ty->getBitWidth() % 16 != 0)
9599 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009600
Chris Lattnerb8105652009-07-20 17:51:36 +00009601 // Okay, we can do this xform, do so now.
9602 const Type *Tys[] = { Ty };
9603 Module *M = CI->getParent()->getParent()->getParent();
9604 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009605
Chris Lattnerb8105652009-07-20 17:51:36 +00009606 Value *Op = CI->getOperand(1);
9607 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009608
Chris Lattnerb8105652009-07-20 17:51:36 +00009609 CI->replaceAllUsesWith(Op);
9610 CI->eraseFromParent();
9611 return true;
9612}
9613
9614bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9615 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9616 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9617
9618 std::string AsmStr = IA->getAsmString();
9619
9620 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009621 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009622 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9623
9624 switch (AsmPieces.size()) {
9625 default: return false;
9626 case 1:
9627 AsmStr = AsmPieces[0];
9628 AsmPieces.clear();
9629 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9630
9631 // bswap $0
9632 if (AsmPieces.size() == 2 &&
9633 (AsmPieces[0] == "bswap" ||
9634 AsmPieces[0] == "bswapq" ||
9635 AsmPieces[0] == "bswapl") &&
9636 (AsmPieces[1] == "$0" ||
9637 AsmPieces[1] == "${0:q}")) {
9638 // No need to check constraints, nothing other than the equivalent of
9639 // "=r,0" would be valid here.
9640 return LowerToBSwap(CI);
9641 }
9642 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009643 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009644 AsmPieces.size() == 3 &&
9645 AsmPieces[0] == "rorw" &&
9646 AsmPieces[1] == "$$8," &&
9647 AsmPieces[2] == "${0:w}" &&
9648 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9649 return LowerToBSwap(CI);
9650 }
9651 break;
9652 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009653 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009654 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009655 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9656 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9657 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009658 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009659 SplitString(AsmPieces[0], Words, " \t");
9660 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9661 Words.clear();
9662 SplitString(AsmPieces[1], Words, " \t");
9663 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9664 Words.clear();
9665 SplitString(AsmPieces[2], Words, " \t,");
9666 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9667 Words[2] == "%edx") {
9668 return LowerToBSwap(CI);
9669 }
9670 }
9671 }
9672 }
9673 break;
9674 }
9675 return false;
9676}
9677
9678
9679
Chris Lattnerf4dff842006-07-11 02:54:03 +00009680/// getConstraintType - Given a constraint letter, return the type of
9681/// constraint it is for this target.
9682X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009683X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9684 if (Constraint.size() == 1) {
9685 switch (Constraint[0]) {
9686 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009687 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009688 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009689 case 'r':
9690 case 'R':
9691 case 'l':
9692 case 'q':
9693 case 'Q':
9694 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009695 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009696 case 'Y':
9697 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009698 case 'e':
9699 case 'Z':
9700 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009701 default:
9702 break;
9703 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009704 }
Chris Lattner4234f572007-03-25 02:14:49 +00009705 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009706}
9707
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009708/// LowerXConstraint - try to replace an X constraint, which matches anything,
9709/// with another that has more specific requirements based on the type of the
9710/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009711const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009712LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009713 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9714 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009715 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009716 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009717 return "Y";
9718 if (Subtarget->hasSSE1())
9719 return "x";
9720 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009721
Chris Lattner5e764232008-04-26 23:02:14 +00009722 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009723}
9724
Chris Lattner48884cd2007-08-25 00:47:38 +00009725/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9726/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009727void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009728 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009729 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009730 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009731 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009732 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009733
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009734 switch (Constraint) {
9735 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009736 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009737 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009738 if (C->getZExtValue() <= 31) {
9739 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009740 break;
9741 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009742 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009743 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009744 case 'J':
9745 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009746 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009747 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9748 break;
9749 }
9750 }
9751 return;
9752 case 'K':
9753 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009754 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009755 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9756 break;
9757 }
9758 }
9759 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009760 case 'N':
9761 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009762 if (C->getZExtValue() <= 255) {
9763 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009764 break;
9765 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009766 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009767 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009768 case 'e': {
9769 // 32-bit signed value
9770 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9771 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009772 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9773 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009774 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009775 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009776 break;
9777 }
9778 // FIXME gcc accepts some relocatable values here too, but only in certain
9779 // memory models; it's complicated.
9780 }
9781 return;
9782 }
9783 case 'Z': {
9784 // 32-bit unsigned value
9785 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9786 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009787 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9788 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009789 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9790 break;
9791 }
9792 }
9793 // FIXME gcc accepts some relocatable values here too, but only in certain
9794 // memory models; it's complicated.
9795 return;
9796 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009797 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009798 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009799 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009800 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009801 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009802 break;
9803 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009804
Chris Lattnerdc43a882007-05-03 16:52:29 +00009805 // If we are in non-pic codegen mode, we allow the address of a global (with
9806 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009807 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009808 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009809
Chris Lattner49921962009-05-08 18:23:14 +00009810 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9811 while (1) {
9812 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9813 Offset += GA->getOffset();
9814 break;
9815 } else if (Op.getOpcode() == ISD::ADD) {
9816 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9817 Offset += C->getZExtValue();
9818 Op = Op.getOperand(0);
9819 continue;
9820 }
9821 } else if (Op.getOpcode() == ISD::SUB) {
9822 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9823 Offset += -C->getZExtValue();
9824 Op = Op.getOperand(0);
9825 continue;
9826 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009827 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009828
Chris Lattner49921962009-05-08 18:23:14 +00009829 // Otherwise, this isn't something we can handle, reject it.
9830 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009831 }
Eric Christopherfd179292009-08-27 18:07:15 +00009832
Chris Lattner36c25012009-07-10 07:34:39 +00009833 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009834 // If we require an extra load to get this address, as in PIC mode, we
9835 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009836 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9837 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009838 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009839
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009840 if (hasMemory)
9841 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9842 else
9843 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009844 Result = Op;
9845 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009846 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009847 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009848
Gabor Greifba36cb52008-08-28 21:40:38 +00009849 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009850 Ops.push_back(Result);
9851 return;
9852 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009853 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9854 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009855}
9856
Chris Lattner259e97c2006-01-31 19:43:35 +00009857std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009858getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009859 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009860 if (Constraint.size() == 1) {
9861 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009862 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009863 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009864 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9865 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009866 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009867 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9868 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9869 X86::R10D,X86::R11D,X86::R12D,
9870 X86::R13D,X86::R14D,X86::R15D,
9871 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009872 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009873 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9874 X86::SI, X86::DI, X86::R8W,X86::R9W,
9875 X86::R10W,X86::R11W,X86::R12W,
9876 X86::R13W,X86::R14W,X86::R15W,
9877 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009878 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009879 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9880 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9881 X86::R10B,X86::R11B,X86::R12B,
9882 X86::R13B,X86::R14B,X86::R15B,
9883 X86::BPL, X86::SPL, 0);
9884
Owen Anderson825b72b2009-08-11 20:47:22 +00009885 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009886 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9887 X86::RSI, X86::RDI, X86::R8, X86::R9,
9888 X86::R10, X86::R11, X86::R12,
9889 X86::R13, X86::R14, X86::R15,
9890 X86::RBP, X86::RSP, 0);
9891
9892 break;
9893 }
Eric Christopherfd179292009-08-27 18:07:15 +00009894 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009895 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009896 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009897 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009898 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009899 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009900 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009901 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009902 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009903 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9904 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009905 }
9906 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009907
Chris Lattner1efa40f2006-02-22 00:56:39 +00009908 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009909}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009910
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009911std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009912X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009913 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009914 // First, see if this is a constraint that directly corresponds to an LLVM
9915 // register class.
9916 if (Constraint.size() == 1) {
9917 // GCC Constraint Letters
9918 switch (Constraint[0]) {
9919 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009920 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009921 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009922 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009923 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009924 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009925 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009926 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009927 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009928 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009929 case 'R': // LEGACY_REGS
9930 if (VT == MVT::i8)
9931 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9932 if (VT == MVT::i16)
9933 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9934 if (VT == MVT::i32 || !Subtarget->is64Bit())
9935 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9936 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009937 case 'f': // FP Stack registers.
9938 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9939 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009940 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009941 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009942 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009943 return std::make_pair(0U, X86::RFP64RegisterClass);
9944 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009945 case 'y': // MMX_REGS if MMX allowed.
9946 if (!Subtarget->hasMMX()) break;
9947 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009948 case 'Y': // SSE_REGS if SSE2 allowed
9949 if (!Subtarget->hasSSE2()) break;
9950 // FALL THROUGH.
9951 case 'x': // SSE_REGS if SSE1 allowed
9952 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009953
Owen Anderson825b72b2009-08-11 20:47:22 +00009954 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009955 default: break;
9956 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009957 case MVT::f32:
9958 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009959 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009960 case MVT::f64:
9961 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009962 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009963 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009964 case MVT::v16i8:
9965 case MVT::v8i16:
9966 case MVT::v4i32:
9967 case MVT::v2i64:
9968 case MVT::v4f32:
9969 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009970 return std::make_pair(0U, X86::VR128RegisterClass);
9971 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009972 break;
9973 }
9974 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009975
Chris Lattnerf76d1802006-07-31 23:26:50 +00009976 // Use the default implementation in TargetLowering to convert the register
9977 // constraint into a member of a register class.
9978 std::pair<unsigned, const TargetRegisterClass*> Res;
9979 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009980
9981 // Not found as a standard register?
9982 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +00009983 // Map st(0) -> st(7) -> ST0
9984 if (Constraint.size() == 7 && Constraint[0] == '{' &&
9985 tolower(Constraint[1]) == 's' &&
9986 tolower(Constraint[2]) == 't' &&
9987 Constraint[3] == '(' &&
9988 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
9989 Constraint[5] == ')' &&
9990 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +00009991
Chris Lattner56d77c72009-09-13 22:41:48 +00009992 Res.first = X86::ST0+Constraint[4]-'0';
9993 Res.second = X86::RFP80RegisterClass;
9994 return Res;
9995 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00009996
Chris Lattner56d77c72009-09-13 22:41:48 +00009997 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +00009998 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +00009999 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010000 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010001 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010002 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010003
10004 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010005 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010006 Res.first = X86::EFLAGS;
10007 Res.second = X86::CCRRegisterClass;
10008 return Res;
10009 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010010
Dale Johannesen330169f2008-11-13 21:52:36 +000010011 // 'A' means EAX + EDX.
10012 if (Constraint == "A") {
10013 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010014 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010015 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010016 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010017 return Res;
10018 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010019
Chris Lattnerf76d1802006-07-31 23:26:50 +000010020 // Otherwise, check to see if this is a register class of the wrong value
10021 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10022 // turn into {ax},{dx}.
10023 if (Res.second->hasType(VT))
10024 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010025
Chris Lattnerf76d1802006-07-31 23:26:50 +000010026 // All of the single-register GCC register classes map their values onto
10027 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10028 // really want an 8-bit or 32-bit register, map to the appropriate register
10029 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010030 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010031 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010032 unsigned DestReg = 0;
10033 switch (Res.first) {
10034 default: break;
10035 case X86::AX: DestReg = X86::AL; break;
10036 case X86::DX: DestReg = X86::DL; break;
10037 case X86::CX: DestReg = X86::CL; break;
10038 case X86::BX: DestReg = X86::BL; break;
10039 }
10040 if (DestReg) {
10041 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010042 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010043 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010044 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010045 unsigned DestReg = 0;
10046 switch (Res.first) {
10047 default: break;
10048 case X86::AX: DestReg = X86::EAX; break;
10049 case X86::DX: DestReg = X86::EDX; break;
10050 case X86::CX: DestReg = X86::ECX; break;
10051 case X86::BX: DestReg = X86::EBX; break;
10052 case X86::SI: DestReg = X86::ESI; break;
10053 case X86::DI: DestReg = X86::EDI; break;
10054 case X86::BP: DestReg = X86::EBP; break;
10055 case X86::SP: DestReg = X86::ESP; break;
10056 }
10057 if (DestReg) {
10058 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010059 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010060 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010061 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010062 unsigned DestReg = 0;
10063 switch (Res.first) {
10064 default: break;
10065 case X86::AX: DestReg = X86::RAX; break;
10066 case X86::DX: DestReg = X86::RDX; break;
10067 case X86::CX: DestReg = X86::RCX; break;
10068 case X86::BX: DestReg = X86::RBX; break;
10069 case X86::SI: DestReg = X86::RSI; break;
10070 case X86::DI: DestReg = X86::RDI; break;
10071 case X86::BP: DestReg = X86::RBP; break;
10072 case X86::SP: DestReg = X86::RSP; break;
10073 }
10074 if (DestReg) {
10075 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010076 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010077 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010078 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010079 } else if (Res.second == X86::FR32RegisterClass ||
10080 Res.second == X86::FR64RegisterClass ||
10081 Res.second == X86::VR128RegisterClass) {
10082 // Handle references to XMM physical registers that got mapped into the
10083 // wrong class. This can happen with constraints like {xmm0} where the
10084 // target independent register mapper will just pick the first match it can
10085 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010086 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010087 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010088 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010089 Res.second = X86::FR64RegisterClass;
10090 else if (X86::VR128RegisterClass->hasType(VT))
10091 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010092 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010093
Chris Lattnerf76d1802006-07-31 23:26:50 +000010094 return Res;
10095}
Mon P Wang0c397192008-10-30 08:01:45 +000010096
10097//===----------------------------------------------------------------------===//
10098// X86 Widen vector type
10099//===----------------------------------------------------------------------===//
10100
10101/// getWidenVectorType: given a vector type, returns the type to widen
10102/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010103/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010104/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010105/// scalarizing vs using the wider vector type.
10106
Owen Andersone50ed302009-08-10 22:56:29 +000010107EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010108 assert(VT.isVector());
10109 if (isTypeLegal(VT))
10110 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010111
Mon P Wang0c397192008-10-30 08:01:45 +000010112 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10113 // type based on element type. This would speed up our search (though
10114 // it may not be worth it since the size of the list is relatively
10115 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010116 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010117 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010118
Mon P Wang0c397192008-10-30 08:01:45 +000010119 // On X86, it make sense to widen any vector wider than 1
10120 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010121 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010122
Owen Anderson825b72b2009-08-11 20:47:22 +000010123 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10124 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10125 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010126
10127 if (isTypeLegal(SVT) &&
10128 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010129 SVT.getVectorNumElements() > NElts)
10130 return SVT;
10131 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010132 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010133}