blob: ce2032bee842e8add4d48727f9adb3dab5b2354d [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000019#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000023#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000024#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000026#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000027#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000028#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000037#include "llvm/MC/MCContext.h"
38#include "llvm/MC/MCExpr.h"
39#include "llvm/MC/MCSymbol.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000040#include "llvm/Target/TargetOptions.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000043#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000044#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000045#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/Support/Debug.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000049#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050using namespace llvm;
51
Mon P Wang3c81d352008-11-23 04:37:22 +000052static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000053DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000054
Dan Gohman2f67df72009-09-03 17:18:51 +000055// Disable16Bit - 16-bit operations typically have a larger encoding than
56// corresponding 32-bit instructions, and 16-bit code is slow on some
57// processors. This is an experimental flag to disable 16-bit operations
58// (which forces them to be Legalized to 32-bit operations).
59static cl::opt<bool>
60Disable16Bit("disable-16bit", cl::Hidden,
61 cl::desc("Disable use of 16-bit instructions"));
62
Evan Cheng10e86422008-04-25 19:11:04 +000063// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000064static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000065 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000066
Chris Lattnerf0144122009-07-28 03:13:23 +000067static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
68 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
69 default: llvm_unreachable("unknown subtarget type");
70 case X86Subtarget::isDarwin:
Chris Lattner8c6ed052009-09-16 01:46:41 +000071 if (TM.getSubtarget<X86Subtarget>().is64Bit())
72 return new X8664_MachoTargetObjectFile();
Chris Lattner228252f2009-09-18 20:22:52 +000073 return new X8632_MachoTargetObjectFile();
Chris Lattnerf0144122009-07-28 03:13:23 +000074 case X86Subtarget::isELF:
75 return new TargetLoweringObjectFileELF();
76 case X86Subtarget::isMingw:
77 case X86Subtarget::isCygwin:
78 case X86Subtarget::isWindows:
79 return new TargetLoweringObjectFileCOFF();
80 }
Eric Christopherfd179292009-08-27 18:07:15 +000081
Chris Lattnerf0144122009-07-28 03:13:23 +000082}
83
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000084X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000085 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000086 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000087 X86ScalarSSEf64 = Subtarget->hasSSE2();
88 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000089 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000092 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000093
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 // Set up the TargetLowering object.
95
96 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000098 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000099 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000101
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000102 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000103 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000104 setUseUnderscoreSetJmp(false);
105 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000106 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000107 // MS runtime is weird: it exports _setjmp, but longjmp!
108 setUseUnderscoreSetJmp(true);
109 setUseUnderscoreLongJmp(false);
110 } else {
111 setUseUnderscoreSetJmp(true);
112 setUseUnderscoreLongJmp(true);
113 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000114
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000115 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000117 if (!Disable16Bit)
118 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000119 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000120 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000121 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000124
Scott Michelfdc40a02009-02-17 22:15:04 +0000125 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000127 if (!Disable16Bit)
128 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000130 if (!Disable16Bit)
131 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
133 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000134
135 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
137 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
138 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
139 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
140 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
141 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000142
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
144 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
147 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000148
Evan Cheng25ab6902006-09-08 06:48:29 +0000149 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000150 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
151 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000152 } else if (!UseSoftFloat) {
153 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000154 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000155 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000156 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000157 // We have an algorithm for SSE2, and we turn this into a 64-bit
158 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000160 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161
162 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
163 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
165 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000166
Devang Patel6a784892009-06-05 18:48:29 +0000167 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000168 // SSE has no i16 to fp conversion, only i32
169 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000171 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000173 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
175 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000177 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
179 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000180 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
Dale Johannesen73328d12007-09-19 23:55:34 +0000182 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
183 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
185 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000186
Evan Cheng02568ff2006-01-30 22:13:22 +0000187 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
188 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
190 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000191
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000192 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000194 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000196 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
198 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000199 }
200
201 // Handle FP_TO_UINT by promoting the destination to a larger signed
202 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
204 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
205 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206
Evan Cheng25ab6902006-09-08 06:48:29 +0000207 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
209 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000210 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000211 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 // Expand FP_TO_UINT into a select.
213 // FIXME: We would like to use a Custom expander here eventually to do
214 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000216 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000217 // With SSE3 we can use fisttpll to convert to a signed i64; without
218 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221
Chris Lattner399610a2006-12-05 18:22:22 +0000222 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000223 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
225 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000226 }
Chris Lattner21f66852005-12-23 05:15:23 +0000227
Dan Gohmanb00ee212008-02-18 19:34:53 +0000228 // Scalar integer divide and remainder are lowered to use operations that
229 // produce two results, to match the available instructions. This exposes
230 // the two-result form to trivial CSE, which is able to combine x/y and x%y
231 // into a single instruction.
232 //
233 // Scalar integer multiply-high is also lowered to use two-result
234 // operations, to match the available instructions. However, plain multiply
235 // (low) operations are left as Legal, as there are single-result
236 // instructions for this in x86. Using the two-result multiply instructions
237 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000238 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
242 setOperationAction(ISD::SREM , MVT::i8 , Expand);
243 setOperationAction(ISD::UREM , MVT::i8 , Expand);
244 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
248 setOperationAction(ISD::SREM , MVT::i16 , Expand);
249 setOperationAction(ISD::UREM , MVT::i16 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
254 setOperationAction(ISD::SREM , MVT::i32 , Expand);
255 setOperationAction(ISD::UREM , MVT::i32 , Expand);
256 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
257 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
258 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
259 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
260 setOperationAction(ISD::SREM , MVT::i64 , Expand);
261 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000262
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
264 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
265 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
266 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
269 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
272 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
273 setOperationAction(ISD::FREM , MVT::f32 , Expand);
274 setOperationAction(ISD::FREM , MVT::f64 , Expand);
275 setOperationAction(ISD::FREM , MVT::f80 , Expand);
276 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
279 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
280 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
281 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000282 if (Disable16Bit) {
283 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
284 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
285 } else {
286 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
288 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
290 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
291 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
294 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
295 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000296 }
297
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
299 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000300
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000302 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000303 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000304 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000305 if (Disable16Bit)
306 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
307 else
308 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
310 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
311 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
313 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000314 if (Disable16Bit)
315 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
316 else
317 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
321 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000322 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000323 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
324 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000325 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000327
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000328 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
330 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
332 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000333 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000334 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
335 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000336 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000337 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
339 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
340 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
341 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000342 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000344 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000345 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
347 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000348 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
351 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000352 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000353
Evan Chengd2cde682008-03-10 19:38:10 +0000354 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000356
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000357 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000359
Mon P Wang63307c32008-05-05 19:05:59 +0000360 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
362 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
363 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
364 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000365
Owen Anderson825b72b2009-08-11 20:47:22 +0000366 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
369 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000370
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000371 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000379 }
380
Evan Cheng3c992d22006-03-07 02:02:57 +0000381 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000382 if (!Subtarget->isTargetDarwin() &&
383 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000384 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000386 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000387
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
389 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
390 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000392 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000393 setExceptionPointerRegister(X86::RAX);
394 setExceptionSelectorRegister(X86::RDX);
395 } else {
396 setExceptionPointerRegister(X86::EAX);
397 setExceptionSelectorRegister(X86::EDX);
398 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
400 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000401
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000405
Nate Begemanacc398c2006-01-25 18:21:52 +0000406 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 setOperationAction(ISD::VASTART , MVT::Other, Custom);
408 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000409 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::VAARG , MVT::Other, Custom);
411 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000412 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::VAARG , MVT::Other, Expand);
414 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000415 }
Evan Chengae642192007-03-02 23:16:35 +0000416
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
418 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000419 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000421 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000423 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000425
Evan Chengc7ce29b2009-02-13 22:36:38 +0000426 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000428 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
430 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000431
Evan Cheng223547a2006-01-31 22:28:30 +0000432 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::FABS , MVT::f64, Custom);
434 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000435
436 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 setOperationAction(ISD::FNEG , MVT::f64, Custom);
438 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000439
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000441 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
442 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000443
Evan Chengd25e9e82006-02-02 00:28:23 +0000444 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000445 setOperationAction(ISD::FSIN , MVT::f64, Expand);
446 setOperationAction(ISD::FCOS , MVT::f64, Expand);
447 setOperationAction(ISD::FSIN , MVT::f32, Expand);
448 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000449
Chris Lattnera54aa942006-01-29 06:26:08 +0000450 // Expand FP immediates into loads from the stack, except for the special
451 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000452 addLegalFPImmediate(APFloat(+0.0)); // xorpd
453 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000454 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455 // Use SSE for f32, x87 for f64.
456 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
458 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000459
460 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
463 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
468 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
470 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
472 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::FSIN , MVT::f32, Expand);
474 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000475
Nate Begemane1795842008-02-14 08:57:00 +0000476 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477 addLegalFPImmediate(APFloat(+0.0f)); // xorps
478 addLegalFPImmediate(APFloat(+0.0)); // FLD0
479 addLegalFPImmediate(APFloat(+1.0)); // FLD1
480 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
481 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
482
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
485 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000486 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000487 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000488 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000489 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
491 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000492
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
494 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
495 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
496 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000497
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
500 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000501 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000502 addLegalFPImmediate(APFloat(+0.0)); // FLD0
503 addLegalFPImmediate(APFloat(+1.0)); // FLD1
504 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
505 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000506 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
507 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
508 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
509 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000510 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000511
Dale Johannesen59a58732007-08-05 18:49:15 +0000512 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000513 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000514 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
515 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
516 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000517 {
518 bool ignored;
519 APFloat TmpFlt(+0.0);
520 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
521 &ignored);
522 addLegalFPImmediate(TmpFlt); // FLD0
523 TmpFlt.changeSign();
524 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
525 APFloat TmpFlt2(+1.0);
526 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
527 &ignored);
528 addLegalFPImmediate(TmpFlt2); // FLD1
529 TmpFlt2.changeSign();
530 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
531 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000532
Evan Chengc7ce29b2009-02-13 22:36:38 +0000533 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000534 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
535 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000536 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000537 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000538
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000539 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
541 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::FLOG, MVT::f80, Expand);
545 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
547 setOperationAction(ISD::FEXP, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000549
Mon P Wangf007a8b2008-11-06 05:31:54 +0000550 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000551 // (for widening) or expand (for scalarization). Then we will selectively
552 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
554 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
555 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
570 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000603 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000604 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
609 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
610 setTruncStoreAction((MVT::SimpleValueType)VT,
611 (MVT::SimpleValueType)InnerVT, Expand);
612 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
613 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
614 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000615 }
616
Evan Chengc7ce29b2009-02-13 22:36:38 +0000617 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
618 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000619 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
621 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
622 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
623 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
624 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000625
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
627 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
628 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
629 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000630
Owen Anderson825b72b2009-08-11 20:47:22 +0000631 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
632 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
633 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
634 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000635
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
637 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::AND, MVT::v8i8, Promote);
640 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
641 setOperationAction(ISD::AND, MVT::v4i16, Promote);
642 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
643 setOperationAction(ISD::AND, MVT::v2i32, Promote);
644 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
645 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::OR, MVT::v8i8, Promote);
648 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
649 setOperationAction(ISD::OR, MVT::v4i16, Promote);
650 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
651 setOperationAction(ISD::OR, MVT::v2i32, Promote);
652 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
653 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000654
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
656 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
657 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
658 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
659 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
660 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
661 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000662
Owen Anderson825b72b2009-08-11 20:47:22 +0000663 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
664 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
665 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
666 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
667 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
668 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
669 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
670 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
671 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000672
Owen Anderson825b72b2009-08-11 20:47:22 +0000673 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
674 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
675 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
676 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
677 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
680 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
681 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
682 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000683
Owen Anderson825b72b2009-08-11 20:47:22 +0000684 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
685 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
686 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
687 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000688
Owen Anderson825b72b2009-08-11 20:47:22 +0000689 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000690
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
692 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
693 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
694 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
697 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000698 }
699
Evan Cheng92722532009-03-26 23:06:32 +0000700 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000702
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
704 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
705 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
706 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
707 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
708 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
709 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
710 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
711 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
713 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
714 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000715 }
716
Evan Cheng92722532009-03-26 23:06:32 +0000717 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000718 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000719
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000720 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
721 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
723 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
724 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
725 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000726
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
728 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
729 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
730 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
731 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
732 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
733 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
734 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
735 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
736 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
737 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
738 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
739 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
740 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
741 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
742 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000743
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
745 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
747 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000748
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
750 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000754
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
756 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
757 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
759 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
760
Evan Cheng2c3ae372006-04-12 21:21:57 +0000761 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
763 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000764 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000765 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000766 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000767 // Do not attempt to custom lower non-128-bit vectors
768 if (!VT.is128BitVector())
769 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::BUILD_VECTOR,
771 VT.getSimpleVT().SimpleTy, Custom);
772 setOperationAction(ISD::VECTOR_SHUFFLE,
773 VT.getSimpleVT().SimpleTy, Custom);
774 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
775 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000776 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000777
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
779 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
780 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
781 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
782 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000784
Nate Begemancdd1eec2008-02-12 22:51:28 +0000785 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000786 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
787 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000788 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000789
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000790 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
792 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000793 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000794
795 // Do not attempt to promote non-128-bit vectors
796 if (!VT.is128BitVector()) {
797 continue;
798 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000799 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000801 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000803 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000805 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000807 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000809 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000810
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000812
Evan Cheng2c3ae372006-04-12 21:21:57 +0000813 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
815 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
816 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
817 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000818
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
820 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000821 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
823 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000824 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000826
Nate Begeman14d12ca2008-02-11 04:19:36 +0000827 if (Subtarget->hasSSE41()) {
828 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000830
831 // i8 and i16 vectors are custom , because the source register and source
832 // source memory operand types are not the same width. f32 vectors are
833 // custom since the immediate controlling the insert encodes additional
834 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
836 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
837 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
838 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000839
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
841 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
842 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
843 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000844
845 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
847 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000848 }
849 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000850
Nate Begeman30a0de92008-07-17 16:51:19 +0000851 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000853 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000854
David Greene9b9838d2009-06-29 16:47:10 +0000855 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
857 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
858 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
859 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000860
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
862 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
863 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
864 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
865 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
866 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
867 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
868 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
869 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
870 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
871 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
872 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
873 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
874 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
875 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000876
877 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000878 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
879 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
880 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
881 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
882 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
883 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
884 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
885 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
886 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
887 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
888 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
889 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
890 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
891 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000892
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
894 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
895 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
896 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000897
Owen Anderson825b72b2009-08-11 20:47:22 +0000898 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
899 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
900 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
901 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
902 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
905 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
906 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
907 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
908 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
909 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000910
911#if 0
912 // Not sure we want to do this since there are no 256-bit integer
913 // operations in AVX
914
915 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
916 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000917 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
918 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000919
920 // Do not attempt to custom lower non-power-of-2 vectors
921 if (!isPowerOf2_32(VT.getVectorNumElements()))
922 continue;
923
924 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
925 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
926 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
927 }
928
929 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000930 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
931 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000932 }
David Greene9b9838d2009-06-29 16:47:10 +0000933#endif
934
935#if 0
936 // Not sure we want to do this since there are no 256-bit integer
937 // operations in AVX
938
939 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
940 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
942 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000943
944 if (!VT.is256BitVector()) {
945 continue;
946 }
947 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000949 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000951 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000953 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000955 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000957 }
958
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000960#endif
961 }
962
Evan Cheng6be2c582006-04-05 23:38:46 +0000963 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000965
Bill Wendling74c37652008-12-09 22:08:41 +0000966 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 setOperationAction(ISD::SADDO, MVT::i32, Custom);
968 setOperationAction(ISD::SADDO, MVT::i64, Custom);
969 setOperationAction(ISD::UADDO, MVT::i32, Custom);
970 setOperationAction(ISD::UADDO, MVT::i64, Custom);
971 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
972 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
973 setOperationAction(ISD::USUBO, MVT::i32, Custom);
974 setOperationAction(ISD::USUBO, MVT::i64, Custom);
975 setOperationAction(ISD::SMULO, MVT::i32, Custom);
976 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000977
Evan Chengd54f2d52009-03-31 19:38:51 +0000978 if (!Subtarget->is64Bit()) {
979 // These libcalls are not available in 32-bit.
980 setLibcallName(RTLIB::SHL_I128, 0);
981 setLibcallName(RTLIB::SRL_I128, 0);
982 setLibcallName(RTLIB::SRA_I128, 0);
983 }
984
Evan Cheng206ee9d2006-07-07 08:33:52 +0000985 // We have target-specific dag combine patterns for the following nodes:
986 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000987 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000988 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000989 setTargetDAGCombine(ISD::SHL);
990 setTargetDAGCombine(ISD::SRA);
991 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000992 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000993 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000994 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +0000995 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000996 if (Subtarget->is64Bit())
997 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000998
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000999 computeRegisterProperties();
1000
Mon P Wangcd6e7252009-11-30 02:42:02 +00001001 // Divide and reminder operations have no vector equivalent and can
1002 // trap. Do a custom widening for these operations in which we never
1003 // generate more divides/remainder than the original vector width.
1004 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
1005 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
1006 if (!isTypeLegal((MVT::SimpleValueType)VT)) {
1007 setOperationAction(ISD::SDIV, (MVT::SimpleValueType) VT, Custom);
1008 setOperationAction(ISD::UDIV, (MVT::SimpleValueType) VT, Custom);
1009 setOperationAction(ISD::SREM, (MVT::SimpleValueType) VT, Custom);
1010 setOperationAction(ISD::UREM, (MVT::SimpleValueType) VT, Custom);
1011 }
1012 }
1013
Evan Cheng87ed7162006-02-14 08:25:08 +00001014 // FIXME: These should be based on subtarget info. Plus, the values should
1015 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001016 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1017 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1018 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001019 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001020 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001021}
1022
Scott Michel5b8f82e2008-03-10 15:42:14 +00001023
Owen Anderson825b72b2009-08-11 20:47:22 +00001024MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1025 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001026}
1027
1028
Evan Cheng29286502008-01-23 23:17:41 +00001029/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1030/// the desired ByVal argument alignment.
1031static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1032 if (MaxAlign == 16)
1033 return;
1034 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1035 if (VTy->getBitWidth() == 128)
1036 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001037 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1038 unsigned EltAlign = 0;
1039 getMaxByValAlign(ATy->getElementType(), EltAlign);
1040 if (EltAlign > MaxAlign)
1041 MaxAlign = EltAlign;
1042 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1043 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1044 unsigned EltAlign = 0;
1045 getMaxByValAlign(STy->getElementType(i), EltAlign);
1046 if (EltAlign > MaxAlign)
1047 MaxAlign = EltAlign;
1048 if (MaxAlign == 16)
1049 break;
1050 }
1051 }
1052 return;
1053}
1054
1055/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1056/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001057/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1058/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001059unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001060 if (Subtarget->is64Bit()) {
1061 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001062 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001063 if (TyAlign > 8)
1064 return TyAlign;
1065 return 8;
1066 }
1067
Evan Cheng29286502008-01-23 23:17:41 +00001068 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001069 if (Subtarget->hasSSE1())
1070 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001071 return Align;
1072}
Chris Lattner2b02a442007-02-25 08:29:00 +00001073
Evan Chengf0df0312008-05-15 08:39:06 +00001074/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001075/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001076/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001077/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001078EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001079X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001080 bool isSrcConst, bool isSrcStr,
1081 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001082 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1083 // linux. This is because the stack realignment code can't handle certain
1084 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001085 const Function *F = DAG.getMachineFunction().getFunction();
1086 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1087 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001088 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001090 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001091 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001092 }
Evan Chengf0df0312008-05-15 08:39:06 +00001093 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001094 return MVT::i64;
1095 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001096}
1097
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001098/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1099/// current function. The returned value is a member of the
1100/// MachineJumpTableInfo::JTEntryKind enum.
1101unsigned X86TargetLowering::getJumpTableEncoding() const {
1102 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1103 // symbol.
1104 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1105 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001106 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001107
1108 // Otherwise, use the normal jump table encoding heuristics.
1109 return TargetLowering::getJumpTableEncoding();
1110}
1111
Chris Lattner589c6f62010-01-26 06:28:43 +00001112/// getPICBaseSymbol - Return the X86-32 PIC base.
1113MCSymbol *
1114X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1115 MCContext &Ctx) const {
1116 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
1117 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1118 Twine(MF->getFunctionNumber())+"$pb");
1119}
1120
1121
Chris Lattnerc64daab2010-01-26 05:02:42 +00001122const MCExpr *
1123X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1124 const MachineBasicBlock *MBB,
1125 unsigned uid,MCContext &Ctx) const{
1126 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1127 Subtarget->isPICStyleGOT());
1128 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1129 // entries.
1130
1131 // FIXME: @GOTOFF should be a property of MCSymbolRefExpr not in the MCSymbol.
1132 std::string Name = MBB->getSymbol(Ctx)->getName() + "@GOTOFF";
1133 return MCSymbolRefExpr::Create(Ctx.GetOrCreateSymbol(StringRef(Name)), Ctx);
1134}
1135
Evan Chengcc415862007-11-09 01:32:10 +00001136/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1137/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001138SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001139 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001140 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001141 // This doesn't have DebugLoc associated with it, but is not really the
1142 // same as a Register.
1143 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1144 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001145 return Table;
1146}
1147
Chris Lattner589c6f62010-01-26 06:28:43 +00001148/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1149/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1150/// MCExpr.
1151const MCExpr *X86TargetLowering::
1152getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1153 MCContext &Ctx) const {
1154 // X86-64 uses RIP relative addressing based on the jump table label.
1155 if (Subtarget->isPICStyleRIPRel())
1156 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1157
1158 // Otherwise, the reference is relative to the PIC base.
1159 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1160}
1161
Bill Wendlingb4202b82009-07-01 18:50:55 +00001162/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001163unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001164 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001165}
1166
Chris Lattner2b02a442007-02-25 08:29:00 +00001167//===----------------------------------------------------------------------===//
1168// Return Value Calling Convention Implementation
1169//===----------------------------------------------------------------------===//
1170
Chris Lattner59ed56b2007-02-28 04:55:35 +00001171#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001172
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001173bool
1174X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1175 const SmallVectorImpl<EVT> &OutTys,
1176 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1177 SelectionDAG &DAG) {
1178 SmallVector<CCValAssign, 16> RVLocs;
1179 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1180 RVLocs, *DAG.getContext());
1181 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1182}
1183
Dan Gohman98ca4f22009-08-05 01:29:28 +00001184SDValue
1185X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001186 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187 const SmallVectorImpl<ISD::OutputArg> &Outs,
1188 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001189
Chris Lattner9774c912007-02-27 05:28:59 +00001190 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1192 RVLocs, *DAG.getContext());
1193 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001194
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001195 // If this is the first return lowered for this function, add the regs to the
1196 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001197 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001198 for (unsigned i = 0; i != RVLocs.size(); ++i)
1199 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001200 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001202
Dan Gohman475871a2008-07-27 21:46:04 +00001203 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001204
Dan Gohman475871a2008-07-27 21:46:04 +00001205 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001206 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1207 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001208 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001210 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001211 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1212 CCValAssign &VA = RVLocs[i];
1213 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001214 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner447ff682008-03-11 03:23:40 +00001216 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1217 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001218 if (VA.getLocReg() == X86::ST0 ||
1219 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001220 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1221 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001222 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001223 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001224 RetOps.push_back(ValToCopy);
1225 // Don't emit a copytoreg.
1226 continue;
1227 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001228
Evan Cheng242b38b2009-02-23 09:03:22 +00001229 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1230 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001231 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001232 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001233 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001235 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001237 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001238 }
1239
Dale Johannesendd64c412009-02-04 00:33:20 +00001240 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001241 Flag = Chain.getValue(1);
1242 }
Dan Gohman61a92132008-04-21 23:59:07 +00001243
1244 // The x86-64 ABI for returning structs by value requires that we copy
1245 // the sret argument into %rax for the return. We saved the argument into
1246 // a virtual register in the entry block, so now we copy the value out
1247 // and into %rax.
1248 if (Subtarget->is64Bit() &&
1249 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1250 MachineFunction &MF = DAG.getMachineFunction();
1251 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1252 unsigned Reg = FuncInfo->getSRetReturnReg();
1253 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001255 FuncInfo->setSRetReturnReg(Reg);
1256 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001257 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001258
Dale Johannesendd64c412009-02-04 00:33:20 +00001259 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001260 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001261
1262 // RAX now acts like a return value.
1263 MF.getRegInfo().addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001264 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Chris Lattner447ff682008-03-11 03:23:40 +00001266 RetOps[0] = Chain; // Update chain.
1267
1268 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001269 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001270 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001271
1272 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001274}
1275
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276/// LowerCallResult - Lower the result values of a call into the
1277/// appropriate copies out of appropriate physical registers.
1278///
1279SDValue
1280X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001281 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001282 const SmallVectorImpl<ISD::InputArg> &Ins,
1283 DebugLoc dl, SelectionDAG &DAG,
1284 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001285
Chris Lattnere32bbf62007-02-28 07:09:55 +00001286 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001287 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001288 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001290 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001292
Chris Lattner3085e152007-02-25 08:59:22 +00001293 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001294 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001295 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001296 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001297
Torok Edwin3f142c32009-02-01 18:15:56 +00001298 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001300 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001301 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001302 }
1303
Chris Lattner8e6da152008-03-10 21:08:41 +00001304 // If this is a call to a function that returns an fp value on the floating
1305 // point stack, but where we prefer to use the value in xmm registers, copy
1306 // 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 +00001307 if ((VA.getLocReg() == X86::ST0 ||
1308 VA.getLocReg() == X86::ST1) &&
1309 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001310 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001311 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001312
Evan Cheng79fb3b42009-02-20 20:43:02 +00001313 SDValue Val;
1314 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001315 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1316 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1317 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001319 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1321 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001322 } else {
1323 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001325 Val = Chain.getValue(0);
1326 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001327 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1328 } else {
1329 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1330 CopyVT, InFlag).getValue(1);
1331 Val = Chain.getValue(0);
1332 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001333 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001334
Dan Gohman37eed792009-02-04 17:28:58 +00001335 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001336 // Round the F80 the right size, which also moves to the appropriate xmm
1337 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001338 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001339 // This truncation won't change the value.
1340 DAG.getIntPtrConstant(1));
1341 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001342
Dan Gohman98ca4f22009-08-05 01:29:28 +00001343 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001344 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001345
Dan Gohman98ca4f22009-08-05 01:29:28 +00001346 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001347}
1348
1349
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001350//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001351// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001352//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001353// StdCall calling convention seems to be standard for many Windows' API
1354// routines and around. It differs from C calling convention just a little:
1355// callee should clean up the stack, not caller. Symbols should be also
1356// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001357// For info on fast calling convention see Fast Calling Convention (tail call)
1358// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001361/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1363 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001364 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001365
Dan Gohman98ca4f22009-08-05 01:29:28 +00001366 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001367}
1368
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001369/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001370/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371static bool
1372ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1373 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001374 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001375
Dan Gohman98ca4f22009-08-05 01:29:28 +00001376 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001377}
1378
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001379/// IsCalleePop - Determines whether the callee is required to pop its
1380/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001381bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 if (IsVarArg)
1383 return false;
1384
Dan Gohman095cc292008-09-13 01:54:27 +00001385 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001386 default:
1387 return false;
1388 case CallingConv::X86_StdCall:
1389 return !Subtarget->is64Bit();
1390 case CallingConv::X86_FastCall:
1391 return !Subtarget->is64Bit();
1392 case CallingConv::Fast:
1393 return PerformTailCallOpt;
1394 }
1395}
1396
Dan Gohman095cc292008-09-13 01:54:27 +00001397/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1398/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001399CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001400 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001401 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001402 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001403 else
1404 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001405 }
1406
Gordon Henriksen86737662008-01-05 16:56:59 +00001407 if (CC == CallingConv::X86_FastCall)
1408 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001409 else if (CC == CallingConv::Fast)
1410 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001411 else
1412 return CC_X86_32_C;
1413}
1414
Dan Gohman98ca4f22009-08-05 01:29:28 +00001415/// NameDecorationForCallConv - Selects the appropriate decoration to
1416/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001417NameDecorationStyle
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001418X86TargetLowering::NameDecorationForCallConv(CallingConv::ID CallConv) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001420 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001421 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001422 return StdCall;
1423 return None;
1424}
1425
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001426
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001427/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1428/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001429/// the specific parameter attribute. The copy will be passed as a byval
1430/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001431static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001432CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001433 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1434 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001436 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001437 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001438}
1439
Dan Gohman98ca4f22009-08-05 01:29:28 +00001440SDValue
1441X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001442 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001443 const SmallVectorImpl<ISD::InputArg> &Ins,
1444 DebugLoc dl, SelectionDAG &DAG,
1445 const CCValAssign &VA,
1446 MachineFrameInfo *MFI,
1447 unsigned i) {
1448
Rafael Espindola7effac52007-09-14 15:48:13 +00001449 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1451 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001452 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001453 EVT ValVT;
1454
1455 // If value is passed by pointer we have address passed instead of the value
1456 // itself.
1457 if (VA.getLocInfo() == CCValAssign::Indirect)
1458 ValVT = VA.getLocVT();
1459 else
1460 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001461
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001462 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001463 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001464 // In case of tail call optimization mark all arguments mutable. Since they
1465 // could be overwritten by lowering of arguments in case of a tail call.
Anton Korobeynikov22472762009-08-14 18:19:10 +00001466 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
David Greene3f2bf852009-11-12 20:49:22 +00001467 VA.getLocMemOffset(), isImmutable, false);
Dan Gohman475871a2008-07-27 21:46:04 +00001468 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001469 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001470 return FIN;
Anton Korobeynikov22472762009-08-14 18:19:10 +00001471 return DAG.getLoad(ValVT, dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00001472 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001473}
1474
Dan Gohman475871a2008-07-27 21:46:04 +00001475SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001476X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001477 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001478 bool isVarArg,
1479 const SmallVectorImpl<ISD::InputArg> &Ins,
1480 DebugLoc dl,
1481 SelectionDAG &DAG,
1482 SmallVectorImpl<SDValue> &InVals) {
1483
Evan Cheng1bc78042006-04-26 01:20:17 +00001484 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001486
Gordon Henriksen86737662008-01-05 16:56:59 +00001487 const Function* Fn = MF.getFunction();
1488 if (Fn->hasExternalLinkage() &&
1489 Subtarget->isTargetCygMing() &&
1490 Fn->getName() == "main")
1491 FuncInfo->setForceFramePointer(true);
1492
1493 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001494 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001495
Evan Cheng1bc78042006-04-26 01:20:17 +00001496 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001497 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001498 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001499
Dan Gohman98ca4f22009-08-05 01:29:28 +00001500 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001501 "Var args not supported with calling convention fastcc");
1502
Chris Lattner638402b2007-02-28 07:00:42 +00001503 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001504 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001505 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1506 ArgLocs, *DAG.getContext());
1507 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001508
Chris Lattnerf39f7712007-02-28 05:46:49 +00001509 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001510 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001511 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1512 CCValAssign &VA = ArgLocs[i];
1513 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1514 // places.
1515 assert(VA.getValNo() != LastVal &&
1516 "Don't support value assigned to multiple locs yet");
1517 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001518
Chris Lattnerf39f7712007-02-28 05:46:49 +00001519 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001520 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001521 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001522 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001523 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001525 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001529 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001530 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001531 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001532 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1533 RC = X86::VR64RegisterClass;
1534 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001535 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001536
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001537 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001539
Chris Lattnerf39f7712007-02-28 05:46:49 +00001540 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1541 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1542 // right size.
1543 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001544 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001545 DAG.getValueType(VA.getValVT()));
1546 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001547 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001548 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001549 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001550 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001551
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001552 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001553 // Handle MMX values passed in XMM regs.
1554 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1556 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001557 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1558 } else
1559 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001560 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001561 } else {
1562 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001564 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001565
1566 // If value is passed via pointer - do a load.
1567 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001568 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001569
Dan Gohman98ca4f22009-08-05 01:29:28 +00001570 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001571 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001572
Dan Gohman61a92132008-04-21 23:59:07 +00001573 // The x86-64 ABI for returning structs by value requires that we copy
1574 // the sret argument into %rax for the return. Save the argument into
1575 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001576 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001577 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1578 unsigned Reg = FuncInfo->getSRetReturnReg();
1579 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001580 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001581 FuncInfo->setSRetReturnReg(Reg);
1582 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001583 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001584 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001585 }
1586
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001588 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001590 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001591
Evan Cheng1bc78042006-04-26 01:20:17 +00001592 // If the function takes variable number of arguments, make a frame index for
1593 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001594 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001595 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001596 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001597 }
1598 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001599 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1600
1601 // FIXME: We should really autogenerate these arrays
1602 static const unsigned GPR64ArgRegsWin64[] = {
1603 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001604 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001605 static const unsigned XMMArgRegsWin64[] = {
1606 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1607 };
1608 static const unsigned GPR64ArgRegs64Bit[] = {
1609 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1610 };
1611 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001612 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1613 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1614 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001615 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1616
1617 if (IsWin64) {
1618 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1619 GPR64ArgRegs = GPR64ArgRegsWin64;
1620 XMMArgRegs = XMMArgRegsWin64;
1621 } else {
1622 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1623 GPR64ArgRegs = GPR64ArgRegs64Bit;
1624 XMMArgRegs = XMMArgRegs64Bit;
1625 }
1626 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1627 TotalNumIntRegs);
1628 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1629 TotalNumXMMRegs);
1630
Devang Patel578efa92009-06-05 21:57:13 +00001631 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001632 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001633 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001634 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001635 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001636 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001637 // Kernel mode asks for SSE to be disabled, so don't push them
1638 // on the stack.
1639 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001640
Gordon Henriksen86737662008-01-05 16:56:59 +00001641 // For X86-64, if there are vararg parameters that are passed via
1642 // registers, then we must store them to their spots on the stack so they
1643 // may be loaded by deferencing the result of va_next.
1644 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001645 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1646 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001647 TotalNumXMMRegs * 16, 16,
1648 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001649
Gordon Henriksen86737662008-01-05 16:56:59 +00001650 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SmallVector<SDValue, 8> MemOps;
1652 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001653 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001654 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001655 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1656 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001657 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1658 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001659 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001660 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001661 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001662 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
Dan Gohmand6708ea2009-08-15 01:38:56 +00001663 Offset);
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001665 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001667
Dan Gohmanface41a2009-08-16 21:24:25 +00001668 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1669 // Now store the XMM (fp + vector) parameter registers.
1670 SmallVector<SDValue, 11> SaveXMMOps;
1671 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001672
Dan Gohmanface41a2009-08-16 21:24:25 +00001673 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1674 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1675 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001676
Dan Gohmanface41a2009-08-16 21:24:25 +00001677 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1678 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001679
Dan Gohmanface41a2009-08-16 21:24:25 +00001680 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1681 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1682 X86::VR128RegisterClass);
1683 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1684 SaveXMMOps.push_back(Val);
1685 }
1686 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1687 MVT::Other,
1688 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001689 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001690
1691 if (!MemOps.empty())
1692 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1693 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001694 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001696
Gordon Henriksen86737662008-01-05 16:56:59 +00001697 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001698 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001700 BytesCallerReserves = 0;
1701 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001702 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001703 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001704 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001705 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001706 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001707 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001708
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 if (!Is64Bit) {
1710 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1713 }
Evan Cheng25caf632006-05-23 21:06:34 +00001714
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001715 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001718}
1719
Dan Gohman475871a2008-07-27 21:46:04 +00001720SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1722 SDValue StackPtr, SDValue Arg,
1723 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001724 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001726 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001727 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001728 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001729 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001730 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001731 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001732 }
Dale Johannesenace16102009-02-03 19:33:06 +00001733 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001734 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001735}
1736
Bill Wendling64e87322009-01-16 19:25:27 +00001737/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001738/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001739SDValue
1740X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001741 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001742 SDValue Chain,
1743 bool IsTailCall,
1744 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001745 int FPDiff,
1746 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001747 if (!IsTailCall || FPDiff==0) return Chain;
1748
1749 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001750 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001751 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001752
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001753 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001754 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001755 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001756}
1757
1758/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1759/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001760static SDValue
1761EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001762 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001763 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001764 // Store the return address to the appropriate stack slot.
1765 if (!FPDiff) return Chain;
1766 // Calculate the new stack slot for the return address.
1767 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001768 int NewReturnAddrFI =
David Greene3f2bf852009-11-12 20:49:22 +00001769 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize,
1770 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001771 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001772 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001773 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Evan Cheng65531552009-10-17 07:53:04 +00001774 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001775 return Chain;
1776}
1777
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778SDValue
1779X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001780 CallingConv::ID CallConv, bool isVarArg,
1781 bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 const SmallVectorImpl<ISD::OutputArg> &Outs,
1783 const SmallVectorImpl<ISD::InputArg> &Ins,
1784 DebugLoc dl, SelectionDAG &DAG,
1785 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001786
Dan Gohman98ca4f22009-08-05 01:29:28 +00001787 MachineFunction &MF = DAG.getMachineFunction();
1788 bool Is64Bit = Subtarget->is64Bit();
1789 bool IsStructRet = CallIsStructReturn(Outs);
1790
1791 assert((!isTailCall ||
1792 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1793 "IsEligibleForTailCallOptimization missed a case!");
1794 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001795 "Var args not supported with calling convention fastcc");
1796
Chris Lattner638402b2007-02-28 07:00:42 +00001797 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001798 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001799 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1800 ArgLocs, *DAG.getContext());
1801 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001802
Chris Lattner423c5f42007-02-28 05:31:48 +00001803 // Get a count of how many bytes are to be pushed on the stack.
1804 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001805 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001806 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001807
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001809 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001811 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001812 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1813 FPDiff = NumBytesCallerPushed - NumBytes;
1814
1815 // Set the delta of movement of the returnaddr stackslot.
1816 // But only set if delta is greater than previous delta.
1817 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1818 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1819 }
1820
Chris Lattnere563bbc2008-10-11 22:08:30 +00001821 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001822
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001824 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001826 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001827
Dan Gohman475871a2008-07-27 21:46:04 +00001828 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1829 SmallVector<SDValue, 8> MemOpChains;
1830 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001831
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001832 // Walk the register/memloc assignments, inserting copies/loads. In the case
1833 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001834 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1835 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001836 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001837 SDValue Arg = Outs[i].Val;
1838 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001839 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Chris Lattner423c5f42007-02-28 05:31:48 +00001841 // Promote the value if needed.
1842 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001843 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001844 case CCValAssign::Full: break;
1845 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001846 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001847 break;
1848 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001849 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001850 break;
1851 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001852 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1853 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001854 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1855 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1856 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001857 } else
1858 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1859 break;
1860 case CCValAssign::BCvt:
1861 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001862 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001863 case CCValAssign::Indirect: {
1864 // Store the argument.
1865 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001866 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001867 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00001868 PseudoSourceValue::getFixedStack(FI), 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001869 Arg = SpillSlot;
1870 break;
1871 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001873
Chris Lattner423c5f42007-02-28 05:31:48 +00001874 if (VA.isRegLoc()) {
1875 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1876 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001877 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001878 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001879 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001880 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001881
Dan Gohman98ca4f22009-08-05 01:29:28 +00001882 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1883 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001884 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001885 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001887
Evan Cheng32fe1032006-05-25 00:59:30 +00001888 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001890 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001891
Evan Cheng347d5f72006-04-28 21:29:37 +00001892 // Build a sequence of copy-to-reg nodes chained together with token chain
1893 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001894 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001895 // Tail call byval lowering might overwrite argument registers so in case of
1896 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001897 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001898 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001899 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001900 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001901 InFlag = Chain.getValue(1);
1902 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001903
Eric Christopherfd179292009-08-27 18:07:15 +00001904
Chris Lattner88e1fd52009-07-09 04:24:46 +00001905 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001906 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1907 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001908 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001909 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1910 DAG.getNode(X86ISD::GlobalBaseReg,
1911 DebugLoc::getUnknownLoc(),
1912 getPointerTy()),
1913 InFlag);
1914 InFlag = Chain.getValue(1);
1915 } else {
1916 // If we are tail calling and generating PIC/GOT style code load the
1917 // address of the callee into ECX. The value in ecx is used as target of
1918 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1919 // for tail calls on PIC/GOT architectures. Normally we would just put the
1920 // address of GOT into ebx and then call target@PLT. But for tail calls
1921 // ebx would be restored (since ebx is callee saved) before jumping to the
1922 // target@PLT.
1923
1924 // Note: The actual moving to ECX is done further down.
1925 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1926 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1927 !G->getGlobal()->hasProtectedVisibility())
1928 Callee = LowerGlobalAddress(Callee, DAG);
1929 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001930 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001931 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001932 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001933
Gordon Henriksen86737662008-01-05 16:56:59 +00001934 if (Is64Bit && isVarArg) {
1935 // From AMD64 ABI document:
1936 // For calls that may call functions that use varargs or stdargs
1937 // (prototype-less calls or calls to functions containing ellipsis (...) in
1938 // the declaration) %al is used as hidden argument to specify the number
1939 // of SSE registers used. The contents of %al do not need to match exactly
1940 // the number of registers, but must be an ubound on the number of SSE
1941 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001942
1943 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001944 // Count the number of XMM registers allocated.
1945 static const unsigned XMMArgRegs[] = {
1946 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1947 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1948 };
1949 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001951 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001952
Dale Johannesendd64c412009-02-04 00:33:20 +00001953 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 InFlag = Chain.getValue(1);
1956 }
1957
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001958
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001959 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001960 if (isTailCall) {
1961 // Force all the incoming stack arguments to be loaded from the stack
1962 // before any new outgoing arguments are stored to the stack, because the
1963 // outgoing stack slots may alias the incoming argument stack slots, and
1964 // the alias isn't otherwise explicit. This is slightly more conservative
1965 // than necessary, because it means that each store effectively depends
1966 // on every argument instead of just those arguments it would clobber.
1967 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969 SmallVector<SDValue, 8> MemOpChains2;
1970 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001971 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001972 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001973 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1975 CCValAssign &VA = ArgLocs[i];
1976 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001977 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001978 SDValue Arg = Outs[i].Val;
1979 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001980 // Create frame index.
1981 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001982 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001983 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001984 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001985
Duncan Sands276dcbd2008-03-21 09:14:45 +00001986 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001987 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001988 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001989 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001990 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001991 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001992 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001993
Dan Gohman98ca4f22009-08-05 01:29:28 +00001994 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1995 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001996 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001998 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001999 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 DAG.getStore(ArgChain, dl, Arg, FIN,
Evan Cheng65531552009-10-17 07:53:04 +00002001 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002002 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002003 }
2004 }
2005
2006 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002008 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002009
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002010 // Copy arguments to their registers.
2011 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002012 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002013 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002014 InFlag = Chain.getValue(1);
2015 }
Dan Gohman475871a2008-07-27 21:46:04 +00002016 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002017
Gordon Henriksen86737662008-01-05 16:56:59 +00002018 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002019 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002020 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 }
2022
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002023 bool WasGlobalOrExternal = false;
2024 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2025 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2026 // In the 64-bit large code model, we have to make all calls
2027 // through a register, since the call instruction's 32-bit
2028 // pc-relative offset may not be large enough to hold the whole
2029 // address.
2030 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2031 WasGlobalOrExternal = true;
2032 // If the callee is a GlobalAddress node (quite common, every direct call
2033 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2034 // it.
2035
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002036 // We should use extra load for direct calls to dllimported functions in
2037 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002038 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002039 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002040 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002041
Chris Lattner48a7d022009-07-09 05:02:21 +00002042 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2043 // external symbols most go through the PLT in PIC mode. If the symbol
2044 // has hidden or protected visibility, or if it is static or local, then
2045 // we don't need to use the PLT - we can directly call it.
2046 if (Subtarget->isTargetELF() &&
2047 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002048 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002049 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002050 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002051 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2052 Subtarget->getDarwinVers() < 9) {
2053 // PC-relative references to external symbols should go through $stub,
2054 // unless we're building with the leopard linker or later, which
2055 // automatically synthesizes these stubs.
2056 OpFlags = X86II::MO_DARWIN_STUB;
2057 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002058
Chris Lattner74e726e2009-07-09 05:27:35 +00002059 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002060 G->getOffset(), OpFlags);
2061 }
Bill Wendling056292f2008-09-16 21:48:12 +00002062 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002063 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002064 unsigned char OpFlags = 0;
2065
2066 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2067 // symbols should go through the PLT.
2068 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002069 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002070 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002071 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002072 Subtarget->getDarwinVers() < 9) {
2073 // PC-relative references to external symbols should go through $stub,
2074 // unless we're building with the leopard linker or later, which
2075 // automatically synthesizes these stubs.
2076 OpFlags = X86II::MO_DARWIN_STUB;
2077 }
Eric Christopherfd179292009-08-27 18:07:15 +00002078
Chris Lattner48a7d022009-07-09 05:02:21 +00002079 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2080 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002081 }
2082
2083 if (isTailCall && !WasGlobalOrExternal) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00002084 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00002085
Dale Johannesendd64c412009-02-04 00:33:20 +00002086 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00002087 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002088 Callee,InFlag);
2089 Callee = DAG.getRegister(Opc, getPointerTy());
2090 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00002091 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002092 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002093
Chris Lattnerd96d0722007-02-25 06:40:16 +00002094 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002095 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002096 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002097
Dan Gohman98ca4f22009-08-05 01:29:28 +00002098 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002099 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2100 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002101 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002103
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002104 Ops.push_back(Chain);
2105 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002106
Dan Gohman98ca4f22009-08-05 01:29:28 +00002107 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002108 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002109
Gordon Henriksen86737662008-01-05 16:56:59 +00002110 // Add argument registers to the end of the list so that they are known live
2111 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002112 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2113 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2114 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002115
Evan Cheng586ccac2008-03-18 23:36:35 +00002116 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002117 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002118 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2119
2120 // Add an implicit use of AL for x86 vararg functions.
2121 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002122 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002123
Gabor Greifba36cb52008-08-28 21:40:38 +00002124 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002125 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002126
Dan Gohman98ca4f22009-08-05 01:29:28 +00002127 if (isTailCall) {
2128 // If this is the first return lowered for this function, add the regs
2129 // to the liveout set for the function.
2130 if (MF.getRegInfo().liveout_empty()) {
2131 SmallVector<CCValAssign, 16> RVLocs;
2132 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2133 *DAG.getContext());
2134 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2135 for (unsigned i = 0; i != RVLocs.size(); ++i)
2136 if (RVLocs[i].isRegLoc())
2137 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2138 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002139
Dan Gohman98ca4f22009-08-05 01:29:28 +00002140 assert(((Callee.getOpcode() == ISD::Register &&
2141 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002142 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2144 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002145 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002146
2147 return DAG.getNode(X86ISD::TC_RETURN, dl,
2148 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002149 }
2150
Dale Johannesenace16102009-02-03 19:33:06 +00002151 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002152 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002153
Chris Lattner2d297092006-05-23 18:50:38 +00002154 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002156 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00002158 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002159 // If this is is a call to a struct-return function, the callee
2160 // pops the hidden struct pointer, so we have to push it back.
2161 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002162 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002164 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002165
Gordon Henriksenae636f82008-01-03 16:47:34 +00002166 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002167 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002168 DAG.getIntPtrConstant(NumBytes, true),
2169 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2170 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002171 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002172 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002173
Chris Lattner3085e152007-02-25 08:59:22 +00002174 // Handle result values, copying them out of physregs into vregs that we
2175 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002176 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2177 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002178}
2179
Evan Cheng25ab6902006-09-08 06:48:29 +00002180
2181//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002182// Fast Calling Convention (tail call) implementation
2183//===----------------------------------------------------------------------===//
2184
2185// Like std call, callee cleans arguments, convention except that ECX is
2186// reserved for storing the tail called function address. Only 2 registers are
2187// free for argument passing (inreg). Tail call optimization is performed
2188// provided:
2189// * tailcallopt is enabled
2190// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002191// On X86_64 architecture with GOT-style position independent code only local
2192// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002193// To keep the stack aligned according to platform abi the function
2194// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2195// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002196// If a tail called function callee has more arguments than the caller the
2197// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002198// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002199// original REtADDR, but before the saved framepointer or the spilled registers
2200// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2201// stack layout:
2202// arg1
2203// arg2
2204// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002205// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002206// move area ]
2207// (possible EBP)
2208// ESI
2209// EDI
2210// local1 ..
2211
2212/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2213/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002214unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002215 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002216 MachineFunction &MF = DAG.getMachineFunction();
2217 const TargetMachine &TM = MF.getTarget();
2218 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2219 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002220 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002221 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002222 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002223 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2224 // Number smaller than 12 so just add the difference.
2225 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2226 } else {
2227 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002228 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002229 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002230 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002231 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002232}
2233
Dan Gohman98ca4f22009-08-05 01:29:28 +00002234/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2235/// for tail call optimization. Targets which want to do tail call
2236/// optimization should implement this function.
2237bool
2238X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002239 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002240 bool isVarArg,
2241 const SmallVectorImpl<ISD::InputArg> &Ins,
2242 SelectionDAG& DAG) const {
2243 MachineFunction &MF = DAG.getMachineFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002244 CallingConv::ID CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00002245 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002246}
2247
Dan Gohman3df24e62008-09-03 23:12:08 +00002248FastISel *
2249X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002250 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002251 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002252 DenseMap<const Value *, unsigned> &vm,
2253 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002254 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002255 DenseMap<const AllocaInst *, int> &am
2256#ifndef NDEBUG
2257 , SmallSet<Instruction*, 8> &cil
2258#endif
2259 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002260 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002261#ifndef NDEBUG
2262 , cil
2263#endif
2264 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002265}
2266
2267
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002268//===----------------------------------------------------------------------===//
2269// Other Lowering Hooks
2270//===----------------------------------------------------------------------===//
2271
2272
Dan Gohman475871a2008-07-27 21:46:04 +00002273SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2276 int ReturnAddrIndex = FuncInfo->getRAIndex();
2277
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002278 if (ReturnAddrIndex == 0) {
2279 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002280 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002281 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
2282 true, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002283 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002284 }
2285
Evan Cheng25ab6902006-09-08 06:48:29 +00002286 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002287}
2288
2289
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002290bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2291 bool hasSymbolicDisplacement) {
2292 // Offset should fit into 32 bit immediate field.
2293 if (!isInt32(Offset))
2294 return false;
2295
2296 // If we don't have a symbolic displacement - we don't have any extra
2297 // restrictions.
2298 if (!hasSymbolicDisplacement)
2299 return true;
2300
2301 // FIXME: Some tweaks might be needed for medium code model.
2302 if (M != CodeModel::Small && M != CodeModel::Kernel)
2303 return false;
2304
2305 // For small code model we assume that latest object is 16MB before end of 31
2306 // bits boundary. We may also accept pretty large negative constants knowing
2307 // that all objects are in the positive half of address space.
2308 if (M == CodeModel::Small && Offset < 16*1024*1024)
2309 return true;
2310
2311 // For kernel code model we know that all object resist in the negative half
2312 // of 32bits address space. We may not accept negative offsets, since they may
2313 // be just off and we may accept pretty large positive ones.
2314 if (M == CodeModel::Kernel && Offset > 0)
2315 return true;
2316
2317 return false;
2318}
2319
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002320/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2321/// specific condition code, returning the condition code and the LHS/RHS of the
2322/// comparison to make.
2323static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2324 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002325 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002326 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2327 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2328 // X > -1 -> X == 0, jump !sign.
2329 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002330 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002331 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2332 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002333 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002334 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002335 // X < 1 -> X <= 0
2336 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002337 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002338 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002339 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002340
Evan Chengd9558e02006-01-06 00:43:03 +00002341 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002342 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002343 case ISD::SETEQ: return X86::COND_E;
2344 case ISD::SETGT: return X86::COND_G;
2345 case ISD::SETGE: return X86::COND_GE;
2346 case ISD::SETLT: return X86::COND_L;
2347 case ISD::SETLE: return X86::COND_LE;
2348 case ISD::SETNE: return X86::COND_NE;
2349 case ISD::SETULT: return X86::COND_B;
2350 case ISD::SETUGT: return X86::COND_A;
2351 case ISD::SETULE: return X86::COND_BE;
2352 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002353 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002354 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002355
Chris Lattner4c78e022008-12-23 23:42:27 +00002356 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002357
Chris Lattner4c78e022008-12-23 23:42:27 +00002358 // If LHS is a foldable load, but RHS is not, flip the condition.
2359 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2360 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2361 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2362 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002363 }
2364
Chris Lattner4c78e022008-12-23 23:42:27 +00002365 switch (SetCCOpcode) {
2366 default: break;
2367 case ISD::SETOLT:
2368 case ISD::SETOLE:
2369 case ISD::SETUGT:
2370 case ISD::SETUGE:
2371 std::swap(LHS, RHS);
2372 break;
2373 }
2374
2375 // On a floating point condition, the flags are set as follows:
2376 // ZF PF CF op
2377 // 0 | 0 | 0 | X > Y
2378 // 0 | 0 | 1 | X < Y
2379 // 1 | 0 | 0 | X == Y
2380 // 1 | 1 | 1 | unordered
2381 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002382 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002383 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002384 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002385 case ISD::SETOLT: // flipped
2386 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002387 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002388 case ISD::SETOLE: // flipped
2389 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002390 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002391 case ISD::SETUGT: // flipped
2392 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002393 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002394 case ISD::SETUGE: // flipped
2395 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002396 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002397 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002398 case ISD::SETNE: return X86::COND_NE;
2399 case ISD::SETUO: return X86::COND_P;
2400 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002401 case ISD::SETOEQ:
2402 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002403 }
Evan Chengd9558e02006-01-06 00:43:03 +00002404}
2405
Evan Cheng4a460802006-01-11 00:33:36 +00002406/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2407/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002408/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002409static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002410 switch (X86CC) {
2411 default:
2412 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002413 case X86::COND_B:
2414 case X86::COND_BE:
2415 case X86::COND_E:
2416 case X86::COND_P:
2417 case X86::COND_A:
2418 case X86::COND_AE:
2419 case X86::COND_NE:
2420 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002421 return true;
2422 }
2423}
2424
Evan Chengeb2f9692009-10-27 19:56:55 +00002425/// isFPImmLegal - Returns true if the target can instruction select the
2426/// specified FP immediate natively. If false, the legalizer will
2427/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002428bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002429 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2430 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2431 return true;
2432 }
2433 return false;
2434}
2435
Nate Begeman9008ca62009-04-27 18:41:29 +00002436/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2437/// the specified range (L, H].
2438static bool isUndefOrInRange(int Val, int Low, int Hi) {
2439 return (Val < 0) || (Val >= Low && Val < Hi);
2440}
2441
2442/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2443/// specified value.
2444static bool isUndefOrEqual(int Val, int CmpVal) {
2445 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002446 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002447 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002448}
2449
Nate Begeman9008ca62009-04-27 18:41:29 +00002450/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2451/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2452/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002453static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002454 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002456 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002457 return (Mask[0] < 2 && Mask[1] < 2);
2458 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002459}
2460
Nate Begeman9008ca62009-04-27 18:41:29 +00002461bool X86::isPSHUFDMask(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 ::isPSHUFDMask(M, N->getValueType(0));
2465}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002466
Nate Begeman9008ca62009-04-27 18:41:29 +00002467/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2468/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002469static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002470 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002471 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002472
Nate Begeman9008ca62009-04-27 18:41:29 +00002473 // Lower quadword copied in order or undef.
2474 for (int i = 0; i != 4; ++i)
2475 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002476 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002477
Evan Cheng506d3df2006-03-29 23:07:14 +00002478 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002479 for (int i = 4; i != 8; ++i)
2480 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002481 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002482
Evan Cheng506d3df2006-03-29 23:07:14 +00002483 return true;
2484}
2485
Nate Begeman9008ca62009-04-27 18:41:29 +00002486bool X86::isPSHUFHWMask(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 ::isPSHUFHWMask(M, N->getValueType(0));
2490}
Evan Cheng506d3df2006-03-29 23:07:14 +00002491
Nate Begeman9008ca62009-04-27 18:41:29 +00002492/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2493/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002494static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002495 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002496 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002497
Rafael Espindola15684b22009-04-24 12:40:33 +00002498 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002499 for (int i = 4; i != 8; ++i)
2500 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002501 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002502
Rafael Espindola15684b22009-04-24 12:40:33 +00002503 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002504 for (int i = 0; i != 4; ++i)
2505 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002506 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002507
Rafael Espindola15684b22009-04-24 12:40:33 +00002508 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002509}
2510
Nate Begeman9008ca62009-04-27 18:41:29 +00002511bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002512 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002513 N->getMask(M);
2514 return ::isPSHUFLWMask(M, N->getValueType(0));
2515}
2516
Nate Begemana09008b2009-10-19 02:17:23 +00002517/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2518/// is suitable for input to PALIGNR.
2519static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2520 bool hasSSSE3) {
2521 int i, e = VT.getVectorNumElements();
2522
2523 // Do not handle v2i64 / v2f64 shuffles with palignr.
2524 if (e < 4 || !hasSSSE3)
2525 return false;
2526
2527 for (i = 0; i != e; ++i)
2528 if (Mask[i] >= 0)
2529 break;
2530
2531 // All undef, not a palignr.
2532 if (i == e)
2533 return false;
2534
2535 // Determine if it's ok to perform a palignr with only the LHS, since we
2536 // don't have access to the actual shuffle elements to see if RHS is undef.
2537 bool Unary = Mask[i] < (int)e;
2538 bool NeedsUnary = false;
2539
2540 int s = Mask[i] - i;
2541
2542 // Check the rest of the elements to see if they are consecutive.
2543 for (++i; i != e; ++i) {
2544 int m = Mask[i];
2545 if (m < 0)
2546 continue;
2547
2548 Unary = Unary && (m < (int)e);
2549 NeedsUnary = NeedsUnary || (m < s);
2550
2551 if (NeedsUnary && !Unary)
2552 return false;
2553 if (Unary && m != ((s+i) & (e-1)))
2554 return false;
2555 if (!Unary && m != (s+i))
2556 return false;
2557 }
2558 return true;
2559}
2560
2561bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2562 SmallVector<int, 8> M;
2563 N->getMask(M);
2564 return ::isPALIGNRMask(M, N->getValueType(0), true);
2565}
2566
Evan Cheng14aed5e2006-03-24 01:18:28 +00002567/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2568/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002569static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002570 int NumElems = VT.getVectorNumElements();
2571 if (NumElems != 2 && NumElems != 4)
2572 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002573
Nate Begeman9008ca62009-04-27 18:41:29 +00002574 int Half = NumElems / 2;
2575 for (int i = 0; i < Half; ++i)
2576 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002577 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002578 for (int i = Half; i < NumElems; ++i)
2579 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002580 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002581
Evan Cheng14aed5e2006-03-24 01:18:28 +00002582 return true;
2583}
2584
Nate Begeman9008ca62009-04-27 18:41:29 +00002585bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2586 SmallVector<int, 8> M;
2587 N->getMask(M);
2588 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002589}
2590
Evan Cheng213d2cf2007-05-17 18:45:50 +00002591/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002592/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2593/// half elements to come from vector 1 (which would equal the dest.) and
2594/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002595static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002597
2598 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002600
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 int Half = NumElems / 2;
2602 for (int i = 0; i < Half; ++i)
2603 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002604 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 for (int i = Half; i < NumElems; ++i)
2606 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002607 return false;
2608 return true;
2609}
2610
Nate Begeman9008ca62009-04-27 18:41:29 +00002611static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2612 SmallVector<int, 8> M;
2613 N->getMask(M);
2614 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002615}
2616
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002617/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2618/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002619bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2620 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002621 return false;
2622
Evan Cheng2064a2b2006-03-28 06:50:32 +00002623 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2625 isUndefOrEqual(N->getMaskElt(1), 7) &&
2626 isUndefOrEqual(N->getMaskElt(2), 2) &&
2627 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002628}
2629
Nate Begeman0b10b912009-11-07 23:17:15 +00002630/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2631/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2632/// <2, 3, 2, 3>
2633bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2634 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2635
2636 if (NumElems != 4)
2637 return false;
2638
2639 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2640 isUndefOrEqual(N->getMaskElt(1), 3) &&
2641 isUndefOrEqual(N->getMaskElt(2), 2) &&
2642 isUndefOrEqual(N->getMaskElt(3), 3);
2643}
2644
Evan Cheng5ced1d82006-04-06 23:23:56 +00002645/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2646/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002647bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2648 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002649
Evan Cheng5ced1d82006-04-06 23:23:56 +00002650 if (NumElems != 2 && NumElems != 4)
2651 return false;
2652
Evan Chengc5cdff22006-04-07 21:53:05 +00002653 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002654 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002655 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002656
Evan Chengc5cdff22006-04-07 21:53:05 +00002657 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002659 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002660
2661 return true;
2662}
2663
Nate Begeman0b10b912009-11-07 23:17:15 +00002664/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2665/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2666bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002667 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002668
Evan Cheng5ced1d82006-04-06 23:23:56 +00002669 if (NumElems != 2 && NumElems != 4)
2670 return false;
2671
Evan Chengc5cdff22006-04-07 21:53:05 +00002672 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002673 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002674 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002675
Nate Begeman9008ca62009-04-27 18:41:29 +00002676 for (unsigned i = 0; i < NumElems/2; ++i)
2677 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002678 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002679
2680 return true;
2681}
2682
Evan Cheng0038e592006-03-28 00:39:58 +00002683/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2684/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002685static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002686 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002687 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002688 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002689 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002690
Nate Begeman9008ca62009-04-27 18:41:29 +00002691 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2692 int BitI = Mask[i];
2693 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002694 if (!isUndefOrEqual(BitI, j))
2695 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002696 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002697 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002698 return false;
2699 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002700 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002701 return false;
2702 }
Evan Cheng0038e592006-03-28 00:39:58 +00002703 }
Evan Cheng0038e592006-03-28 00:39:58 +00002704 return true;
2705}
2706
Nate Begeman9008ca62009-04-27 18:41:29 +00002707bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2708 SmallVector<int, 8> M;
2709 N->getMask(M);
2710 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002711}
2712
Evan Cheng4fcb9222006-03-28 02:43:26 +00002713/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2714/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002715static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002716 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002717 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002718 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002719 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002720
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2722 int BitI = Mask[i];
2723 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002724 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002725 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002726 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002727 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002728 return false;
2729 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002730 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002731 return false;
2732 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002733 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002734 return true;
2735}
2736
Nate Begeman9008ca62009-04-27 18:41:29 +00002737bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2738 SmallVector<int, 8> M;
2739 N->getMask(M);
2740 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002741}
2742
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002743/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2744/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2745/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002746static bool isUNPCKL_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)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002749 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002750
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2752 int BitI = Mask[i];
2753 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002754 if (!isUndefOrEqual(BitI, j))
2755 return false;
2756 if (!isUndefOrEqual(BitI1, j))
2757 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002758 }
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::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2763 SmallVector<int, 8> M;
2764 N->getMask(M);
2765 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2766}
2767
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002768/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2769/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2770/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002771static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002772 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002773 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2774 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002775
Nate Begeman9008ca62009-04-27 18:41:29 +00002776 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2777 int BitI = Mask[i];
2778 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002779 if (!isUndefOrEqual(BitI, j))
2780 return false;
2781 if (!isUndefOrEqual(BitI1, j))
2782 return false;
2783 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002784 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002785}
2786
Nate Begeman9008ca62009-04-27 18:41:29 +00002787bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2788 SmallVector<int, 8> M;
2789 N->getMask(M);
2790 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2791}
2792
Evan Cheng017dcc62006-04-21 01:05:10 +00002793/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2794/// specifies a shuffle of elements that is suitable for input to MOVSS,
2795/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002796static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002797 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002798 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002799
2800 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002801
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002803 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002804
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 for (int i = 1; i < NumElts; ++i)
2806 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002807 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002808
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002809 return true;
2810}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002811
Nate Begeman9008ca62009-04-27 18:41:29 +00002812bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2813 SmallVector<int, 8> M;
2814 N->getMask(M);
2815 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002816}
2817
Evan Cheng017dcc62006-04-21 01:05:10 +00002818/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2819/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002820/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002821static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 bool V2IsSplat = false, bool V2IsUndef = false) {
2823 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002824 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002825 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002826
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002829
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 for (int i = 1; i < NumOps; ++i)
2831 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2832 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2833 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002834 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002835
Evan Cheng39623da2006-04-20 08:58:49 +00002836 return true;
2837}
2838
Nate Begeman9008ca62009-04-27 18:41:29 +00002839static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002840 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002841 SmallVector<int, 8> M;
2842 N->getMask(M);
2843 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002844}
2845
Evan Chengd9539472006-04-14 21:59:03 +00002846/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2847/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002848bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2849 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002850 return false;
2851
2852 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002853 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 int Elt = N->getMaskElt(i);
2855 if (Elt >= 0 && Elt != 1)
2856 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002857 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002858
2859 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002860 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 int Elt = N->getMaskElt(i);
2862 if (Elt >= 0 && Elt != 3)
2863 return false;
2864 if (Elt == 3)
2865 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002866 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002867 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002868 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002869 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002870}
2871
2872/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2873/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002874bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2875 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002876 return false;
2877
2878 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002879 for (unsigned i = 0; i < 2; ++i)
2880 if (N->getMaskElt(i) > 0)
2881 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002882
2883 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002884 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002885 int Elt = N->getMaskElt(i);
2886 if (Elt >= 0 && Elt != 2)
2887 return false;
2888 if (Elt == 2)
2889 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002890 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002892 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002893}
2894
Evan Cheng0b457f02008-09-25 20:50:48 +00002895/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2896/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002897bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2898 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00002899
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 for (int i = 0; i < e; ++i)
2901 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002902 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002903 for (int i = 0; i < e; ++i)
2904 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002905 return false;
2906 return true;
2907}
2908
Evan Cheng63d33002006-03-22 08:01:21 +00002909/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002910/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00002911unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2913 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2914
Evan Chengb9df0ca2006-03-22 02:53:00 +00002915 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2916 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 for (int i = 0; i < NumOperands; ++i) {
2918 int Val = SVOp->getMaskElt(NumOperands-i-1);
2919 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002920 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002921 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002922 if (i != NumOperands - 1)
2923 Mask <<= Shift;
2924 }
Evan Cheng63d33002006-03-22 08:01:21 +00002925 return Mask;
2926}
2927
Evan Cheng506d3df2006-03-29 23:07:14 +00002928/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002929/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002930unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002932 unsigned Mask = 0;
2933 // 8 nodes, but we only care about the last 4.
2934 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 int Val = SVOp->getMaskElt(i);
2936 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002937 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002938 if (i != 4)
2939 Mask <<= 2;
2940 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002941 return Mask;
2942}
2943
2944/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00002945/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00002946unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002948 unsigned Mask = 0;
2949 // 8 nodes, but we only care about the first 4.
2950 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 int Val = SVOp->getMaskElt(i);
2952 if (Val >= 0)
2953 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002954 if (i != 0)
2955 Mask <<= 2;
2956 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002957 return Mask;
2958}
2959
Nate Begemana09008b2009-10-19 02:17:23 +00002960/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
2961/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
2962unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
2963 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2964 EVT VVT = N->getValueType(0);
2965 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
2966 int Val = 0;
2967
2968 unsigned i, e;
2969 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
2970 Val = SVOp->getMaskElt(i);
2971 if (Val >= 0)
2972 break;
2973 }
2974 return (Val - i) * EltSize;
2975}
2976
Evan Cheng37b73872009-07-30 08:33:02 +00002977/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2978/// constant +0.0.
2979bool X86::isZeroNode(SDValue Elt) {
2980 return ((isa<ConstantSDNode>(Elt) &&
2981 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2982 (isa<ConstantFPSDNode>(Elt) &&
2983 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2984}
2985
Nate Begeman9008ca62009-04-27 18:41:29 +00002986/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2987/// their permute mask.
2988static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2989 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00002990 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002991 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002992 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00002993
Nate Begeman5a5ca152009-04-29 05:20:52 +00002994 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 int idx = SVOp->getMaskElt(i);
2996 if (idx < 0)
2997 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002998 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002999 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003000 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003001 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003002 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3004 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003005}
3006
Evan Cheng779ccea2007-12-07 21:30:01 +00003007/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3008/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003009static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003010 unsigned NumElems = VT.getVectorNumElements();
3011 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 int idx = Mask[i];
3013 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003014 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003015 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003017 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003018 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003019 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003020}
3021
Evan Cheng533a0aa2006-04-19 20:35:22 +00003022/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3023/// match movhlps. The lower half elements should come from upper half of
3024/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003025/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003026static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3027 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003028 return false;
3029 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003031 return false;
3032 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003034 return false;
3035 return true;
3036}
3037
Evan Cheng5ced1d82006-04-06 23:23:56 +00003038/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003039/// is promoted to a vector. It also returns the LoadSDNode by reference if
3040/// required.
3041static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003042 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3043 return false;
3044 N = N->getOperand(0).getNode();
3045 if (!ISD::isNON_EXTLoad(N))
3046 return false;
3047 if (LD)
3048 *LD = cast<LoadSDNode>(N);
3049 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003050}
3051
Evan Cheng533a0aa2006-04-19 20:35:22 +00003052/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3053/// match movlp{s|d}. The lower half elements should come from lower half of
3054/// V1 (and in order), and the upper half elements should come from the upper
3055/// half of V2 (and in order). And since V1 will become the source of the
3056/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003057static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3058 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003059 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003060 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003061 // Is V2 is a vector load, don't do this transformation. We will try to use
3062 // load folding shufps op.
3063 if (ISD::isNON_EXTLoad(V2))
3064 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003065
Nate Begeman5a5ca152009-04-29 05:20:52 +00003066 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003067
Evan Cheng533a0aa2006-04-19 20:35:22 +00003068 if (NumElems != 2 && NumElems != 4)
3069 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003070 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003071 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003072 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003073 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003075 return false;
3076 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003077}
3078
Evan Cheng39623da2006-04-20 08:58:49 +00003079/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3080/// all the same.
3081static bool isSplatVector(SDNode *N) {
3082 if (N->getOpcode() != ISD::BUILD_VECTOR)
3083 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003084
Dan Gohman475871a2008-07-27 21:46:04 +00003085 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003086 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3087 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003088 return false;
3089 return true;
3090}
3091
Evan Cheng213d2cf2007-05-17 18:45:50 +00003092/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003093/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003094/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003095static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003096 SDValue V1 = N->getOperand(0);
3097 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003098 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3099 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003101 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003103 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3104 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003105 if (Opc != ISD::BUILD_VECTOR ||
3106 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 return false;
3108 } else if (Idx >= 0) {
3109 unsigned Opc = V1.getOpcode();
3110 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3111 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003112 if (Opc != ISD::BUILD_VECTOR ||
3113 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003114 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003115 }
3116 }
3117 return true;
3118}
3119
3120/// getZeroVector - Returns a vector of specified type with all zero elements.
3121///
Owen Andersone50ed302009-08-10 22:56:29 +00003122static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003123 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003124 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003125
Chris Lattner8a594482007-11-25 00:24:49 +00003126 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3127 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003128 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003129 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3131 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003132 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3134 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003135 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3137 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003138 }
Dale Johannesenace16102009-02-03 19:33:06 +00003139 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003140}
3141
Chris Lattner8a594482007-11-25 00:24:49 +00003142/// getOnesVector - Returns a vector of specified type with all bits set.
3143///
Owen Andersone50ed302009-08-10 22:56:29 +00003144static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003145 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003146
Chris Lattner8a594482007-11-25 00:24:49 +00003147 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3148 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003150 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003151 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003153 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003155 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003156}
3157
3158
Evan Cheng39623da2006-04-20 08:58:49 +00003159/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3160/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003161static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003162 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003163 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Evan Cheng39623da2006-04-20 08:58:49 +00003165 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 SmallVector<int, 8> MaskVec;
3167 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003168
Nate Begeman5a5ca152009-04-29 05:20:52 +00003169 for (unsigned i = 0; i != NumElems; ++i) {
3170 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003171 MaskVec[i] = NumElems;
3172 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003173 }
Evan Cheng39623da2006-04-20 08:58:49 +00003174 }
Evan Cheng39623da2006-04-20 08:58:49 +00003175 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003176 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3177 SVOp->getOperand(1), &MaskVec[0]);
3178 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003179}
3180
Evan Cheng017dcc62006-04-21 01:05:10 +00003181/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3182/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003183static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003184 SDValue V2) {
3185 unsigned NumElems = VT.getVectorNumElements();
3186 SmallVector<int, 8> Mask;
3187 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003188 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003189 Mask.push_back(i);
3190 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003191}
3192
Nate Begeman9008ca62009-04-27 18:41:29 +00003193/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003194static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 SDValue V2) {
3196 unsigned NumElems = VT.getVectorNumElements();
3197 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003198 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 Mask.push_back(i);
3200 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003201 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003203}
3204
Nate Begeman9008ca62009-04-27 18:41:29 +00003205/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003206static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003207 SDValue V2) {
3208 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003209 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003211 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 Mask.push_back(i + Half);
3213 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003214 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003216}
3217
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003218/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003219static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 bool HasSSE2) {
3221 if (SV->getValueType(0).getVectorNumElements() <= 4)
3222 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003223
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003225 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003226 DebugLoc dl = SV->getDebugLoc();
3227 SDValue V1 = SV->getOperand(0);
3228 int NumElems = VT.getVectorNumElements();
3229 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003230
Nate Begeman9008ca62009-04-27 18:41:29 +00003231 // unpack elements to the correct location
3232 while (NumElems > 4) {
3233 if (EltNo < NumElems/2) {
3234 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3235 } else {
3236 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3237 EltNo -= NumElems/2;
3238 }
3239 NumElems >>= 1;
3240 }
Eric Christopherfd179292009-08-27 18:07:15 +00003241
Nate Begeman9008ca62009-04-27 18:41:29 +00003242 // Perform the splat.
3243 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003244 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003245 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3246 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003247}
3248
Evan Chengba05f722006-04-21 23:03:30 +00003249/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003250/// vector of zero or undef vector. This produces a shuffle where the low
3251/// element of V2 is swizzled into the zero/undef vector, landing at element
3252/// 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 +00003253static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003254 bool isZero, bool HasSSE2,
3255 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003256 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003257 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003258 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3259 unsigned NumElems = VT.getVectorNumElements();
3260 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003261 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003262 // If this is the insertion idx, put the low elt of V2 here.
3263 MaskVec.push_back(i == Idx ? NumElems : i);
3264 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003265}
3266
Evan Chengf26ffe92008-05-29 08:22:04 +00003267/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3268/// a shuffle that is zero.
3269static
Nate Begeman9008ca62009-04-27 18:41:29 +00003270unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3271 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003272 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003273 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003274 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 int Idx = SVOp->getMaskElt(Index);
3276 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003277 ++NumZeros;
3278 continue;
3279 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003281 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003282 ++NumZeros;
3283 else
3284 break;
3285 }
3286 return NumZeros;
3287}
3288
3289/// isVectorShift - Returns true if the shuffle can be implemented as a
3290/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003291/// FIXME: split into pslldqi, psrldqi, palignr variants.
3292static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003293 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003294 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003295
3296 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003297 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003298 if (!NumZeros) {
3299 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003300 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003301 if (!NumZeros)
3302 return false;
3303 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003304 bool SeenV1 = false;
3305 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 for (int i = NumZeros; i < NumElems; ++i) {
3307 int Val = isLeft ? (i - NumZeros) : i;
3308 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3309 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003310 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003312 SeenV1 = true;
3313 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003315 SeenV2 = true;
3316 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003317 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003318 return false;
3319 }
3320 if (SeenV1 && SeenV2)
3321 return false;
3322
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003324 ShAmt = NumZeros;
3325 return true;
3326}
3327
3328
Evan Chengc78d3b42006-04-24 18:01:45 +00003329/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3330///
Dan Gohman475871a2008-07-27 21:46:04 +00003331static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003332 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003333 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003334 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003335 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003336
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003337 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003338 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003339 bool First = true;
3340 for (unsigned i = 0; i < 16; ++i) {
3341 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3342 if (ThisIsNonZero && First) {
3343 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003345 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003347 First = false;
3348 }
3349
3350 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003351 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003352 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3353 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003354 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003356 }
3357 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3359 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3360 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003361 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003363 } else
3364 ThisElt = LastElt;
3365
Gabor Greifba36cb52008-08-28 21:40:38 +00003366 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003368 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003369 }
3370 }
3371
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003373}
3374
Bill Wendlinga348c562007-03-22 18:42:45 +00003375/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003376///
Dan Gohman475871a2008-07-27 21:46:04 +00003377static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003378 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003379 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003380 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003381 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003382
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003383 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003384 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003385 bool First = true;
3386 for (unsigned i = 0; i < 8; ++i) {
3387 bool isNonZero = (NonZeros & (1 << i)) != 0;
3388 if (isNonZero) {
3389 if (First) {
3390 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003392 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003394 First = false;
3395 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003396 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003398 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003399 }
3400 }
3401
3402 return V;
3403}
3404
Evan Chengf26ffe92008-05-29 08:22:04 +00003405/// getVShift - Return a vector logical shift node.
3406///
Owen Andersone50ed302009-08-10 22:56:29 +00003407static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003408 unsigned NumBits, SelectionDAG &DAG,
3409 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003410 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003412 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003413 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3414 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3415 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003416 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003417}
3418
Dan Gohman475871a2008-07-27 21:46:04 +00003419SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003420X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3421 SelectionDAG &DAG) {
3422
3423 // Check if the scalar load can be widened into a vector load. And if
3424 // the address is "base + cst" see if the cst can be "absorbed" into
3425 // the shuffle mask.
3426 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3427 SDValue Ptr = LD->getBasePtr();
3428 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3429 return SDValue();
3430 EVT PVT = LD->getValueType(0);
3431 if (PVT != MVT::i32 && PVT != MVT::f32)
3432 return SDValue();
3433
3434 int FI = -1;
3435 int64_t Offset = 0;
3436 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3437 FI = FINode->getIndex();
3438 Offset = 0;
3439 } else if (Ptr.getOpcode() == ISD::ADD &&
3440 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3441 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3442 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3443 Offset = Ptr.getConstantOperandVal(1);
3444 Ptr = Ptr.getOperand(0);
3445 } else {
3446 return SDValue();
3447 }
3448
3449 SDValue Chain = LD->getChain();
3450 // Make sure the stack object alignment is at least 16.
3451 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3452 if (DAG.InferPtrAlignment(Ptr) < 16) {
3453 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003454 // Can't change the alignment. FIXME: It's possible to compute
3455 // the exact stack offset and reference FI + adjust offset instead.
3456 // If someone *really* cares about this. That's the way to implement it.
3457 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003458 } else {
3459 MFI->setObjectAlignment(FI, 16);
3460 }
3461 }
3462
3463 // (Offset % 16) must be multiple of 4. Then address is then
3464 // Ptr + (Offset & ~15).
3465 if (Offset < 0)
3466 return SDValue();
3467 if ((Offset % 16) & 3)
3468 return SDValue();
3469 int64_t StartOffset = Offset & ~15;
3470 if (StartOffset)
3471 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3472 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3473
3474 int EltNo = (Offset - StartOffset) >> 2;
3475 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3476 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
3477 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0);
3478 // Canonicalize it to a v4i32 shuffle.
3479 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3480 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3481 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3482 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3483 }
3484
3485 return SDValue();
3486}
3487
3488SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003489X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003490 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003491 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003492 if (ISD::isBuildVectorAllZeros(Op.getNode())
3493 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003494 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3495 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3496 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003498 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003499
Gabor Greifba36cb52008-08-28 21:40:38 +00003500 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003501 return getOnesVector(Op.getValueType(), DAG, dl);
3502 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003503 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003504
Owen Andersone50ed302009-08-10 22:56:29 +00003505 EVT VT = Op.getValueType();
3506 EVT ExtVT = VT.getVectorElementType();
3507 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003508
3509 unsigned NumElems = Op.getNumOperands();
3510 unsigned NumZero = 0;
3511 unsigned NumNonZero = 0;
3512 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003513 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003514 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003515 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003516 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003517 if (Elt.getOpcode() == ISD::UNDEF)
3518 continue;
3519 Values.insert(Elt);
3520 if (Elt.getOpcode() != ISD::Constant &&
3521 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003522 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003523 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003524 NumZero++;
3525 else {
3526 NonZeros |= (1 << i);
3527 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003528 }
3529 }
3530
Dan Gohman7f321562007-06-25 16:23:39 +00003531 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003532 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003533 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003534 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003535
Chris Lattner67f453a2008-03-09 05:42:06 +00003536 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003537 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003538 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003539 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003540
Chris Lattner62098042008-03-09 01:05:04 +00003541 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3542 // the value are obviously zero, truncate the value to i32 and do the
3543 // insertion that way. Only do this if the value is non-constant or if the
3544 // value is a constant being inserted into element 0. It is cheaper to do
3545 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003547 (!IsAllConstants || Idx == 0)) {
3548 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3549 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3551 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003552
Chris Lattner62098042008-03-09 01:05:04 +00003553 // Truncate the value (which may itself be a constant) to i32, and
3554 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003556 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003557 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3558 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003559
Chris Lattner62098042008-03-09 01:05:04 +00003560 // Now we have our 32-bit value zero extended in the low element of
3561 // a vector. If Idx != 0, swizzle it into place.
3562 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003563 SmallVector<int, 4> Mask;
3564 Mask.push_back(Idx);
3565 for (unsigned i = 1; i != VecElts; ++i)
3566 Mask.push_back(i);
3567 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003568 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003569 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003570 }
Dale Johannesenace16102009-02-03 19:33:06 +00003571 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003572 }
3573 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003574
Chris Lattner19f79692008-03-08 22:59:52 +00003575 // If we have a constant or non-constant insertion into the low element of
3576 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3577 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003578 // depending on what the source datatype is.
3579 if (Idx == 0) {
3580 if (NumZero == 0) {
3581 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3583 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003584 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3585 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3586 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3587 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3589 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3590 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003591 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3592 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3593 Subtarget->hasSSE2(), DAG);
3594 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3595 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003596 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003597
3598 // Is it a vector logical left shift?
3599 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003600 X86::isZeroNode(Op.getOperand(0)) &&
3601 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003602 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003603 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003604 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003605 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003606 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003608
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003609 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003610 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611
Chris Lattner19f79692008-03-08 22:59:52 +00003612 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3613 // is a non-constant being inserted into an element other than the low one,
3614 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3615 // movd/movss) to move this into the low element, then shuffle it into
3616 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003617 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003618 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003619
Evan Cheng0db9fe62006-04-25 20:13:52 +00003620 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003621 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3622 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003624 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003625 MaskVec.push_back(i == Idx ? 0 : 1);
3626 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003627 }
3628 }
3629
Chris Lattner67f453a2008-03-09 05:42:06 +00003630 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003631 if (Values.size() == 1) {
3632 if (EVTBits == 32) {
3633 // Instead of a shuffle like this:
3634 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3635 // Check if it's possible to issue this instead.
3636 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3637 unsigned Idx = CountTrailingZeros_32(NonZeros);
3638 SDValue Item = Op.getOperand(Idx);
3639 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3640 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3641 }
Dan Gohman475871a2008-07-27 21:46:04 +00003642 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003643 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003644
Dan Gohmana3941172007-07-24 22:55:08 +00003645 // A vector full of immediates; various special cases are already
3646 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003647 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003648 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003649
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003650 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003651 if (EVTBits == 64) {
3652 if (NumNonZero == 1) {
3653 // One half is zero or undef.
3654 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003655 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003656 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003657 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3658 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003659 }
Dan Gohman475871a2008-07-27 21:46:04 +00003660 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003661 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003662
3663 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003664 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003665 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003666 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003667 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003668 }
3669
Bill Wendling826f36f2007-03-28 00:57:11 +00003670 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003671 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003672 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003673 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 }
3675
3676 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003677 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003678 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003679 if (NumElems == 4 && NumZero > 0) {
3680 for (unsigned i = 0; i < 4; ++i) {
3681 bool isZero = !(NonZeros & (1 << i));
3682 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003683 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684 else
Dale Johannesenace16102009-02-03 19:33:06 +00003685 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003686 }
3687
3688 for (unsigned i = 0; i < 2; ++i) {
3689 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3690 default: break;
3691 case 0:
3692 V[i] = V[i*2]; // Must be a zero vector.
3693 break;
3694 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003695 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003696 break;
3697 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003699 break;
3700 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003702 break;
3703 }
3704 }
3705
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003707 bool Reverse = (NonZeros & 0x3) == 2;
3708 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003709 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003710 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3711 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3713 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003714 }
3715
3716 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003717 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3718 // values to be inserted is equal to the number of elements, in which case
3719 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003720 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003721 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003722 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003723 getSubtarget()->hasSSE41()) {
3724 V[0] = DAG.getUNDEF(VT);
3725 for (unsigned i = 0; i < NumElems; ++i)
3726 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3727 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3728 Op.getOperand(i), DAG.getIntPtrConstant(i));
3729 return V[0];
3730 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003731 // Expand into a number of unpckl*.
3732 // e.g. for v4f32
3733 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3734 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3735 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003737 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003738 NumElems >>= 1;
3739 while (NumElems != 0) {
3740 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003741 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003742 NumElems >>= 1;
3743 }
3744 return V[0];
3745 }
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003748}
3749
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003750SDValue
3751X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3752 // We support concatenate two MMX registers and place them in a MMX
3753 // register. This is better than doing a stack convert.
3754 DebugLoc dl = Op.getDebugLoc();
3755 EVT ResVT = Op.getValueType();
3756 assert(Op.getNumOperands() == 2);
3757 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3758 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3759 int Mask[2];
3760 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3761 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3762 InVec = Op.getOperand(1);
3763 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3764 unsigned NumElts = ResVT.getVectorNumElements();
3765 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3766 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3767 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3768 } else {
3769 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3770 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3771 Mask[0] = 0; Mask[1] = 2;
3772 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3773 }
3774 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3775}
3776
Nate Begemanb9a47b82009-02-23 08:49:38 +00003777// v8i16 shuffles - Prefer shuffles in the following order:
3778// 1. [all] pshuflw, pshufhw, optional move
3779// 2. [ssse3] 1 x pshufb
3780// 3. [ssse3] 2 x pshufb + 1 x por
3781// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003782static
Nate Begeman9008ca62009-04-27 18:41:29 +00003783SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3784 SelectionDAG &DAG, X86TargetLowering &TLI) {
3785 SDValue V1 = SVOp->getOperand(0);
3786 SDValue V2 = SVOp->getOperand(1);
3787 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003788 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003789
Nate Begemanb9a47b82009-02-23 08:49:38 +00003790 // Determine if more than 1 of the words in each of the low and high quadwords
3791 // of the result come from the same quadword of one of the two inputs. Undef
3792 // mask values count as coming from any quadword, for better codegen.
3793 SmallVector<unsigned, 4> LoQuad(4);
3794 SmallVector<unsigned, 4> HiQuad(4);
3795 BitVector InputQuads(4);
3796 for (unsigned i = 0; i < 8; ++i) {
3797 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003798 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003799 MaskVals.push_back(EltIdx);
3800 if (EltIdx < 0) {
3801 ++Quad[0];
3802 ++Quad[1];
3803 ++Quad[2];
3804 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003805 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003806 }
3807 ++Quad[EltIdx / 4];
3808 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003809 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003810
Nate Begemanb9a47b82009-02-23 08:49:38 +00003811 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003812 unsigned MaxQuad = 1;
3813 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003814 if (LoQuad[i] > MaxQuad) {
3815 BestLoQuad = i;
3816 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003817 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003818 }
3819
Nate Begemanb9a47b82009-02-23 08:49:38 +00003820 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003821 MaxQuad = 1;
3822 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003823 if (HiQuad[i] > MaxQuad) {
3824 BestHiQuad = i;
3825 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003826 }
3827 }
3828
Nate Begemanb9a47b82009-02-23 08:49:38 +00003829 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003830 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003831 // single pshufb instruction is necessary. If There are more than 2 input
3832 // quads, disable the next transformation since it does not help SSSE3.
3833 bool V1Used = InputQuads[0] || InputQuads[1];
3834 bool V2Used = InputQuads[2] || InputQuads[3];
3835 if (TLI.getSubtarget()->hasSSSE3()) {
3836 if (InputQuads.count() == 2 && V1Used && V2Used) {
3837 BestLoQuad = InputQuads.find_first();
3838 BestHiQuad = InputQuads.find_next(BestLoQuad);
3839 }
3840 if (InputQuads.count() > 2) {
3841 BestLoQuad = -1;
3842 BestHiQuad = -1;
3843 }
3844 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003845
Nate Begemanb9a47b82009-02-23 08:49:38 +00003846 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3847 // the shuffle mask. If a quad is scored as -1, that means that it contains
3848 // words from all 4 input quadwords.
3849 SDValue NewV;
3850 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 SmallVector<int, 8> MaskV;
3852 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3853 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003854 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003855 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3856 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3857 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003858
Nate Begemanb9a47b82009-02-23 08:49:38 +00003859 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3860 // source words for the shuffle, to aid later transformations.
3861 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003862 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003863 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003864 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003865 if (idx != (int)i)
3866 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003867 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003868 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003869 AllWordsInNewV = false;
3870 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003871 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003872
Nate Begemanb9a47b82009-02-23 08:49:38 +00003873 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3874 if (AllWordsInNewV) {
3875 for (int i = 0; i != 8; ++i) {
3876 int idx = MaskVals[i];
3877 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003878 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003879 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003880 if ((idx != i) && idx < 4)
3881 pshufhw = false;
3882 if ((idx != i) && idx > 3)
3883 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003884 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003885 V1 = NewV;
3886 V2Used = false;
3887 BestLoQuad = 0;
3888 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003889 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003890
Nate Begemanb9a47b82009-02-23 08:49:38 +00003891 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3892 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003893 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003894 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003896 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003897 }
Eric Christopherfd179292009-08-27 18:07:15 +00003898
Nate Begemanb9a47b82009-02-23 08:49:38 +00003899 // If we have SSSE3, and all words of the result are from 1 input vector,
3900 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3901 // is present, fall back to case 4.
3902 if (TLI.getSubtarget()->hasSSSE3()) {
3903 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00003904
Nate Begemanb9a47b82009-02-23 08:49:38 +00003905 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00003906 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00003907 // mask, and elements that come from V1 in the V2 mask, so that the two
3908 // results can be OR'd together.
3909 bool TwoInputs = V1Used && V2Used;
3910 for (unsigned i = 0; i != 8; ++i) {
3911 int EltIdx = MaskVals[i] * 2;
3912 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3914 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003915 continue;
3916 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3918 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003919 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003921 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003922 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003924 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00003926
Nate Begemanb9a47b82009-02-23 08:49:38 +00003927 // Calculate the shuffle mask for the second input, shuffle it, and
3928 // OR it with the first shuffled input.
3929 pshufbMask.clear();
3930 for (unsigned i = 0; i != 8; ++i) {
3931 int EltIdx = MaskVals[i] * 2;
3932 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003933 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3934 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003935 continue;
3936 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3938 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003939 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003940 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00003941 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003942 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003943 MVT::v16i8, &pshufbMask[0], 16));
3944 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3945 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003946 }
3947
3948 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3949 // and update MaskVals with new element order.
3950 BitVector InOrder(8);
3951 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003952 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003953 for (int i = 0; i != 4; ++i) {
3954 int idx = MaskVals[i];
3955 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003957 InOrder.set(i);
3958 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003959 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003960 InOrder.set(i);
3961 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003963 }
3964 }
3965 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003966 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003969 }
Eric Christopherfd179292009-08-27 18:07:15 +00003970
Nate Begemanb9a47b82009-02-23 08:49:38 +00003971 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3972 // and update MaskVals with the new element order.
3973 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003975 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003977 for (unsigned i = 4; i != 8; ++i) {
3978 int idx = MaskVals[i];
3979 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003981 InOrder.set(i);
3982 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003983 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003984 InOrder.set(i);
3985 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003986 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003987 }
3988 }
Owen Anderson825b72b2009-08-11 20:47:22 +00003989 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00003990 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003991 }
Eric Christopherfd179292009-08-27 18:07:15 +00003992
Nate Begemanb9a47b82009-02-23 08:49:38 +00003993 // In case BestHi & BestLo were both -1, which means each quadword has a word
3994 // from each of the four input quadwords, calculate the InOrder bitvector now
3995 // before falling through to the insert/extract cleanup.
3996 if (BestLoQuad == -1 && BestHiQuad == -1) {
3997 NewV = V1;
3998 for (int i = 0; i != 8; ++i)
3999 if (MaskVals[i] < 0 || MaskVals[i] == i)
4000 InOrder.set(i);
4001 }
Eric Christopherfd179292009-08-27 18:07:15 +00004002
Nate Begemanb9a47b82009-02-23 08:49:38 +00004003 // The other elements are put in the right place using pextrw and pinsrw.
4004 for (unsigned i = 0; i != 8; ++i) {
4005 if (InOrder[i])
4006 continue;
4007 int EltIdx = MaskVals[i];
4008 if (EltIdx < 0)
4009 continue;
4010 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004012 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004013 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004014 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004016 DAG.getIntPtrConstant(i));
4017 }
4018 return NewV;
4019}
4020
4021// v16i8 shuffles - Prefer shuffles in the following order:
4022// 1. [ssse3] 1 x pshufb
4023// 2. [ssse3] 2 x pshufb + 1 x por
4024// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4025static
Nate Begeman9008ca62009-04-27 18:41:29 +00004026SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4027 SelectionDAG &DAG, X86TargetLowering &TLI) {
4028 SDValue V1 = SVOp->getOperand(0);
4029 SDValue V2 = SVOp->getOperand(1);
4030 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004031 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004033
Nate Begemanb9a47b82009-02-23 08:49:38 +00004034 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004035 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004036 // present, fall back to case 3.
4037 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4038 bool V1Only = true;
4039 bool V2Only = true;
4040 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004041 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004042 if (EltIdx < 0)
4043 continue;
4044 if (EltIdx < 16)
4045 V2Only = false;
4046 else
4047 V1Only = false;
4048 }
Eric Christopherfd179292009-08-27 18:07:15 +00004049
Nate Begemanb9a47b82009-02-23 08:49:38 +00004050 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4051 if (TLI.getSubtarget()->hasSSSE3()) {
4052 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004053
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004055 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004056 //
4057 // Otherwise, we have elements from both input vectors, and must zero out
4058 // elements that come from V2 in the first mask, and V1 in the second mask
4059 // so that we can OR them together.
4060 bool TwoInputs = !(V1Only || V2Only);
4061 for (unsigned i = 0; i != 16; ++i) {
4062 int EltIdx = MaskVals[i];
4063 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004064 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 continue;
4066 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 }
4069 // If all the elements are from V2, assign it to V1 and return after
4070 // building the first pshufb.
4071 if (V2Only)
4072 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004074 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004076 if (!TwoInputs)
4077 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004078
Nate Begemanb9a47b82009-02-23 08:49:38 +00004079 // Calculate the shuffle mask for the second input, shuffle it, and
4080 // OR it with the first shuffled input.
4081 pshufbMask.clear();
4082 for (unsigned i = 0; i != 16; ++i) {
4083 int EltIdx = MaskVals[i];
4084 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004086 continue;
4087 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004091 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 MVT::v16i8, &pshufbMask[0], 16));
4093 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004094 }
Eric Christopherfd179292009-08-27 18:07:15 +00004095
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 // No SSSE3 - Calculate in place words and then fix all out of place words
4097 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4098 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004099 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4100 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 SDValue NewV = V2Only ? V2 : V1;
4102 for (int i = 0; i != 8; ++i) {
4103 int Elt0 = MaskVals[i*2];
4104 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004105
Nate Begemanb9a47b82009-02-23 08:49:38 +00004106 // This word of the result is all undef, skip it.
4107 if (Elt0 < 0 && Elt1 < 0)
4108 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004109
Nate Begemanb9a47b82009-02-23 08:49:38 +00004110 // This word of the result is already in the correct place, skip it.
4111 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4112 continue;
4113 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4114 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004115
Nate Begemanb9a47b82009-02-23 08:49:38 +00004116 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4117 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4118 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004119
4120 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4121 // using a single extract together, load it and store it.
4122 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004124 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004126 DAG.getIntPtrConstant(i));
4127 continue;
4128 }
4129
Nate Begemanb9a47b82009-02-23 08:49:38 +00004130 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004131 // source byte is not also odd, shift the extracted word left 8 bits
4132 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004133 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 DAG.getIntPtrConstant(Elt1 / 2));
4136 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004138 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004139 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4141 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004142 }
4143 // If Elt0 is defined, extract it from the appropriate source. If the
4144 // source byte is not also even, shift the extracted word right 8 bits. If
4145 // Elt1 was also defined, OR the extracted values together before
4146 // inserting them in the result.
4147 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004149 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4150 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004152 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004153 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4155 DAG.getConstant(0x00FF, MVT::i16));
4156 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004157 : InsElt0;
4158 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004160 DAG.getIntPtrConstant(i));
4161 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004163}
4164
Evan Cheng7a831ce2007-12-15 03:00:47 +00004165/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4166/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4167/// done when every pair / quad of shuffle mask elements point to elements in
4168/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004169/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4170static
Nate Begeman9008ca62009-04-27 18:41:29 +00004171SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4172 SelectionDAG &DAG,
4173 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004174 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 SDValue V1 = SVOp->getOperand(0);
4176 SDValue V2 = SVOp->getOperand(1);
4177 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004178 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004180 EVT MaskEltVT = MaskVT.getVectorElementType();
4181 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004183 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 case MVT::v4f32: NewVT = MVT::v2f64; break;
4185 case MVT::v4i32: NewVT = MVT::v2i64; break;
4186 case MVT::v8i16: NewVT = MVT::v4i32; break;
4187 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004188 }
4189
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004190 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004191 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004193 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004194 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004195 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004196 int Scale = NumElems / NewWidth;
4197 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004198 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004199 int StartIdx = -1;
4200 for (int j = 0; j < Scale; ++j) {
4201 int EltIdx = SVOp->getMaskElt(i+j);
4202 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004203 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004205 StartIdx = EltIdx - (EltIdx % Scale);
4206 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004207 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004208 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004209 if (StartIdx == -1)
4210 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004211 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004213 }
4214
Dale Johannesenace16102009-02-03 19:33:06 +00004215 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4216 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004217 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004218}
4219
Evan Chengd880b972008-05-09 21:53:03 +00004220/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004221///
Owen Andersone50ed302009-08-10 22:56:29 +00004222static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004223 SDValue SrcOp, SelectionDAG &DAG,
4224 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004226 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004227 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004228 LD = dyn_cast<LoadSDNode>(SrcOp);
4229 if (!LD) {
4230 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4231 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004232 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4233 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004234 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4235 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004236 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004237 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004239 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4240 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4241 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4242 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004243 SrcOp.getOperand(0)
4244 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004245 }
4246 }
4247 }
4248
Dale Johannesenace16102009-02-03 19:33:06 +00004249 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4250 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004251 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004252 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004253}
4254
Evan Chengace3c172008-07-22 21:13:36 +00004255/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4256/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004257static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004258LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4259 SDValue V1 = SVOp->getOperand(0);
4260 SDValue V2 = SVOp->getOperand(1);
4261 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004262 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004263
Evan Chengace3c172008-07-22 21:13:36 +00004264 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004265 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004266 SmallVector<int, 8> Mask1(4U, -1);
4267 SmallVector<int, 8> PermMask;
4268 SVOp->getMask(PermMask);
4269
Evan Chengace3c172008-07-22 21:13:36 +00004270 unsigned NumHi = 0;
4271 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004272 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004273 int Idx = PermMask[i];
4274 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004275 Locs[i] = std::make_pair(-1, -1);
4276 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004277 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4278 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004279 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004281 NumLo++;
4282 } else {
4283 Locs[i] = std::make_pair(1, NumHi);
4284 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004285 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004286 NumHi++;
4287 }
4288 }
4289 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004290
Evan Chengace3c172008-07-22 21:13:36 +00004291 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004292 // If no more than two elements come from either vector. This can be
4293 // implemented with two shuffles. First shuffle gather the elements.
4294 // The second shuffle, which takes the first shuffle as both of its
4295 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004296 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004297
Nate Begeman9008ca62009-04-27 18:41:29 +00004298 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004299
Evan Chengace3c172008-07-22 21:13:36 +00004300 for (unsigned i = 0; i != 4; ++i) {
4301 if (Locs[i].first == -1)
4302 continue;
4303 else {
4304 unsigned Idx = (i < 2) ? 0 : 4;
4305 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004306 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004307 }
4308 }
4309
Nate Begeman9008ca62009-04-27 18:41:29 +00004310 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004311 } else if (NumLo == 3 || NumHi == 3) {
4312 // Otherwise, we must have three elements from one vector, call it X, and
4313 // one element from the other, call it Y. First, use a shufps to build an
4314 // intermediate vector with the one element from Y and the element from X
4315 // that will be in the same half in the final destination (the indexes don't
4316 // matter). Then, use a shufps to build the final vector, taking the half
4317 // containing the element from Y from the intermediate, and the other half
4318 // from X.
4319 if (NumHi == 3) {
4320 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004321 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004322 std::swap(V1, V2);
4323 }
4324
4325 // Find the element from V2.
4326 unsigned HiIndex;
4327 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 int Val = PermMask[HiIndex];
4329 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004330 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004331 if (Val >= 4)
4332 break;
4333 }
4334
Nate Begeman9008ca62009-04-27 18:41:29 +00004335 Mask1[0] = PermMask[HiIndex];
4336 Mask1[1] = -1;
4337 Mask1[2] = PermMask[HiIndex^1];
4338 Mask1[3] = -1;
4339 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004340
4341 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004342 Mask1[0] = PermMask[0];
4343 Mask1[1] = PermMask[1];
4344 Mask1[2] = HiIndex & 1 ? 6 : 4;
4345 Mask1[3] = HiIndex & 1 ? 4 : 6;
4346 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004347 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004348 Mask1[0] = HiIndex & 1 ? 2 : 0;
4349 Mask1[1] = HiIndex & 1 ? 0 : 2;
4350 Mask1[2] = PermMask[2];
4351 Mask1[3] = PermMask[3];
4352 if (Mask1[2] >= 0)
4353 Mask1[2] += 4;
4354 if (Mask1[3] >= 0)
4355 Mask1[3] += 4;
4356 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004357 }
Evan Chengace3c172008-07-22 21:13:36 +00004358 }
4359
4360 // Break it into (shuffle shuffle_hi, shuffle_lo).
4361 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 SmallVector<int,8> LoMask(4U, -1);
4363 SmallVector<int,8> HiMask(4U, -1);
4364
4365 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004366 unsigned MaskIdx = 0;
4367 unsigned LoIdx = 0;
4368 unsigned HiIdx = 2;
4369 for (unsigned i = 0; i != 4; ++i) {
4370 if (i == 2) {
4371 MaskPtr = &HiMask;
4372 MaskIdx = 1;
4373 LoIdx = 0;
4374 HiIdx = 2;
4375 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004376 int Idx = PermMask[i];
4377 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004378 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004379 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004380 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004381 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004382 LoIdx++;
4383 } else {
4384 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004386 HiIdx++;
4387 }
4388 }
4389
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4391 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4392 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004393 for (unsigned i = 0; i != 4; ++i) {
4394 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004395 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004396 } else {
4397 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004398 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004399 }
4400 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004402}
4403
Dan Gohman475871a2008-07-27 21:46:04 +00004404SDValue
4405X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004407 SDValue V1 = Op.getOperand(0);
4408 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004409 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004410 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004412 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4414 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004415 bool V1IsSplat = false;
4416 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004417
Nate Begeman9008ca62009-04-27 18:41:29 +00004418 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004419 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004420
Nate Begeman9008ca62009-04-27 18:41:29 +00004421 // Promote splats to v4f32.
4422 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004423 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 return Op;
4425 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426 }
4427
Evan Cheng7a831ce2007-12-15 03:00:47 +00004428 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4429 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004431 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004432 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004433 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004434 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004436 // FIXME: Figure out a cleaner way to do this.
4437 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004438 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004440 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004441 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4442 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4443 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004444 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004445 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4447 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004448 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004450 }
4451 }
Eric Christopherfd179292009-08-27 18:07:15 +00004452
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 if (X86::isPSHUFDMask(SVOp))
4454 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004455
Evan Chengf26ffe92008-05-29 08:22:04 +00004456 // Check if this can be converted into a logical shift.
4457 bool isLeft = false;
4458 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004459 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004461 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004462 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004463 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004464 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004465 EVT EltVT = VT.getVectorElementType();
4466 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004467 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004468 }
Eric Christopherfd179292009-08-27 18:07:15 +00004469
Nate Begeman9008ca62009-04-27 18:41:29 +00004470 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004471 if (V1IsUndef)
4472 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004473 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004474 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004475 if (!isMMX)
4476 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004477 }
Eric Christopherfd179292009-08-27 18:07:15 +00004478
Nate Begeman9008ca62009-04-27 18:41:29 +00004479 // FIXME: fold these into legal mask.
4480 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4481 X86::isMOVSLDUPMask(SVOp) ||
4482 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004483 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004485 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004486
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 if (ShouldXformToMOVHLPS(SVOp) ||
4488 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4489 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490
Evan Chengf26ffe92008-05-29 08:22:04 +00004491 if (isShift) {
4492 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004493 EVT EltVT = VT.getVectorElementType();
4494 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004495 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004496 }
Eric Christopherfd179292009-08-27 18:07:15 +00004497
Evan Cheng9eca5e82006-10-25 21:49:50 +00004498 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004499 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4500 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004501 V1IsSplat = isSplatVector(V1.getNode());
4502 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004503
Chris Lattner8a594482007-11-25 00:24:49 +00004504 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004505 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 Op = CommuteVectorShuffle(SVOp, DAG);
4507 SVOp = cast<ShuffleVectorSDNode>(Op);
4508 V1 = SVOp->getOperand(0);
4509 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004510 std::swap(V1IsSplat, V2IsSplat);
4511 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004512 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004513 }
4514
Nate Begeman9008ca62009-04-27 18:41:29 +00004515 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4516 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004517 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004518 return V1;
4519 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4520 // the instruction selector will not match, so get a canonical MOVL with
4521 // swapped operands to undo the commute.
4522 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004523 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004524
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4526 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4527 X86::isUNPCKLMask(SVOp) ||
4528 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004529 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004530
Evan Cheng9bbbb982006-10-25 20:48:19 +00004531 if (V2IsSplat) {
4532 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004533 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004534 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 SDValue NewMask = NormalizeMask(SVOp, DAG);
4536 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4537 if (NSVOp != SVOp) {
4538 if (X86::isUNPCKLMask(NSVOp, true)) {
4539 return NewMask;
4540 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4541 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542 }
4543 }
4544 }
4545
Evan Cheng9eca5e82006-10-25 21:49:50 +00004546 if (Commuted) {
4547 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004548 // FIXME: this seems wrong.
4549 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4550 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4551 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4552 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4553 X86::isUNPCKLMask(NewSVOp) ||
4554 X86::isUNPCKHMask(NewSVOp))
4555 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004556 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557
Nate Begemanb9a47b82009-02-23 08:49:38 +00004558 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004559
4560 // Normalize the node to match x86 shuffle ops if needed
4561 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4562 return CommuteVectorShuffle(SVOp, DAG);
4563
4564 // Check for legal shuffle and return?
4565 SmallVector<int, 16> PermMask;
4566 SVOp->getMask(PermMask);
4567 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004568 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004569
Evan Cheng14b32e12007-12-11 01:46:18 +00004570 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004571 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004572 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004573 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004574 return NewOp;
4575 }
4576
Owen Anderson825b72b2009-08-11 20:47:22 +00004577 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004578 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004579 if (NewOp.getNode())
4580 return NewOp;
4581 }
Eric Christopherfd179292009-08-27 18:07:15 +00004582
Evan Chengace3c172008-07-22 21:13:36 +00004583 // Handle all 4 wide cases with a number of shuffles except for MMX.
4584 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004585 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586
Dan Gohman475871a2008-07-27 21:46:04 +00004587 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004588}
4589
Dan Gohman475871a2008-07-27 21:46:04 +00004590SDValue
4591X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004592 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004593 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004594 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004595 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004596 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004597 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004599 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004600 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004601 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004602 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4603 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4604 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004605 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4606 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004607 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004609 Op.getOperand(0)),
4610 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004611 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004612 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004614 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004615 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004616 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004617 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4618 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004619 // result has a single use which is a store or a bitcast to i32. And in
4620 // the case of a store, it's not worth it if the index is a constant 0,
4621 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004622 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004623 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004624 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004625 if ((User->getOpcode() != ISD::STORE ||
4626 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4627 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004628 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004629 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004630 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004631 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4632 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004633 Op.getOperand(0)),
4634 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004635 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4636 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004637 // ExtractPS works with constant index.
4638 if (isa<ConstantSDNode>(Op.getOperand(1)))
4639 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004640 }
Dan Gohman475871a2008-07-27 21:46:04 +00004641 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004642}
4643
4644
Dan Gohman475871a2008-07-27 21:46:04 +00004645SDValue
4646X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004648 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649
Evan Cheng62a3f152008-03-24 21:52:23 +00004650 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004651 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004652 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004653 return Res;
4654 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004655
Owen Andersone50ed302009-08-10 22:56:29 +00004656 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004657 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004658 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004659 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004660 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004661 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004662 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004663 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4664 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004665 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004666 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004667 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004668 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004669 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004670 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004672 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004673 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004674 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004675 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004676 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004677 if (Idx == 0)
4678 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004679
Evan Cheng0db9fe62006-04-25 20:13:52 +00004680 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004683 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004685 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004686 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004687 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004688 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4689 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4690 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004691 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004692 if (Idx == 0)
4693 return Op;
4694
4695 // UNPCKHPD the element to the lowest double word, then movsd.
4696 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4697 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004698 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004699 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004700 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004701 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004702 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004703 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004704 }
4705
Dan Gohman475871a2008-07-27 21:46:04 +00004706 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004707}
4708
Dan Gohman475871a2008-07-27 21:46:04 +00004709SDValue
4710X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004711 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004712 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004713 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004714
Dan Gohman475871a2008-07-27 21:46:04 +00004715 SDValue N0 = Op.getOperand(0);
4716 SDValue N1 = Op.getOperand(1);
4717 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004718
Dan Gohman8a55ce42009-09-23 21:02:20 +00004719 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004720 isa<ConstantSDNode>(N2)) {
Dan Gohman8a55ce42009-09-23 21:02:20 +00004721 unsigned Opc = (EltVT.getSizeInBits() == 8) ? X86ISD::PINSRB
4722 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004723 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4724 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004725 if (N1.getValueType() != MVT::i32)
4726 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4727 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004728 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004729 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004730 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004731 // Bits [7:6] of the constant are the source select. This will always be
4732 // zero here. The DAG Combiner may combine an extract_elt index into these
4733 // bits. For example (insert (extract, 3), 2) could be matched by putting
4734 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004735 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004736 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004737 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004738 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004739 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004740 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004741 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004742 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004743 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004744 // PINSR* works with constant index.
4745 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004746 }
Dan Gohman475871a2008-07-27 21:46:04 +00004747 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004748}
4749
Dan Gohman475871a2008-07-27 21:46:04 +00004750SDValue
4751X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004752 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004753 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004754
4755 if (Subtarget->hasSSE41())
4756 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4757
Dan Gohman8a55ce42009-09-23 21:02:20 +00004758 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004759 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004760
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004761 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue N0 = Op.getOperand(0);
4763 SDValue N1 = Op.getOperand(1);
4764 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004765
Dan Gohman8a55ce42009-09-23 21:02:20 +00004766 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004767 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4768 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004769 if (N1.getValueType() != MVT::i32)
4770 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4771 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004772 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004773 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004774 }
Dan Gohman475871a2008-07-27 21:46:04 +00004775 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004776}
4777
Dan Gohman475871a2008-07-27 21:46:04 +00004778SDValue
4779X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004780 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 if (Op.getValueType() == MVT::v2f32)
4782 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4783 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4784 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004785 Op.getOperand(0))));
4786
Owen Anderson825b72b2009-08-11 20:47:22 +00004787 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4788 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004789
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4791 EVT VT = MVT::v2i32;
4792 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004793 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 case MVT::v16i8:
4795 case MVT::v8i16:
4796 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004797 break;
4798 }
Dale Johannesenace16102009-02-03 19:33:06 +00004799 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4800 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801}
4802
Bill Wendling056292f2008-09-16 21:48:12 +00004803// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4804// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4805// one of the above mentioned nodes. It has to be wrapped because otherwise
4806// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4807// be used to form addressing mode. These wrapped nodes will be selected
4808// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004809SDValue
4810X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004812
Chris Lattner41621a22009-06-26 19:22:52 +00004813 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4814 // global base reg.
4815 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004816 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004817 CodeModel::Model M = getTargetMachine().getCodeModel();
4818
Chris Lattner4f066492009-07-11 20:29:19 +00004819 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004820 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004821 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004822 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004823 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004824 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004825 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004826
Evan Cheng1606e8e2009-03-13 07:51:59 +00004827 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004828 CP->getAlignment(),
4829 CP->getOffset(), OpFlag);
4830 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004831 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004832 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004833 if (OpFlag) {
4834 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004835 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004836 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004837 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838 }
4839
4840 return Result;
4841}
4842
Chris Lattner18c59872009-06-27 04:16:01 +00004843SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4844 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004845
Chris Lattner18c59872009-06-27 04:16:01 +00004846 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4847 // global base reg.
4848 unsigned char OpFlag = 0;
4849 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004850 CodeModel::Model M = getTargetMachine().getCodeModel();
4851
Chris Lattner4f066492009-07-11 20:29:19 +00004852 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004853 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004854 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004855 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004856 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004857 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004858 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004859
Chris Lattner18c59872009-06-27 04:16:01 +00004860 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4861 OpFlag);
4862 DebugLoc DL = JT->getDebugLoc();
4863 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004864
Chris Lattner18c59872009-06-27 04:16:01 +00004865 // With PIC, the address is actually $g + Offset.
4866 if (OpFlag) {
4867 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4868 DAG.getNode(X86ISD::GlobalBaseReg,
4869 DebugLoc::getUnknownLoc(), getPointerTy()),
4870 Result);
4871 }
Eric Christopherfd179292009-08-27 18:07:15 +00004872
Chris Lattner18c59872009-06-27 04:16:01 +00004873 return Result;
4874}
4875
4876SDValue
4877X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4878 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004879
Chris Lattner18c59872009-06-27 04:16:01 +00004880 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4881 // global base reg.
4882 unsigned char OpFlag = 0;
4883 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004884 CodeModel::Model M = getTargetMachine().getCodeModel();
4885
Chris Lattner4f066492009-07-11 20:29:19 +00004886 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004887 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004888 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004889 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004890 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004891 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004892 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004893
Chris Lattner18c59872009-06-27 04:16:01 +00004894 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00004895
Chris Lattner18c59872009-06-27 04:16:01 +00004896 DebugLoc DL = Op.getDebugLoc();
4897 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004898
4899
Chris Lattner18c59872009-06-27 04:16:01 +00004900 // With PIC, the address is actually $g + Offset.
4901 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004902 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004903 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4904 DAG.getNode(X86ISD::GlobalBaseReg,
4905 DebugLoc::getUnknownLoc(),
4906 getPointerTy()),
4907 Result);
4908 }
Eric Christopherfd179292009-08-27 18:07:15 +00004909
Chris Lattner18c59872009-06-27 04:16:01 +00004910 return Result;
4911}
4912
Dan Gohman475871a2008-07-27 21:46:04 +00004913SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00004914X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00004915 // Create the TargetBlockAddressAddress node.
4916 unsigned char OpFlags =
4917 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00004918 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00004919 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
4920 DebugLoc dl = Op.getDebugLoc();
4921 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
4922 /*isTarget=*/true, OpFlags);
4923
Dan Gohmanf705adb2009-10-30 01:28:02 +00004924 if (Subtarget->isPICStyleRIPRel() &&
4925 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00004926 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4927 else
4928 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00004929
Dan Gohman29cbade2009-11-20 23:18:13 +00004930 // With PIC, the address is actually $g + Offset.
4931 if (isGlobalRelativeToPICBase(OpFlags)) {
4932 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4933 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
4934 Result);
4935 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00004936
4937 return Result;
4938}
4939
4940SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004941X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004942 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004943 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004944 // Create the TargetGlobalAddress node, folding in the constant
4945 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004946 unsigned char OpFlags =
4947 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004948 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00004949 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004950 if (OpFlags == X86II::MO_NO_FLAG &&
4951 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004952 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004953 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004954 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004955 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004956 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004957 }
Eric Christopherfd179292009-08-27 18:07:15 +00004958
Chris Lattner4f066492009-07-11 20:29:19 +00004959 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004960 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00004961 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4962 else
4963 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004964
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004965 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004966 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004967 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4968 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004969 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004970 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004971
Chris Lattner36c25012009-07-10 07:34:39 +00004972 // For globals that require a load from a stub to get the address, emit the
4973 // load.
4974 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004975 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004976 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004977
Dan Gohman6520e202008-10-18 02:06:02 +00004978 // If there was a non-zero offset that we didn't fold, create an explicit
4979 // addition for it.
4980 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004981 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004982 DAG.getConstant(Offset, getPointerTy()));
4983
Evan Cheng0db9fe62006-04-25 20:13:52 +00004984 return Result;
4985}
4986
Evan Chengda43bcf2008-09-24 00:05:32 +00004987SDValue
4988X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4989 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004990 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004991 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004992}
4993
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004994static SDValue
4995GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00004996 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004997 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00004998 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00004999 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005000 DebugLoc dl = GA->getDebugLoc();
5001 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5002 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005003 GA->getOffset(),
5004 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005005 if (InFlag) {
5006 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005007 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005008 } else {
5009 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005010 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005011 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005012
5013 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5014 MFI->setHasCalls(true);
5015
Rafael Espindola15f1b662009-04-24 12:59:40 +00005016 SDValue Flag = Chain.getValue(1);
5017 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005018}
5019
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005020// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005021static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005022LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005023 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005024 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005025 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5026 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005027 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005028 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005029 PtrVT), InFlag);
5030 InFlag = Chain.getValue(1);
5031
Chris Lattnerb903bed2009-06-26 21:20:29 +00005032 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005033}
5034
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005035// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005036static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005037LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005038 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005039 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5040 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005041}
5042
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005043// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5044// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005045static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005046 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005047 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005048 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005049 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005050 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5051 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005052 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005053 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005054
5055 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
5056 NULL, 0);
5057
Chris Lattnerb903bed2009-06-26 21:20:29 +00005058 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005059 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5060 // initialexec.
5061 unsigned WrapperKind = X86ISD::Wrapper;
5062 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005063 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005064 } else if (is64Bit) {
5065 assert(model == TLSModel::InitialExec);
5066 OperandFlags = X86II::MO_GOTTPOFF;
5067 WrapperKind = X86ISD::WrapperRIP;
5068 } else {
5069 assert(model == TLSModel::InitialExec);
5070 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005071 }
Eric Christopherfd179292009-08-27 18:07:15 +00005072
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005073 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5074 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005075 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005076 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005077 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005078
Rafael Espindola9a580232009-02-27 13:37:18 +00005079 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005080 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00005081 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005082
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005083 // The address of the thread local variable is the add of the thread
5084 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005085 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005086}
5087
Dan Gohman475871a2008-07-27 21:46:04 +00005088SDValue
5089X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005090 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005091 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005092 assert(Subtarget->isTargetELF() &&
5093 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005094 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005095 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Chris Lattnerb903bed2009-06-26 21:20:29 +00005097 // If GV is an alias then use the aliasee for determining
5098 // thread-localness.
5099 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5100 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005101
Chris Lattnerb903bed2009-06-26 21:20:29 +00005102 TLSModel::Model model = getTLSModel(GV,
5103 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005104
Chris Lattnerb903bed2009-06-26 21:20:29 +00005105 switch (model) {
5106 case TLSModel::GeneralDynamic:
5107 case TLSModel::LocalDynamic: // not implemented
5108 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005109 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005110 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005111
Chris Lattnerb903bed2009-06-26 21:20:29 +00005112 case TLSModel::InitialExec:
5113 case TLSModel::LocalExec:
5114 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5115 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005116 }
Eric Christopherfd179292009-08-27 18:07:15 +00005117
Torok Edwinc23197a2009-07-14 16:55:14 +00005118 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005119 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005120}
5121
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005123/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005124/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005125SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005126 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005127 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005128 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005129 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005130 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005131 SDValue ShOpLo = Op.getOperand(0);
5132 SDValue ShOpHi = Op.getOperand(1);
5133 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005134 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005135 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005136 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005137
Dan Gohman475871a2008-07-27 21:46:04 +00005138 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005139 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005140 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5141 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005142 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005143 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5144 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005145 }
Evan Chenge3413162006-01-09 18:33:28 +00005146
Owen Anderson825b72b2009-08-11 20:47:22 +00005147 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5148 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005149 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005151
Dan Gohman475871a2008-07-27 21:46:04 +00005152 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005154 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5155 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005156
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005157 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005158 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5159 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005160 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005161 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5162 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005163 }
5164
Dan Gohman475871a2008-07-27 21:46:04 +00005165 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005166 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005167}
Evan Chenga3195e82006-01-12 22:54:21 +00005168
Dan Gohman475871a2008-07-27 21:46:04 +00005169SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005170 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005171
5172 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005173 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005174 return Op;
5175 }
5176 return SDValue();
5177 }
5178
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005180 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005181
Eli Friedman36df4992009-05-27 00:47:34 +00005182 // These are really Legal; return the operand so the caller accepts it as
5183 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005185 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005186 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005187 Subtarget->is64Bit()) {
5188 return Op;
5189 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005190
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005191 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005192 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005193 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005194 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005195 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005196 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005197 StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005198 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005199 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5200}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005201
Owen Andersone50ed302009-08-10 22:56:29 +00005202SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005203 SDValue StackSlot,
5204 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005205 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005206 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005207 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005208 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005209 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005210 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005211 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005212 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005213 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005214 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005215 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005216
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005217 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005218 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005220
5221 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5222 // shouldn't be necessary except that RFP cannot be live across
5223 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005224 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005225 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005226 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005227 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005228 SDValue Ops[] = {
5229 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5230 };
5231 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005232 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005233 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005234 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005235
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236 return Result;
5237}
5238
Bill Wendling8b8a6362009-01-17 03:56:04 +00005239// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5240SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5241 // This algorithm is not obvious. Here it is in C code, more or less:
5242 /*
5243 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5244 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5245 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005246
Bill Wendling8b8a6362009-01-17 03:56:04 +00005247 // Copy ints to xmm registers.
5248 __m128i xh = _mm_cvtsi32_si128( hi );
5249 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005250
Bill Wendling8b8a6362009-01-17 03:56:04 +00005251 // Combine into low half of a single xmm register.
5252 __m128i x = _mm_unpacklo_epi32( xh, xl );
5253 __m128d d;
5254 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005255
Bill Wendling8b8a6362009-01-17 03:56:04 +00005256 // Merge in appropriate exponents to give the integer bits the right
5257 // magnitude.
5258 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005259
Bill Wendling8b8a6362009-01-17 03:56:04 +00005260 // Subtract away the biases to deal with the IEEE-754 double precision
5261 // implicit 1.
5262 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005263
Bill Wendling8b8a6362009-01-17 03:56:04 +00005264 // All conversions up to here are exact. The correctly rounded result is
5265 // calculated using the current rounding mode using the following
5266 // horizontal add.
5267 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5268 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5269 // store doesn't really need to be here (except
5270 // maybe to zero the other double)
5271 return sd;
5272 }
5273 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005274
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005275 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005276 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005277
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005278 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005279 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005280 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5281 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5282 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5283 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005284 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005285 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005286
Bill Wendling8b8a6362009-01-17 03:56:04 +00005287 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005288 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005289 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005290 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005291 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005292 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005293 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005294
Owen Anderson825b72b2009-08-11 20:47:22 +00005295 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5296 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005297 Op.getOperand(0),
5298 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005299 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5300 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005301 Op.getOperand(0),
5302 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5304 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005305 PseudoSourceValue::getConstantPool(), 0,
5306 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005307 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5308 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5309 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005310 PseudoSourceValue::getConstantPool(), 0,
5311 false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005313
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005314 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005315 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005316 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5317 DAG.getUNDEF(MVT::v2f64), ShufMask);
5318 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5319 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005320 DAG.getIntPtrConstant(0));
5321}
5322
Bill Wendling8b8a6362009-01-17 03:56:04 +00005323// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5324SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005325 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005326 // FP constant to bias correct the final result.
5327 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005328 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005329
5330 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005331 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5332 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005333 Op.getOperand(0),
5334 DAG.getIntPtrConstant(0)));
5335
Owen Anderson825b72b2009-08-11 20:47:22 +00005336 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5337 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005338 DAG.getIntPtrConstant(0));
5339
5340 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005341 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5342 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005343 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005344 MVT::v2f64, Load)),
5345 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005346 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005347 MVT::v2f64, Bias)));
5348 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5349 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005350 DAG.getIntPtrConstant(0));
5351
5352 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005353 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005354
5355 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005356 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005357
Owen Anderson825b72b2009-08-11 20:47:22 +00005358 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005359 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005360 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005362 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005363 }
5364
5365 // Handle final rounding.
5366 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005367}
5368
5369SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005370 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005371 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005372
Evan Chenga06ec9e2009-01-19 08:08:22 +00005373 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5374 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5375 // the optimization here.
5376 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005377 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005378
Owen Andersone50ed302009-08-10 22:56:29 +00005379 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005380 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005381 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005382 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005383 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005384
Bill Wendling8b8a6362009-01-17 03:56:04 +00005385 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005386 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005387 return LowerUINT_TO_FP_i32(Op, DAG);
5388 }
5389
Owen Anderson825b72b2009-08-11 20:47:22 +00005390 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005391
5392 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005393 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005394 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5395 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5396 getPointerTy(), StackSlot, WordOff);
5397 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5398 StackSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005399 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Eli Friedman948e95a2009-05-23 09:59:16 +00005400 OffsetSlot, NULL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005401 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005402}
5403
Dan Gohman475871a2008-07-27 21:46:04 +00005404std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005405FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005406 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005407
Owen Andersone50ed302009-08-10 22:56:29 +00005408 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005409
5410 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5412 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005413 }
5414
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5416 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005417 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005419 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005420 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005421 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005422 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005423 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005424 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005425 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005426 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005427
Evan Cheng87c89352007-10-15 20:11:21 +00005428 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5429 // stack slot.
5430 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005431 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005432 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005433 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005434
Evan Cheng0db9fe62006-04-25 20:13:52 +00005435 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005436 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005437 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005438 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5439 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5440 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005441 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005442
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue Chain = DAG.getEntryNode();
5444 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005445 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005447 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Evan Chengff89dcb2009-10-18 18:16:27 +00005448 PseudoSourceValue::getFixedStack(SSFI), 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005449 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005450 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005451 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5452 };
Dale Johannesenace16102009-02-03 19:33:06 +00005453 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005455 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5457 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005458
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005461 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005462
Chris Lattner27a6c732007-11-24 07:07:01 +00005463 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464}
5465
Dan Gohman475871a2008-07-27 21:46:04 +00005466SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005467 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005468 if (Op.getValueType() == MVT::v2i32 &&
5469 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005470 return Op;
5471 }
5472 return SDValue();
5473 }
5474
Eli Friedman948e95a2009-05-23 09:59:16 +00005475 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005476 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005477 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5478 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005479
Chris Lattner27a6c732007-11-24 07:07:01 +00005480 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005481 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005482 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005483}
5484
Eli Friedman948e95a2009-05-23 09:59:16 +00005485SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5486 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5487 SDValue FIST = Vals.first, StackSlot = Vals.second;
5488 assert(FIST.getNode() && "Unexpected failure");
5489
5490 // Load the result.
5491 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5492 FIST, StackSlot, NULL, 0);
5493}
5494
Dan Gohman475871a2008-07-27 21:46:04 +00005495SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005496 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005497 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005498 EVT VT = Op.getValueType();
5499 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005500 if (VT.isVector())
5501 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005502 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005503 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005504 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005505 CV.push_back(C);
5506 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005508 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005509 CV.push_back(C);
5510 CV.push_back(C);
5511 CV.push_back(C);
5512 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005513 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005514 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005515 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005516 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005517 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005518 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005519 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005520}
5521
Dan Gohman475871a2008-07-27 21:46:04 +00005522SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005523 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005524 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005525 EVT VT = Op.getValueType();
5526 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005527 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005528 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005529 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005530 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005531 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005532 CV.push_back(C);
5533 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005534 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005535 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005536 CV.push_back(C);
5537 CV.push_back(C);
5538 CV.push_back(C);
5539 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005540 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005541 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005542 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005543 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005544 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005545 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005546 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005547 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5549 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005550 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005551 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005552 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005553 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005554 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005555}
5556
Dan Gohman475871a2008-07-27 21:46:04 +00005557SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005558 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue Op0 = Op.getOperand(0);
5560 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005561 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005562 EVT VT = Op.getValueType();
5563 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005564
5565 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005566 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005567 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005568 SrcVT = VT;
5569 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005570 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005571 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005572 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005573 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005574 }
5575
5576 // At this point the operands and the result should have the same
5577 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005578
Evan Cheng68c47cb2007-01-05 07:55:56 +00005579 // First get the sign bit of second operand.
5580 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005582 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5583 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005584 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005585 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5586 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5587 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5588 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005589 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005590 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005591 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005592 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005593 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005594 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005595 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005596
5597 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005598 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005599 // Op0 is MVT::f32, Op1 is MVT::f64.
5600 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5601 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5602 DAG.getConstant(32, MVT::i32));
5603 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5604 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005605 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005606 }
5607
Evan Cheng73d6cf12007-01-05 21:37:56 +00005608 // Clear first operand sign bit.
5609 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005610 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005611 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5612 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005613 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005614 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5615 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5616 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5617 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005618 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005619 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005620 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005621 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005622 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005623 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005624 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005625
5626 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005627 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005628}
5629
Dan Gohman076aee32009-03-04 19:44:21 +00005630/// Emit nodes that will be selected as "test Op0,Op0", or something
5631/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005632SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5633 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005634 DebugLoc dl = Op.getDebugLoc();
5635
Dan Gohman31125812009-03-07 01:58:32 +00005636 // CF and OF aren't always set the way we want. Determine which
5637 // of these we need.
5638 bool NeedCF = false;
5639 bool NeedOF = false;
5640 switch (X86CC) {
5641 case X86::COND_A: case X86::COND_AE:
5642 case X86::COND_B: case X86::COND_BE:
5643 NeedCF = true;
5644 break;
5645 case X86::COND_G: case X86::COND_GE:
5646 case X86::COND_L: case X86::COND_LE:
5647 case X86::COND_O: case X86::COND_NO:
5648 NeedOF = true;
5649 break;
5650 default: break;
5651 }
5652
Dan Gohman076aee32009-03-04 19:44:21 +00005653 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005654 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5655 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5656 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005657 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005658 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005659 switch (Op.getNode()->getOpcode()) {
5660 case ISD::ADD:
5661 // Due to an isel shortcoming, be conservative if this add is likely to
5662 // be selected as part of a load-modify-store instruction. When the root
5663 // node in a match is a store, isel doesn't know how to remap non-chain
5664 // non-flag uses of other nodes in the match, such as the ADD in this
5665 // case. This leads to the ADD being left around and reselected, with
5666 // the result being two adds in the output.
5667 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5668 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5669 if (UI->getOpcode() == ISD::STORE)
5670 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005671 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005672 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5673 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005674 if (C->getAPIntValue() == 1) {
5675 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005676 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005677 break;
5678 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005679 // An add of negative one (subtract of one) will be selected as a DEC.
5680 if (C->getAPIntValue().isAllOnesValue()) {
5681 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005682 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005683 break;
5684 }
5685 }
Dan Gohman076aee32009-03-04 19:44:21 +00005686 // Otherwise use a regular EFLAGS-setting add.
5687 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005688 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005689 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005690 case ISD::AND: {
5691 // If the primary and result isn't used, don't bother using X86ISD::AND,
5692 // because a TEST instruction will be better.
5693 bool NonFlagUse = false;
5694 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005695 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5696 SDNode *User = *UI;
5697 unsigned UOpNo = UI.getOperandNo();
5698 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5699 // Look pass truncate.
5700 UOpNo = User->use_begin().getOperandNo();
5701 User = *User->use_begin();
5702 }
5703 if (User->getOpcode() != ISD::BRCOND &&
5704 User->getOpcode() != ISD::SETCC &&
5705 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005706 NonFlagUse = true;
5707 break;
5708 }
Evan Cheng17751da2010-01-07 00:54:06 +00005709 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005710 if (!NonFlagUse)
5711 break;
5712 }
5713 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005714 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005715 case ISD::OR:
5716 case ISD::XOR:
5717 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005718 // likely to be selected as part of a load-modify-store instruction.
5719 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5720 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5721 if (UI->getOpcode() == ISD::STORE)
5722 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005723 // Otherwise use a regular EFLAGS-setting instruction.
5724 switch (Op.getNode()->getOpcode()) {
5725 case ISD::SUB: Opcode = X86ISD::SUB; break;
5726 case ISD::OR: Opcode = X86ISD::OR; break;
5727 case ISD::XOR: Opcode = X86ISD::XOR; break;
5728 case ISD::AND: Opcode = X86ISD::AND; break;
5729 default: llvm_unreachable("unexpected operator!");
5730 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005731 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005732 break;
5733 case X86ISD::ADD:
5734 case X86ISD::SUB:
5735 case X86ISD::INC:
5736 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005737 case X86ISD::OR:
5738 case X86ISD::XOR:
5739 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005740 return SDValue(Op.getNode(), 1);
5741 default:
5742 default_case:
5743 break;
5744 }
5745 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005746 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005747 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005748 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005749 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005750 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005751 DAG.ReplaceAllUsesWith(Op, New);
5752 return SDValue(New.getNode(), 1);
5753 }
5754 }
5755
5756 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005757 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005758 DAG.getConstant(0, Op.getValueType()));
5759}
5760
5761/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5762/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005763SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5764 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5766 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005767 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005768
5769 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005771}
5772
Evan Chengd40d03e2010-01-06 19:38:29 +00005773/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5774/// if it's possible.
5775static SDValue LowerToBT(SDValue Op0, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005776 DebugLoc dl, SelectionDAG &DAG) {
Evan Chengd40d03e2010-01-06 19:38:29 +00005777 SDValue LHS, RHS;
5778 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5779 if (ConstantSDNode *Op010C =
5780 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5781 if (Op010C->getZExtValue() == 1) {
5782 LHS = Op0.getOperand(0);
5783 RHS = Op0.getOperand(1).getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005784 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005785 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5786 if (ConstantSDNode *Op000C =
5787 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5788 if (Op000C->getZExtValue() == 1) {
5789 LHS = Op0.getOperand(1);
5790 RHS = Op0.getOperand(0).getOperand(1);
5791 }
5792 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5793 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5794 SDValue AndLHS = Op0.getOperand(0);
5795 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5796 LHS = AndLHS.getOperand(0);
5797 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005798 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005799 }
Evan Cheng0488db92007-09-25 01:57:46 +00005800
Evan Chengd40d03e2010-01-06 19:38:29 +00005801 if (LHS.getNode()) {
5802 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5803 // instruction. Since the shift amount is in-range-or-undefined, we know
5804 // that doing a bittest on the i16 value is ok. We extend to i32 because
5805 // the encoding for the i16 version is larger than the i32 version.
5806 if (LHS.getValueType() == MVT::i8)
5807 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005808
Evan Chengd40d03e2010-01-06 19:38:29 +00005809 // If the operand types disagree, extend the shift amount to match. Since
5810 // BT ignores high bits (like shifts) we can use anyextend.
5811 if (LHS.getValueType() != RHS.getValueType())
5812 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005813
Evan Chengd40d03e2010-01-06 19:38:29 +00005814 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5815 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5816 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5817 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005818 }
5819
Evan Cheng54de3ea2010-01-05 06:52:31 +00005820 return SDValue();
5821}
5822
5823SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5824 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5825 SDValue Op0 = Op.getOperand(0);
5826 SDValue Op1 = Op.getOperand(1);
5827 DebugLoc dl = Op.getDebugLoc();
5828 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5829
5830 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005831 // Lower (X & (1 << N)) == 0 to BT(X, N).
5832 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5833 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5834 if (Op0.getOpcode() == ISD::AND &&
5835 Op0.hasOneUse() &&
5836 Op1.getOpcode() == ISD::Constant &&
5837 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5838 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5839 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5840 if (NewSetCC.getNode())
5841 return NewSetCC;
5842 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005843
Chris Lattnere55484e2008-12-25 05:34:37 +00005844 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5845 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005846 if (X86CC == X86::COND_INVALID)
5847 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005848
Dan Gohman31125812009-03-07 01:58:32 +00005849 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005850
5851 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005852 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005853 return DAG.getNode(ISD::AND, dl, MVT::i8,
5854 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5855 DAG.getConstant(X86CC, MVT::i8), Cond),
5856 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005857
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5859 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005860}
5861
Dan Gohman475871a2008-07-27 21:46:04 +00005862SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5863 SDValue Cond;
5864 SDValue Op0 = Op.getOperand(0);
5865 SDValue Op1 = Op.getOperand(1);
5866 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00005867 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00005868 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5869 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005870 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005871
5872 if (isFP) {
5873 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00005874 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005875 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5876 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005877 bool Swap = false;
5878
5879 switch (SetCCOpcode) {
5880 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005881 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005882 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005883 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005884 case ISD::SETGT: Swap = true; // Fallthrough
5885 case ISD::SETLT:
5886 case ISD::SETOLT: SSECC = 1; break;
5887 case ISD::SETOGE:
5888 case ISD::SETGE: Swap = true; // Fallthrough
5889 case ISD::SETLE:
5890 case ISD::SETOLE: SSECC = 2; break;
5891 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005892 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005893 case ISD::SETNE: SSECC = 4; break;
5894 case ISD::SETULE: Swap = true;
5895 case ISD::SETUGE: SSECC = 5; break;
5896 case ISD::SETULT: Swap = true;
5897 case ISD::SETUGT: SSECC = 6; break;
5898 case ISD::SETO: SSECC = 7; break;
5899 }
5900 if (Swap)
5901 std::swap(Op0, Op1);
5902
Nate Begemanfb8ead02008-07-25 19:05:58 +00005903 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005904 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005905 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005906 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005907 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5908 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005909 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005910 }
5911 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005912 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5914 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00005915 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005916 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005917 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005918 }
5919 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005921 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005922
Nate Begeman30a0de92008-07-17 16:51:19 +00005923 // We are handling one of the integer comparisons here. Since SSE only has
5924 // GT and EQ comparisons for integer, swapping operands and multiple
5925 // operations may be required for some comparisons.
5926 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5927 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005928
Owen Anderson825b72b2009-08-11 20:47:22 +00005929 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00005930 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 case MVT::v8i8:
5932 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5933 case MVT::v4i16:
5934 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5935 case MVT::v2i32:
5936 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5937 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00005938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005939
Nate Begeman30a0de92008-07-17 16:51:19 +00005940 switch (SetCCOpcode) {
5941 default: break;
5942 case ISD::SETNE: Invert = true;
5943 case ISD::SETEQ: Opc = EQOpc; break;
5944 case ISD::SETLT: Swap = true;
5945 case ISD::SETGT: Opc = GTOpc; break;
5946 case ISD::SETGE: Swap = true;
5947 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5948 case ISD::SETULT: Swap = true;
5949 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5950 case ISD::SETUGE: Swap = true;
5951 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5952 }
5953 if (Swap)
5954 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005955
Nate Begeman30a0de92008-07-17 16:51:19 +00005956 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5957 // bits of the inputs before performing those operations.
5958 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00005959 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005960 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5961 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005962 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005963 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5964 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005965 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5966 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005967 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005968
Dale Johannesenace16102009-02-03 19:33:06 +00005969 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005970
5971 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005972 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005973 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005974
Nate Begeman30a0de92008-07-17 16:51:19 +00005975 return Result;
5976}
Evan Cheng0488db92007-09-25 01:57:46 +00005977
Evan Cheng370e5342008-12-03 08:38:43 +00005978// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005979static bool isX86LogicalCmp(SDValue Op) {
5980 unsigned Opc = Op.getNode()->getOpcode();
5981 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5982 return true;
5983 if (Op.getResNo() == 1 &&
5984 (Opc == X86ISD::ADD ||
5985 Opc == X86ISD::SUB ||
5986 Opc == X86ISD::SMUL ||
5987 Opc == X86ISD::UMUL ||
5988 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00005989 Opc == X86ISD::DEC ||
5990 Opc == X86ISD::OR ||
5991 Opc == X86ISD::XOR ||
5992 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00005993 return true;
5994
5995 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005996}
5997
Dan Gohman475871a2008-07-27 21:46:04 +00005998SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005999 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006000 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006001 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006002 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006003
Dan Gohman1a492952009-10-20 16:22:37 +00006004 if (Cond.getOpcode() == ISD::SETCC) {
6005 SDValue NewCond = LowerSETCC(Cond, DAG);
6006 if (NewCond.getNode())
6007 Cond = NewCond;
6008 }
Evan Cheng734503b2006-09-11 02:19:56 +00006009
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006010 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6011 SDValue Op1 = Op.getOperand(1);
6012 SDValue Op2 = Op.getOperand(2);
6013 if (Cond.getOpcode() == X86ISD::SETCC &&
6014 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6015 SDValue Cmp = Cond.getOperand(1);
6016 if (Cmp.getOpcode() == X86ISD::CMP) {
6017 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6018 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6019 ConstantSDNode *RHSC =
6020 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6021 if (N1C && N1C->isAllOnesValue() &&
6022 N2C && N2C->isNullValue() &&
6023 RHSC && RHSC->isNullValue()) {
6024 SDValue CmpOp0 = Cmp.getOperand(0);
6025 Cmp = DAG.getNode(X86ISD::CMP, dl, Op.getValueType(),
6026 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6027 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6028 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6029 }
6030 }
6031 }
6032
Evan Chengad9c0a32009-12-15 00:53:42 +00006033 // Look pass (and (setcc_carry (cmp ...)), 1).
6034 if (Cond.getOpcode() == ISD::AND &&
6035 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6036 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6037 if (C && C->getAPIntValue() == 1)
6038 Cond = Cond.getOperand(0);
6039 }
6040
Evan Cheng3f41d662007-10-08 22:16:29 +00006041 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6042 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006043 if (Cond.getOpcode() == X86ISD::SETCC ||
6044 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006045 CC = Cond.getOperand(0);
6046
Dan Gohman475871a2008-07-27 21:46:04 +00006047 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006048 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006049 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006050
Evan Cheng3f41d662007-10-08 22:16:29 +00006051 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006052 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006053 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006054 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006055
Chris Lattnerd1980a52009-03-12 06:52:53 +00006056 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6057 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006058 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006059 addTest = false;
6060 }
6061 }
6062
6063 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006064 // Look pass the truncate.
6065 if (Cond.getOpcode() == ISD::TRUNCATE)
6066 Cond = Cond.getOperand(0);
6067
6068 // We know the result of AND is compared against zero. Try to match
6069 // it to BT.
6070 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6071 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6072 if (NewSetCC.getNode()) {
6073 CC = NewSetCC.getOperand(0);
6074 Cond = NewSetCC.getOperand(1);
6075 addTest = false;
6076 }
6077 }
6078 }
6079
6080 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006082 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006083 }
6084
Evan Cheng0488db92007-09-25 01:57:46 +00006085 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6086 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006087 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6088 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006089 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006090}
6091
Evan Cheng370e5342008-12-03 08:38:43 +00006092// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6093// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6094// from the AND / OR.
6095static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6096 Opc = Op.getOpcode();
6097 if (Opc != ISD::OR && Opc != ISD::AND)
6098 return false;
6099 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6100 Op.getOperand(0).hasOneUse() &&
6101 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6102 Op.getOperand(1).hasOneUse());
6103}
6104
Evan Cheng961d6d42009-02-02 08:19:07 +00006105// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6106// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006107static bool isXor1OfSetCC(SDValue Op) {
6108 if (Op.getOpcode() != ISD::XOR)
6109 return false;
6110 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6111 if (N1C && N1C->getAPIntValue() == 1) {
6112 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6113 Op.getOperand(0).hasOneUse();
6114 }
6115 return false;
6116}
6117
Dan Gohman475871a2008-07-27 21:46:04 +00006118SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006119 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006120 SDValue Chain = Op.getOperand(0);
6121 SDValue Cond = Op.getOperand(1);
6122 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006123 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006124 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006125
Dan Gohman1a492952009-10-20 16:22:37 +00006126 if (Cond.getOpcode() == ISD::SETCC) {
6127 SDValue NewCond = LowerSETCC(Cond, DAG);
6128 if (NewCond.getNode())
6129 Cond = NewCond;
6130 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006131#if 0
6132 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006133 else if (Cond.getOpcode() == X86ISD::ADD ||
6134 Cond.getOpcode() == X86ISD::SUB ||
6135 Cond.getOpcode() == X86ISD::SMUL ||
6136 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006137 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006138#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006139
Evan Chengad9c0a32009-12-15 00:53:42 +00006140 // Look pass (and (setcc_carry (cmp ...)), 1).
6141 if (Cond.getOpcode() == ISD::AND &&
6142 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6143 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6144 if (C && C->getAPIntValue() == 1)
6145 Cond = Cond.getOperand(0);
6146 }
6147
Evan Cheng3f41d662007-10-08 22:16:29 +00006148 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6149 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006150 if (Cond.getOpcode() == X86ISD::SETCC ||
6151 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006152 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006153
Dan Gohman475871a2008-07-27 21:46:04 +00006154 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006155 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006156 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006157 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006158 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006159 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006160 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006161 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006162 default: break;
6163 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006164 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006165 // These can only come from an arithmetic instruction with overflow,
6166 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006167 Cond = Cond.getNode()->getOperand(1);
6168 addTest = false;
6169 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006170 }
Evan Cheng0488db92007-09-25 01:57:46 +00006171 }
Evan Cheng370e5342008-12-03 08:38:43 +00006172 } else {
6173 unsigned CondOpc;
6174 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6175 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006176 if (CondOpc == ISD::OR) {
6177 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6178 // two branches instead of an explicit OR instruction with a
6179 // separate test.
6180 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006181 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006182 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006183 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006184 Chain, Dest, CC, Cmp);
6185 CC = Cond.getOperand(1).getOperand(0);
6186 Cond = Cmp;
6187 addTest = false;
6188 }
6189 } else { // ISD::AND
6190 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6191 // two branches instead of an explicit AND instruction with a
6192 // separate test. However, we only do this if this block doesn't
6193 // have a fall-through edge, because this requires an explicit
6194 // jmp when the condition is false.
6195 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006196 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006197 Op.getNode()->hasOneUse()) {
6198 X86::CondCode CCode =
6199 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6200 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006201 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006202 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6203 // Look for an unconditional branch following this conditional branch.
6204 // We need this because we need to reverse the successors in order
6205 // to implement FCMP_OEQ.
6206 if (User.getOpcode() == ISD::BR) {
6207 SDValue FalseBB = User.getOperand(1);
6208 SDValue NewBR =
6209 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6210 assert(NewBR == User);
6211 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006212
Dale Johannesene4d209d2009-02-03 20:21:25 +00006213 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006214 Chain, Dest, CC, Cmp);
6215 X86::CondCode CCode =
6216 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6217 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006219 Cond = Cmp;
6220 addTest = false;
6221 }
6222 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006223 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006224 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6225 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6226 // It should be transformed during dag combiner except when the condition
6227 // is set by a arithmetics with overflow node.
6228 X86::CondCode CCode =
6229 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6230 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006231 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006232 Cond = Cond.getOperand(0).getOperand(1);
6233 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006234 }
Evan Cheng0488db92007-09-25 01:57:46 +00006235 }
6236
6237 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006238 // Look pass the truncate.
6239 if (Cond.getOpcode() == ISD::TRUNCATE)
6240 Cond = Cond.getOperand(0);
6241
6242 // We know the result of AND is compared against zero. Try to match
6243 // it to BT.
6244 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6245 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6246 if (NewSetCC.getNode()) {
6247 CC = NewSetCC.getOperand(0);
6248 Cond = NewSetCC.getOperand(1);
6249 addTest = false;
6250 }
6251 }
6252 }
6253
6254 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006255 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006256 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006257 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006258 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006259 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006260}
6261
Anton Korobeynikove060b532007-04-17 19:34:00 +00006262
6263// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6264// Calls to _alloca is needed to probe the stack when allocating more than 4k
6265// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6266// that the guard pages used by the OS virtual memory manager are allocated in
6267// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006268SDValue
6269X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006270 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006271 assert(Subtarget->isTargetCygMing() &&
6272 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006273 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006274
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006275 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006276 SDValue Chain = Op.getOperand(0);
6277 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006278 // FIXME: Ensure alignment here
6279
Dan Gohman475871a2008-07-27 21:46:04 +00006280 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006281
Owen Andersone50ed302009-08-10 22:56:29 +00006282 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006283 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006284
Chris Lattnere563bbc2008-10-11 22:08:30 +00006285 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006286
Dale Johannesendd64c412009-02-04 00:33:20 +00006287 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006288 Flag = Chain.getValue(1);
6289
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006291 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00006292 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006293 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006294 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006295 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006296 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006297 Flag = Chain.getValue(1);
6298
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006299 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00006300 DAG.getIntPtrConstant(0, true),
6301 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006302 Flag);
6303
Dale Johannesendd64c412009-02-04 00:33:20 +00006304 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006305
Dan Gohman475871a2008-07-27 21:46:04 +00006306 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006307 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006308}
6309
Dan Gohman475871a2008-07-27 21:46:04 +00006310SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006311X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006312 SDValue Chain,
6313 SDValue Dst, SDValue Src,
6314 SDValue Size, unsigned Align,
6315 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006316 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006317 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006318
Bill Wendling6f287b22008-09-30 21:22:07 +00006319 // If not DWORD aligned or size is more than the threshold, call the library.
6320 // The libc version is likely to be faster for these cases. It can use the
6321 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006322 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006323 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006324 ConstantSize->getZExtValue() >
6325 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006326 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006327
6328 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006329 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006330
Bill Wendling6158d842008-10-01 00:59:58 +00006331 if (const char *bzeroEntry = V &&
6332 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006333 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006334 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006335 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006336 TargetLowering::ArgListEntry Entry;
6337 Entry.Node = Dst;
6338 Entry.Ty = IntPtrTy;
6339 Args.push_back(Entry);
6340 Entry.Node = Size;
6341 Args.push_back(Entry);
6342 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006343 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6344 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006345 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006346 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl,
6347 DAG.GetOrdering(Chain.getNode()));
Bill Wendling6158d842008-10-01 00:59:58 +00006348 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006349 }
6350
Dan Gohman707e0182008-04-12 04:36:06 +00006351 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006352 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006353 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006354
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006355 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006356 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006357 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006358 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006359 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006360 unsigned BytesLeft = 0;
6361 bool TwoRepStos = false;
6362 if (ValC) {
6363 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006364 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006365
Evan Cheng0db9fe62006-04-25 20:13:52 +00006366 // If the value is a constant, then we can potentially use larger sets.
6367 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006368 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006369 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006370 ValReg = X86::AX;
6371 Val = (Val << 8) | Val;
6372 break;
6373 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006374 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006375 ValReg = X86::EAX;
6376 Val = (Val << 8) | Val;
6377 Val = (Val << 16) | Val;
6378 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006379 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006380 ValReg = X86::RAX;
6381 Val = (Val << 32) | Val;
6382 }
6383 break;
6384 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006385 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006386 ValReg = X86::AL;
6387 Count = DAG.getIntPtrConstant(SizeVal);
6388 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006389 }
6390
Owen Anderson825b72b2009-08-11 20:47:22 +00006391 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006392 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006393 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6394 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006395 }
6396
Dale Johannesen0f502f62009-02-03 22:26:09 +00006397 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006398 InFlag);
6399 InFlag = Chain.getValue(1);
6400 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006401 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006402 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006403 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006404 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006405 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006406
Scott Michelfdc40a02009-02-17 22:15:04 +00006407 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006408 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006409 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006410 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006411 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006412 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006413 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006414 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006415
Owen Anderson825b72b2009-08-11 20:47:22 +00006416 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006417 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6418 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006419
Evan Cheng0db9fe62006-04-25 20:13:52 +00006420 if (TwoRepStos) {
6421 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006422 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006423 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006424 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006425 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6426 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006427 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006428 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006429 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006430 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006431 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6432 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006433 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006434 // Handle the last 1 - 7 bytes.
6435 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006436 EVT AddrVT = Dst.getValueType();
6437 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006438
Dale Johannesen0f502f62009-02-03 22:26:09 +00006439 Chain = DAG.getMemset(Chain, dl,
6440 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006441 DAG.getConstant(Offset, AddrVT)),
6442 Src,
6443 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006444 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006445 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006446
Dan Gohman707e0182008-04-12 04:36:06 +00006447 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006448 return Chain;
6449}
Evan Cheng11e15b32006-04-03 20:53:28 +00006450
Dan Gohman475871a2008-07-27 21:46:04 +00006451SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006452X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006453 SDValue Chain, SDValue Dst, SDValue Src,
6454 SDValue Size, unsigned Align,
6455 bool AlwaysInline,
6456 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006457 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006458 // This requires the copy size to be a constant, preferrably
6459 // within a subtarget-specific limit.
6460 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6461 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006462 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006463 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006464 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006465 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006466
Evan Cheng1887c1c2008-08-21 21:00:15 +00006467 /// If not DWORD aligned, call the library.
6468 if ((Align & 3) != 0)
6469 return SDValue();
6470
6471 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006472 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006473 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006474 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006475
Duncan Sands83ec4b62008-06-06 12:08:01 +00006476 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006477 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006478 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006479 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006480
Dan Gohman475871a2008-07-27 21:46:04 +00006481 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006482 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006483 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006484 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006485 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006486 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006487 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006488 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006489 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006490 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006491 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006492 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006493 InFlag = Chain.getValue(1);
6494
Owen Anderson825b72b2009-08-11 20:47:22 +00006495 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006496 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6497 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6498 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006499
Dan Gohman475871a2008-07-27 21:46:04 +00006500 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006501 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006502 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006503 // Handle the last 1 - 7 bytes.
6504 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006505 EVT DstVT = Dst.getValueType();
6506 EVT SrcVT = Src.getValueType();
6507 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006508 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006509 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006510 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006511 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006512 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006513 DAG.getConstant(BytesLeft, SizeVT),
6514 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006515 DstSV, DstSVOff + Offset,
6516 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006517 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006518
Owen Anderson825b72b2009-08-11 20:47:22 +00006519 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006520 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006521}
6522
Dan Gohman475871a2008-07-27 21:46:04 +00006523SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006524 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006525 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006526
Evan Cheng25ab6902006-09-08 06:48:29 +00006527 if (!Subtarget->is64Bit()) {
6528 // vastart just stores the address of the VarArgsFrameIndex slot into the
6529 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006530 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006531 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006532 }
6533
6534 // __va_list_tag:
6535 // gp_offset (0 - 6 * 8)
6536 // fp_offset (48 - 48 + 8 * 16)
6537 // overflow_arg_area (point to parameters coming in memory).
6538 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006539 SmallVector<SDValue, 8> MemOps;
6540 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006541 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006542 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006543 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006544 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006545 MemOps.push_back(Store);
6546
6547 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006548 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006549 FIN, DAG.getIntPtrConstant(4));
6550 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006551 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006552 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006553 MemOps.push_back(Store);
6554
6555 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006556 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006557 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006558 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006559 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006560 MemOps.push_back(Store);
6561
6562 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006563 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006564 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006565 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006566 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006567 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006568 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006569 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006570}
6571
Dan Gohman475871a2008-07-27 21:46:04 +00006572SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006573 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6574 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006575 SDValue Chain = Op.getOperand(0);
6576 SDValue SrcPtr = Op.getOperand(1);
6577 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006578
Torok Edwindac237e2009-07-08 20:53:28 +00006579 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006580 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006581}
6582
Dan Gohman475871a2008-07-27 21:46:04 +00006583SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006584 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006585 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue Chain = Op.getOperand(0);
6587 SDValue DstPtr = Op.getOperand(1);
6588 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006589 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6590 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006591 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006592
Dale Johannesendd64c412009-02-04 00:33:20 +00006593 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006594 DAG.getIntPtrConstant(24), 8, false,
6595 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006596}
6597
Dan Gohman475871a2008-07-27 21:46:04 +00006598SDValue
6599X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006600 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006601 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006602 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006603 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006604 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006605 case Intrinsic::x86_sse_comieq_ss:
6606 case Intrinsic::x86_sse_comilt_ss:
6607 case Intrinsic::x86_sse_comile_ss:
6608 case Intrinsic::x86_sse_comigt_ss:
6609 case Intrinsic::x86_sse_comige_ss:
6610 case Intrinsic::x86_sse_comineq_ss:
6611 case Intrinsic::x86_sse_ucomieq_ss:
6612 case Intrinsic::x86_sse_ucomilt_ss:
6613 case Intrinsic::x86_sse_ucomile_ss:
6614 case Intrinsic::x86_sse_ucomigt_ss:
6615 case Intrinsic::x86_sse_ucomige_ss:
6616 case Intrinsic::x86_sse_ucomineq_ss:
6617 case Intrinsic::x86_sse2_comieq_sd:
6618 case Intrinsic::x86_sse2_comilt_sd:
6619 case Intrinsic::x86_sse2_comile_sd:
6620 case Intrinsic::x86_sse2_comigt_sd:
6621 case Intrinsic::x86_sse2_comige_sd:
6622 case Intrinsic::x86_sse2_comineq_sd:
6623 case Intrinsic::x86_sse2_ucomieq_sd:
6624 case Intrinsic::x86_sse2_ucomilt_sd:
6625 case Intrinsic::x86_sse2_ucomile_sd:
6626 case Intrinsic::x86_sse2_ucomigt_sd:
6627 case Intrinsic::x86_sse2_ucomige_sd:
6628 case Intrinsic::x86_sse2_ucomineq_sd: {
6629 unsigned Opc = 0;
6630 ISD::CondCode CC = ISD::SETCC_INVALID;
6631 switch (IntNo) {
6632 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006633 case Intrinsic::x86_sse_comieq_ss:
6634 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006635 Opc = X86ISD::COMI;
6636 CC = ISD::SETEQ;
6637 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006638 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006639 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006640 Opc = X86ISD::COMI;
6641 CC = ISD::SETLT;
6642 break;
6643 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006644 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645 Opc = X86ISD::COMI;
6646 CC = ISD::SETLE;
6647 break;
6648 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006649 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006650 Opc = X86ISD::COMI;
6651 CC = ISD::SETGT;
6652 break;
6653 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006654 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655 Opc = X86ISD::COMI;
6656 CC = ISD::SETGE;
6657 break;
6658 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006659 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006660 Opc = X86ISD::COMI;
6661 CC = ISD::SETNE;
6662 break;
6663 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006664 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665 Opc = X86ISD::UCOMI;
6666 CC = ISD::SETEQ;
6667 break;
6668 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006669 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006670 Opc = X86ISD::UCOMI;
6671 CC = ISD::SETLT;
6672 break;
6673 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006674 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006675 Opc = X86ISD::UCOMI;
6676 CC = ISD::SETLE;
6677 break;
6678 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006679 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680 Opc = X86ISD::UCOMI;
6681 CC = ISD::SETGT;
6682 break;
6683 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006684 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006685 Opc = X86ISD::UCOMI;
6686 CC = ISD::SETGE;
6687 break;
6688 case Intrinsic::x86_sse_ucomineq_ss:
6689 case Intrinsic::x86_sse2_ucomineq_sd:
6690 Opc = X86ISD::UCOMI;
6691 CC = ISD::SETNE;
6692 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006693 }
Evan Cheng734503b2006-09-11 02:19:56 +00006694
Dan Gohman475871a2008-07-27 21:46:04 +00006695 SDValue LHS = Op.getOperand(1);
6696 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006697 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006698 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006699 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6700 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6701 DAG.getConstant(X86CC, MVT::i8), Cond);
6702 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006703 }
Eric Christopher71c67532009-07-29 00:28:05 +00006704 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006705 // an integer value, not just an instruction so lower it to the ptest
6706 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006707 case Intrinsic::x86_sse41_ptestz:
6708 case Intrinsic::x86_sse41_ptestc:
6709 case Intrinsic::x86_sse41_ptestnzc:{
6710 unsigned X86CC = 0;
6711 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006712 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006713 case Intrinsic::x86_sse41_ptestz:
6714 // ZF = 1
6715 X86CC = X86::COND_E;
6716 break;
6717 case Intrinsic::x86_sse41_ptestc:
6718 // CF = 1
6719 X86CC = X86::COND_B;
6720 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006721 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006722 // ZF and CF = 0
6723 X86CC = X86::COND_A;
6724 break;
6725 }
Eric Christopherfd179292009-08-27 18:07:15 +00006726
Eric Christopher71c67532009-07-29 00:28:05 +00006727 SDValue LHS = Op.getOperand(1);
6728 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006729 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6730 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6731 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6732 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006733 }
Evan Cheng5759f972008-05-04 09:15:50 +00006734
6735 // Fix vector shift instructions where the last operand is a non-immediate
6736 // i32 value.
6737 case Intrinsic::x86_sse2_pslli_w:
6738 case Intrinsic::x86_sse2_pslli_d:
6739 case Intrinsic::x86_sse2_pslli_q:
6740 case Intrinsic::x86_sse2_psrli_w:
6741 case Intrinsic::x86_sse2_psrli_d:
6742 case Intrinsic::x86_sse2_psrli_q:
6743 case Intrinsic::x86_sse2_psrai_w:
6744 case Intrinsic::x86_sse2_psrai_d:
6745 case Intrinsic::x86_mmx_pslli_w:
6746 case Intrinsic::x86_mmx_pslli_d:
6747 case Intrinsic::x86_mmx_pslli_q:
6748 case Intrinsic::x86_mmx_psrli_w:
6749 case Intrinsic::x86_mmx_psrli_d:
6750 case Intrinsic::x86_mmx_psrli_q:
6751 case Intrinsic::x86_mmx_psrai_w:
6752 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006753 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006754 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006755 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006756
6757 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006759 switch (IntNo) {
6760 case Intrinsic::x86_sse2_pslli_w:
6761 NewIntNo = Intrinsic::x86_sse2_psll_w;
6762 break;
6763 case Intrinsic::x86_sse2_pslli_d:
6764 NewIntNo = Intrinsic::x86_sse2_psll_d;
6765 break;
6766 case Intrinsic::x86_sse2_pslli_q:
6767 NewIntNo = Intrinsic::x86_sse2_psll_q;
6768 break;
6769 case Intrinsic::x86_sse2_psrli_w:
6770 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6771 break;
6772 case Intrinsic::x86_sse2_psrli_d:
6773 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6774 break;
6775 case Intrinsic::x86_sse2_psrli_q:
6776 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6777 break;
6778 case Intrinsic::x86_sse2_psrai_w:
6779 NewIntNo = Intrinsic::x86_sse2_psra_w;
6780 break;
6781 case Intrinsic::x86_sse2_psrai_d:
6782 NewIntNo = Intrinsic::x86_sse2_psra_d;
6783 break;
6784 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006785 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006786 switch (IntNo) {
6787 case Intrinsic::x86_mmx_pslli_w:
6788 NewIntNo = Intrinsic::x86_mmx_psll_w;
6789 break;
6790 case Intrinsic::x86_mmx_pslli_d:
6791 NewIntNo = Intrinsic::x86_mmx_psll_d;
6792 break;
6793 case Intrinsic::x86_mmx_pslli_q:
6794 NewIntNo = Intrinsic::x86_mmx_psll_q;
6795 break;
6796 case Intrinsic::x86_mmx_psrli_w:
6797 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6798 break;
6799 case Intrinsic::x86_mmx_psrli_d:
6800 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6801 break;
6802 case Intrinsic::x86_mmx_psrli_q:
6803 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6804 break;
6805 case Intrinsic::x86_mmx_psrai_w:
6806 NewIntNo = Intrinsic::x86_mmx_psra_w;
6807 break;
6808 case Intrinsic::x86_mmx_psrai_d:
6809 NewIntNo = Intrinsic::x86_mmx_psra_d;
6810 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006811 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006812 }
6813 break;
6814 }
6815 }
Mon P Wangefa42202009-09-03 19:56:25 +00006816
6817 // The vector shift intrinsics with scalars uses 32b shift amounts but
6818 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6819 // to be zero.
6820 SDValue ShOps[4];
6821 ShOps[0] = ShAmt;
6822 ShOps[1] = DAG.getConstant(0, MVT::i32);
6823 if (ShAmtVT == MVT::v4i32) {
6824 ShOps[2] = DAG.getUNDEF(MVT::i32);
6825 ShOps[3] = DAG.getUNDEF(MVT::i32);
6826 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6827 } else {
6828 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6829 }
6830
Owen Andersone50ed302009-08-10 22:56:29 +00006831 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006832 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006833 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006834 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006835 Op.getOperand(1), ShAmt);
6836 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006837 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006838}
Evan Cheng72261582005-12-20 06:22:03 +00006839
Dan Gohman475871a2008-07-27 21:46:04 +00006840SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006841 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006842 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006843
6844 if (Depth > 0) {
6845 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6846 SDValue Offset =
6847 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006848 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006849 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006850 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006851 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006852 NULL, 0);
6853 }
6854
6855 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006856 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006857 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006858 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006859}
6860
Dan Gohman475871a2008-07-27 21:46:04 +00006861SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006862 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6863 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006864 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006865 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006866 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6867 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006868 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006869 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006870 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006871 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006872}
6873
Dan Gohman475871a2008-07-27 21:46:04 +00006874SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006875 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006876 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006877}
6878
Dan Gohman475871a2008-07-27 21:46:04 +00006879SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006880{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006881 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006882 SDValue Chain = Op.getOperand(0);
6883 SDValue Offset = Op.getOperand(1);
6884 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006885 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006886
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006887 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6888 getPointerTy());
6889 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006890
Dale Johannesene4d209d2009-02-03 20:21:25 +00006891 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006892 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006893 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6894 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006895 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006896 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006897
Dale Johannesene4d209d2009-02-03 20:21:25 +00006898 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006899 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006900 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006901}
6902
Dan Gohman475871a2008-07-27 21:46:04 +00006903SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006904 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006905 SDValue Root = Op.getOperand(0);
6906 SDValue Trmp = Op.getOperand(1); // trampoline
6907 SDValue FPtr = Op.getOperand(2); // nested function
6908 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006909 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006910
Dan Gohman69de1932008-02-06 22:27:42 +00006911 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006912
Duncan Sands339e14f2008-01-16 22:55:25 +00006913 const X86InstrInfo *TII =
6914 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6915
Duncan Sandsb116fac2007-07-27 20:02:49 +00006916 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006917 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006918
6919 // Large code-model.
6920
6921 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6922 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6923
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006924 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6925 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006926
6927 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6928
6929 // Load the pointer to the nested function into R11.
6930 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006931 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00006932 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006933 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006934
Owen Anderson825b72b2009-08-11 20:47:22 +00006935 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6936 DAG.getConstant(2, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006937 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006938
6939 // Load the 'nest' parameter value into R10.
6940 // R10 is specified in X86CallingConv.td
6941 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00006942 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6943 DAG.getConstant(10, MVT::i64));
6944 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006945 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006946
Owen Anderson825b72b2009-08-11 20:47:22 +00006947 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6948 DAG.getConstant(12, MVT::i64));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006949 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006950
6951 // Jump to the nested function.
6952 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00006953 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6954 DAG.getConstant(20, MVT::i64));
6955 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006956 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006957
6958 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00006959 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
6960 DAG.getConstant(22, MVT::i64));
6961 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006962 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006963
Dan Gohman475871a2008-07-27 21:46:04 +00006964 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00006965 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006966 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006967 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006968 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006969 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006970 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006971 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006972
6973 switch (CC) {
6974 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006975 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006976 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006977 case CallingConv::X86_StdCall: {
6978 // Pass 'nest' parameter in ECX.
6979 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006980 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006981
6982 // Check that ECX wasn't needed by an 'inreg' parameter.
6983 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006984 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006985
Chris Lattner58d74912008-03-12 17:45:29 +00006986 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006987 unsigned InRegCount = 0;
6988 unsigned Idx = 1;
6989
6990 for (FunctionType::param_iterator I = FTy->param_begin(),
6991 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006992 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006993 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006994 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006995
6996 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006997 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006998 }
6999 }
7000 break;
7001 }
7002 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007003 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007004 // Pass 'nest' parameter in EAX.
7005 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007006 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007007 break;
7008 }
7009
Dan Gohman475871a2008-07-27 21:46:04 +00007010 SDValue OutChains[4];
7011 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007012
Owen Anderson825b72b2009-08-11 20:47:22 +00007013 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7014 DAG.getConstant(10, MVT::i32));
7015 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007016
Duncan Sands339e14f2008-01-16 22:55:25 +00007017 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007018 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007019 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00007021 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007022
Owen Anderson825b72b2009-08-11 20:47:22 +00007023 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7024 DAG.getConstant(1, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007025 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007026
Duncan Sands339e14f2008-01-16 22:55:25 +00007027 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7029 DAG.getConstant(5, MVT::i32));
7030 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00007031 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007032
Owen Anderson825b72b2009-08-11 20:47:22 +00007033 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7034 DAG.getConstant(6, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007035 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007036
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007038 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007039 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007040 }
7041}
7042
Dan Gohman475871a2008-07-27 21:46:04 +00007043SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007044 /*
7045 The rounding mode is in bits 11:10 of FPSR, and has the following
7046 settings:
7047 00 Round to nearest
7048 01 Round to -inf
7049 10 Round to +inf
7050 11 Round to 0
7051
7052 FLT_ROUNDS, on the other hand, expects the following:
7053 -1 Undefined
7054 0 Round to 0
7055 1 Round to nearest
7056 2 Round to +inf
7057 3 Round to -inf
7058
7059 To perform the conversion, we do:
7060 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7061 */
7062
7063 MachineFunction &MF = DAG.getMachineFunction();
7064 const TargetMachine &TM = MF.getTarget();
7065 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7066 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007067 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007068 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007069
7070 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007071 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007072 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007073
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007075 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007076
7077 // Load FP Control Word from stack slot
Owen Anderson825b72b2009-08-11 20:47:22 +00007078 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007079
7080 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 DAG.getNode(ISD::SRL, dl, MVT::i16,
7083 DAG.getNode(ISD::AND, dl, MVT::i16,
7084 CWD, DAG.getConstant(0x800, MVT::i16)),
7085 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007086 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007087 DAG.getNode(ISD::SRL, dl, MVT::i16,
7088 DAG.getNode(ISD::AND, dl, MVT::i16,
7089 CWD, DAG.getConstant(0x400, MVT::i16)),
7090 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007091
Dan Gohman475871a2008-07-27 21:46:04 +00007092 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007093 DAG.getNode(ISD::AND, dl, MVT::i16,
7094 DAG.getNode(ISD::ADD, dl, MVT::i16,
7095 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7096 DAG.getConstant(1, MVT::i16)),
7097 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007098
7099
Duncan Sands83ec4b62008-06-06 12:08:01 +00007100 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007101 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007102}
7103
Dan Gohman475871a2008-07-27 21:46:04 +00007104SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007105 EVT VT = Op.getValueType();
7106 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007107 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007108 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007109
7110 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007111 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007112 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007114 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007115 }
Evan Cheng18efe262007-12-14 02:13:44 +00007116
Evan Cheng152804e2007-12-14 08:30:15 +00007117 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007119 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007120
7121 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007122 SDValue Ops[] = {
7123 Op,
7124 DAG.getConstant(NumBits+NumBits-1, OpVT),
7125 DAG.getConstant(X86::COND_E, MVT::i8),
7126 Op.getValue(1)
7127 };
7128 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007129
7130 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007131 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007132
Owen Anderson825b72b2009-08-11 20:47:22 +00007133 if (VT == MVT::i8)
7134 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007135 return Op;
7136}
7137
Dan Gohman475871a2008-07-27 21:46:04 +00007138SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007139 EVT VT = Op.getValueType();
7140 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007141 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007142 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007143
7144 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007145 if (VT == MVT::i8) {
7146 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007148 }
Evan Cheng152804e2007-12-14 08:30:15 +00007149
7150 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007151 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007153
7154 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007155 SDValue Ops[] = {
7156 Op,
7157 DAG.getConstant(NumBits, OpVT),
7158 DAG.getConstant(X86::COND_E, MVT::i8),
7159 Op.getValue(1)
7160 };
7161 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007162
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 if (VT == MVT::i8)
7164 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007165 return Op;
7166}
7167
Mon P Wangaf9b9522008-12-18 21:42:19 +00007168SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007169 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007170 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007171 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007172
Mon P Wangaf9b9522008-12-18 21:42:19 +00007173 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7174 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7175 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7176 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7177 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7178 //
7179 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7180 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7181 // return AloBlo + AloBhi + AhiBlo;
7182
7183 SDValue A = Op.getOperand(0);
7184 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007185
Dale Johannesene4d209d2009-02-03 20:21:25 +00007186 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007187 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7188 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007189 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007190 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7191 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007192 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007194 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007195 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007197 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007198 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007199 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007200 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007201 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007202 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7203 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007204 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007205 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7206 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007207 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7208 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007209 return Res;
7210}
7211
7212
Bill Wendling74c37652008-12-09 22:08:41 +00007213SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7214 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7215 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007216 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7217 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007218 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007219 SDValue LHS = N->getOperand(0);
7220 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007221 unsigned BaseOp = 0;
7222 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007223 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007224
7225 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007226 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007227 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007228 // A subtract of one will be selected as a INC. Note that INC doesn't
7229 // set CF, so we can't do this for UADDO.
7230 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7231 if (C->getAPIntValue() == 1) {
7232 BaseOp = X86ISD::INC;
7233 Cond = X86::COND_O;
7234 break;
7235 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007236 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007237 Cond = X86::COND_O;
7238 break;
7239 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007240 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007241 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007242 break;
7243 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007244 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7245 // set CF, so we can't do this for USUBO.
7246 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7247 if (C->getAPIntValue() == 1) {
7248 BaseOp = X86ISD::DEC;
7249 Cond = X86::COND_O;
7250 break;
7251 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007252 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007253 Cond = X86::COND_O;
7254 break;
7255 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007256 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007257 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007258 break;
7259 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007260 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007261 Cond = X86::COND_O;
7262 break;
7263 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007264 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007265 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007266 break;
7267 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007268
Bill Wendling61edeb52008-12-02 01:06:39 +00007269 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007270 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007271 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007272
Bill Wendling61edeb52008-12-02 01:06:39 +00007273 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007274 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007275 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007276
Bill Wendling61edeb52008-12-02 01:06:39 +00007277 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7278 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007279}
7280
Dan Gohman475871a2008-07-27 21:46:04 +00007281SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007282 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007283 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007284 unsigned Reg = 0;
7285 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007286 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007287 default:
7288 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007289 case MVT::i8: Reg = X86::AL; size = 1; break;
7290 case MVT::i16: Reg = X86::AX; size = 2; break;
7291 case MVT::i32: Reg = X86::EAX; size = 4; break;
7292 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007293 assert(Subtarget->is64Bit() && "Node not type legal!");
7294 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007295 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007296 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007297 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007298 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007299 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007300 Op.getOperand(1),
7301 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007303 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007304 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007305 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007306 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007307 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007308 return cpOut;
7309}
7310
Duncan Sands1607f052008-12-01 11:39:25 +00007311SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007312 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007313 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007315 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007316 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007317 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7319 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007320 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7322 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007323 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007325 rdx.getValue(1)
7326 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007327 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007328}
7329
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007330SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7331 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007332 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007333 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007334 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007335 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007336 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007337 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007338 Node->getOperand(0),
7339 Node->getOperand(1), negOp,
7340 cast<AtomicSDNode>(Node)->getSrcValue(),
7341 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007342}
7343
Evan Cheng0db9fe62006-04-25 20:13:52 +00007344/// LowerOperation - Provide custom lowering hooks for some operations.
7345///
Dan Gohman475871a2008-07-27 21:46:04 +00007346SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007347 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007348 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007349 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7350 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007351 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007352 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007353 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7354 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7355 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7356 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7357 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7358 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007359 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007360 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007361 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007362 case ISD::SHL_PARTS:
7363 case ISD::SRA_PARTS:
7364 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7365 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007366 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007367 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007368 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007369 case ISD::FABS: return LowerFABS(Op, DAG);
7370 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007371 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007372 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007373 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007374 case ISD::SELECT: return LowerSELECT(Op, DAG);
7375 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007376 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007377 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007378 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007379 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007380 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007381 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7382 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007383 case ISD::FRAME_TO_ARGS_OFFSET:
7384 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007385 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007386 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007387 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007388 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007389 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7390 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007391 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007392 case ISD::SADDO:
7393 case ISD::UADDO:
7394 case ISD::SSUBO:
7395 case ISD::USUBO:
7396 case ISD::SMULO:
7397 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007398 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007399 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007400}
7401
Duncan Sands1607f052008-12-01 11:39:25 +00007402void X86TargetLowering::
7403ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7404 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007405 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007408
7409 SDValue Chain = Node->getOperand(0);
7410 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007412 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007413 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007414 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007415 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007416 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007417 SDValue Result =
7418 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7419 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007420 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007421 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007422 Results.push_back(Result.getValue(2));
7423}
7424
Duncan Sands126d9072008-07-04 11:47:58 +00007425/// ReplaceNodeResults - Replace a node with an illegal result type
7426/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007427void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7428 SmallVectorImpl<SDValue>&Results,
7429 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007430 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007431 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007432 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007433 assert(false && "Do not know how to custom type legalize this operation!");
7434 return;
7435 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007436 std::pair<SDValue,SDValue> Vals =
7437 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007438 SDValue FIST = Vals.first, StackSlot = Vals.second;
7439 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007440 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007441 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007442 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007443 }
7444 return;
7445 }
7446 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007448 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007449 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007450 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007451 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007452 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007453 eax.getValue(2));
7454 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7455 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007457 Results.push_back(edx.getValue(1));
7458 return;
7459 }
Mon P Wangcd6e7252009-11-30 02:42:02 +00007460 case ISD::SDIV:
7461 case ISD::UDIV:
7462 case ISD::SREM:
7463 case ISD::UREM: {
7464 EVT WidenVT = getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
7465 Results.push_back(DAG.UnrollVectorOp(N, WidenVT.getVectorNumElements()));
7466 return;
7467 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007468 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007469 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007470 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007471 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007472 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7473 DAG.getConstant(0, MVT::i32));
7474 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7475 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007476 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7477 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007478 cpInL.getValue(1));
7479 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007480 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7481 DAG.getConstant(0, MVT::i32));
7482 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7483 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007484 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007485 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007486 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007487 swapInL.getValue(1));
7488 SDValue Ops[] = { swapInH.getValue(0),
7489 N->getOperand(1),
7490 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007492 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007493 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007495 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007496 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007497 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007498 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007499 Results.push_back(cpOutH.getValue(1));
7500 return;
7501 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007502 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007503 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7504 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007505 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007506 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7507 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007508 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007509 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7510 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007511 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007512 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7513 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007514 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007515 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7516 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007517 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007518 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7519 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007520 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007521 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7522 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007523 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007524}
7525
Evan Cheng72261582005-12-20 06:22:03 +00007526const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7527 switch (Opcode) {
7528 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007529 case X86ISD::BSF: return "X86ISD::BSF";
7530 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007531 case X86ISD::SHLD: return "X86ISD::SHLD";
7532 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007533 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007534 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007535 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007536 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007537 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007538 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007539 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7540 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7541 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007542 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007543 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007544 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007545 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007546 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007547 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007548 case X86ISD::COMI: return "X86ISD::COMI";
7549 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007550 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007551 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007552 case X86ISD::CMOV: return "X86ISD::CMOV";
7553 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007554 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007555 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7556 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007557 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007558 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007559 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007560 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007561 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007562 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7563 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007564 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007565 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007566 case X86ISD::FMAX: return "X86ISD::FMAX";
7567 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007568 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7569 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007570 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007571 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007572 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007573 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007574 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007575 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7576 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007577 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7578 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7579 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7580 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7581 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7582 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007583 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7584 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007585 case X86ISD::VSHL: return "X86ISD::VSHL";
7586 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007587 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7588 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7589 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7590 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7591 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7592 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7593 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7594 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7595 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7596 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007597 case X86ISD::ADD: return "X86ISD::ADD";
7598 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007599 case X86ISD::SMUL: return "X86ISD::SMUL";
7600 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007601 case X86ISD::INC: return "X86ISD::INC";
7602 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007603 case X86ISD::OR: return "X86ISD::OR";
7604 case X86ISD::XOR: return "X86ISD::XOR";
7605 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007606 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007607 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007608 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Evan Cheng72261582005-12-20 06:22:03 +00007609 }
7610}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007611
Chris Lattnerc9addb72007-03-30 23:15:24 +00007612// isLegalAddressingMode - Return true if the addressing mode represented
7613// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007614bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007615 const Type *Ty) const {
7616 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007617 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007618
Chris Lattnerc9addb72007-03-30 23:15:24 +00007619 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007620 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007621 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007622
Chris Lattnerc9addb72007-03-30 23:15:24 +00007623 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007624 unsigned GVFlags =
7625 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007626
Chris Lattnerdfed4132009-07-10 07:38:24 +00007627 // If a reference to this global requires an extra load, we can't fold it.
7628 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007629 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007630
Chris Lattnerdfed4132009-07-10 07:38:24 +00007631 // If BaseGV requires a register for the PIC base, we cannot also have a
7632 // BaseReg specified.
7633 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007634 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007635
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007636 // If lower 4G is not available, then we must use rip-relative addressing.
7637 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7638 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007639 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007640
Chris Lattnerc9addb72007-03-30 23:15:24 +00007641 switch (AM.Scale) {
7642 case 0:
7643 case 1:
7644 case 2:
7645 case 4:
7646 case 8:
7647 // These scales always work.
7648 break;
7649 case 3:
7650 case 5:
7651 case 9:
7652 // These scales are formed with basereg+scalereg. Only accept if there is
7653 // no basereg yet.
7654 if (AM.HasBaseReg)
7655 return false;
7656 break;
7657 default: // Other stuff never works.
7658 return false;
7659 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007660
Chris Lattnerc9addb72007-03-30 23:15:24 +00007661 return true;
7662}
7663
7664
Evan Cheng2bd122c2007-10-26 01:56:11 +00007665bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7666 if (!Ty1->isInteger() || !Ty2->isInteger())
7667 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007668 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7669 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007670 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007671 return false;
7672 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007673}
7674
Owen Andersone50ed302009-08-10 22:56:29 +00007675bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007676 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007677 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007678 unsigned NumBits1 = VT1.getSizeInBits();
7679 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007680 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007681 return false;
7682 return Subtarget->is64Bit() || NumBits1 < 64;
7683}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007684
Dan Gohman97121ba2009-04-08 00:15:30 +00007685bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007686 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman5ad7de22010-01-15 22:18:15 +00007687 return Ty1->isInteger(32) && Ty2->isInteger(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007688}
7689
Owen Andersone50ed302009-08-10 22:56:29 +00007690bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007691 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007692 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007693}
7694
Owen Andersone50ed302009-08-10 22:56:29 +00007695bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007696 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007697 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007698}
7699
Evan Cheng60c07e12006-07-05 22:17:51 +00007700/// isShuffleMaskLegal - Targets can use this to indicate that they only
7701/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7702/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7703/// are assumed to be legal.
7704bool
Eric Christopherfd179292009-08-27 18:07:15 +00007705X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007706 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007707 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007708 if (VT.getSizeInBits() == 64)
7709 return false;
7710
Nate Begemana09008b2009-10-19 02:17:23 +00007711 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007712 return (VT.getVectorNumElements() == 2 ||
7713 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7714 isMOVLMask(M, VT) ||
7715 isSHUFPMask(M, VT) ||
7716 isPSHUFDMask(M, VT) ||
7717 isPSHUFHWMask(M, VT) ||
7718 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007719 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007720 isUNPCKLMask(M, VT) ||
7721 isUNPCKHMask(M, VT) ||
7722 isUNPCKL_v_undef_Mask(M, VT) ||
7723 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007724}
7725
Dan Gohman7d8143f2008-04-09 20:09:42 +00007726bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007727X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007728 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007729 unsigned NumElts = VT.getVectorNumElements();
7730 // FIXME: This collection of masks seems suspect.
7731 if (NumElts == 2)
7732 return true;
7733 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7734 return (isMOVLMask(Mask, VT) ||
7735 isCommutedMOVLMask(Mask, VT, true) ||
7736 isSHUFPMask(Mask, VT) ||
7737 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007738 }
7739 return false;
7740}
7741
7742//===----------------------------------------------------------------------===//
7743// X86 Scheduler Hooks
7744//===----------------------------------------------------------------------===//
7745
Mon P Wang63307c32008-05-05 19:05:59 +00007746// private utility function
7747MachineBasicBlock *
7748X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7749 MachineBasicBlock *MBB,
7750 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007751 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007752 unsigned LoadOpc,
7753 unsigned CXchgOpc,
7754 unsigned copyOpc,
7755 unsigned notOpc,
7756 unsigned EAXreg,
7757 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007758 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007759 // For the atomic bitwise operator, we generate
7760 // thisMBB:
7761 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007762 // ld t1 = [bitinstr.addr]
7763 // op t2 = t1, [bitinstr.val]
7764 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007765 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7766 // bz newMBB
7767 // fallthrough -->nextMBB
7768 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7769 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007770 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007771 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007772
Mon P Wang63307c32008-05-05 19:05:59 +00007773 /// First build the CFG
7774 MachineFunction *F = MBB->getParent();
7775 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007776 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7777 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7778 F->insert(MBBIter, newMBB);
7779 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007780
Mon P Wang63307c32008-05-05 19:05:59 +00007781 // Move all successors to thisMBB to nextMBB
7782 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007783
Mon P Wang63307c32008-05-05 19:05:59 +00007784 // Update thisMBB to fall through to newMBB
7785 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007786
Mon P Wang63307c32008-05-05 19:05:59 +00007787 // newMBB jumps to itself and fall through to nextMBB
7788 newMBB->addSuccessor(nextMBB);
7789 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007790
Mon P Wang63307c32008-05-05 19:05:59 +00007791 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007792 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007793 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007794 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007795 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007796 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007797 int numArgs = bInstr->getNumOperands() - 1;
7798 for (int i=0; i < numArgs; ++i)
7799 argOpers[i] = &bInstr->getOperand(i+1);
7800
7801 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007802 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7803 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007804
Dale Johannesen140be2d2008-08-19 18:47:28 +00007805 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007806 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007807 for (int i=0; i <= lastAddrIndx; ++i)
7808 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007809
Dale Johannesen140be2d2008-08-19 18:47:28 +00007810 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007811 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007812 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007813 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007814 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007815 tt = t1;
7816
Dale Johannesen140be2d2008-08-19 18:47:28 +00007817 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007818 assert((argOpers[valArgIndx]->isReg() ||
7819 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007820 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007821 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007822 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007823 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007824 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007825 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007826 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007827
Dale Johannesene4d209d2009-02-03 20:21:25 +00007828 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007829 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007830
Dale Johannesene4d209d2009-02-03 20:21:25 +00007831 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007832 for (int i=0; i <= lastAddrIndx; ++i)
7833 (*MIB).addOperand(*argOpers[i]);
7834 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007835 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007836 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7837 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007838
Dale Johannesene4d209d2009-02-03 20:21:25 +00007839 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007840 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007841
Mon P Wang63307c32008-05-05 19:05:59 +00007842 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007843 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007844
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007845 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007846 return nextMBB;
7847}
7848
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007849// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007850MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007851X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7852 MachineBasicBlock *MBB,
7853 unsigned regOpcL,
7854 unsigned regOpcH,
7855 unsigned immOpcL,
7856 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007857 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007858 // For the atomic bitwise operator, we generate
7859 // thisMBB (instructions are in pairs, except cmpxchg8b)
7860 // ld t1,t2 = [bitinstr.addr]
7861 // newMBB:
7862 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7863 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007864 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007865 // mov ECX, EBX <- t5, t6
7866 // mov EAX, EDX <- t1, t2
7867 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7868 // mov t3, t4 <- EAX, EDX
7869 // bz newMBB
7870 // result in out1, out2
7871 // fallthrough -->nextMBB
7872
7873 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7874 const unsigned LoadOpc = X86::MOV32rm;
7875 const unsigned copyOpc = X86::MOV32rr;
7876 const unsigned NotOpc = X86::NOT32r;
7877 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7878 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7879 MachineFunction::iterator MBBIter = MBB;
7880 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007881
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007882 /// First build the CFG
7883 MachineFunction *F = MBB->getParent();
7884 MachineBasicBlock *thisMBB = MBB;
7885 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7886 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7887 F->insert(MBBIter, newMBB);
7888 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007889
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007890 // Move all successors to thisMBB to nextMBB
7891 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007892
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007893 // Update thisMBB to fall through to newMBB
7894 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007895
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007896 // newMBB jumps to itself and fall through to nextMBB
7897 newMBB->addSuccessor(nextMBB);
7898 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007899
Dale Johannesene4d209d2009-02-03 20:21:25 +00007900 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007901 // Insert instructions into newMBB based on incoming instruction
7902 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007903 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007904 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007905 MachineOperand& dest1Oper = bInstr->getOperand(0);
7906 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007907 MachineOperand* argOpers[2 + X86AddrNumOperands];
7908 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007909 argOpers[i] = &bInstr->getOperand(i+2);
7910
Evan Chengad5b52f2010-01-08 19:14:57 +00007911 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007912 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007913
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007914 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007915 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007916 for (int i=0; i <= lastAddrIndx; ++i)
7917 (*MIB).addOperand(*argOpers[i]);
7918 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007919 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007920 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007921 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007922 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007923 MachineOperand newOp3 = *(argOpers[3]);
7924 if (newOp3.isImm())
7925 newOp3.setImm(newOp3.getImm()+4);
7926 else
7927 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007928 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007929 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007930
7931 // t3/4 are defined later, at the bottom of the loop
7932 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7933 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007934 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007935 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007936 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007937 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7938
Evan Cheng306b4ca2010-01-08 23:41:50 +00007939 // The subsequent operations should be using the destination registers of
7940 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00007941 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007942 t1 = F->getRegInfo().createVirtualRegister(RC);
7943 t2 = F->getRegInfo().createVirtualRegister(RC);
7944 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
7945 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007946 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00007947 t1 = dest1Oper.getReg();
7948 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007949 }
7950
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007951 int valArgIndx = lastAddrIndx + 1;
7952 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007953 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007954 "invalid operand");
7955 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7956 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007957 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007958 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007959 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007960 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007961 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007962 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007963 (*MIB).addOperand(*argOpers[valArgIndx]);
7964 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007965 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007966 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007967 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007968 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007969 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007970 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007971 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007972 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00007973 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007974 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007975
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007978 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007979 MIB.addReg(t2);
7980
Dale Johannesene4d209d2009-02-03 20:21:25 +00007981 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007982 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007983 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007984 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007985
Dale Johannesene4d209d2009-02-03 20:21:25 +00007986 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007987 for (int i=0; i <= lastAddrIndx; ++i)
7988 (*MIB).addOperand(*argOpers[i]);
7989
7990 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007991 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7992 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007993
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007995 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007997 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007998
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007999 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008000 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008001
8002 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8003 return nextMBB;
8004}
8005
8006// private utility function
8007MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008008X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8009 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008010 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008011 // For the atomic min/max operator, we generate
8012 // thisMBB:
8013 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008014 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008015 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008016 // cmp t1, t2
8017 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008018 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008019 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8020 // bz newMBB
8021 // fallthrough -->nextMBB
8022 //
8023 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8024 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008025 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008026 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008027
Mon P Wang63307c32008-05-05 19:05:59 +00008028 /// First build the CFG
8029 MachineFunction *F = MBB->getParent();
8030 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008031 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8032 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8033 F->insert(MBBIter, newMBB);
8034 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008035
Dan Gohmand6708ea2009-08-15 01:38:56 +00008036 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008037 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008038
Mon P Wang63307c32008-05-05 19:05:59 +00008039 // Update thisMBB to fall through to newMBB
8040 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008041
Mon P Wang63307c32008-05-05 19:05:59 +00008042 // newMBB jumps to newMBB and fall through to nextMBB
8043 newMBB->addSuccessor(nextMBB);
8044 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008045
Dale Johannesene4d209d2009-02-03 20:21:25 +00008046 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008047 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008048 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008049 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008050 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008051 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008052 int numArgs = mInstr->getNumOperands() - 1;
8053 for (int i=0; i < numArgs; ++i)
8054 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008055
Mon P Wang63307c32008-05-05 19:05:59 +00008056 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008057 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8058 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008059
Mon P Wangab3e7472008-05-05 22:56:23 +00008060 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008061 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008062 for (int i=0; i <= lastAddrIndx; ++i)
8063 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008064
Mon P Wang63307c32008-05-05 19:05:59 +00008065 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008066 assert((argOpers[valArgIndx]->isReg() ||
8067 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008068 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008069
8070 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008071 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008072 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008073 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008074 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008075 (*MIB).addOperand(*argOpers[valArgIndx]);
8076
Dale Johannesene4d209d2009-02-03 20:21:25 +00008077 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008078 MIB.addReg(t1);
8079
Dale Johannesene4d209d2009-02-03 20:21:25 +00008080 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008081 MIB.addReg(t1);
8082 MIB.addReg(t2);
8083
8084 // Generate movc
8085 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008086 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008087 MIB.addReg(t2);
8088 MIB.addReg(t1);
8089
8090 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008091 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008092 for (int i=0; i <= lastAddrIndx; ++i)
8093 (*MIB).addOperand(*argOpers[i]);
8094 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008095 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008096 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8097 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008098
Dale Johannesene4d209d2009-02-03 20:21:25 +00008099 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008100 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008101
Mon P Wang63307c32008-05-05 19:05:59 +00008102 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00008103 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008104
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008105 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008106 return nextMBB;
8107}
8108
Eric Christopherf83a5de2009-08-27 18:08:16 +00008109// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8110// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008111MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008112X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008113 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008114
8115 MachineFunction *F = BB->getParent();
8116 DebugLoc dl = MI->getDebugLoc();
8117 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8118
8119 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008120 if (memArg)
8121 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8122 else
8123 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008124
8125 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8126
8127 for (unsigned i = 0; i < numArgs; ++i) {
8128 MachineOperand &Op = MI->getOperand(i+1);
8129
8130 if (!(Op.isReg() && Op.isImplicit()))
8131 MIB.addOperand(Op);
8132 }
8133
8134 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8135 .addReg(X86::XMM0);
8136
8137 F->DeleteMachineInstr(MI);
8138
8139 return BB;
8140}
8141
8142MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008143X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8144 MachineInstr *MI,
8145 MachineBasicBlock *MBB) const {
8146 // Emit code to save XMM registers to the stack. The ABI says that the
8147 // number of registers to save is given in %al, so it's theoretically
8148 // possible to do an indirect jump trick to avoid saving all of them,
8149 // however this code takes a simpler approach and just executes all
8150 // of the stores if %al is non-zero. It's less code, and it's probably
8151 // easier on the hardware branch predictor, and stores aren't all that
8152 // expensive anyway.
8153
8154 // Create the new basic blocks. One block contains all the XMM stores,
8155 // and one block is the final destination regardless of whether any
8156 // stores were performed.
8157 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8158 MachineFunction *F = MBB->getParent();
8159 MachineFunction::iterator MBBIter = MBB;
8160 ++MBBIter;
8161 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8162 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8163 F->insert(MBBIter, XMMSaveMBB);
8164 F->insert(MBBIter, EndMBB);
8165
8166 // Set up the CFG.
8167 // Move any original successors of MBB to the end block.
8168 EndMBB->transferSuccessors(MBB);
8169 // The original block will now fall through to the XMM save block.
8170 MBB->addSuccessor(XMMSaveMBB);
8171 // The XMMSaveMBB will fall through to the end block.
8172 XMMSaveMBB->addSuccessor(EndMBB);
8173
8174 // Now add the instructions.
8175 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8176 DebugLoc DL = MI->getDebugLoc();
8177
8178 unsigned CountReg = MI->getOperand(0).getReg();
8179 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8180 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8181
8182 if (!Subtarget->isTargetWin64()) {
8183 // If %al is 0, branch around the XMM save block.
8184 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
8185 BuildMI(MBB, DL, TII->get(X86::JE)).addMBB(EndMBB);
8186 MBB->addSuccessor(EndMBB);
8187 }
8188
8189 // In the XMM save block, save all the XMM argument registers.
8190 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8191 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008192 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008193 F->getMachineMemOperand(
8194 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8195 MachineMemOperand::MOStore, Offset,
8196 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008197 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8198 .addFrameIndex(RegSaveFrameIndex)
8199 .addImm(/*Scale=*/1)
8200 .addReg(/*IndexReg=*/0)
8201 .addImm(/*Disp=*/Offset)
8202 .addReg(/*Segment=*/0)
8203 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008204 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008205 }
8206
8207 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8208
8209 return EndMBB;
8210}
Mon P Wang63307c32008-05-05 19:05:59 +00008211
Evan Cheng60c07e12006-07-05 22:17:51 +00008212MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008213X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008214 MachineBasicBlock *BB,
8215 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8217 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008218
Chris Lattner52600972009-09-02 05:57:00 +00008219 // To "insert" a SELECT_CC instruction, we actually have to insert the
8220 // diamond control-flow pattern. The incoming instruction knows the
8221 // destination vreg to set, the condition code register to branch on, the
8222 // true/false values to select between, and a branch opcode to use.
8223 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8224 MachineFunction::iterator It = BB;
8225 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008226
Chris Lattner52600972009-09-02 05:57:00 +00008227 // thisMBB:
8228 // ...
8229 // TrueVal = ...
8230 // cmpTY ccX, r1, r2
8231 // bCC copy1MBB
8232 // fallthrough --> copy0MBB
8233 MachineBasicBlock *thisMBB = BB;
8234 MachineFunction *F = BB->getParent();
8235 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8236 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8237 unsigned Opc =
8238 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8239 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8240 F->insert(It, copy0MBB);
8241 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008242 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008243 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008244 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008245 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008246 E = BB->succ_end(); I != E; ++I) {
8247 EM->insert(std::make_pair(*I, sinkMBB));
8248 sinkMBB->addSuccessor(*I);
8249 }
8250 // Next, remove all successors of the current block, and add the true
8251 // and fallthrough blocks as its successors.
8252 while (!BB->succ_empty())
8253 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008254 // Add the true and fallthrough blocks as its successors.
8255 BB->addSuccessor(copy0MBB);
8256 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008257
Chris Lattner52600972009-09-02 05:57:00 +00008258 // copy0MBB:
8259 // %FalseValue = ...
8260 // # fallthrough to sinkMBB
8261 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008262
Chris Lattner52600972009-09-02 05:57:00 +00008263 // Update machine-CFG edges
8264 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008265
Chris Lattner52600972009-09-02 05:57:00 +00008266 // sinkMBB:
8267 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8268 // ...
8269 BB = sinkMBB;
8270 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8271 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8272 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8273
8274 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8275 return BB;
8276}
8277
8278
8279MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008280X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008281 MachineBasicBlock *BB,
8282 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008283 switch (MI->getOpcode()) {
8284 default: assert(false && "Unexpected instr type to insert");
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008285 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008286 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008287 case X86::CMOV_FR32:
8288 case X86::CMOV_FR64:
8289 case X86::CMOV_V4F32:
8290 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008291 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008292 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008293
Dale Johannesen849f2142007-07-03 00:53:03 +00008294 case X86::FP32_TO_INT16_IN_MEM:
8295 case X86::FP32_TO_INT32_IN_MEM:
8296 case X86::FP32_TO_INT64_IN_MEM:
8297 case X86::FP64_TO_INT16_IN_MEM:
8298 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008299 case X86::FP64_TO_INT64_IN_MEM:
8300 case X86::FP80_TO_INT16_IN_MEM:
8301 case X86::FP80_TO_INT32_IN_MEM:
8302 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008303 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8304 DebugLoc DL = MI->getDebugLoc();
8305
Evan Cheng60c07e12006-07-05 22:17:51 +00008306 // Change the floating point control register to use "round towards zero"
8307 // mode when truncating to an integer value.
8308 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008309 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008310 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008311
8312 // Load the old value of the high byte of the control word...
8313 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008314 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008315 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008316 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008317
8318 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008319 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008320 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008321
8322 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008323 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008324
8325 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008326 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008327 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008328
8329 // Get the X86 opcode to use.
8330 unsigned Opc;
8331 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008332 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008333 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8334 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8335 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8336 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8337 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8338 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008339 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8340 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8341 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008342 }
8343
8344 X86AddressMode AM;
8345 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008346 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008347 AM.BaseType = X86AddressMode::RegBase;
8348 AM.Base.Reg = Op.getReg();
8349 } else {
8350 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008351 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008352 }
8353 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008354 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008355 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008356 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008357 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008358 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008359 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008360 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008361 AM.GV = Op.getGlobal();
8362 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008363 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008364 }
Chris Lattner52600972009-09-02 05:57:00 +00008365 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008366 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008367
8368 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008369 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008370
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008371 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008372 return BB;
8373 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008374 // String/text processing lowering.
8375 case X86::PCMPISTRM128REG:
8376 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8377 case X86::PCMPISTRM128MEM:
8378 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8379 case X86::PCMPESTRM128REG:
8380 return EmitPCMP(MI, BB, 5, false /* in mem */);
8381 case X86::PCMPESTRM128MEM:
8382 return EmitPCMP(MI, BB, 5, true /* in mem */);
8383
8384 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008385 case X86::ATOMAND32:
8386 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008387 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008388 X86::LCMPXCHG32, X86::MOV32rr,
8389 X86::NOT32r, X86::EAX,
8390 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008391 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008392 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8393 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008394 X86::LCMPXCHG32, X86::MOV32rr,
8395 X86::NOT32r, X86::EAX,
8396 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008397 case X86::ATOMXOR32:
8398 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008399 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008400 X86::LCMPXCHG32, X86::MOV32rr,
8401 X86::NOT32r, X86::EAX,
8402 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008403 case X86::ATOMNAND32:
8404 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008405 X86::AND32ri, X86::MOV32rm,
8406 X86::LCMPXCHG32, X86::MOV32rr,
8407 X86::NOT32r, X86::EAX,
8408 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008409 case X86::ATOMMIN32:
8410 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8411 case X86::ATOMMAX32:
8412 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8413 case X86::ATOMUMIN32:
8414 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8415 case X86::ATOMUMAX32:
8416 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008417
8418 case X86::ATOMAND16:
8419 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8420 X86::AND16ri, X86::MOV16rm,
8421 X86::LCMPXCHG16, X86::MOV16rr,
8422 X86::NOT16r, X86::AX,
8423 X86::GR16RegisterClass);
8424 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008425 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008426 X86::OR16ri, X86::MOV16rm,
8427 X86::LCMPXCHG16, X86::MOV16rr,
8428 X86::NOT16r, X86::AX,
8429 X86::GR16RegisterClass);
8430 case X86::ATOMXOR16:
8431 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8432 X86::XOR16ri, X86::MOV16rm,
8433 X86::LCMPXCHG16, X86::MOV16rr,
8434 X86::NOT16r, X86::AX,
8435 X86::GR16RegisterClass);
8436 case X86::ATOMNAND16:
8437 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8438 X86::AND16ri, X86::MOV16rm,
8439 X86::LCMPXCHG16, X86::MOV16rr,
8440 X86::NOT16r, X86::AX,
8441 X86::GR16RegisterClass, true);
8442 case X86::ATOMMIN16:
8443 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8444 case X86::ATOMMAX16:
8445 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8446 case X86::ATOMUMIN16:
8447 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8448 case X86::ATOMUMAX16:
8449 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8450
8451 case X86::ATOMAND8:
8452 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8453 X86::AND8ri, X86::MOV8rm,
8454 X86::LCMPXCHG8, X86::MOV8rr,
8455 X86::NOT8r, X86::AL,
8456 X86::GR8RegisterClass);
8457 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008458 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008459 X86::OR8ri, X86::MOV8rm,
8460 X86::LCMPXCHG8, X86::MOV8rr,
8461 X86::NOT8r, X86::AL,
8462 X86::GR8RegisterClass);
8463 case X86::ATOMXOR8:
8464 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8465 X86::XOR8ri, X86::MOV8rm,
8466 X86::LCMPXCHG8, X86::MOV8rr,
8467 X86::NOT8r, X86::AL,
8468 X86::GR8RegisterClass);
8469 case X86::ATOMNAND8:
8470 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8471 X86::AND8ri, X86::MOV8rm,
8472 X86::LCMPXCHG8, X86::MOV8rr,
8473 X86::NOT8r, X86::AL,
8474 X86::GR8RegisterClass, true);
8475 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008476 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008477 case X86::ATOMAND64:
8478 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008479 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008480 X86::LCMPXCHG64, X86::MOV64rr,
8481 X86::NOT64r, X86::RAX,
8482 X86::GR64RegisterClass);
8483 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008484 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8485 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008486 X86::LCMPXCHG64, X86::MOV64rr,
8487 X86::NOT64r, X86::RAX,
8488 X86::GR64RegisterClass);
8489 case X86::ATOMXOR64:
8490 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008491 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008492 X86::LCMPXCHG64, X86::MOV64rr,
8493 X86::NOT64r, X86::RAX,
8494 X86::GR64RegisterClass);
8495 case X86::ATOMNAND64:
8496 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8497 X86::AND64ri32, X86::MOV64rm,
8498 X86::LCMPXCHG64, X86::MOV64rr,
8499 X86::NOT64r, X86::RAX,
8500 X86::GR64RegisterClass, true);
8501 case X86::ATOMMIN64:
8502 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8503 case X86::ATOMMAX64:
8504 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8505 case X86::ATOMUMIN64:
8506 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8507 case X86::ATOMUMAX64:
8508 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008509
8510 // This group does 64-bit operations on a 32-bit host.
8511 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008512 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008513 X86::AND32rr, X86::AND32rr,
8514 X86::AND32ri, X86::AND32ri,
8515 false);
8516 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008517 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008518 X86::OR32rr, X86::OR32rr,
8519 X86::OR32ri, X86::OR32ri,
8520 false);
8521 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008522 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008523 X86::XOR32rr, X86::XOR32rr,
8524 X86::XOR32ri, X86::XOR32ri,
8525 false);
8526 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008527 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008528 X86::AND32rr, X86::AND32rr,
8529 X86::AND32ri, X86::AND32ri,
8530 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008531 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008532 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008533 X86::ADD32rr, X86::ADC32rr,
8534 X86::ADD32ri, X86::ADC32ri,
8535 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008536 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008537 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008538 X86::SUB32rr, X86::SBB32rr,
8539 X86::SUB32ri, X86::SBB32ri,
8540 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008541 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008542 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008543 X86::MOV32rr, X86::MOV32rr,
8544 X86::MOV32ri, X86::MOV32ri,
8545 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008546 case X86::VASTART_SAVE_XMM_REGS:
8547 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008548 }
8549}
8550
8551//===----------------------------------------------------------------------===//
8552// X86 Optimization Hooks
8553//===----------------------------------------------------------------------===//
8554
Dan Gohman475871a2008-07-27 21:46:04 +00008555void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008556 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008557 APInt &KnownZero,
8558 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008559 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008560 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008561 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008562 assert((Opc >= ISD::BUILTIN_OP_END ||
8563 Opc == ISD::INTRINSIC_WO_CHAIN ||
8564 Opc == ISD::INTRINSIC_W_CHAIN ||
8565 Opc == ISD::INTRINSIC_VOID) &&
8566 "Should use MaskedValueIsZero if you don't know whether Op"
8567 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008568
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008569 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008570 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008571 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008572 case X86ISD::ADD:
8573 case X86ISD::SUB:
8574 case X86ISD::SMUL:
8575 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008576 case X86ISD::INC:
8577 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008578 case X86ISD::OR:
8579 case X86ISD::XOR:
8580 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008581 // These nodes' second result is a boolean.
8582 if (Op.getResNo() == 0)
8583 break;
8584 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008585 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008586 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8587 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008588 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008589 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008590}
Chris Lattner259e97c2006-01-31 19:43:35 +00008591
Evan Cheng206ee9d2006-07-07 08:33:52 +00008592/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008593/// node is a GlobalAddress + offset.
8594bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8595 GlobalValue* &GA, int64_t &Offset) const{
8596 if (N->getOpcode() == X86ISD::Wrapper) {
8597 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008598 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008599 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008600 return true;
8601 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008602 }
Evan Chengad4196b2008-05-12 19:56:52 +00008603 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008604}
8605
Nate Begeman9008ca62009-04-27 18:41:29 +00008606static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008607 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008608 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008609 SelectionDAG &DAG, MachineFrameInfo *MFI,
8610 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008611 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008612 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008613 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008614 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008615 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008616 return false;
8617 continue;
8618 }
8619
Dan Gohman475871a2008-07-27 21:46:04 +00008620 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008621 if (!Elt.getNode() ||
8622 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008623 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008624 if (!LDBase) {
8625 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008626 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008627 LDBase = cast<LoadSDNode>(Elt.getNode());
8628 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008629 continue;
8630 }
8631 if (Elt.getOpcode() == ISD::UNDEF)
8632 continue;
8633
Nate Begemanabc01992009-06-05 21:37:30 +00008634 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008635 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008636 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008637 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008638 }
8639 return true;
8640}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008641
8642/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8643/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8644/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008645/// order. In the case of v2i64, it will see if it can rewrite the
8646/// shuffle to be an appropriate build vector so it can take advantage of
8647// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008648static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008649 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008650 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008651 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008652 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008653 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8654 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008655
Eli Friedman7a5e5552009-06-07 06:52:44 +00008656 if (VT.getSizeInBits() != 128)
8657 return SDValue();
8658
Mon P Wang1e955802009-04-03 02:43:30 +00008659 // Try to combine a vector_shuffle into a 128-bit load.
8660 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008661 LoadSDNode *LD = NULL;
8662 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008663 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008664 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008665 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008666
Eli Friedman7a5e5552009-06-07 06:52:44 +00008667 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008668 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008669 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8670 LD->getSrcValue(), LD->getSrcValueOffset(),
8671 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00008672 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008673 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00008674 LD->isVolatile(), LD->getAlignment());
8675 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008676 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008677 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8678 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008679 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8680 }
8681 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008682}
Evan Chengd880b972008-05-09 21:53:03 +00008683
Chris Lattner83e6c992006-10-04 06:57:07 +00008684/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008685static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008686 const X86Subtarget *Subtarget) {
8687 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008688 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008689 // Get the LHS/RHS of the select.
8690 SDValue LHS = N->getOperand(1);
8691 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008692
Dan Gohman670e5392009-09-21 18:03:22 +00008693 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
8694 // instructions have the peculiarity that if either operand is a NaN,
8695 // they chose what we call the RHS operand (and as such are not symmetric).
8696 // It happens that this matches the semantics of the common C idiom
8697 // x<y?x:y and related forms, so we can recognize these cases.
Chris Lattner83e6c992006-10-04 06:57:07 +00008698 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008699 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008700 Cond.getOpcode() == ISD::SETCC) {
8701 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008702
Chris Lattner47b4ce82009-03-11 05:48:52 +00008703 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008704 // Check for x CC y ? x : y.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008705 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8706 switch (CC) {
8707 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008708 case ISD::SETULT:
8709 // This can be a min if we can prove that at least one of the operands
8710 // is not a nan.
8711 if (!FiniteOnlyFPMath()) {
8712 if (DAG.isKnownNeverNaN(RHS)) {
8713 // Put the potential NaN in the RHS so that SSE will preserve it.
8714 std::swap(LHS, RHS);
8715 } else if (!DAG.isKnownNeverNaN(LHS))
8716 break;
8717 }
8718 Opcode = X86ISD::FMIN;
8719 break;
8720 case ISD::SETOLE:
8721 // This can be a min if we can prove that at least one of the operands
8722 // is not a nan.
8723 if (!FiniteOnlyFPMath()) {
8724 if (DAG.isKnownNeverNaN(LHS)) {
8725 // Put the potential NaN in the RHS so that SSE will preserve it.
8726 std::swap(LHS, RHS);
8727 } else if (!DAG.isKnownNeverNaN(RHS))
8728 break;
8729 }
8730 Opcode = X86ISD::FMIN;
8731 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008732 case ISD::SETULE:
Dan Gohman670e5392009-09-21 18:03:22 +00008733 // This can be a min, but if either operand is a NaN we need it to
8734 // preserve the original LHS.
8735 std::swap(LHS, RHS);
8736 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008737 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008738 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008739 Opcode = X86ISD::FMIN;
8740 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008741
Dan Gohman670e5392009-09-21 18:03:22 +00008742 case ISD::SETOGE:
8743 // This can be a max if we can prove that at least one of the operands
8744 // is not a nan.
8745 if (!FiniteOnlyFPMath()) {
8746 if (DAG.isKnownNeverNaN(LHS)) {
8747 // Put the potential NaN in the RHS so that SSE will preserve it.
8748 std::swap(LHS, RHS);
8749 } else if (!DAG.isKnownNeverNaN(RHS))
8750 break;
8751 }
8752 Opcode = X86ISD::FMAX;
8753 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008754 case ISD::SETUGT:
Dan Gohman670e5392009-09-21 18:03:22 +00008755 // This can be a max if we can prove that at least one of the operands
8756 // is not a nan.
8757 if (!FiniteOnlyFPMath()) {
8758 if (DAG.isKnownNeverNaN(RHS)) {
8759 // Put the potential NaN in the RHS so that SSE will preserve it.
8760 std::swap(LHS, RHS);
8761 } else if (!DAG.isKnownNeverNaN(LHS))
8762 break;
8763 }
8764 Opcode = X86ISD::FMAX;
8765 break;
8766 case ISD::SETUGE:
8767 // This can be a max, but if either operand is a NaN we need it to
8768 // preserve the original LHS.
8769 std::swap(LHS, RHS);
8770 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008771 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008772 case ISD::SETGE:
8773 Opcode = X86ISD::FMAX;
8774 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008775 }
Dan Gohman670e5392009-09-21 18:03:22 +00008776 // Check for x CC y ? y : x -- a min/max with reversed arms.
Chris Lattner47b4ce82009-03-11 05:48:52 +00008777 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8778 switch (CC) {
8779 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008780 case ISD::SETOGE:
8781 // This can be a min if we can prove that at least one of the operands
8782 // is not a nan.
8783 if (!FiniteOnlyFPMath()) {
8784 if (DAG.isKnownNeverNaN(RHS)) {
8785 // Put the potential NaN in the RHS so that SSE will preserve it.
8786 std::swap(LHS, RHS);
8787 } else if (!DAG.isKnownNeverNaN(LHS))
8788 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008789 }
Dan Gohman670e5392009-09-21 18:03:22 +00008790 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008791 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008792 case ISD::SETUGT:
8793 // This can be a min if we can prove that at least one of the operands
8794 // is not a nan.
8795 if (!FiniteOnlyFPMath()) {
8796 if (DAG.isKnownNeverNaN(LHS)) {
8797 // Put the potential NaN in the RHS so that SSE will preserve it.
8798 std::swap(LHS, RHS);
8799 } else if (!DAG.isKnownNeverNaN(RHS))
8800 break;
8801 }
8802 Opcode = X86ISD::FMIN;
8803 break;
8804 case ISD::SETUGE:
8805 // This can be a min, but if either operand is a NaN we need it to
8806 // preserve the original LHS.
8807 std::swap(LHS, RHS);
8808 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008809 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008810 case ISD::SETGE:
8811 Opcode = X86ISD::FMIN;
8812 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008813
Dan Gohman670e5392009-09-21 18:03:22 +00008814 case ISD::SETULT:
8815 // This can be a max if we can prove that at least one of the operands
8816 // is not a nan.
8817 if (!FiniteOnlyFPMath()) {
8818 if (DAG.isKnownNeverNaN(LHS)) {
8819 // Put the potential NaN in the RHS so that SSE will preserve it.
8820 std::swap(LHS, RHS);
8821 } else if (!DAG.isKnownNeverNaN(RHS))
8822 break;
Dan Gohman8d44b282009-09-03 20:34:31 +00008823 }
Dan Gohman670e5392009-09-21 18:03:22 +00008824 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008825 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008826 case ISD::SETOLE:
8827 // This can be a max if we can prove that at least one of the operands
8828 // is not a nan.
8829 if (!FiniteOnlyFPMath()) {
8830 if (DAG.isKnownNeverNaN(RHS)) {
8831 // Put the potential NaN in the RHS so that SSE will preserve it.
8832 std::swap(LHS, RHS);
8833 } else if (!DAG.isKnownNeverNaN(LHS))
8834 break;
8835 }
8836 Opcode = X86ISD::FMAX;
8837 break;
8838 case ISD::SETULE:
8839 // This can be a max, but if either operand is a NaN we need it to
8840 // preserve the original LHS.
8841 std::swap(LHS, RHS);
8842 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008843 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008844 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008845 Opcode = X86ISD::FMAX;
8846 break;
8847 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008848 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008849
Chris Lattner47b4ce82009-03-11 05:48:52 +00008850 if (Opcode)
8851 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008852 }
Eric Christopherfd179292009-08-27 18:07:15 +00008853
Chris Lattnerd1980a52009-03-12 06:52:53 +00008854 // If this is a select between two integer constants, try to do some
8855 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008856 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8857 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008858 // Don't do this for crazy integer types.
8859 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8860 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008861 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008862 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00008863
Chris Lattnercee56e72009-03-13 05:53:31 +00008864 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008865 // Efficiently invertible.
8866 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8867 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8868 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8869 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008870 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008871 }
Eric Christopherfd179292009-08-27 18:07:15 +00008872
Chris Lattnerd1980a52009-03-12 06:52:53 +00008873 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008874 if (FalseC->getAPIntValue() == 0 &&
8875 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008876 if (NeedsCondInvert) // Invert the condition if needed.
8877 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8878 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008879
Chris Lattnerd1980a52009-03-12 06:52:53 +00008880 // Zero extend the condition if needed.
8881 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008882
Chris Lattnercee56e72009-03-13 05:53:31 +00008883 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008884 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008885 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008886 }
Eric Christopherfd179292009-08-27 18:07:15 +00008887
Chris Lattner97a29a52009-03-13 05:22:11 +00008888 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008889 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008890 if (NeedsCondInvert) // Invert the condition if needed.
8891 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8892 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008893
Chris Lattner97a29a52009-03-13 05:22:11 +00008894 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008895 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8896 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008897 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008898 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008899 }
Eric Christopherfd179292009-08-27 18:07:15 +00008900
Chris Lattnercee56e72009-03-13 05:53:31 +00008901 // Optimize cases that will turn into an LEA instruction. This requires
8902 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00008903 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00008904 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008905 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00008906
Chris Lattnercee56e72009-03-13 05:53:31 +00008907 bool isFastMultiplier = false;
8908 if (Diff < 10) {
8909 switch ((unsigned char)Diff) {
8910 default: break;
8911 case 1: // result = add base, cond
8912 case 2: // result = lea base( , cond*2)
8913 case 3: // result = lea base(cond, cond*2)
8914 case 4: // result = lea base( , cond*4)
8915 case 5: // result = lea base(cond, cond*4)
8916 case 8: // result = lea base( , cond*8)
8917 case 9: // result = lea base(cond, cond*8)
8918 isFastMultiplier = true;
8919 break;
8920 }
8921 }
Eric Christopherfd179292009-08-27 18:07:15 +00008922
Chris Lattnercee56e72009-03-13 05:53:31 +00008923 if (isFastMultiplier) {
8924 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8925 if (NeedsCondInvert) // Invert the condition if needed.
8926 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8927 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008928
Chris Lattnercee56e72009-03-13 05:53:31 +00008929 // Zero extend the condition if needed.
8930 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8931 Cond);
8932 // Scale the condition by the difference.
8933 if (Diff != 1)
8934 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8935 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00008936
Chris Lattnercee56e72009-03-13 05:53:31 +00008937 // Add the base if non-zero.
8938 if (FalseC->getAPIntValue() != 0)
8939 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8940 SDValue(FalseC, 0));
8941 return Cond;
8942 }
Eric Christopherfd179292009-08-27 18:07:15 +00008943 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008944 }
8945 }
Eric Christopherfd179292009-08-27 18:07:15 +00008946
Dan Gohman475871a2008-07-27 21:46:04 +00008947 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008948}
8949
Chris Lattnerd1980a52009-03-12 06:52:53 +00008950/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8951static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8952 TargetLowering::DAGCombinerInfo &DCI) {
8953 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00008954
Chris Lattnerd1980a52009-03-12 06:52:53 +00008955 // If the flag operand isn't dead, don't touch this CMOV.
8956 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8957 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00008958
Chris Lattnerd1980a52009-03-12 06:52:53 +00008959 // If this is a select between two integer constants, try to do some
8960 // optimizations. Note that the operands are ordered the opposite of SELECT
8961 // operands.
8962 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8963 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8964 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8965 // larger than FalseC (the false value).
8966 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008967
Chris Lattnerd1980a52009-03-12 06:52:53 +00008968 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8969 CC = X86::GetOppositeBranchCondition(CC);
8970 std::swap(TrueC, FalseC);
8971 }
Eric Christopherfd179292009-08-27 18:07:15 +00008972
Chris Lattnerd1980a52009-03-12 06:52:53 +00008973 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008974 // This is efficient for any integer data type (including i8/i16) and
8975 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008976 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8977 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008978 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8979 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008980
Chris Lattnerd1980a52009-03-12 06:52:53 +00008981 // Zero extend the condition if needed.
8982 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008983
Chris Lattnerd1980a52009-03-12 06:52:53 +00008984 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8985 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00008986 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00008987 if (N->getNumValues() == 2) // Dead flag value?
8988 return DCI.CombineTo(N, Cond, SDValue());
8989 return Cond;
8990 }
Eric Christopherfd179292009-08-27 18:07:15 +00008991
Chris Lattnercee56e72009-03-13 05:53:31 +00008992 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8993 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008994 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8995 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00008996 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8997 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00008998
Chris Lattner97a29a52009-03-13 05:22:11 +00008999 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009000 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9001 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009002 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9003 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009004
Chris Lattner97a29a52009-03-13 05:22:11 +00009005 if (N->getNumValues() == 2) // Dead flag value?
9006 return DCI.CombineTo(N, Cond, SDValue());
9007 return Cond;
9008 }
Eric Christopherfd179292009-08-27 18:07:15 +00009009
Chris Lattnercee56e72009-03-13 05:53:31 +00009010 // Optimize cases that will turn into an LEA instruction. This requires
9011 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009012 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009013 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009014 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009015
Chris Lattnercee56e72009-03-13 05:53:31 +00009016 bool isFastMultiplier = false;
9017 if (Diff < 10) {
9018 switch ((unsigned char)Diff) {
9019 default: break;
9020 case 1: // result = add base, cond
9021 case 2: // result = lea base( , cond*2)
9022 case 3: // result = lea base(cond, cond*2)
9023 case 4: // result = lea base( , cond*4)
9024 case 5: // result = lea base(cond, cond*4)
9025 case 8: // result = lea base( , cond*8)
9026 case 9: // result = lea base(cond, cond*8)
9027 isFastMultiplier = true;
9028 break;
9029 }
9030 }
Eric Christopherfd179292009-08-27 18:07:15 +00009031
Chris Lattnercee56e72009-03-13 05:53:31 +00009032 if (isFastMultiplier) {
9033 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9034 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009035 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9036 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009037 // Zero extend the condition if needed.
9038 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9039 Cond);
9040 // Scale the condition by the difference.
9041 if (Diff != 1)
9042 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9043 DAG.getConstant(Diff, Cond.getValueType()));
9044
9045 // Add the base if non-zero.
9046 if (FalseC->getAPIntValue() != 0)
9047 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9048 SDValue(FalseC, 0));
9049 if (N->getNumValues() == 2) // Dead flag value?
9050 return DCI.CombineTo(N, Cond, SDValue());
9051 return Cond;
9052 }
Eric Christopherfd179292009-08-27 18:07:15 +00009053 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009054 }
9055 }
9056 return SDValue();
9057}
9058
9059
Evan Cheng0b0cd912009-03-28 05:57:29 +00009060/// PerformMulCombine - Optimize a single multiply with constant into two
9061/// in order to implement it with two cheaper instructions, e.g.
9062/// LEA + SHL, LEA + LEA.
9063static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9064 TargetLowering::DAGCombinerInfo &DCI) {
9065 if (DAG.getMachineFunction().
9066 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
9067 return SDValue();
9068
9069 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9070 return SDValue();
9071
Owen Andersone50ed302009-08-10 22:56:29 +00009072 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009073 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009074 return SDValue();
9075
9076 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9077 if (!C)
9078 return SDValue();
9079 uint64_t MulAmt = C->getZExtValue();
9080 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9081 return SDValue();
9082
9083 uint64_t MulAmt1 = 0;
9084 uint64_t MulAmt2 = 0;
9085 if ((MulAmt % 9) == 0) {
9086 MulAmt1 = 9;
9087 MulAmt2 = MulAmt / 9;
9088 } else if ((MulAmt % 5) == 0) {
9089 MulAmt1 = 5;
9090 MulAmt2 = MulAmt / 5;
9091 } else if ((MulAmt % 3) == 0) {
9092 MulAmt1 = 3;
9093 MulAmt2 = MulAmt / 3;
9094 }
9095 if (MulAmt2 &&
9096 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9097 DebugLoc DL = N->getDebugLoc();
9098
9099 if (isPowerOf2_64(MulAmt2) &&
9100 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9101 // If second multiplifer is pow2, issue it first. We want the multiply by
9102 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9103 // is an add.
9104 std::swap(MulAmt1, MulAmt2);
9105
9106 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009107 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009108 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009109 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009110 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009111 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009112 DAG.getConstant(MulAmt1, VT));
9113
Eric Christopherfd179292009-08-27 18:07:15 +00009114 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009115 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009116 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009117 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009118 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009119 DAG.getConstant(MulAmt2, VT));
9120
9121 // Do not add new nodes to DAG combiner worklist.
9122 DCI.CombineTo(N, NewMul, false);
9123 }
9124 return SDValue();
9125}
9126
Evan Chengad9c0a32009-12-15 00:53:42 +00009127static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9128 SDValue N0 = N->getOperand(0);
9129 SDValue N1 = N->getOperand(1);
9130 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9131 EVT VT = N0.getValueType();
9132
9133 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9134 // since the result of setcc_c is all zero's or all ones.
9135 if (N1C && N0.getOpcode() == ISD::AND &&
9136 N0.getOperand(1).getOpcode() == ISD::Constant) {
9137 SDValue N00 = N0.getOperand(0);
9138 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9139 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9140 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9141 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9142 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9143 APInt ShAmt = N1C->getAPIntValue();
9144 Mask = Mask.shl(ShAmt);
9145 if (Mask != 0)
9146 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9147 N00, DAG.getConstant(Mask, VT));
9148 }
9149 }
9150
9151 return SDValue();
9152}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009153
Nate Begeman740ab032009-01-26 00:52:55 +00009154/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9155/// when possible.
9156static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9157 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009158 EVT VT = N->getValueType(0);
9159 if (!VT.isVector() && VT.isInteger() &&
9160 N->getOpcode() == ISD::SHL)
9161 return PerformSHLCombine(N, DAG);
9162
Nate Begeman740ab032009-01-26 00:52:55 +00009163 // On X86 with SSE2 support, we can transform this to a vector shift if
9164 // all elements are shifted by the same amount. We can't do this in legalize
9165 // because the a constant vector is typically transformed to a constant pool
9166 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009167 if (!Subtarget->hasSSE2())
9168 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009169
Owen Anderson825b72b2009-08-11 20:47:22 +00009170 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009171 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009172
Mon P Wang3becd092009-01-28 08:12:05 +00009173 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009174 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009175 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009176 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009177 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9178 unsigned NumElts = VT.getVectorNumElements();
9179 unsigned i = 0;
9180 for (; i != NumElts; ++i) {
9181 SDValue Arg = ShAmtOp.getOperand(i);
9182 if (Arg.getOpcode() == ISD::UNDEF) continue;
9183 BaseShAmt = Arg;
9184 break;
9185 }
9186 for (; i != NumElts; ++i) {
9187 SDValue Arg = ShAmtOp.getOperand(i);
9188 if (Arg.getOpcode() == ISD::UNDEF) continue;
9189 if (Arg != BaseShAmt) {
9190 return SDValue();
9191 }
9192 }
9193 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009194 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009195 SDValue InVec = ShAmtOp.getOperand(0);
9196 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9197 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9198 unsigned i = 0;
9199 for (; i != NumElts; ++i) {
9200 SDValue Arg = InVec.getOperand(i);
9201 if (Arg.getOpcode() == ISD::UNDEF) continue;
9202 BaseShAmt = Arg;
9203 break;
9204 }
9205 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9206 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
9207 unsigned SplatIdx = cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
9208 if (C->getZExtValue() == SplatIdx)
9209 BaseShAmt = InVec.getOperand(1);
9210 }
9211 }
9212 if (BaseShAmt.getNode() == 0)
9213 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9214 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009215 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009216 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009217
Mon P Wangefa42202009-09-03 19:56:25 +00009218 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009219 if (EltVT.bitsGT(MVT::i32))
9220 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9221 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009222 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009223
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009224 // The shift amount is identical so we can do a vector shift.
9225 SDValue ValOp = N->getOperand(0);
9226 switch (N->getOpcode()) {
9227 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009228 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009229 break;
9230 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009231 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009232 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009233 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009234 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009235 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009236 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009237 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009238 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009239 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009240 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009241 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009242 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009243 break;
9244 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009245 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009246 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009247 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009248 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009249 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009250 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009251 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009252 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009253 break;
9254 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009255 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009256 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009257 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009258 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009259 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009260 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009261 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009262 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009263 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009264 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009265 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009266 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009267 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009268 }
9269 return SDValue();
9270}
9271
Evan Cheng760d1942010-01-04 21:22:48 +00009272static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9273 const X86Subtarget *Subtarget) {
9274 EVT VT = N->getValueType(0);
9275 if (VT != MVT::i64 || !Subtarget->is64Bit())
9276 return SDValue();
9277
9278 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9279 SDValue N0 = N->getOperand(0);
9280 SDValue N1 = N->getOperand(1);
9281 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9282 std::swap(N0, N1);
9283 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9284 return SDValue();
9285
9286 SDValue ShAmt0 = N0.getOperand(1);
9287 if (ShAmt0.getValueType() != MVT::i8)
9288 return SDValue();
9289 SDValue ShAmt1 = N1.getOperand(1);
9290 if (ShAmt1.getValueType() != MVT::i8)
9291 return SDValue();
9292 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9293 ShAmt0 = ShAmt0.getOperand(0);
9294 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9295 ShAmt1 = ShAmt1.getOperand(0);
9296
9297 DebugLoc DL = N->getDebugLoc();
9298 unsigned Opc = X86ISD::SHLD;
9299 SDValue Op0 = N0.getOperand(0);
9300 SDValue Op1 = N1.getOperand(0);
9301 if (ShAmt0.getOpcode() == ISD::SUB) {
9302 Opc = X86ISD::SHRD;
9303 std::swap(Op0, Op1);
9304 std::swap(ShAmt0, ShAmt1);
9305 }
9306
9307 if (ShAmt1.getOpcode() == ISD::SUB) {
9308 SDValue Sum = ShAmt1.getOperand(0);
9309 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9310 if (SumC->getSExtValue() == 64 &&
9311 ShAmt1.getOperand(1) == ShAmt0)
9312 return DAG.getNode(Opc, DL, VT,
9313 Op0, Op1,
9314 DAG.getNode(ISD::TRUNCATE, DL,
9315 MVT::i8, ShAmt0));
9316 }
9317 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9318 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9319 if (ShAmt0C &&
9320 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9321 return DAG.getNode(Opc, DL, VT,
9322 N0.getOperand(0), N1.getOperand(0),
9323 DAG.getNode(ISD::TRUNCATE, DL,
9324 MVT::i8, ShAmt0));
9325 }
9326
9327 return SDValue();
9328}
9329
Chris Lattner149a4e52008-02-22 02:09:43 +00009330/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009331static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009332 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009333 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9334 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009335 // A preferable solution to the general problem is to figure out the right
9336 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009337
9338 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009339 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009340 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009341 if (VT.getSizeInBits() != 64)
9342 return SDValue();
9343
Devang Patel578efa92009-06-05 21:57:13 +00009344 const Function *F = DAG.getMachineFunction().getFunction();
9345 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009346 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009347 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009348 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009349 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009350 isa<LoadSDNode>(St->getValue()) &&
9351 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9352 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009353 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009354 LoadSDNode *Ld = 0;
9355 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009356 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009357 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009358 // Must be a store of a load. We currently handle two cases: the load
9359 // is a direct child, and it's under an intervening TokenFactor. It is
9360 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009361 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009362 Ld = cast<LoadSDNode>(St->getChain());
9363 else if (St->getValue().hasOneUse() &&
9364 ChainVal->getOpcode() == ISD::TokenFactor) {
9365 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009366 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009367 TokenFactorIndex = i;
9368 Ld = cast<LoadSDNode>(St->getValue());
9369 } else
9370 Ops.push_back(ChainVal->getOperand(i));
9371 }
9372 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009373
Evan Cheng536e6672009-03-12 05:59:15 +00009374 if (!Ld || !ISD::isNormalLoad(Ld))
9375 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009376
Evan Cheng536e6672009-03-12 05:59:15 +00009377 // If this is not the MMX case, i.e. we are just turning i64 load/store
9378 // into f64 load/store, avoid the transformation if there are multiple
9379 // uses of the loaded value.
9380 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9381 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009382
Evan Cheng536e6672009-03-12 05:59:15 +00009383 DebugLoc LdDL = Ld->getDebugLoc();
9384 DebugLoc StDL = N->getDebugLoc();
9385 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9386 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9387 // pair instead.
9388 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009389 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009390 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9391 Ld->getBasePtr(), Ld->getSrcValue(),
9392 Ld->getSrcValueOffset(), Ld->isVolatile(),
9393 Ld->getAlignment());
9394 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009395 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009396 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009398 Ops.size());
9399 }
Evan Cheng536e6672009-03-12 05:59:15 +00009400 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009401 St->getSrcValue(), St->getSrcValueOffset(),
9402 St->isVolatile(), St->getAlignment());
9403 }
Evan Cheng536e6672009-03-12 05:59:15 +00009404
9405 // Otherwise, lower to two pairs of 32-bit loads / stores.
9406 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009407 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9408 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009409
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009411 Ld->getSrcValue(), Ld->getSrcValueOffset(),
9412 Ld->isVolatile(), Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009414 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
9415 Ld->isVolatile(),
9416 MinAlign(Ld->getAlignment(), 4));
9417
9418 SDValue NewChain = LoLd.getValue(1);
9419 if (TokenFactorIndex != -1) {
9420 Ops.push_back(LoLd);
9421 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009422 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009423 Ops.size());
9424 }
9425
9426 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009427 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9428 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009429
9430 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9431 St->getSrcValue(), St->getSrcValueOffset(),
9432 St->isVolatile(), St->getAlignment());
9433 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9434 St->getSrcValue(),
9435 St->getSrcValueOffset() + 4,
9436 St->isVolatile(),
9437 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009439 }
Dan Gohman475871a2008-07-27 21:46:04 +00009440 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009441}
9442
Chris Lattner6cf73262008-01-25 06:14:17 +00009443/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9444/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009445static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009446 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9447 // F[X]OR(0.0, x) -> x
9448 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009449 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9450 if (C->getValueAPF().isPosZero())
9451 return N->getOperand(1);
9452 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9453 if (C->getValueAPF().isPosZero())
9454 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009455 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009456}
9457
9458/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009459static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009460 // FAND(0.0, x) -> 0.0
9461 // FAND(x, 0.0) -> 0.0
9462 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9463 if (C->getValueAPF().isPosZero())
9464 return N->getOperand(0);
9465 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9466 if (C->getValueAPF().isPosZero())
9467 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009468 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009469}
9470
Dan Gohmane5af2d32009-01-29 01:59:02 +00009471static SDValue PerformBTCombine(SDNode *N,
9472 SelectionDAG &DAG,
9473 TargetLowering::DAGCombinerInfo &DCI) {
9474 // BT ignores high bits in the bit index operand.
9475 SDValue Op1 = N->getOperand(1);
9476 if (Op1.hasOneUse()) {
9477 unsigned BitWidth = Op1.getValueSizeInBits();
9478 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9479 APInt KnownZero, KnownOne;
9480 TargetLowering::TargetLoweringOpt TLO(DAG);
9481 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9482 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9483 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9484 DCI.CommitTargetLoweringOpt(TLO);
9485 }
9486 return SDValue();
9487}
Chris Lattner83e6c992006-10-04 06:57:07 +00009488
Eli Friedman7a5e5552009-06-07 06:52:44 +00009489static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9490 SDValue Op = N->getOperand(0);
9491 if (Op.getOpcode() == ISD::BIT_CONVERT)
9492 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009493 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009494 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009495 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009496 OpVT.getVectorElementType().getSizeInBits()) {
9497 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9498 }
9499 return SDValue();
9500}
9501
Owen Anderson99177002009-06-29 18:04:45 +00009502// On X86 and X86-64, atomic operations are lowered to locked instructions.
9503// Locked instructions, in turn, have implicit fence semantics (all memory
9504// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009505// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009506// fence-atomic-fence.
9507static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9508 SDValue atomic = N->getOperand(0);
9509 switch (atomic.getOpcode()) {
9510 case ISD::ATOMIC_CMP_SWAP:
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 break;
9523 default:
9524 return SDValue();
9525 }
Eric Christopherfd179292009-08-27 18:07:15 +00009526
Owen Anderson99177002009-06-29 18:04:45 +00009527 SDValue fence = atomic.getOperand(0);
9528 if (fence.getOpcode() != ISD::MEMBARRIER)
9529 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009530
Owen Anderson99177002009-06-29 18:04:45 +00009531 switch (atomic.getOpcode()) {
9532 case ISD::ATOMIC_CMP_SWAP:
9533 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9534 atomic.getOperand(1), atomic.getOperand(2),
9535 atomic.getOperand(3));
9536 case ISD::ATOMIC_SWAP:
9537 case ISD::ATOMIC_LOAD_ADD:
9538 case ISD::ATOMIC_LOAD_SUB:
9539 case ISD::ATOMIC_LOAD_AND:
9540 case ISD::ATOMIC_LOAD_OR:
9541 case ISD::ATOMIC_LOAD_XOR:
9542 case ISD::ATOMIC_LOAD_NAND:
9543 case ISD::ATOMIC_LOAD_MIN:
9544 case ISD::ATOMIC_LOAD_MAX:
9545 case ISD::ATOMIC_LOAD_UMIN:
9546 case ISD::ATOMIC_LOAD_UMAX:
9547 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9548 atomic.getOperand(1), atomic.getOperand(2));
9549 default:
9550 return SDValue();
9551 }
9552}
9553
Evan Cheng2e489c42009-12-16 00:53:11 +00009554static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9555 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9556 // (and (i32 x86isd::setcc_carry), 1)
9557 // This eliminates the zext. This transformation is necessary because
9558 // ISD::SETCC is always legalized to i8.
9559 DebugLoc dl = N->getDebugLoc();
9560 SDValue N0 = N->getOperand(0);
9561 EVT VT = N->getValueType(0);
9562 if (N0.getOpcode() == ISD::AND &&
9563 N0.hasOneUse() &&
9564 N0.getOperand(0).hasOneUse()) {
9565 SDValue N00 = N0.getOperand(0);
9566 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9567 return SDValue();
9568 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9569 if (!C || C->getZExtValue() != 1)
9570 return SDValue();
9571 return DAG.getNode(ISD::AND, dl, VT,
9572 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9573 N00.getOperand(0), N00.getOperand(1)),
9574 DAG.getConstant(1, VT));
9575 }
9576
9577 return SDValue();
9578}
9579
Dan Gohman475871a2008-07-27 21:46:04 +00009580SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009581 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009582 SelectionDAG &DAG = DCI.DAG;
9583 switch (N->getOpcode()) {
9584 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009585 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009586 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009587 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009588 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009589 case ISD::SHL:
9590 case ISD::SRA:
9591 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009592 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009593 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009594 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009595 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9596 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009597 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009598 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009599 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009600 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009601 }
9602
Dan Gohman475871a2008-07-27 21:46:04 +00009603 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009604}
9605
Evan Cheng60c07e12006-07-05 22:17:51 +00009606//===----------------------------------------------------------------------===//
9607// X86 Inline Assembly Support
9608//===----------------------------------------------------------------------===//
9609
Chris Lattnerb8105652009-07-20 17:51:36 +00009610static bool LowerToBSwap(CallInst *CI) {
9611 // FIXME: this should verify that we are targetting a 486 or better. If not,
9612 // we will turn this bswap into something that will be lowered to logical ops
9613 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9614 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009615
Chris Lattnerb8105652009-07-20 17:51:36 +00009616 // Verify this is a simple bswap.
9617 if (CI->getNumOperands() != 2 ||
9618 CI->getType() != CI->getOperand(1)->getType() ||
9619 !CI->getType()->isInteger())
9620 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009621
Chris Lattnerb8105652009-07-20 17:51:36 +00009622 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9623 if (!Ty || Ty->getBitWidth() % 16 != 0)
9624 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009625
Chris Lattnerb8105652009-07-20 17:51:36 +00009626 // Okay, we can do this xform, do so now.
9627 const Type *Tys[] = { Ty };
9628 Module *M = CI->getParent()->getParent()->getParent();
9629 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009630
Chris Lattnerb8105652009-07-20 17:51:36 +00009631 Value *Op = CI->getOperand(1);
9632 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009633
Chris Lattnerb8105652009-07-20 17:51:36 +00009634 CI->replaceAllUsesWith(Op);
9635 CI->eraseFromParent();
9636 return true;
9637}
9638
9639bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9640 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9641 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9642
9643 std::string AsmStr = IA->getAsmString();
9644
9645 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009646 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009647 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9648
9649 switch (AsmPieces.size()) {
9650 default: return false;
9651 case 1:
9652 AsmStr = AsmPieces[0];
9653 AsmPieces.clear();
9654 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9655
9656 // bswap $0
9657 if (AsmPieces.size() == 2 &&
9658 (AsmPieces[0] == "bswap" ||
9659 AsmPieces[0] == "bswapq" ||
9660 AsmPieces[0] == "bswapl") &&
9661 (AsmPieces[1] == "$0" ||
9662 AsmPieces[1] == "${0:q}")) {
9663 // No need to check constraints, nothing other than the equivalent of
9664 // "=r,0" would be valid here.
9665 return LowerToBSwap(CI);
9666 }
9667 // rorw $$8, ${0:w} --> llvm.bswap.i16
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009668 if (CI->getType()->isInteger(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009669 AsmPieces.size() == 3 &&
9670 AsmPieces[0] == "rorw" &&
9671 AsmPieces[1] == "$$8," &&
9672 AsmPieces[2] == "${0:w}" &&
9673 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
9674 return LowerToBSwap(CI);
9675 }
9676 break;
9677 case 3:
Benjamin Kramer11acaa32010-01-05 20:07:06 +00009678 if (CI->getType()->isInteger(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009679 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009680 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9681 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9682 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009683 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009684 SplitString(AsmPieces[0], Words, " \t");
9685 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9686 Words.clear();
9687 SplitString(AsmPieces[1], Words, " \t");
9688 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9689 Words.clear();
9690 SplitString(AsmPieces[2], Words, " \t,");
9691 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9692 Words[2] == "%edx") {
9693 return LowerToBSwap(CI);
9694 }
9695 }
9696 }
9697 }
9698 break;
9699 }
9700 return false;
9701}
9702
9703
9704
Chris Lattnerf4dff842006-07-11 02:54:03 +00009705/// getConstraintType - Given a constraint letter, return the type of
9706/// constraint it is for this target.
9707X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009708X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9709 if (Constraint.size() == 1) {
9710 switch (Constraint[0]) {
9711 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009712 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009713 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009714 case 'r':
9715 case 'R':
9716 case 'l':
9717 case 'q':
9718 case 'Q':
9719 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009720 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009721 case 'Y':
9722 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009723 case 'e':
9724 case 'Z':
9725 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009726 default:
9727 break;
9728 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009729 }
Chris Lattner4234f572007-03-25 02:14:49 +00009730 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009731}
9732
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009733/// LowerXConstraint - try to replace an X constraint, which matches anything,
9734/// with another that has more specific requirements based on the type of the
9735/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009736const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009737LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009738 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9739 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009740 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009741 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009742 return "Y";
9743 if (Subtarget->hasSSE1())
9744 return "x";
9745 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009746
Chris Lattner5e764232008-04-26 23:02:14 +00009747 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009748}
9749
Chris Lattner48884cd2007-08-25 00:47:38 +00009750/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9751/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009752void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009753 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009754 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009755 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009756 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009757 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009758
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009759 switch (Constraint) {
9760 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009761 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009763 if (C->getZExtValue() <= 31) {
9764 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009765 break;
9766 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009767 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009768 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009769 case 'J':
9770 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009771 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009772 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9773 break;
9774 }
9775 }
9776 return;
9777 case 'K':
9778 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009779 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009780 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9781 break;
9782 }
9783 }
9784 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009785 case 'N':
9786 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009787 if (C->getZExtValue() <= 255) {
9788 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009789 break;
9790 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009791 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009792 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009793 case 'e': {
9794 // 32-bit signed value
9795 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9796 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009797 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9798 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009799 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009800 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009801 break;
9802 }
9803 // FIXME gcc accepts some relocatable values here too, but only in certain
9804 // memory models; it's complicated.
9805 }
9806 return;
9807 }
9808 case 'Z': {
9809 // 32-bit unsigned value
9810 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9811 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009812 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9813 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009814 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9815 break;
9816 }
9817 }
9818 // FIXME gcc accepts some relocatable values here too, but only in certain
9819 // memory models; it's complicated.
9820 return;
9821 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009822 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009823 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009824 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009825 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009826 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009827 break;
9828 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009829
Chris Lattnerdc43a882007-05-03 16:52:29 +00009830 // If we are in non-pic codegen mode, we allow the address of a global (with
9831 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00009832 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009833 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00009834
Chris Lattner49921962009-05-08 18:23:14 +00009835 // Match either (GA), (GA+C), (GA+C1+C2), etc.
9836 while (1) {
9837 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
9838 Offset += GA->getOffset();
9839 break;
9840 } else if (Op.getOpcode() == ISD::ADD) {
9841 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9842 Offset += C->getZExtValue();
9843 Op = Op.getOperand(0);
9844 continue;
9845 }
9846 } else if (Op.getOpcode() == ISD::SUB) {
9847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
9848 Offset += -C->getZExtValue();
9849 Op = Op.getOperand(0);
9850 continue;
9851 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009852 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009853
Chris Lattner49921962009-05-08 18:23:14 +00009854 // Otherwise, this isn't something we can handle, reject it.
9855 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00009856 }
Eric Christopherfd179292009-08-27 18:07:15 +00009857
Chris Lattner36c25012009-07-10 07:34:39 +00009858 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009859 // If we require an extra load to get this address, as in PIC mode, we
9860 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00009861 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
9862 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00009863 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00009864
Dale Johannesen60b3ba02009-07-21 00:12:29 +00009865 if (hasMemory)
9866 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
9867 else
9868 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00009869 Result = Op;
9870 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009871 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009873
Gabor Greifba36cb52008-08-28 21:40:38 +00009874 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00009875 Ops.push_back(Result);
9876 return;
9877 }
Evan Chengda43bcf2008-09-24 00:05:32 +00009878 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
9879 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009880}
9881
Chris Lattner259e97c2006-01-31 19:43:35 +00009882std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00009883getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009884 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00009885 if (Constraint.size() == 1) {
9886 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00009887 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00009888 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00009889 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
9890 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009891 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009892 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
9893 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
9894 X86::R10D,X86::R11D,X86::R12D,
9895 X86::R13D,X86::R14D,X86::R15D,
9896 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009897 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009898 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
9899 X86::SI, X86::DI, X86::R8W,X86::R9W,
9900 X86::R10W,X86::R11W,X86::R12W,
9901 X86::R13W,X86::R14W,X86::R15W,
9902 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009903 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009904 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
9905 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
9906 X86::R10B,X86::R11B,X86::R12B,
9907 X86::R13B,X86::R14B,X86::R15B,
9908 X86::BPL, X86::SPL, 0);
9909
Owen Anderson825b72b2009-08-11 20:47:22 +00009910 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +00009911 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
9912 X86::RSI, X86::RDI, X86::R8, X86::R9,
9913 X86::R10, X86::R11, X86::R12,
9914 X86::R13, X86::R14, X86::R15,
9915 X86::RBP, X86::RSP, 0);
9916
9917 break;
9918 }
Eric Christopherfd179292009-08-27 18:07:15 +00009919 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00009920 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009921 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009922 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009923 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +00009924 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009925 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00009926 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009927 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +00009928 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
9929 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00009930 }
9931 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009932
Chris Lattner1efa40f2006-02-22 00:56:39 +00009933 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00009934}
Chris Lattnerf76d1802006-07-31 23:26:50 +00009935
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009936std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009937X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00009938 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009939 // First, see if this is a constraint that directly corresponds to an LLVM
9940 // register class.
9941 if (Constraint.size() == 1) {
9942 // GCC Constraint Letters
9943 switch (Constraint[0]) {
9944 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009945 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +00009946 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +00009947 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009948 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009949 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +00009950 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009951 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009952 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009953 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +00009954 case 'R': // LEGACY_REGS
9955 if (VT == MVT::i8)
9956 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
9957 if (VT == MVT::i16)
9958 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
9959 if (VT == MVT::i32 || !Subtarget->is64Bit())
9960 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
9961 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009962 case 'f': // FP Stack registers.
9963 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9964 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +00009965 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009966 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009967 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009968 return std::make_pair(0U, X86::RFP64RegisterClass);
9969 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009970 case 'y': // MMX_REGS if MMX allowed.
9971 if (!Subtarget->hasMMX()) break;
9972 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009973 case 'Y': // SSE_REGS if SSE2 allowed
9974 if (!Subtarget->hasSSE2()) break;
9975 // FALL THROUGH.
9976 case 'x': // SSE_REGS if SSE1 allowed
9977 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009978
Owen Anderson825b72b2009-08-11 20:47:22 +00009979 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009980 default: break;
9981 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009982 case MVT::f32:
9983 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009984 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00009985 case MVT::f64:
9986 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009987 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009988 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +00009989 case MVT::v16i8:
9990 case MVT::v8i16:
9991 case MVT::v4i32:
9992 case MVT::v2i64:
9993 case MVT::v4f32:
9994 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +00009995 return std::make_pair(0U, X86::VR128RegisterClass);
9996 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009997 break;
9998 }
9999 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010000
Chris Lattnerf76d1802006-07-31 23:26:50 +000010001 // Use the default implementation in TargetLowering to convert the register
10002 // constraint into a member of a register class.
10003 std::pair<unsigned, const TargetRegisterClass*> Res;
10004 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010005
10006 // Not found as a standard register?
10007 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010008 // Map st(0) -> st(7) -> ST0
10009 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10010 tolower(Constraint[1]) == 's' &&
10011 tolower(Constraint[2]) == 't' &&
10012 Constraint[3] == '(' &&
10013 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10014 Constraint[5] == ')' &&
10015 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010016
Chris Lattner56d77c72009-09-13 22:41:48 +000010017 Res.first = X86::ST0+Constraint[4]-'0';
10018 Res.second = X86::RFP80RegisterClass;
10019 return Res;
10020 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010021
Chris Lattner56d77c72009-09-13 22:41:48 +000010022 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010023 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010024 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010025 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010026 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010027 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010028
10029 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010030 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010031 Res.first = X86::EFLAGS;
10032 Res.second = X86::CCRRegisterClass;
10033 return Res;
10034 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010035
Dale Johannesen330169f2008-11-13 21:52:36 +000010036 // 'A' means EAX + EDX.
10037 if (Constraint == "A") {
10038 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010039 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010040 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010041 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010042 return Res;
10043 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010044
Chris Lattnerf76d1802006-07-31 23:26:50 +000010045 // Otherwise, check to see if this is a register class of the wrong value
10046 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10047 // turn into {ax},{dx}.
10048 if (Res.second->hasType(VT))
10049 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010050
Chris Lattnerf76d1802006-07-31 23:26:50 +000010051 // All of the single-register GCC register classes map their values onto
10052 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10053 // really want an 8-bit or 32-bit register, map to the appropriate register
10054 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010055 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010056 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010057 unsigned DestReg = 0;
10058 switch (Res.first) {
10059 default: break;
10060 case X86::AX: DestReg = X86::AL; break;
10061 case X86::DX: DestReg = X86::DL; break;
10062 case X86::CX: DestReg = X86::CL; break;
10063 case X86::BX: DestReg = X86::BL; break;
10064 }
10065 if (DestReg) {
10066 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010067 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010068 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010069 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010070 unsigned DestReg = 0;
10071 switch (Res.first) {
10072 default: break;
10073 case X86::AX: DestReg = X86::EAX; break;
10074 case X86::DX: DestReg = X86::EDX; break;
10075 case X86::CX: DestReg = X86::ECX; break;
10076 case X86::BX: DestReg = X86::EBX; break;
10077 case X86::SI: DestReg = X86::ESI; break;
10078 case X86::DI: DestReg = X86::EDI; break;
10079 case X86::BP: DestReg = X86::EBP; break;
10080 case X86::SP: DestReg = X86::ESP; break;
10081 }
10082 if (DestReg) {
10083 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010084 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010085 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010086 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010087 unsigned DestReg = 0;
10088 switch (Res.first) {
10089 default: break;
10090 case X86::AX: DestReg = X86::RAX; break;
10091 case X86::DX: DestReg = X86::RDX; break;
10092 case X86::CX: DestReg = X86::RCX; break;
10093 case X86::BX: DestReg = X86::RBX; break;
10094 case X86::SI: DestReg = X86::RSI; break;
10095 case X86::DI: DestReg = X86::RDI; break;
10096 case X86::BP: DestReg = X86::RBP; break;
10097 case X86::SP: DestReg = X86::RSP; break;
10098 }
10099 if (DestReg) {
10100 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010101 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010102 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010103 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010104 } else if (Res.second == X86::FR32RegisterClass ||
10105 Res.second == X86::FR64RegisterClass ||
10106 Res.second == X86::VR128RegisterClass) {
10107 // Handle references to XMM physical registers that got mapped into the
10108 // wrong class. This can happen with constraints like {xmm0} where the
10109 // target independent register mapper will just pick the first match it can
10110 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010111 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010112 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010113 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010114 Res.second = X86::FR64RegisterClass;
10115 else if (X86::VR128RegisterClass->hasType(VT))
10116 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010117 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010118
Chris Lattnerf76d1802006-07-31 23:26:50 +000010119 return Res;
10120}
Mon P Wang0c397192008-10-30 08:01:45 +000010121
10122//===----------------------------------------------------------------------===//
10123// X86 Widen vector type
10124//===----------------------------------------------------------------------===//
10125
10126/// getWidenVectorType: given a vector type, returns the type to widen
10127/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
Owen Anderson825b72b2009-08-11 20:47:22 +000010128/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +000010129/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +000010130/// scalarizing vs using the wider vector type.
10131
Owen Andersone50ed302009-08-10 22:56:29 +000010132EVT X86TargetLowering::getWidenVectorType(EVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +000010133 assert(VT.isVector());
10134 if (isTypeLegal(VT))
10135 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010136
Mon P Wang0c397192008-10-30 08:01:45 +000010137 // TODO: In computeRegisterProperty, we can compute the list of legal vector
10138 // type based on element type. This would speed up our search (though
10139 // it may not be worth it since the size of the list is relatively
10140 // small).
Owen Andersone50ed302009-08-10 22:56:29 +000010141 EVT EltVT = VT.getVectorElementType();
Mon P Wang0c397192008-10-30 08:01:45 +000010142 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +000010143
Mon P Wang0c397192008-10-30 08:01:45 +000010144 // On X86, it make sense to widen any vector wider than 1
10145 if (NElts <= 1)
Owen Anderson825b72b2009-08-11 20:47:22 +000010146 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +000010147
Owen Anderson825b72b2009-08-11 20:47:22 +000010148 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
10149 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
10150 EVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +000010151
10152 if (isTypeLegal(SVT) &&
10153 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +000010154 SVT.getVectorNumElements() > NElts)
10155 return SVT;
10156 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010157 return MVT::Other;
Mon P Wang0c397192008-10-30 08:01:45 +000010158}