blob: abc5d9bc1d5b8e7a7a72a75ea56fad4f122cc96b [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
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
Chris Lattner017ec352010-02-08 22:33:55 +000019#include "X86MCTargetExpr.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000040#include "llvm/MC/MCSectionMachO.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000043#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000044#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000045#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000046#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000047#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000048#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000049#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000050#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000052#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000054using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000055
Evan Chengb1712452010-01-27 06:25:16 +000056STATISTIC(NumTailCalls, "Number of tail calls");
57
Mon P Wang3c81d352008-11-23 04:37:22 +000058static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000059DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000060
Dan Gohman2f67df72009-09-03 17:18:51 +000061// Disable16Bit - 16-bit operations typically have a larger encoding than
62// corresponding 32-bit instructions, and 16-bit code is slow on some
63// processors. This is an experimental flag to disable 16-bit operations
64// (which forces them to be Legalized to 32-bit operations).
65static cl::opt<bool>
66Disable16Bit("disable-16bit", cl::Hidden,
67 cl::desc("Disable use of 16-bit instructions"));
68
Evan Cheng10e86422008-04-25 19:11:04 +000069// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000070static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000071 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000072
Bill Wendlingec041eb2010-03-12 19:20:40 +000073// FIXME: This is for a test.
74static cl::opt<bool>
75EnableX86EHTest("enable-x86-eh-test", cl::Hidden);
76
77namespace llvm {
78 class X86_test_MachoTargetObjectFile : public TargetLoweringObjectFileMachO {
79 public:
80 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM) {
81 TargetLoweringObjectFileMachO::Initialize(Ctx, TM);
82
83 // Exception Handling.
84 LSDASection = getMachOSection("__TEXT", "__gcc_except_tab", 0,
85 SectionKind::getReadOnlyWithRel());
86 }
87
88 virtual unsigned getTTypeEncoding() const {
89 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
90 }
91 };
92
93 class X8664_test_MachoTargetObjectFile : public X8664_MachoTargetObjectFile {
94 public:
95 virtual void Initialize(MCContext &Ctx, const TargetMachine &TM) {
96 TargetLoweringObjectFileMachO::Initialize(Ctx, TM);
97
98 // Exception Handling.
99 LSDASection = getMachOSection("__TEXT", "__gcc_except_tab", 0,
100 SectionKind::getReadOnlyWithRel());
101 }
102
103 virtual unsigned getTTypeEncoding() const {
104 return DW_EH_PE_indirect | DW_EH_PE_pcrel | DW_EH_PE_sdata4;
105 }
106 };
107}
108
Chris Lattnerf0144122009-07-28 03:13:23 +0000109static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
110 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
111 default: llvm_unreachable("unknown subtarget type");
112 case X86Subtarget::isDarwin:
Bill Wendlingec041eb2010-03-12 19:20:40 +0000113 // FIXME: This is for an EH test.
114 if (EnableX86EHTest) {
115 if (TM.getSubtarget<X86Subtarget>().is64Bit())
116 return new X8664_test_MachoTargetObjectFile();
117 else
118 return new X86_test_MachoTargetObjectFile();
119 }
120
Chris Lattner8c6ed052009-09-16 01:46:41 +0000121 if (TM.getSubtarget<X86Subtarget>().is64Bit())
122 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +0000123 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +0000124 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000125 if (TM.getSubtarget<X86Subtarget>().is64Bit())
126 return new X8664_ELFTargetObjectFile(TM);
127 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 case X86Subtarget::isMingw:
129 case X86Subtarget::isCygwin:
130 case X86Subtarget::isWindows:
131 return new TargetLoweringObjectFileCOFF();
132 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000133}
134
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +0000135X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +0000136 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +0000137 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000138 X86ScalarSSEf64 = Subtarget->hasSSE2();
139 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +0000140 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000141
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000142 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000143 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000144
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000145 // Set up the TargetLowering object.
146
147 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000149 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +0000150 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000151 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000152
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000153 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000154 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000155 setUseUnderscoreSetJmp(false);
156 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000157 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000158 // MS runtime is weird: it exports _setjmp, but longjmp!
159 setUseUnderscoreSetJmp(true);
160 setUseUnderscoreLongJmp(false);
161 } else {
162 setUseUnderscoreSetJmp(true);
163 setUseUnderscoreLongJmp(true);
164 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000165
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000168 if (!Disable16Bit)
169 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000171 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000173
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000175
Scott Michelfdc40a02009-02-17 22:15:04 +0000176 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000177 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000178 if (!Disable16Bit)
179 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000181 if (!Disable16Bit)
182 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
184 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000185
186 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
188 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
189 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
190 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
191 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
192 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000193
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000194 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
195 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
197 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
198 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000199
Evan Cheng25ab6902006-09-08 06:48:29 +0000200 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
202 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000203 } else if (!UseSoftFloat) {
204 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000205 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000207 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000208 // We have an algorithm for SSE2, and we turn this into a 64-bit
209 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212
213 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
214 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
216 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000217
Devang Patel6a784892009-06-05 18:48:29 +0000218 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000219 // SSE has no i16 to fp conversion, only i32
220 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000222 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000224 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
226 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000227 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000228 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000229 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
230 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000231 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Dale Johannesen73328d12007-09-19 23:55:34 +0000233 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
234 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000235 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
236 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000237
Evan Cheng02568ff2006-01-30 22:13:22 +0000238 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
239 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
241 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000242
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000243 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000245 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000247 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000248 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
249 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000250 }
251
252 // Handle FP_TO_UINT by promoting the destination to a larger signed
253 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
255 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
256 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000257
Evan Cheng25ab6902006-09-08 06:48:29 +0000258 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000259 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
260 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000261 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000262 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000263 // Expand FP_TO_UINT into a select.
264 // FIXME: We would like to use a Custom expander here eventually to do
265 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000267 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000268 // With SSE3 we can use fisttpll to convert to a signed i64; without
269 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000272
Chris Lattner399610a2006-12-05 18:22:22 +0000273 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000274 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000275 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
276 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000277 }
Chris Lattner21f66852005-12-23 05:15:23 +0000278
Dan Gohmanb00ee212008-02-18 19:34:53 +0000279 // Scalar integer divide and remainder are lowered to use operations that
280 // produce two results, to match the available instructions. This exposes
281 // the two-result form to trivial CSE, which is able to combine x/y and x%y
282 // into a single instruction.
283 //
284 // Scalar integer multiply-high is also lowered to use two-result
285 // operations, to match the available instructions. However, plain multiply
286 // (low) operations are left as Legal, as there are single-result
287 // instructions for this in x86. Using the two-result multiply instructions
288 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
290 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
291 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
292 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
293 setOperationAction(ISD::SREM , MVT::i8 , Expand);
294 setOperationAction(ISD::UREM , MVT::i8 , Expand);
295 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
296 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
297 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
298 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
299 setOperationAction(ISD::SREM , MVT::i16 , Expand);
300 setOperationAction(ISD::UREM , MVT::i16 , Expand);
301 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
302 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
303 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
304 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
305 setOperationAction(ISD::SREM , MVT::i32 , Expand);
306 setOperationAction(ISD::UREM , MVT::i32 , Expand);
307 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
308 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
309 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
310 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
311 setOperationAction(ISD::SREM , MVT::i64 , Expand);
312 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000313
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
315 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
316 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
317 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000318 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000319 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
320 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
321 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
322 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
323 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
324 setOperationAction(ISD::FREM , MVT::f32 , Expand);
325 setOperationAction(ISD::FREM , MVT::f64 , Expand);
326 setOperationAction(ISD::FREM , MVT::f80 , Expand);
327 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000328
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
330 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
331 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
332 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000333 if (Disable16Bit) {
334 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
335 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
336 } else {
337 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
338 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
339 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
341 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
342 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
345 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
346 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 }
348
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
350 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000351
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000353 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000354 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000355 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000356 if (Disable16Bit)
357 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
358 else
359 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000360 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
361 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
362 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
363 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
364 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000365 if (Disable16Bit)
366 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
367 else
368 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000369 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
370 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
371 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
372 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000373 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
375 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000376 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000378
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000379 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
381 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
382 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
383 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000384 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000385 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
386 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000387 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000388 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
390 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
391 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
392 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000393 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000394 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000395 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
397 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
398 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000399 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000400 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
401 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
402 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000403 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000404
Evan Chengd2cde682008-03-10 19:38:10 +0000405 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000407
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000408 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000410
Mon P Wang63307c32008-05-05 19:05:59 +0000411 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
413 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
414 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
415 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000416
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
418 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
419 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
420 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000421
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000422 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
424 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
425 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
426 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
427 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
428 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
429 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000430 }
431
Evan Cheng3c992d22006-03-07 02:02:57 +0000432 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000433 if (!Subtarget->isTargetDarwin() &&
434 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000435 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000437 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000438
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
440 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
441 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
442 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000443 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000444 setExceptionPointerRegister(X86::RAX);
445 setExceptionSelectorRegister(X86::RDX);
446 } else {
447 setExceptionPointerRegister(X86::EAX);
448 setExceptionSelectorRegister(X86::EDX);
449 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
451 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000452
Owen Anderson825b72b2009-08-11 20:47:22 +0000453 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000454
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000456
Nate Begemanacc398c2006-01-25 18:21:52 +0000457 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000458 setOperationAction(ISD::VASTART , MVT::Other, Custom);
459 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000460 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000461 setOperationAction(ISD::VAARG , MVT::Other, Custom);
462 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000463 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::VAARG , MVT::Other, Expand);
465 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000466 }
Evan Chengae642192007-03-02 23:16:35 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
469 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000470 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000472 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000474 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000476
Evan Chengc7ce29b2009-02-13 22:36:38 +0000477 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000480 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
481 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482
Evan Cheng223547a2006-01-31 22:28:30 +0000483 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::FABS , MVT::f64, Custom);
485 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000486
487 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000488 setOperationAction(ISD::FNEG , MVT::f64, Custom);
489 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000490
Evan Cheng68c47cb2007-01-05 07:55:56 +0000491 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000494
Evan Chengd25e9e82006-02-02 00:28:23 +0000495 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 setOperationAction(ISD::FSIN , MVT::f64, Expand);
497 setOperationAction(ISD::FCOS , MVT::f64, Expand);
498 setOperationAction(ISD::FSIN , MVT::f32, Expand);
499 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500
Chris Lattnera54aa942006-01-29 06:26:08 +0000501 // Expand FP immediates into loads from the stack, except for the special
502 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000503 addLegalFPImmediate(APFloat(+0.0)); // xorpd
504 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000505 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000506 // Use SSE for f32, x87 for f64.
507 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
509 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000510
511 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000512 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000513
514 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000516
Owen Anderson825b72b2009-08-11 20:47:22 +0000517 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000518
519 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
521 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000522
523 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000524 setOperationAction(ISD::FSIN , MVT::f32, Expand);
525 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000526
Nate Begemane1795842008-02-14 08:57:00 +0000527 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000528 addLegalFPImmediate(APFloat(+0.0f)); // xorps
529 addLegalFPImmediate(APFloat(+0.0)); // FLD0
530 addLegalFPImmediate(APFloat(+1.0)); // FLD1
531 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
532 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
533
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000534 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
536 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000537 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000538 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000539 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000540 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
542 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000543
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
545 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
546 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
547 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000548
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000549 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
551 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000552 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000553 addLegalFPImmediate(APFloat(+0.0)); // FLD0
554 addLegalFPImmediate(APFloat(+1.0)); // FLD1
555 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
556 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000557 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
558 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
559 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
560 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000561 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000562
Dale Johannesen59a58732007-08-05 18:49:15 +0000563 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000564 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
566 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
567 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000568 {
569 bool ignored;
570 APFloat TmpFlt(+0.0);
571 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
572 &ignored);
573 addLegalFPImmediate(TmpFlt); // FLD0
574 TmpFlt.changeSign();
575 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
576 APFloat TmpFlt2(+1.0);
577 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
578 &ignored);
579 addLegalFPImmediate(TmpFlt2); // FLD1
580 TmpFlt2.changeSign();
581 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
582 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000583
Evan Chengc7ce29b2009-02-13 22:36:38 +0000584 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
586 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000587 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000588 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000589
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000590 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
592 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
593 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000594
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FLOG, MVT::f80, Expand);
596 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
597 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
598 setOperationAction(ISD::FEXP, MVT::f80, Expand);
599 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000600
Mon P Wangf007a8b2008-11-06 05:31:54 +0000601 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000602 // (for widening) or expand (for scalarization). Then we will selectively
603 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000604 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
605 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
606 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
610 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
612 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
613 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
614 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
615 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
616 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
617 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
618 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
619 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
620 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
621 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
622 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
623 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
624 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
625 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
626 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
627 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
628 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
629 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
630 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
631 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
632 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
633 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
634 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
635 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
636 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
637 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
638 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
639 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
640 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
641 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
642 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
643 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
644 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
645 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
646 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
647 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
648 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
649 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
650 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
651 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
652 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
653 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000654 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000655 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
656 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
657 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
658 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
659 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
660 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
661 setTruncStoreAction((MVT::SimpleValueType)VT,
662 (MVT::SimpleValueType)InnerVT, Expand);
663 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
664 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
665 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000666 }
667
Evan Chengc7ce29b2009-02-13 22:36:38 +0000668 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
669 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000670 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000671 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
672 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
673 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
674 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
675 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000676
Owen Anderson825b72b2009-08-11 20:47:22 +0000677 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
680 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000681
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
683 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
684 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
685 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000686
Owen Anderson825b72b2009-08-11 20:47:22 +0000687 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
688 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000689
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::AND, MVT::v8i8, Promote);
691 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
692 setOperationAction(ISD::AND, MVT::v4i16, Promote);
693 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
694 setOperationAction(ISD::AND, MVT::v2i32, Promote);
695 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
696 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000697
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::OR, MVT::v8i8, Promote);
699 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
700 setOperationAction(ISD::OR, MVT::v4i16, Promote);
701 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
702 setOperationAction(ISD::OR, MVT::v2i32, Promote);
703 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
704 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000705
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
707 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
708 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
709 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
710 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
711 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
712 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000713
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
715 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
716 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
717 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
718 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
719 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
720 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
721 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
722 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000723
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
725 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
726 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
727 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
728 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000729
Owen Anderson825b72b2009-08-11 20:47:22 +0000730 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
731 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
732 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
733 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000734
Owen Anderson825b72b2009-08-11 20:47:22 +0000735 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
736 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
737 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
738 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000739
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000741
Owen Anderson825b72b2009-08-11 20:47:22 +0000742 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
743 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
744 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
745 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
747 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
748 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000749 }
750
Evan Cheng92722532009-03-26 23:06:32 +0000751 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000753
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
755 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
756 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
757 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
758 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
759 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
760 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
761 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
762 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
763 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
764 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
765 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000766 }
767
Evan Cheng92722532009-03-26 23:06:32 +0000768 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000770
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000771 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
772 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000773 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
774 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
775 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
776 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000777
Owen Anderson825b72b2009-08-11 20:47:22 +0000778 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
779 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
780 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
781 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
782 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
783 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
784 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
785 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
786 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
787 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
788 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
789 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
790 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
791 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
792 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
793 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000794
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
796 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
797 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
798 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000799
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
801 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
802 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
803 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000805
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000806 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
807 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
808 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
809 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
810 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
811
Evan Cheng2c3ae372006-04-12 21:21:57 +0000812 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
814 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000815 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000816 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000817 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000818 // Do not attempt to custom lower non-128-bit vectors
819 if (!VT.is128BitVector())
820 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::BUILD_VECTOR,
822 VT.getSimpleVT().SimpleTy, Custom);
823 setOperationAction(ISD::VECTOR_SHUFFLE,
824 VT.getSimpleVT().SimpleTy, Custom);
825 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
826 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000827 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
830 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
831 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
832 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
833 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
834 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000835
Nate Begemancdd1eec2008-02-12 22:51:28 +0000836 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000837 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
838 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000839 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000840
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000841 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
843 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000844 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000845
846 // Do not attempt to promote non-128-bit vectors
847 if (!VT.is128BitVector()) {
848 continue;
849 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000850 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000852 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000854 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000856 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000858 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000860 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000861
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000863
Evan Cheng2c3ae372006-04-12 21:21:57 +0000864 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
866 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
867 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
868 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000869
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
871 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000872 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
874 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000875 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000876 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000877
Nate Begeman14d12ca2008-02-11 04:19:36 +0000878 if (Subtarget->hasSSE41()) {
879 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000881
882 // i8 and i16 vectors are custom , because the source register and source
883 // source memory operand types are not the same width. f32 vectors are
884 // custom since the immediate controlling the insert encodes additional
885 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
887 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
888 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
889 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000890
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
892 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
893 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
894 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000895
896 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
898 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000899 }
900 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000901
Nate Begeman30a0de92008-07-17 16:51:19 +0000902 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000904 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000905
David Greene9b9838d2009-06-29 16:47:10 +0000906 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
908 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
909 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
910 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000911
Owen Anderson825b72b2009-08-11 20:47:22 +0000912 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
913 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
914 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
915 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
916 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
917 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
918 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
919 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
920 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
921 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
922 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
923 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
924 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
925 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
926 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000927
928 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
930 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
931 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
932 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
933 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
934 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
935 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
936 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
937 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
938 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
939 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
940 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
941 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
942 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000943
Owen Anderson825b72b2009-08-11 20:47:22 +0000944 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
945 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
946 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
947 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000948
Owen Anderson825b72b2009-08-11 20:47:22 +0000949 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
950 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
951 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
952 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
953 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000954
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
956 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
957 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
958 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
959 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
960 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000961
962#if 0
963 // Not sure we want to do this since there are no 256-bit integer
964 // operations in AVX
965
966 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
967 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
969 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000970
971 // Do not attempt to custom lower non-power-of-2 vectors
972 if (!isPowerOf2_32(VT.getVectorNumElements()))
973 continue;
974
975 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
976 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
977 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
978 }
979
980 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000981 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
982 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000983 }
David Greene9b9838d2009-06-29 16:47:10 +0000984#endif
985
986#if 0
987 // Not sure we want to do this since there are no 256-bit integer
988 // operations in AVX
989
990 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
991 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000992 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
993 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000994
995 if (!VT.is256BitVector()) {
996 continue;
997 }
998 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +00001000 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +00001001 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +00001002 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +00001003 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +00001004 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +00001005 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +00001006 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +00001007 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +00001008 }
1009
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +00001011#endif
1012 }
1013
Evan Cheng6be2c582006-04-05 23:38:46 +00001014 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +00001015 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +00001016
Bill Wendling74c37652008-12-09 22:08:41 +00001017 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +00001018 setOperationAction(ISD::SADDO, MVT::i32, Custom);
1019 setOperationAction(ISD::SADDO, MVT::i64, Custom);
1020 setOperationAction(ISD::UADDO, MVT::i32, Custom);
1021 setOperationAction(ISD::UADDO, MVT::i64, Custom);
1022 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
1023 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
1024 setOperationAction(ISD::USUBO, MVT::i32, Custom);
1025 setOperationAction(ISD::USUBO, MVT::i64, Custom);
1026 setOperationAction(ISD::SMULO, MVT::i32, Custom);
1027 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +00001028
Evan Chengd54f2d52009-03-31 19:38:51 +00001029 if (!Subtarget->is64Bit()) {
1030 // These libcalls are not available in 32-bit.
1031 setLibcallName(RTLIB::SHL_I128, 0);
1032 setLibcallName(RTLIB::SRL_I128, 0);
1033 setLibcallName(RTLIB::SRA_I128, 0);
1034 }
1035
Evan Cheng206ee9d2006-07-07 08:33:52 +00001036 // We have target-specific dag combine patterns for the following nodes:
1037 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +00001038 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001039 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001040 setTargetDAGCombine(ISD::SHL);
1041 setTargetDAGCombine(ISD::SRA);
1042 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001043 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001044 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001045 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001046 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001047 if (Subtarget->is64Bit())
1048 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001049
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001050 computeRegisterProperties();
1051
Evan Cheng87ed7162006-02-14 08:25:08 +00001052 // FIXME: These should be based on subtarget info. Plus, the values should
1053 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001054 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1055 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1056 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001057 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001058 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001059}
1060
Scott Michel5b8f82e2008-03-10 15:42:14 +00001061
Owen Anderson825b72b2009-08-11 20:47:22 +00001062MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1063 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001064}
1065
1066
Evan Cheng29286502008-01-23 23:17:41 +00001067/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1068/// the desired ByVal argument alignment.
1069static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1070 if (MaxAlign == 16)
1071 return;
1072 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1073 if (VTy->getBitWidth() == 128)
1074 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001075 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1076 unsigned EltAlign = 0;
1077 getMaxByValAlign(ATy->getElementType(), EltAlign);
1078 if (EltAlign > MaxAlign)
1079 MaxAlign = EltAlign;
1080 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1081 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1082 unsigned EltAlign = 0;
1083 getMaxByValAlign(STy->getElementType(i), EltAlign);
1084 if (EltAlign > MaxAlign)
1085 MaxAlign = EltAlign;
1086 if (MaxAlign == 16)
1087 break;
1088 }
1089 }
1090 return;
1091}
1092
1093/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1094/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001095/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1096/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001097unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001098 if (Subtarget->is64Bit()) {
1099 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001100 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001101 if (TyAlign > 8)
1102 return TyAlign;
1103 return 8;
1104 }
1105
Evan Cheng29286502008-01-23 23:17:41 +00001106 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001107 if (Subtarget->hasSSE1())
1108 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001109 return Align;
1110}
Chris Lattner2b02a442007-02-25 08:29:00 +00001111
Evan Chengf0df0312008-05-15 08:39:06 +00001112/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001113/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001114/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001115/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001116EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001117X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001118 bool isSrcConst, bool isSrcStr,
1119 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001120 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1121 // linux. This is because the stack realignment code can't handle certain
1122 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001123 const Function *F = DAG.getMachineFunction().getFunction();
1124 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1125 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001126 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001127 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001128 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001129 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001130 }
Evan Chengf0df0312008-05-15 08:39:06 +00001131 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001132 return MVT::i64;
1133 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001134}
1135
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001136/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1137/// current function. The returned value is a member of the
1138/// MachineJumpTableInfo::JTEntryKind enum.
1139unsigned X86TargetLowering::getJumpTableEncoding() const {
1140 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1141 // symbol.
1142 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1143 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001144 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001145
1146 // Otherwise, use the normal jump table encoding heuristics.
1147 return TargetLowering::getJumpTableEncoding();
1148}
1149
Chris Lattner589c6f62010-01-26 06:28:43 +00001150/// getPICBaseSymbol - Return the X86-32 PIC base.
1151MCSymbol *
1152X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1153 MCContext &Ctx) const {
1154 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner98cdab52010-03-10 02:25:11 +00001155 return Ctx.GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix())+
1156 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001157}
1158
1159
Chris Lattnerc64daab2010-01-26 05:02:42 +00001160const MCExpr *
1161X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1162 const MachineBasicBlock *MBB,
1163 unsigned uid,MCContext &Ctx) const{
1164 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1165 Subtarget->isPICStyleGOT());
1166 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1167 // entries.
Chris Lattner017ec352010-02-08 22:33:55 +00001168 return X86MCTargetExpr::Create(MBB->getSymbol(Ctx),
1169 X86MCTargetExpr::GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001170}
1171
Evan Chengcc415862007-11-09 01:32:10 +00001172/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1173/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001174SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001175 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001176 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001177 // This doesn't have DebugLoc associated with it, but is not really the
1178 // same as a Register.
1179 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1180 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001181 return Table;
1182}
1183
Chris Lattner589c6f62010-01-26 06:28:43 +00001184/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1185/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1186/// MCExpr.
1187const MCExpr *X86TargetLowering::
1188getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1189 MCContext &Ctx) const {
1190 // X86-64 uses RIP relative addressing based on the jump table label.
1191 if (Subtarget->isPICStyleRIPRel())
1192 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1193
1194 // Otherwise, the reference is relative to the PIC base.
1195 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1196}
1197
Bill Wendlingb4202b82009-07-01 18:50:55 +00001198/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001199unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001200 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001201}
1202
Chris Lattner2b02a442007-02-25 08:29:00 +00001203//===----------------------------------------------------------------------===//
1204// Return Value Calling Convention Implementation
1205//===----------------------------------------------------------------------===//
1206
Chris Lattner59ed56b2007-02-28 04:55:35 +00001207#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001208
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001209bool
1210X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1211 const SmallVectorImpl<EVT> &OutTys,
1212 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1213 SelectionDAG &DAG) {
1214 SmallVector<CCValAssign, 16> RVLocs;
1215 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1216 RVLocs, *DAG.getContext());
1217 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1218}
1219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220SDValue
1221X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001222 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001223 const SmallVectorImpl<ISD::OutputArg> &Outs,
1224 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001225
Chris Lattner9774c912007-02-27 05:28:59 +00001226 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001227 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1228 RVLocs, *DAG.getContext());
1229 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001230
Evan Chengdcea1632010-02-04 02:40:39 +00001231 // Add the regs to the liveout set for the function.
1232 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1233 for (unsigned i = 0; i != RVLocs.size(); ++i)
1234 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1235 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001236
Dan Gohman475871a2008-07-27 21:46:04 +00001237 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001238
Dan Gohman475871a2008-07-27 21:46:04 +00001239 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001240 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1241 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001242 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001243
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001244 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001245 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1246 CCValAssign &VA = RVLocs[i];
1247 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001248 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001249
Chris Lattner447ff682008-03-11 03:23:40 +00001250 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1251 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001252 if (VA.getLocReg() == X86::ST0 ||
1253 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001254 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1255 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001256 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001257 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001258 RetOps.push_back(ValToCopy);
1259 // Don't emit a copytoreg.
1260 continue;
1261 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001262
Evan Cheng242b38b2009-02-23 09:03:22 +00001263 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1264 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001265 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001266 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001267 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001269 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001271 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001272 }
1273
Dale Johannesendd64c412009-02-04 00:33:20 +00001274 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001275 Flag = Chain.getValue(1);
1276 }
Dan Gohman61a92132008-04-21 23:59:07 +00001277
1278 // The x86-64 ABI for returning structs by value requires that we copy
1279 // the sret argument into %rax for the return. We saved the argument into
1280 // a virtual register in the entry block, so now we copy the value out
1281 // and into %rax.
1282 if (Subtarget->is64Bit() &&
1283 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1284 MachineFunction &MF = DAG.getMachineFunction();
1285 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1286 unsigned Reg = FuncInfo->getSRetReturnReg();
1287 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001288 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001289 FuncInfo->setSRetReturnReg(Reg);
1290 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001291 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001292
Dale Johannesendd64c412009-02-04 00:33:20 +00001293 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001294 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001295
1296 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001297 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001298 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001299
Chris Lattner447ff682008-03-11 03:23:40 +00001300 RetOps[0] = Chain; // Update chain.
1301
1302 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001304 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001305
1306 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001308}
1309
Dan Gohman98ca4f22009-08-05 01:29:28 +00001310/// LowerCallResult - Lower the result values of a call into the
1311/// appropriate copies out of appropriate physical registers.
1312///
1313SDValue
1314X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001315 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001316 const SmallVectorImpl<ISD::InputArg> &Ins,
1317 DebugLoc dl, SelectionDAG &DAG,
1318 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001319
Chris Lattnere32bbf62007-02-28 07:09:55 +00001320 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001321 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001322 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001323 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001324 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001325 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001326
Chris Lattner3085e152007-02-25 08:59:22 +00001327 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001328 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001329 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001330 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
Torok Edwin3f142c32009-02-01 18:15:56 +00001332 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001333 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001334 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001335 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001336 }
1337
Chris Lattner8e6da152008-03-10 21:08:41 +00001338 // If this is a call to a function that returns an fp value on the floating
1339 // point stack, but where we prefer to use the value in xmm registers, copy
1340 // 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 +00001341 if ((VA.getLocReg() == X86::ST0 ||
1342 VA.getLocReg() == X86::ST1) &&
1343 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001344 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001345 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001346
Evan Cheng79fb3b42009-02-20 20:43:02 +00001347 SDValue Val;
1348 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001349 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1350 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1351 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001352 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001353 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1355 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001356 } else {
1357 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001358 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001359 Val = Chain.getValue(0);
1360 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001361 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1362 } else {
1363 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1364 CopyVT, InFlag).getValue(1);
1365 Val = Chain.getValue(0);
1366 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001367 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001368
Dan Gohman37eed792009-02-04 17:28:58 +00001369 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001370 // Round the F80 the right size, which also moves to the appropriate xmm
1371 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001372 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001373 // This truncation won't change the value.
1374 DAG.getIntPtrConstant(1));
1375 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001376
Dan Gohman98ca4f22009-08-05 01:29:28 +00001377 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001378 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001379
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001381}
1382
1383
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001384//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001385// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001386//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001387// StdCall calling convention seems to be standard for many Windows' API
1388// routines and around. It differs from C calling convention just a little:
1389// callee should clean up the stack, not caller. Symbols should be also
1390// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001391// For info on fast calling convention see Fast Calling Convention (tail call)
1392// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001393
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001395/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001396static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1397 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001398 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001399
Dan Gohman98ca4f22009-08-05 01:29:28 +00001400 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001401}
1402
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001403/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001404/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001405static bool
1406ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1407 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001408 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001409
Dan Gohman98ca4f22009-08-05 01:29:28 +00001410 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001411}
1412
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001413/// IsCalleePop - Determines whether the callee is required to pop its
1414/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001415bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001416 if (IsVarArg)
1417 return false;
1418
Dan Gohman095cc292008-09-13 01:54:27 +00001419 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001420 default:
1421 return false;
1422 case CallingConv::X86_StdCall:
1423 return !Subtarget->is64Bit();
1424 case CallingConv::X86_FastCall:
1425 return !Subtarget->is64Bit();
1426 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001427 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001428 case CallingConv::GHC:
1429 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 }
1431}
1432
Dan Gohman095cc292008-09-13 01:54:27 +00001433/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1434/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001435CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001436 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001437 if (CC == CallingConv::GHC)
1438 return CC_X86_64_GHC;
1439 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001440 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001441 else
1442 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001443 }
1444
Gordon Henriksen86737662008-01-05 16:56:59 +00001445 if (CC == CallingConv::X86_FastCall)
1446 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001447 else if (CC == CallingConv::Fast)
1448 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001449 else if (CC == CallingConv::GHC)
1450 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 else
1452 return CC_X86_32_C;
1453}
1454
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001455/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1456/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001457/// the specific parameter attribute. The copy will be passed as a byval
1458/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001459static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001460CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001461 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1462 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001463 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001464 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001465 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001466}
1467
Chris Lattner29689432010-03-11 00:22:57 +00001468/// IsTailCallConvention - Return true if the calling convention is one that
1469/// supports tail call optimization.
1470static bool IsTailCallConvention(CallingConv::ID CC) {
1471 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1472}
1473
Evan Cheng0c439eb2010-01-27 00:07:07 +00001474/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1475/// a tailcall target by changing its ABI.
1476static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001477 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001478}
1479
Dan Gohman98ca4f22009-08-05 01:29:28 +00001480SDValue
1481X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001482 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 const SmallVectorImpl<ISD::InputArg> &Ins,
1484 DebugLoc dl, SelectionDAG &DAG,
1485 const CCValAssign &VA,
1486 MachineFrameInfo *MFI,
1487 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001488 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001489 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001490 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001491 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001492 EVT ValVT;
1493
1494 // If value is passed by pointer we have address passed instead of the value
1495 // itself.
1496 if (VA.getLocInfo() == CCValAssign::Indirect)
1497 ValVT = VA.getLocVT();
1498 else
1499 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001500
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001501 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001502 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001503 // In case of tail call optimization mark all arguments mutable. Since they
1504 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001505 if (Flags.isByVal()) {
1506 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1507 VA.getLocMemOffset(), isImmutable, false);
1508 return DAG.getFrameIndex(FI, getPointerTy());
1509 } else {
1510 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1511 VA.getLocMemOffset(), isImmutable, false);
1512 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1513 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001514 PseudoSourceValue::getFixedStack(FI), 0,
1515 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001516 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001517}
1518
Dan Gohman475871a2008-07-27 21:46:04 +00001519SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001520X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001521 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001522 bool isVarArg,
1523 const SmallVectorImpl<ISD::InputArg> &Ins,
1524 DebugLoc dl,
1525 SelectionDAG &DAG,
1526 SmallVectorImpl<SDValue> &InVals) {
1527
Evan Cheng1bc78042006-04-26 01:20:17 +00001528 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001529 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001530
Gordon Henriksen86737662008-01-05 16:56:59 +00001531 const Function* Fn = MF.getFunction();
1532 if (Fn->hasExternalLinkage() &&
1533 Subtarget->isTargetCygMing() &&
1534 Fn->getName() == "main")
1535 FuncInfo->setForceFramePointer(true);
1536
Evan Cheng1bc78042006-04-26 01:20:17 +00001537 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001538 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001539 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001540
Chris Lattner29689432010-03-11 00:22:57 +00001541 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1542 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001543
Chris Lattner638402b2007-02-28 07:00:42 +00001544 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001545 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1547 ArgLocs, *DAG.getContext());
1548 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001549
Chris Lattnerf39f7712007-02-28 05:46:49 +00001550 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001551 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001552 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1553 CCValAssign &VA = ArgLocs[i];
1554 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1555 // places.
1556 assert(VA.getValNo() != LastVal &&
1557 "Don't support value assigned to multiple locs yet");
1558 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Chris Lattnerf39f7712007-02-28 05:46:49 +00001560 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001561 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001562 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001564 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001565 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001566 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001568 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001569 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001570 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001571 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001572 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001573 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1574 RC = X86::VR64RegisterClass;
1575 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001576 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001577
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001578 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001580
Chris Lattnerf39f7712007-02-28 05:46:49 +00001581 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1582 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1583 // right size.
1584 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001585 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001586 DAG.getValueType(VA.getValVT()));
1587 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001588 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001589 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001590 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001591 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001593 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001594 // Handle MMX values passed in XMM regs.
1595 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1597 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001598 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1610 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001637 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001638 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 }
1640 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001641 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1642
1643 // FIXME: We should really autogenerate these arrays
1644 static const unsigned GPR64ArgRegsWin64[] = {
1645 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001647 static const unsigned XMMArgRegsWin64[] = {
1648 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1649 };
1650 static const unsigned GPR64ArgRegs64Bit[] = {
1651 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1652 };
1653 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1655 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1656 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1658
1659 if (IsWin64) {
1660 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1661 GPR64ArgRegs = GPR64ArgRegsWin64;
1662 XMMArgRegs = XMMArgRegsWin64;
1663 } else {
1664 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1665 GPR64ArgRegs = GPR64ArgRegs64Bit;
1666 XMMArgRegs = XMMArgRegs64Bit;
1667 }
1668 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1669 TotalNumIntRegs);
1670 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1671 TotalNumXMMRegs);
1672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001674 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Gordon Henriksen86737662008-01-05 16:56:59 +00001683 // For X86-64, if there are vararg parameters that are passed via
1684 // registers, then we must store them to their spots on the stack so they
1685 // may be loaded by deferencing the result of va_next.
1686 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001687 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1688 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001689 TotalNumXMMRegs * 16, 16,
1690 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001691
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001693 SmallVector<SDValue, 8> MemOps;
1694 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001695 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001696 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001697 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1698 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001699 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1700 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001702 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001703 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001704 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene67c9d422010-02-15 16:53:33 +00001705 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001707 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001709
Dan Gohmanface41a2009-08-16 21:24:25 +00001710 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1711 // Now store the XMM (fp + vector) parameter registers.
1712 SmallVector<SDValue, 11> SaveXMMOps;
1713 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001714
Dan Gohmanface41a2009-08-16 21:24:25 +00001715 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1716 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1717 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001718
Dan Gohmanface41a2009-08-16 21:24:25 +00001719 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1720 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1723 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1724 X86::VR128RegisterClass);
1725 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1726 SaveXMMOps.push_back(Val);
1727 }
1728 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1729 MVT::Other,
1730 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001732
1733 if (!MemOps.empty())
1734 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1735 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001736 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001738
Gordon Henriksen86737662008-01-05 16:56:59 +00001739 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001740 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001741 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001742 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001743 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001744 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001745 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001746 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001747 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001748
Gordon Henriksen86737662008-01-05 16:56:59 +00001749 if (!Is64Bit) {
1750 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001752 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1753 }
Evan Cheng25caf632006-05-23 21:06:34 +00001754
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001755 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001756
Dan Gohman98ca4f22009-08-05 01:29:28 +00001757 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001758}
1759
Dan Gohman475871a2008-07-27 21:46:04 +00001760SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1762 SDValue StackPtr, SDValue Arg,
1763 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001764 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001765 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001766 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001767 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001768 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001769 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001770 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001771 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001772 }
Dale Johannesenace16102009-02-03 19:33:06 +00001773 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001774 PseudoSourceValue::getStack(), LocMemOffset,
1775 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001776}
1777
Bill Wendling64e87322009-01-16 19:25:27 +00001778/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001779/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001780SDValue
1781X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001782 SDValue &OutRetAddr, SDValue Chain,
1783 bool IsTailCall, bool Is64Bit,
1784 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001785 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001786 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001787 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001788
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001789 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001790 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001791 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001792}
1793
1794/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1795/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001796static SDValue
1797EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001798 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001799 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Store the return address to the appropriate stack slot.
1801 if (!FPDiff) return Chain;
1802 // Calculate the new stack slot for the return address.
1803 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001804 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001805 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001806 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001807 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001808 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001809 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1810 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001811 return Chain;
1812}
1813
Dan Gohman98ca4f22009-08-05 01:29:28 +00001814SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001815X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001816 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001817 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001818 const SmallVectorImpl<ISD::OutputArg> &Outs,
1819 const SmallVectorImpl<ISD::InputArg> &Ins,
1820 DebugLoc dl, SelectionDAG &DAG,
1821 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001822 MachineFunction &MF = DAG.getMachineFunction();
1823 bool Is64Bit = Subtarget->is64Bit();
1824 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001825 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001826
Evan Cheng5f941932010-02-05 02:21:12 +00001827 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001828 // Check if it's really possible to do a tail call.
Evan Cheng022d9e12010-02-02 23:55:14 +00001829 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg,
1830 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001831
1832 // Sibcalls are automatically detected tailcalls which do not require
1833 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001834 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001835 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001836
1837 if (isTailCall)
1838 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001839 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001840
Chris Lattner29689432010-03-11 00:22:57 +00001841 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1842 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001843
Chris Lattner638402b2007-02-28 07:00:42 +00001844 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001845 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001846 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1847 ArgLocs, *DAG.getContext());
1848 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001849
Chris Lattner423c5f42007-02-28 05:31:48 +00001850 // Get a count of how many bytes are to be pushed on the stack.
1851 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001852 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001853 // This is a sibcall. The memory operands are available in caller's
1854 // own caller's stack.
1855 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001856 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001857 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001858
Gordon Henriksen86737662008-01-05 16:56:59 +00001859 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001860 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001862 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1864 FPDiff = NumBytesCallerPushed - NumBytes;
1865
1866 // Set the delta of movement of the returnaddr stackslot.
1867 // But only set if delta is greater than previous delta.
1868 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1869 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1870 }
1871
Evan Chengf22f9b32010-02-06 03:28:46 +00001872 if (!IsSibcall)
1873 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001874
Dan Gohman475871a2008-07-27 21:46:04 +00001875 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001876 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001877 if (isTailCall && FPDiff)
1878 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1879 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001880
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1882 SmallVector<SDValue, 8> MemOpChains;
1883 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001884
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001885 // Walk the register/memloc assignments, inserting copies/loads. In the case
1886 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001887 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1888 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001889 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001890 SDValue Arg = Outs[i].Val;
1891 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001892 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Chris Lattner423c5f42007-02-28 05:31:48 +00001894 // Promote the value if needed.
1895 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001896 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001897 case CCValAssign::Full: break;
1898 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001899 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001900 break;
1901 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001902 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001903 break;
1904 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001905 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1906 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001907 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1908 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1909 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001910 } else
1911 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1912 break;
1913 case CCValAssign::BCvt:
1914 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001915 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001916 case CCValAssign::Indirect: {
1917 // Store the argument.
1918 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001919 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001920 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001921 PseudoSourceValue::getFixedStack(FI), 0,
1922 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001923 Arg = SpillSlot;
1924 break;
1925 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001926 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001927
Chris Lattner423c5f42007-02-28 05:31:48 +00001928 if (VA.isRegLoc()) {
1929 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001930 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001931 assert(VA.isMemLoc());
1932 if (StackPtr.getNode() == 0)
1933 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1934 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1935 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001936 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001937 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001938
Evan Cheng32fe1032006-05-25 00:59:30 +00001939 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001940 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001941 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001942
Evan Cheng347d5f72006-04-28 21:29:37 +00001943 // Build a sequence of copy-to-reg nodes chained together with token chain
1944 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001946 // Tail call byval lowering might overwrite argument registers so in case of
1947 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001949 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001950 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001951 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001952 InFlag = Chain.getValue(1);
1953 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001954
Chris Lattner88e1fd52009-07-09 04:24:46 +00001955 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001956 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1957 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001958 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001959 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1960 DAG.getNode(X86ISD::GlobalBaseReg,
1961 DebugLoc::getUnknownLoc(),
1962 getPointerTy()),
1963 InFlag);
1964 InFlag = Chain.getValue(1);
1965 } else {
1966 // If we are tail calling and generating PIC/GOT style code load the
1967 // address of the callee into ECX. The value in ecx is used as target of
1968 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1969 // for tail calls on PIC/GOT architectures. Normally we would just put the
1970 // address of GOT into ebx and then call target@PLT. But for tail calls
1971 // ebx would be restored (since ebx is callee saved) before jumping to the
1972 // target@PLT.
1973
1974 // Note: The actual moving to ECX is done further down.
1975 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1976 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1977 !G->getGlobal()->hasProtectedVisibility())
1978 Callee = LowerGlobalAddress(Callee, DAG);
1979 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001980 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001981 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001982 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001983
Gordon Henriksen86737662008-01-05 16:56:59 +00001984 if (Is64Bit && isVarArg) {
1985 // From AMD64 ABI document:
1986 // For calls that may call functions that use varargs or stdargs
1987 // (prototype-less calls or calls to functions containing ellipsis (...) in
1988 // the declaration) %al is used as hidden argument to specify the number
1989 // of SSE registers used. The contents of %al do not need to match exactly
1990 // the number of registers, but must be an ubound on the number of SSE
1991 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001992
1993 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 // Count the number of XMM registers allocated.
1995 static const unsigned XMMArgRegs[] = {
1996 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1997 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1998 };
1999 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00002000 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002001 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002002
Dale Johannesendd64c412009-02-04 00:33:20 +00002003 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002004 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002005 InFlag = Chain.getValue(1);
2006 }
2007
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002008
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002009 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002010 if (isTailCall) {
2011 // Force all the incoming stack arguments to be loaded from the stack
2012 // before any new outgoing arguments are stored to the stack, because the
2013 // outgoing stack slots may alias the incoming argument stack slots, and
2014 // the alias isn't otherwise explicit. This is slightly more conservative
2015 // than necessary, because it means that each store effectively depends
2016 // on every argument instead of just those arguments it would clobber.
2017 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2018
Dan Gohman475871a2008-07-27 21:46:04 +00002019 SmallVector<SDValue, 8> MemOpChains2;
2020 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002022 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002023 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002024 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002025 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2026 CCValAssign &VA = ArgLocs[i];
2027 if (VA.isRegLoc())
2028 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002029 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002030 SDValue Arg = Outs[i].Val;
2031 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Create frame index.
2033 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002034 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002035 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002036 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002037
Duncan Sands276dcbd2008-03-21 09:14:45 +00002038 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002039 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002041 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002042 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002043 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002044 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002045
Dan Gohman98ca4f22009-08-05 01:29:28 +00002046 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2047 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002048 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002049 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002050 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002051 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002052 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002053 PseudoSourceValue::getFixedStack(FI), 0,
2054 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002055 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002056 }
2057 }
2058
2059 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002060 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002061 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002062
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002063 // Copy arguments to their registers.
2064 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002065 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002066 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002067 InFlag = Chain.getValue(1);
2068 }
Dan Gohman475871a2008-07-27 21:46:04 +00002069 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002070
Gordon Henriksen86737662008-01-05 16:56:59 +00002071 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002072 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002073 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002074 }
2075
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002076 bool WasGlobalOrExternal = false;
2077 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2078 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2079 // In the 64-bit large code model, we have to make all calls
2080 // through a register, since the call instruction's 32-bit
2081 // pc-relative offset may not be large enough to hold the whole
2082 // address.
2083 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2084 WasGlobalOrExternal = true;
2085 // If the callee is a GlobalAddress node (quite common, every direct call
2086 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2087 // it.
2088
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002089 // We should use extra load for direct calls to dllimported functions in
2090 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002091 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002092 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002093 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002094
Chris Lattner48a7d022009-07-09 05:02:21 +00002095 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2096 // external symbols most go through the PLT in PIC mode. If the symbol
2097 // has hidden or protected visibility, or if it is static or local, then
2098 // we don't need to use the PLT - we can directly call it.
2099 if (Subtarget->isTargetELF() &&
2100 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002101 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002102 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002103 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002104 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2105 Subtarget->getDarwinVers() < 9) {
2106 // PC-relative references to external symbols should go through $stub,
2107 // unless we're building with the leopard linker or later, which
2108 // automatically synthesizes these stubs.
2109 OpFlags = X86II::MO_DARWIN_STUB;
2110 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002111
Chris Lattner74e726e2009-07-09 05:27:35 +00002112 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002113 G->getOffset(), OpFlags);
2114 }
Bill Wendling056292f2008-09-16 21:48:12 +00002115 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002116 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002117 unsigned char OpFlags = 0;
2118
2119 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2120 // symbols should go through the PLT.
2121 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002122 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002123 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002124 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002125 Subtarget->getDarwinVers() < 9) {
2126 // PC-relative references to external symbols should go through $stub,
2127 // unless we're building with the leopard linker or later, which
2128 // automatically synthesizes these stubs.
2129 OpFlags = X86II::MO_DARWIN_STUB;
2130 }
Eric Christopherfd179292009-08-27 18:07:15 +00002131
Chris Lattner48a7d022009-07-09 05:02:21 +00002132 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2133 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002134 }
2135
2136 if (isTailCall && !WasGlobalOrExternal) {
Evan Chengdcea1632010-02-04 02:40:39 +00002137 // Force the address into a (call preserved) caller-saved register since
2138 // tailcall must happen after callee-saved registers are poped.
2139 // FIXME: Give it a special register class that contains caller-saved
2140 // register instead?
Bill Wendlingc6678b02010-03-11 19:50:31 +00002141 unsigned TCReg = Is64Bit ? X86::R11 : X86::EAX;
Dale Johannesendd64c412009-02-04 00:33:20 +00002142 Chain = DAG.getCopyToReg(Chain, dl,
Evan Chengdcea1632010-02-04 02:40:39 +00002143 DAG.getRegister(TCReg, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 Callee,InFlag);
Evan Chengdcea1632010-02-04 02:40:39 +00002145 Callee = DAG.getRegister(TCReg, getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00002146 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002147
Chris Lattnerd96d0722007-02-25 06:40:16 +00002148 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002151
Evan Chengf22f9b32010-02-06 03:28:46 +00002152 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002153 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2154 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002155 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002157
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002158 Ops.push_back(Chain);
2159 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002160
Dan Gohman98ca4f22009-08-05 01:29:28 +00002161 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002162 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002163
Gordon Henriksen86737662008-01-05 16:56:59 +00002164 // Add argument registers to the end of the list so that they are known live
2165 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002166 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2167 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2168 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002169
Evan Cheng586ccac2008-03-18 23:36:35 +00002170 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002172 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2173
2174 // Add an implicit use of AL for x86 vararg functions.
2175 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002177
Gabor Greifba36cb52008-08-28 21:40:38 +00002178 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002179 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002180
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181 if (isTailCall) {
2182 // If this is the first return lowered for this function, add the regs
2183 // to the liveout set for the function.
2184 if (MF.getRegInfo().liveout_empty()) {
2185 SmallVector<CCValAssign, 16> RVLocs;
2186 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2187 *DAG.getContext());
2188 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2189 for (unsigned i = 0; i != RVLocs.size(); ++i)
2190 if (RVLocs[i].isRegLoc())
2191 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2192 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002193
Dan Gohman98ca4f22009-08-05 01:29:28 +00002194 assert(((Callee.getOpcode() == ISD::Register &&
Bill Wendlingc6678b02010-03-11 19:50:31 +00002195 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002196 cast<RegisterSDNode>(Callee)->getReg() == X86::R11)) ||
Dan Gohman98ca4f22009-08-05 01:29:28 +00002197 Callee.getOpcode() == ISD::TargetExternalSymbol ||
2198 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
Jeffrey Yasskina77169d2010-01-09 18:56:43 +00002199 "Expecting a global address, external symbol, or scratch register");
Dan Gohman98ca4f22009-08-05 01:29:28 +00002200
2201 return DAG.getNode(X86ISD::TC_RETURN, dl,
2202 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002203 }
2204
Dale Johannesenace16102009-02-03 19:33:06 +00002205 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002206 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002207
Chris Lattner2d297092006-05-23 18:50:38 +00002208 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002209 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002210 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002211 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002212 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002213 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002214 // pops the hidden struct pointer, so we have to push it back.
2215 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002216 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002217 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002218 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002219
Gordon Henriksenae636f82008-01-03 16:47:34 +00002220 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002221 if (!IsSibcall) {
2222 Chain = DAG.getCALLSEQ_END(Chain,
2223 DAG.getIntPtrConstant(NumBytes, true),
2224 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2225 true),
2226 InFlag);
2227 InFlag = Chain.getValue(1);
2228 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002229
Chris Lattner3085e152007-02-25 08:59:22 +00002230 // Handle result values, copying them out of physregs into vregs that we
2231 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002232 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2233 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002234}
2235
Evan Cheng25ab6902006-09-08 06:48:29 +00002236
2237//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002238// Fast Calling Convention (tail call) implementation
2239//===----------------------------------------------------------------------===//
2240
2241// Like std call, callee cleans arguments, convention except that ECX is
2242// reserved for storing the tail called function address. Only 2 registers are
2243// free for argument passing (inreg). Tail call optimization is performed
2244// provided:
2245// * tailcallopt is enabled
2246// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002247// On X86_64 architecture with GOT-style position independent code only local
2248// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002249// To keep the stack aligned according to platform abi the function
2250// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2251// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002252// If a tail called function callee has more arguments than the caller the
2253// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002254// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002255// original REtADDR, but before the saved framepointer or the spilled registers
2256// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2257// stack layout:
2258// arg1
2259// arg2
2260// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002261// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002262// move area ]
2263// (possible EBP)
2264// ESI
2265// EDI
2266// local1 ..
2267
2268/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2269/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002270unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002271 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002272 MachineFunction &MF = DAG.getMachineFunction();
2273 const TargetMachine &TM = MF.getTarget();
2274 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2275 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002277 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002278 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002279 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2280 // Number smaller than 12 so just add the difference.
2281 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2282 } else {
2283 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002284 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002285 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002286 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002287 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002288}
2289
Evan Cheng5f941932010-02-05 02:21:12 +00002290/// MatchingStackOffset - Return true if the given stack call argument is
2291/// already available in the same position (relatively) of the caller's
2292/// incoming argument stack.
2293static
2294bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2295 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2296 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002297 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2298 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002299 if (Arg.getOpcode() == ISD::CopyFromReg) {
2300 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2301 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2302 return false;
2303 MachineInstr *Def = MRI->getVRegDef(VR);
2304 if (!Def)
2305 return false;
2306 if (!Flags.isByVal()) {
2307 if (!TII->isLoadFromStackSlot(Def, FI))
2308 return false;
2309 } else {
2310 unsigned Opcode = Def->getOpcode();
2311 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2312 Def->getOperand(1).isFI()) {
2313 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002314 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002315 } else
2316 return false;
2317 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002318 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2319 if (Flags.isByVal())
2320 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002321 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002322 // define @foo(%struct.X* %A) {
2323 // tail call @bar(%struct.X* byval %A)
2324 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002325 return false;
2326 SDValue Ptr = Ld->getBasePtr();
2327 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2328 if (!FINode)
2329 return false;
2330 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002331 } else
2332 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002333
Evan Cheng4cae1332010-03-05 08:38:04 +00002334 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002335 if (!MFI->isFixedObjectIndex(FI))
2336 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002337 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002338}
2339
Dan Gohman98ca4f22009-08-05 01:29:28 +00002340/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2341/// for tail call optimization. Targets which want to do tail call
2342/// optimization should implement this function.
2343bool
2344X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002345 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002346 bool isVarArg,
Evan Chengb1712452010-01-27 06:25:16 +00002347 const SmallVectorImpl<ISD::OutputArg> &Outs,
2348 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002349 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002350 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002351 CalleeCC != CallingConv::C)
2352 return false;
2353
Evan Cheng7096ae42010-01-29 06:45:59 +00002354 // If -tailcallopt is specified, make fastcc functions tail-callable.
2355 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002356 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002357 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002358 CallerF->getCallingConv() == CalleeCC)
2359 return true;
2360 return false;
2361 }
2362
Evan Chengb2c92902010-02-02 02:22:50 +00002363 // Look for obvious safe cases to perform tail call optimization that does not
2364 // requite ABI changes. This is what gcc calls sibcall.
2365
Evan Cheng843bd692010-01-31 06:44:49 +00002366 // Do not tail call optimize vararg calls for now.
2367 if (isVarArg)
2368 return false;
2369
Evan Chenga6bff982010-01-30 01:22:00 +00002370 // If the callee takes no arguments then go on to check the results of the
2371 // call.
2372 if (!Outs.empty()) {
2373 // Check if stack adjustment is needed. For now, do not do this if any
2374 // argument is passed on the stack.
2375 SmallVector<CCValAssign, 16> ArgLocs;
2376 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2377 ArgLocs, *DAG.getContext());
2378 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002379 if (CCInfo.getNextStackOffset()) {
2380 MachineFunction &MF = DAG.getMachineFunction();
2381 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2382 return false;
2383 if (Subtarget->isTargetWin64())
2384 // Win64 ABI has additional complications.
2385 return false;
2386
2387 // Check if the arguments are already laid out in the right way as
2388 // the caller's fixed stack objects.
2389 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002390 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2391 const X86InstrInfo *TII =
2392 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002393 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2394 CCValAssign &VA = ArgLocs[i];
2395 EVT RegVT = VA.getLocVT();
2396 SDValue Arg = Outs[i].Val;
2397 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002398 if (VA.getLocInfo() == CCValAssign::Indirect)
2399 return false;
2400 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002401 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2402 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002403 return false;
2404 }
2405 }
2406 }
Evan Chenga6bff982010-01-30 01:22:00 +00002407 }
Evan Chengb1712452010-01-27 06:25:16 +00002408
Evan Cheng86809cc2010-02-03 03:28:02 +00002409 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002410}
2411
Dan Gohman3df24e62008-09-03 23:12:08 +00002412FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002413X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2414 DwarfWriter *dw,
2415 DenseMap<const Value *, unsigned> &vm,
2416 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2417 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002418#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002419 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002420#endif
2421 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002422 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002423#ifndef NDEBUG
2424 , cil
2425#endif
2426 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002427}
2428
2429
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002430//===----------------------------------------------------------------------===//
2431// Other Lowering Hooks
2432//===----------------------------------------------------------------------===//
2433
2434
Dan Gohman475871a2008-07-27 21:46:04 +00002435SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002436 MachineFunction &MF = DAG.getMachineFunction();
2437 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2438 int ReturnAddrIndex = FuncInfo->getRAIndex();
2439
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002440 if (ReturnAddrIndex == 0) {
2441 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002442 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002443 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002444 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002445 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002446 }
2447
Evan Cheng25ab6902006-09-08 06:48:29 +00002448 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002449}
2450
2451
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002452bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2453 bool hasSymbolicDisplacement) {
2454 // Offset should fit into 32 bit immediate field.
2455 if (!isInt32(Offset))
2456 return false;
2457
2458 // If we don't have a symbolic displacement - we don't have any extra
2459 // restrictions.
2460 if (!hasSymbolicDisplacement)
2461 return true;
2462
2463 // FIXME: Some tweaks might be needed for medium code model.
2464 if (M != CodeModel::Small && M != CodeModel::Kernel)
2465 return false;
2466
2467 // For small code model we assume that latest object is 16MB before end of 31
2468 // bits boundary. We may also accept pretty large negative constants knowing
2469 // that all objects are in the positive half of address space.
2470 if (M == CodeModel::Small && Offset < 16*1024*1024)
2471 return true;
2472
2473 // For kernel code model we know that all object resist in the negative half
2474 // of 32bits address space. We may not accept negative offsets, since they may
2475 // be just off and we may accept pretty large positive ones.
2476 if (M == CodeModel::Kernel && Offset > 0)
2477 return true;
2478
2479 return false;
2480}
2481
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002482/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2483/// specific condition code, returning the condition code and the LHS/RHS of the
2484/// comparison to make.
2485static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2486 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002487 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002488 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2489 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2490 // X > -1 -> X == 0, jump !sign.
2491 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002492 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002493 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2494 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002495 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002496 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002497 // X < 1 -> X <= 0
2498 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002499 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002500 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002501 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002502
Evan Chengd9558e02006-01-06 00:43:03 +00002503 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002504 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002505 case ISD::SETEQ: return X86::COND_E;
2506 case ISD::SETGT: return X86::COND_G;
2507 case ISD::SETGE: return X86::COND_GE;
2508 case ISD::SETLT: return X86::COND_L;
2509 case ISD::SETLE: return X86::COND_LE;
2510 case ISD::SETNE: return X86::COND_NE;
2511 case ISD::SETULT: return X86::COND_B;
2512 case ISD::SETUGT: return X86::COND_A;
2513 case ISD::SETULE: return X86::COND_BE;
2514 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002515 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002516 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002517
Chris Lattner4c78e022008-12-23 23:42:27 +00002518 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002519
Chris Lattner4c78e022008-12-23 23:42:27 +00002520 // If LHS is a foldable load, but RHS is not, flip the condition.
2521 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2522 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2523 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2524 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002525 }
2526
Chris Lattner4c78e022008-12-23 23:42:27 +00002527 switch (SetCCOpcode) {
2528 default: break;
2529 case ISD::SETOLT:
2530 case ISD::SETOLE:
2531 case ISD::SETUGT:
2532 case ISD::SETUGE:
2533 std::swap(LHS, RHS);
2534 break;
2535 }
2536
2537 // On a floating point condition, the flags are set as follows:
2538 // ZF PF CF op
2539 // 0 | 0 | 0 | X > Y
2540 // 0 | 0 | 1 | X < Y
2541 // 1 | 0 | 0 | X == Y
2542 // 1 | 1 | 1 | unordered
2543 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002544 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002545 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002546 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002547 case ISD::SETOLT: // flipped
2548 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002549 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002550 case ISD::SETOLE: // flipped
2551 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002552 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002553 case ISD::SETUGT: // flipped
2554 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002555 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002556 case ISD::SETUGE: // flipped
2557 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002558 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002559 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002560 case ISD::SETNE: return X86::COND_NE;
2561 case ISD::SETUO: return X86::COND_P;
2562 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002563 case ISD::SETOEQ:
2564 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002565 }
Evan Chengd9558e02006-01-06 00:43:03 +00002566}
2567
Evan Cheng4a460802006-01-11 00:33:36 +00002568/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2569/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002570/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002571static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002572 switch (X86CC) {
2573 default:
2574 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002575 case X86::COND_B:
2576 case X86::COND_BE:
2577 case X86::COND_E:
2578 case X86::COND_P:
2579 case X86::COND_A:
2580 case X86::COND_AE:
2581 case X86::COND_NE:
2582 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002583 return true;
2584 }
2585}
2586
Evan Chengeb2f9692009-10-27 19:56:55 +00002587/// isFPImmLegal - Returns true if the target can instruction select the
2588/// specified FP immediate natively. If false, the legalizer will
2589/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002590bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002591 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2592 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2593 return true;
2594 }
2595 return false;
2596}
2597
Nate Begeman9008ca62009-04-27 18:41:29 +00002598/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2599/// the specified range (L, H].
2600static bool isUndefOrInRange(int Val, int Low, int Hi) {
2601 return (Val < 0) || (Val >= Low && Val < Hi);
2602}
2603
2604/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2605/// specified value.
2606static bool isUndefOrEqual(int Val, int CmpVal) {
2607 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002608 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002610}
2611
Nate Begeman9008ca62009-04-27 18:41:29 +00002612/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2613/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2614/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002615static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002616 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002618 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 return (Mask[0] < 2 && Mask[1] < 2);
2620 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002621}
2622
Nate Begeman9008ca62009-04-27 18:41:29 +00002623bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002624 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 N->getMask(M);
2626 return ::isPSHUFDMask(M, N->getValueType(0));
2627}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002628
Nate Begeman9008ca62009-04-27 18:41:29 +00002629/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2630/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002631static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002632 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002633 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002634
Nate Begeman9008ca62009-04-27 18:41:29 +00002635 // Lower quadword copied in order or undef.
2636 for (int i = 0; i != 4; ++i)
2637 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002639
Evan Cheng506d3df2006-03-29 23:07:14 +00002640 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002641 for (int i = 4; i != 8; ++i)
2642 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002643 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002644
Evan Cheng506d3df2006-03-29 23:07:14 +00002645 return true;
2646}
2647
Nate Begeman9008ca62009-04-27 18:41:29 +00002648bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002649 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 N->getMask(M);
2651 return ::isPSHUFHWMask(M, N->getValueType(0));
2652}
Evan Cheng506d3df2006-03-29 23:07:14 +00002653
Nate Begeman9008ca62009-04-27 18:41:29 +00002654/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2655/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002656static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002658 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002659
Rafael Espindola15684b22009-04-24 12:40:33 +00002660 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002661 for (int i = 4; i != 8; ++i)
2662 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002663 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002664
Rafael Espindola15684b22009-04-24 12:40:33 +00002665 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 for (int i = 0; i != 4; ++i)
2667 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002668 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002669
Rafael Espindola15684b22009-04-24 12:40:33 +00002670 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002671}
2672
Nate Begeman9008ca62009-04-27 18:41:29 +00002673bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002674 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002675 N->getMask(M);
2676 return ::isPSHUFLWMask(M, N->getValueType(0));
2677}
2678
Nate Begemana09008b2009-10-19 02:17:23 +00002679/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2680/// is suitable for input to PALIGNR.
2681static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2682 bool hasSSSE3) {
2683 int i, e = VT.getVectorNumElements();
2684
2685 // Do not handle v2i64 / v2f64 shuffles with palignr.
2686 if (e < 4 || !hasSSSE3)
2687 return false;
2688
2689 for (i = 0; i != e; ++i)
2690 if (Mask[i] >= 0)
2691 break;
2692
2693 // All undef, not a palignr.
2694 if (i == e)
2695 return false;
2696
2697 // Determine if it's ok to perform a palignr with only the LHS, since we
2698 // don't have access to the actual shuffle elements to see if RHS is undef.
2699 bool Unary = Mask[i] < (int)e;
2700 bool NeedsUnary = false;
2701
2702 int s = Mask[i] - i;
2703
2704 // Check the rest of the elements to see if they are consecutive.
2705 for (++i; i != e; ++i) {
2706 int m = Mask[i];
2707 if (m < 0)
2708 continue;
2709
2710 Unary = Unary && (m < (int)e);
2711 NeedsUnary = NeedsUnary || (m < s);
2712
2713 if (NeedsUnary && !Unary)
2714 return false;
2715 if (Unary && m != ((s+i) & (e-1)))
2716 return false;
2717 if (!Unary && m != (s+i))
2718 return false;
2719 }
2720 return true;
2721}
2722
2723bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2724 SmallVector<int, 8> M;
2725 N->getMask(M);
2726 return ::isPALIGNRMask(M, N->getValueType(0), true);
2727}
2728
Evan Cheng14aed5e2006-03-24 01:18:28 +00002729/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2730/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002731static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002732 int NumElems = VT.getVectorNumElements();
2733 if (NumElems != 2 && NumElems != 4)
2734 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002735
Nate Begeman9008ca62009-04-27 18:41:29 +00002736 int Half = NumElems / 2;
2737 for (int i = 0; i < Half; ++i)
2738 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002739 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002740 for (int i = Half; i < NumElems; ++i)
2741 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002742 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002743
Evan Cheng14aed5e2006-03-24 01:18:28 +00002744 return true;
2745}
2746
Nate Begeman9008ca62009-04-27 18:41:29 +00002747bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2748 SmallVector<int, 8> M;
2749 N->getMask(M);
2750 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002751}
2752
Evan Cheng213d2cf2007-05-17 18:45:50 +00002753/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002754/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2755/// half elements to come from vector 1 (which would equal the dest.) and
2756/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002757static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002758 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002759
2760 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002761 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002762
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 int Half = NumElems / 2;
2764 for (int i = 0; i < Half; ++i)
2765 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002766 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002767 for (int i = Half; i < NumElems; ++i)
2768 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002769 return false;
2770 return true;
2771}
2772
Nate Begeman9008ca62009-04-27 18:41:29 +00002773static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2774 SmallVector<int, 8> M;
2775 N->getMask(M);
2776 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002777}
2778
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002779/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2780/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002781bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2782 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002783 return false;
2784
Evan Cheng2064a2b2006-03-28 06:50:32 +00002785 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002786 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2787 isUndefOrEqual(N->getMaskElt(1), 7) &&
2788 isUndefOrEqual(N->getMaskElt(2), 2) &&
2789 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002790}
2791
Nate Begeman0b10b912009-11-07 23:17:15 +00002792/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2793/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2794/// <2, 3, 2, 3>
2795bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2796 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2797
2798 if (NumElems != 4)
2799 return false;
2800
2801 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2802 isUndefOrEqual(N->getMaskElt(1), 3) &&
2803 isUndefOrEqual(N->getMaskElt(2), 2) &&
2804 isUndefOrEqual(N->getMaskElt(3), 3);
2805}
2806
Evan Cheng5ced1d82006-04-06 23:23:56 +00002807/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2808/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002809bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2810 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002811
Evan Cheng5ced1d82006-04-06 23:23:56 +00002812 if (NumElems != 2 && NumElems != 4)
2813 return false;
2814
Evan Chengc5cdff22006-04-07 21:53:05 +00002815 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002817 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002818
Evan Chengc5cdff22006-04-07 21:53:05 +00002819 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002820 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002821 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002822
2823 return true;
2824}
2825
Nate Begeman0b10b912009-11-07 23:17:15 +00002826/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2827/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2828bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002830
Evan Cheng5ced1d82006-04-06 23:23:56 +00002831 if (NumElems != 2 && NumElems != 4)
2832 return false;
2833
Evan Chengc5cdff22006-04-07 21:53:05 +00002834 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002836 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002837
Nate Begeman9008ca62009-04-27 18:41:29 +00002838 for (unsigned i = 0; i < NumElems/2; ++i)
2839 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002840 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002841
2842 return true;
2843}
2844
Evan Cheng0038e592006-03-28 00:39:58 +00002845/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2846/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002847static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002848 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002849 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002850 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002851 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002852
Nate Begeman9008ca62009-04-27 18:41:29 +00002853 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2854 int BitI = Mask[i];
2855 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002856 if (!isUndefOrEqual(BitI, j))
2857 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002858 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002859 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002860 return false;
2861 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002862 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002863 return false;
2864 }
Evan Cheng0038e592006-03-28 00:39:58 +00002865 }
Evan Cheng0038e592006-03-28 00:39:58 +00002866 return true;
2867}
2868
Nate Begeman9008ca62009-04-27 18:41:29 +00002869bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2870 SmallVector<int, 8> M;
2871 N->getMask(M);
2872 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002873}
2874
Evan Cheng4fcb9222006-03-28 02:43:26 +00002875/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2876/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002877static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002878 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002879 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002880 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002881 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002882
Nate Begeman9008ca62009-04-27 18:41:29 +00002883 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2884 int BitI = Mask[i];
2885 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002886 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002887 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002888 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002889 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002890 return false;
2891 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002892 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002893 return false;
2894 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002895 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002896 return true;
2897}
2898
Nate Begeman9008ca62009-04-27 18:41:29 +00002899bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2900 SmallVector<int, 8> M;
2901 N->getMask(M);
2902 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002903}
2904
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002905/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2906/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2907/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002908static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002909 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002910 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002911 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002912
Nate Begeman9008ca62009-04-27 18:41:29 +00002913 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2914 int BitI = Mask[i];
2915 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002916 if (!isUndefOrEqual(BitI, j))
2917 return false;
2918 if (!isUndefOrEqual(BitI1, j))
2919 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002920 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002921 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002922}
2923
Nate Begeman9008ca62009-04-27 18:41:29 +00002924bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2925 SmallVector<int, 8> M;
2926 N->getMask(M);
2927 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2928}
2929
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002930/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2931/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2932/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002933static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002935 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002937
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2939 int BitI = Mask[i];
2940 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002941 if (!isUndefOrEqual(BitI, j))
2942 return false;
2943 if (!isUndefOrEqual(BitI1, j))
2944 return false;
2945 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002946 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002947}
2948
Nate Begeman9008ca62009-04-27 18:41:29 +00002949bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2950 SmallVector<int, 8> M;
2951 N->getMask(M);
2952 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2953}
2954
Evan Cheng017dcc62006-04-21 01:05:10 +00002955/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2956/// specifies a shuffle of elements that is suitable for input to MOVSS,
2957/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002958static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002959 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002960 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002961
2962 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002963
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002965 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 for (int i = 1; i < NumElts; ++i)
2968 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002969 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002970
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002971 return true;
2972}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002973
Nate Begeman9008ca62009-04-27 18:41:29 +00002974bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2975 SmallVector<int, 8> M;
2976 N->getMask(M);
2977 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002978}
2979
Evan Cheng017dcc62006-04-21 01:05:10 +00002980/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2981/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002982/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002983static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002984 bool V2IsSplat = false, bool V2IsUndef = false) {
2985 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002986 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002987 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002988
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002990 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002991
Nate Begeman9008ca62009-04-27 18:41:29 +00002992 for (int i = 1; i < NumOps; ++i)
2993 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2994 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2995 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002996 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002997
Evan Cheng39623da2006-04-20 08:58:49 +00002998 return true;
2999}
3000
Nate Begeman9008ca62009-04-27 18:41:29 +00003001static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003002 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 SmallVector<int, 8> M;
3004 N->getMask(M);
3005 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003006}
3007
Evan Chengd9539472006-04-14 21:59:03 +00003008/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3009/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003010bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3011 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003012 return false;
3013
3014 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003015 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 int Elt = N->getMaskElt(i);
3017 if (Elt >= 0 && Elt != 1)
3018 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003019 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003020
3021 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003022 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 int Elt = N->getMaskElt(i);
3024 if (Elt >= 0 && Elt != 3)
3025 return false;
3026 if (Elt == 3)
3027 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003028 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003029 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003031 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003032}
3033
3034/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3035/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003036bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3037 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003038 return false;
3039
3040 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 for (unsigned i = 0; i < 2; ++i)
3042 if (N->getMaskElt(i) > 0)
3043 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003044
3045 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003046 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 int Elt = N->getMaskElt(i);
3048 if (Elt >= 0 && Elt != 2)
3049 return false;
3050 if (Elt == 2)
3051 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003052 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003053 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003054 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003055}
3056
Evan Cheng0b457f02008-09-25 20:50:48 +00003057/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3058/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003059bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3060 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003061
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 for (int i = 0; i < e; ++i)
3063 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003064 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 for (int i = 0; i < e; ++i)
3066 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003067 return false;
3068 return true;
3069}
3070
Evan Cheng63d33002006-03-22 08:01:21 +00003071/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003072/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003073unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3075 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3076
Evan Chengb9df0ca2006-03-22 02:53:00 +00003077 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3078 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 for (int i = 0; i < NumOperands; ++i) {
3080 int Val = SVOp->getMaskElt(NumOperands-i-1);
3081 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003082 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003083 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003084 if (i != NumOperands - 1)
3085 Mask <<= Shift;
3086 }
Evan Cheng63d33002006-03-22 08:01:21 +00003087 return Mask;
3088}
3089
Evan Cheng506d3df2006-03-29 23:07:14 +00003090/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003091/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003092unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003093 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003094 unsigned Mask = 0;
3095 // 8 nodes, but we only care about the last 4.
3096 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003097 int Val = SVOp->getMaskElt(i);
3098 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003099 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003100 if (i != 4)
3101 Mask <<= 2;
3102 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003103 return Mask;
3104}
3105
3106/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003107/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003108unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003109 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003110 unsigned Mask = 0;
3111 // 8 nodes, but we only care about the first 4.
3112 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003113 int Val = SVOp->getMaskElt(i);
3114 if (Val >= 0)
3115 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003116 if (i != 0)
3117 Mask <<= 2;
3118 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003119 return Mask;
3120}
3121
Nate Begemana09008b2009-10-19 02:17:23 +00003122/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3123/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3124unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3125 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3126 EVT VVT = N->getValueType(0);
3127 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3128 int Val = 0;
3129
3130 unsigned i, e;
3131 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3132 Val = SVOp->getMaskElt(i);
3133 if (Val >= 0)
3134 break;
3135 }
3136 return (Val - i) * EltSize;
3137}
3138
Evan Cheng37b73872009-07-30 08:33:02 +00003139/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3140/// constant +0.0.
3141bool X86::isZeroNode(SDValue Elt) {
3142 return ((isa<ConstantSDNode>(Elt) &&
3143 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3144 (isa<ConstantFPSDNode>(Elt) &&
3145 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3146}
3147
Nate Begeman9008ca62009-04-27 18:41:29 +00003148/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3149/// their permute mask.
3150static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3151 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003152 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003153 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003155
Nate Begeman5a5ca152009-04-29 05:20:52 +00003156 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 int idx = SVOp->getMaskElt(i);
3158 if (idx < 0)
3159 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003160 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003162 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003163 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003164 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3166 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003167}
3168
Evan Cheng779ccea2007-12-07 21:30:01 +00003169/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3170/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003171static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003172 unsigned NumElems = VT.getVectorNumElements();
3173 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003174 int idx = Mask[i];
3175 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003176 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003177 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003179 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003180 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003181 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003182}
3183
Evan Cheng533a0aa2006-04-19 20:35:22 +00003184/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3185/// match movhlps. The lower half elements should come from upper half of
3186/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003187/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003188static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3189 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003190 return false;
3191 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003193 return false;
3194 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003196 return false;
3197 return true;
3198}
3199
Evan Cheng5ced1d82006-04-06 23:23:56 +00003200/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003201/// is promoted to a vector. It also returns the LoadSDNode by reference if
3202/// required.
3203static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003204 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3205 return false;
3206 N = N->getOperand(0).getNode();
3207 if (!ISD::isNON_EXTLoad(N))
3208 return false;
3209 if (LD)
3210 *LD = cast<LoadSDNode>(N);
3211 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003212}
3213
Evan Cheng533a0aa2006-04-19 20:35:22 +00003214/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3215/// match movlp{s|d}. The lower half elements should come from lower half of
3216/// V1 (and in order), and the upper half elements should come from the upper
3217/// half of V2 (and in order). And since V1 will become the source of the
3218/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003219static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3220 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003221 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003222 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003223 // Is V2 is a vector load, don't do this transformation. We will try to use
3224 // load folding shufps op.
3225 if (ISD::isNON_EXTLoad(V2))
3226 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003227
Nate Begeman5a5ca152009-04-29 05:20:52 +00003228 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003229
Evan Cheng533a0aa2006-04-19 20:35:22 +00003230 if (NumElems != 2 && NumElems != 4)
3231 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003232 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003234 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003235 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003237 return false;
3238 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003239}
3240
Evan Cheng39623da2006-04-20 08:58:49 +00003241/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3242/// all the same.
3243static bool isSplatVector(SDNode *N) {
3244 if (N->getOpcode() != ISD::BUILD_VECTOR)
3245 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003246
Dan Gohman475871a2008-07-27 21:46:04 +00003247 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003248 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3249 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250 return false;
3251 return true;
3252}
3253
Evan Cheng213d2cf2007-05-17 18:45:50 +00003254/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003255/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003256/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003257static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003258 SDValue V1 = N->getOperand(0);
3259 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003260 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3261 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003262 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003263 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003264 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003265 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3266 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003267 if (Opc != ISD::BUILD_VECTOR ||
3268 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 return false;
3270 } else if (Idx >= 0) {
3271 unsigned Opc = V1.getOpcode();
3272 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3273 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003274 if (Opc != ISD::BUILD_VECTOR ||
3275 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003276 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003277 }
3278 }
3279 return true;
3280}
3281
3282/// getZeroVector - Returns a vector of specified type with all zero elements.
3283///
Owen Andersone50ed302009-08-10 22:56:29 +00003284static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003285 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003286 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003287
Chris Lattner8a594482007-11-25 00:24:49 +00003288 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3289 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003290 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3293 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003294 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3296 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003297 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3299 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003300 }
Dale Johannesenace16102009-02-03 19:33:06 +00003301 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003302}
3303
Chris Lattner8a594482007-11-25 00:24:49 +00003304/// getOnesVector - Returns a vector of specified type with all bits set.
3305///
Owen Andersone50ed302009-08-10 22:56:29 +00003306static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003307 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003308
Chris Lattner8a594482007-11-25 00:24:49 +00003309 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3310 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003312 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003313 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003315 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003317 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003318}
3319
3320
Evan Cheng39623da2006-04-20 08:58:49 +00003321/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3322/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003323static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003324 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003325 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003326
Evan Cheng39623da2006-04-20 08:58:49 +00003327 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 SmallVector<int, 8> MaskVec;
3329 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003330
Nate Begeman5a5ca152009-04-29 05:20:52 +00003331 for (unsigned i = 0; i != NumElems; ++i) {
3332 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 MaskVec[i] = NumElems;
3334 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003335 }
Evan Cheng39623da2006-04-20 08:58:49 +00003336 }
Evan Cheng39623da2006-04-20 08:58:49 +00003337 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3339 SVOp->getOperand(1), &MaskVec[0]);
3340 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003341}
3342
Evan Cheng017dcc62006-04-21 01:05:10 +00003343/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3344/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003345static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 SDValue V2) {
3347 unsigned NumElems = VT.getVectorNumElements();
3348 SmallVector<int, 8> Mask;
3349 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003350 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 Mask.push_back(i);
3352 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003353}
3354
Nate Begeman9008ca62009-04-27 18:41:29 +00003355/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003356static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003357 SDValue V2) {
3358 unsigned NumElems = VT.getVectorNumElements();
3359 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003360 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 Mask.push_back(i);
3362 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003363 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003365}
3366
Nate Begeman9008ca62009-04-27 18:41:29 +00003367/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003368static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 SDValue V2) {
3370 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003371 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003372 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003373 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 Mask.push_back(i + Half);
3375 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003376 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003378}
3379
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003380/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003381static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 bool HasSSE2) {
3383 if (SV->getValueType(0).getVectorNumElements() <= 4)
3384 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003385
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003387 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 DebugLoc dl = SV->getDebugLoc();
3389 SDValue V1 = SV->getOperand(0);
3390 int NumElems = VT.getVectorNumElements();
3391 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003392
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 // unpack elements to the correct location
3394 while (NumElems > 4) {
3395 if (EltNo < NumElems/2) {
3396 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3397 } else {
3398 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3399 EltNo -= NumElems/2;
3400 }
3401 NumElems >>= 1;
3402 }
Eric Christopherfd179292009-08-27 18:07:15 +00003403
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 // Perform the splat.
3405 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003406 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3408 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003409}
3410
Evan Chengba05f722006-04-21 23:03:30 +00003411/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003412/// vector of zero or undef vector. This produces a shuffle where the low
3413/// element of V2 is swizzled into the zero/undef vector, landing at element
3414/// 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 +00003415static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003416 bool isZero, bool HasSSE2,
3417 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003418 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003419 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3421 unsigned NumElems = VT.getVectorNumElements();
3422 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003423 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 // If this is the insertion idx, put the low elt of V2 here.
3425 MaskVec.push_back(i == Idx ? NumElems : i);
3426 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003427}
3428
Evan Chengf26ffe92008-05-29 08:22:04 +00003429/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3430/// a shuffle that is zero.
3431static
Nate Begeman9008ca62009-04-27 18:41:29 +00003432unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3433 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003434 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003435 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003436 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003437 int Idx = SVOp->getMaskElt(Index);
3438 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003439 ++NumZeros;
3440 continue;
3441 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003443 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003444 ++NumZeros;
3445 else
3446 break;
3447 }
3448 return NumZeros;
3449}
3450
3451/// isVectorShift - Returns true if the shuffle can be implemented as a
3452/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003453/// FIXME: split into pslldqi, psrldqi, palignr variants.
3454static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003455 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003457
3458 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003459 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003460 if (!NumZeros) {
3461 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003463 if (!NumZeros)
3464 return false;
3465 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003466 bool SeenV1 = false;
3467 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003468 for (int i = NumZeros; i < NumElems; ++i) {
3469 int Val = isLeft ? (i - NumZeros) : i;
3470 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3471 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003472 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003474 SeenV1 = true;
3475 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003477 SeenV2 = true;
3478 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003480 return false;
3481 }
3482 if (SeenV1 && SeenV2)
3483 return false;
3484
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003486 ShAmt = NumZeros;
3487 return true;
3488}
3489
3490
Evan Chengc78d3b42006-04-24 18:01:45 +00003491/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3492///
Dan Gohman475871a2008-07-27 21:46:04 +00003493static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003494 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003495 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003496 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003497 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003498
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003499 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003500 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003501 bool First = true;
3502 for (unsigned i = 0; i < 16; ++i) {
3503 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3504 if (ThisIsNonZero && First) {
3505 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003507 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003509 First = false;
3510 }
3511
3512 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003513 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003514 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3515 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003516 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003518 }
3519 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3521 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3522 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003523 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003525 } else
3526 ThisElt = LastElt;
3527
Gabor Greifba36cb52008-08-28 21:40:38 +00003528 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003530 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003531 }
3532 }
3533
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003535}
3536
Bill Wendlinga348c562007-03-22 18:42:45 +00003537/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003538///
Dan Gohman475871a2008-07-27 21:46:04 +00003539static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003540 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003541 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003542 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003543 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003544
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003545 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003546 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003547 bool First = true;
3548 for (unsigned i = 0; i < 8; ++i) {
3549 bool isNonZero = (NonZeros & (1 << i)) != 0;
3550 if (isNonZero) {
3551 if (First) {
3552 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003554 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003556 First = false;
3557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003558 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003560 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003561 }
3562 }
3563
3564 return V;
3565}
3566
Evan Chengf26ffe92008-05-29 08:22:04 +00003567/// getVShift - Return a vector logical shift node.
3568///
Owen Andersone50ed302009-08-10 22:56:29 +00003569static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003570 unsigned NumBits, SelectionDAG &DAG,
3571 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003574 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003575 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3576 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3577 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003578 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003579}
3580
Dan Gohman475871a2008-07-27 21:46:04 +00003581SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003582X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3583 SelectionDAG &DAG) {
3584
3585 // Check if the scalar load can be widened into a vector load. And if
3586 // the address is "base + cst" see if the cst can be "absorbed" into
3587 // the shuffle mask.
3588 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3589 SDValue Ptr = LD->getBasePtr();
3590 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3591 return SDValue();
3592 EVT PVT = LD->getValueType(0);
3593 if (PVT != MVT::i32 && PVT != MVT::f32)
3594 return SDValue();
3595
3596 int FI = -1;
3597 int64_t Offset = 0;
3598 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3599 FI = FINode->getIndex();
3600 Offset = 0;
3601 } else if (Ptr.getOpcode() == ISD::ADD &&
3602 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3603 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3604 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3605 Offset = Ptr.getConstantOperandVal(1);
3606 Ptr = Ptr.getOperand(0);
3607 } else {
3608 return SDValue();
3609 }
3610
3611 SDValue Chain = LD->getChain();
3612 // Make sure the stack object alignment is at least 16.
3613 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3614 if (DAG.InferPtrAlignment(Ptr) < 16) {
3615 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003616 // Can't change the alignment. FIXME: It's possible to compute
3617 // the exact stack offset and reference FI + adjust offset instead.
3618 // If someone *really* cares about this. That's the way to implement it.
3619 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003620 } else {
3621 MFI->setObjectAlignment(FI, 16);
3622 }
3623 }
3624
3625 // (Offset % 16) must be multiple of 4. Then address is then
3626 // Ptr + (Offset & ~15).
3627 if (Offset < 0)
3628 return SDValue();
3629 if ((Offset % 16) & 3)
3630 return SDValue();
3631 int64_t StartOffset = Offset & ~15;
3632 if (StartOffset)
3633 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3634 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3635
3636 int EltNo = (Offset - StartOffset) >> 2;
3637 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3638 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003639 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3640 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003641 // Canonicalize it to a v4i32 shuffle.
3642 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3643 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3644 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3645 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3646 }
3647
3648 return SDValue();
3649}
3650
3651SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003652X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003653 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003654 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003655 if (ISD::isBuildVectorAllZeros(Op.getNode())
3656 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003657 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3658 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3659 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003660 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003661 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003662
Gabor Greifba36cb52008-08-28 21:40:38 +00003663 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003664 return getOnesVector(Op.getValueType(), DAG, dl);
3665 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003666 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003667
Owen Andersone50ed302009-08-10 22:56:29 +00003668 EVT VT = Op.getValueType();
3669 EVT ExtVT = VT.getVectorElementType();
3670 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003671
3672 unsigned NumElems = Op.getNumOperands();
3673 unsigned NumZero = 0;
3674 unsigned NumNonZero = 0;
3675 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003676 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003677 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003678 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003679 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003680 if (Elt.getOpcode() == ISD::UNDEF)
3681 continue;
3682 Values.insert(Elt);
3683 if (Elt.getOpcode() != ISD::Constant &&
3684 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003685 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003686 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003687 NumZero++;
3688 else {
3689 NonZeros |= (1 << i);
3690 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003691 }
3692 }
3693
Dan Gohman7f321562007-06-25 16:23:39 +00003694 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003695 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003696 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003697 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003698
Chris Lattner67f453a2008-03-09 05:42:06 +00003699 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003700 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003701 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003702 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003703
Chris Lattner62098042008-03-09 01:05:04 +00003704 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3705 // the value are obviously zero, truncate the value to i32 and do the
3706 // insertion that way. Only do this if the value is non-constant or if the
3707 // value is a constant being inserted into element 0. It is cheaper to do
3708 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003709 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003710 (!IsAllConstants || Idx == 0)) {
3711 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3712 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003713 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3714 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003715
Chris Lattner62098042008-03-09 01:05:04 +00003716 // Truncate the value (which may itself be a constant) to i32, and
3717 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003719 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003720 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3721 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003722
Chris Lattner62098042008-03-09 01:05:04 +00003723 // Now we have our 32-bit value zero extended in the low element of
3724 // a vector. If Idx != 0, swizzle it into place.
3725 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003726 SmallVector<int, 4> Mask;
3727 Mask.push_back(Idx);
3728 for (unsigned i = 1; i != VecElts; ++i)
3729 Mask.push_back(i);
3730 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003731 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003732 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003733 }
Dale Johannesenace16102009-02-03 19:33:06 +00003734 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003735 }
3736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003737
Chris Lattner19f79692008-03-08 22:59:52 +00003738 // If we have a constant or non-constant insertion into the low element of
3739 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3740 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003741 // depending on what the source datatype is.
3742 if (Idx == 0) {
3743 if (NumZero == 0) {
3744 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003745 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3746 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003747 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3748 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3749 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3750 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003751 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3752 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3753 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003754 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3755 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3756 Subtarget->hasSSE2(), DAG);
3757 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3758 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003759 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003760
3761 // Is it a vector logical left shift?
3762 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003763 X86::isZeroNode(Op.getOperand(0)) &&
3764 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003765 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003766 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003767 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003768 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003769 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003770 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003771
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003772 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003773 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774
Chris Lattner19f79692008-03-08 22:59:52 +00003775 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3776 // is a non-constant being inserted into an element other than the low one,
3777 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3778 // movd/movss) to move this into the low element, then shuffle it into
3779 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003780 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003781 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003782
Evan Cheng0db9fe62006-04-25 20:13:52 +00003783 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003784 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3785 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003786 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003787 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003788 MaskVec.push_back(i == Idx ? 0 : 1);
3789 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003790 }
3791 }
3792
Chris Lattner67f453a2008-03-09 05:42:06 +00003793 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003794 if (Values.size() == 1) {
3795 if (EVTBits == 32) {
3796 // Instead of a shuffle like this:
3797 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3798 // Check if it's possible to issue this instead.
3799 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3800 unsigned Idx = CountTrailingZeros_32(NonZeros);
3801 SDValue Item = Op.getOperand(Idx);
3802 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3803 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3804 }
Dan Gohman475871a2008-07-27 21:46:04 +00003805 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003806 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003807
Dan Gohmana3941172007-07-24 22:55:08 +00003808 // A vector full of immediates; various special cases are already
3809 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003810 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003811 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003812
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003813 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003814 if (EVTBits == 64) {
3815 if (NumNonZero == 1) {
3816 // One half is zero or undef.
3817 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003818 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003819 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003820 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3821 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003822 }
Dan Gohman475871a2008-07-27 21:46:04 +00003823 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003824 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003825
3826 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003827 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003828 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003829 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003830 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003831 }
3832
Bill Wendling826f36f2007-03-28 00:57:11 +00003833 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003834 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003835 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003836 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003837 }
3838
3839 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003840 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003841 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003842 if (NumElems == 4 && NumZero > 0) {
3843 for (unsigned i = 0; i < 4; ++i) {
3844 bool isZero = !(NonZeros & (1 << i));
3845 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003846 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003847 else
Dale Johannesenace16102009-02-03 19:33:06 +00003848 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 }
3850
3851 for (unsigned i = 0; i < 2; ++i) {
3852 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3853 default: break;
3854 case 0:
3855 V[i] = V[i*2]; // Must be a zero vector.
3856 break;
3857 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003858 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003859 break;
3860 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003861 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003862 break;
3863 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003864 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003865 break;
3866 }
3867 }
3868
Nate Begeman9008ca62009-04-27 18:41:29 +00003869 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 bool Reverse = (NonZeros & 0x3) == 2;
3871 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003872 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003873 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3874 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003875 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3876 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003877 }
3878
3879 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003880 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3881 // values to be inserted is equal to the number of elements, in which case
3882 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003883 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003885 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003886 getSubtarget()->hasSSE41()) {
3887 V[0] = DAG.getUNDEF(VT);
3888 for (unsigned i = 0; i < NumElems; ++i)
3889 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3890 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3891 Op.getOperand(i), DAG.getIntPtrConstant(i));
3892 return V[0];
3893 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894 // Expand into a number of unpckl*.
3895 // e.g. for v4f32
3896 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3897 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3898 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003899 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003900 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003901 NumElems >>= 1;
3902 while (NumElems != 0) {
3903 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003905 NumElems >>= 1;
3906 }
3907 return V[0];
3908 }
3909
Dan Gohman475871a2008-07-27 21:46:04 +00003910 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003911}
3912
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003913SDValue
3914X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3915 // We support concatenate two MMX registers and place them in a MMX
3916 // register. This is better than doing a stack convert.
3917 DebugLoc dl = Op.getDebugLoc();
3918 EVT ResVT = Op.getValueType();
3919 assert(Op.getNumOperands() == 2);
3920 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3921 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3922 int Mask[2];
3923 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3924 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3925 InVec = Op.getOperand(1);
3926 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3927 unsigned NumElts = ResVT.getVectorNumElements();
3928 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3929 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3930 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3931 } else {
3932 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3933 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3934 Mask[0] = 0; Mask[1] = 2;
3935 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3936 }
3937 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3938}
3939
Nate Begemanb9a47b82009-02-23 08:49:38 +00003940// v8i16 shuffles - Prefer shuffles in the following order:
3941// 1. [all] pshuflw, pshufhw, optional move
3942// 2. [ssse3] 1 x pshufb
3943// 3. [ssse3] 2 x pshufb + 1 x por
3944// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003945static
Nate Begeman9008ca62009-04-27 18:41:29 +00003946SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3947 SelectionDAG &DAG, X86TargetLowering &TLI) {
3948 SDValue V1 = SVOp->getOperand(0);
3949 SDValue V2 = SVOp->getOperand(1);
3950 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003951 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003952
Nate Begemanb9a47b82009-02-23 08:49:38 +00003953 // Determine if more than 1 of the words in each of the low and high quadwords
3954 // of the result come from the same quadword of one of the two inputs. Undef
3955 // mask values count as coming from any quadword, for better codegen.
3956 SmallVector<unsigned, 4> LoQuad(4);
3957 SmallVector<unsigned, 4> HiQuad(4);
3958 BitVector InputQuads(4);
3959 for (unsigned i = 0; i < 8; ++i) {
3960 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003962 MaskVals.push_back(EltIdx);
3963 if (EltIdx < 0) {
3964 ++Quad[0];
3965 ++Quad[1];
3966 ++Quad[2];
3967 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003968 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003969 }
3970 ++Quad[EltIdx / 4];
3971 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003972 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003973
Nate Begemanb9a47b82009-02-23 08:49:38 +00003974 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003975 unsigned MaxQuad = 1;
3976 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003977 if (LoQuad[i] > MaxQuad) {
3978 BestLoQuad = i;
3979 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003980 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003981 }
3982
Nate Begemanb9a47b82009-02-23 08:49:38 +00003983 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003984 MaxQuad = 1;
3985 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003986 if (HiQuad[i] > MaxQuad) {
3987 BestHiQuad = i;
3988 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003989 }
3990 }
3991
Nate Begemanb9a47b82009-02-23 08:49:38 +00003992 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003993 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003994 // single pshufb instruction is necessary. If There are more than 2 input
3995 // quads, disable the next transformation since it does not help SSSE3.
3996 bool V1Used = InputQuads[0] || InputQuads[1];
3997 bool V2Used = InputQuads[2] || InputQuads[3];
3998 if (TLI.getSubtarget()->hasSSSE3()) {
3999 if (InputQuads.count() == 2 && V1Used && V2Used) {
4000 BestLoQuad = InputQuads.find_first();
4001 BestHiQuad = InputQuads.find_next(BestLoQuad);
4002 }
4003 if (InputQuads.count() > 2) {
4004 BestLoQuad = -1;
4005 BestHiQuad = -1;
4006 }
4007 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004008
Nate Begemanb9a47b82009-02-23 08:49:38 +00004009 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4010 // the shuffle mask. If a quad is scored as -1, that means that it contains
4011 // words from all 4 input quadwords.
4012 SDValue NewV;
4013 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004014 SmallVector<int, 8> MaskV;
4015 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4016 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004017 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4019 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4020 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004021
Nate Begemanb9a47b82009-02-23 08:49:38 +00004022 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4023 // source words for the shuffle, to aid later transformations.
4024 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004025 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004026 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004027 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004028 if (idx != (int)i)
4029 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004030 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004031 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004032 AllWordsInNewV = false;
4033 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004034 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004035
Nate Begemanb9a47b82009-02-23 08:49:38 +00004036 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4037 if (AllWordsInNewV) {
4038 for (int i = 0; i != 8; ++i) {
4039 int idx = MaskVals[i];
4040 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004041 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004042 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004043 if ((idx != i) && idx < 4)
4044 pshufhw = false;
4045 if ((idx != i) && idx > 3)
4046 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004047 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048 V1 = NewV;
4049 V2Used = false;
4050 BestLoQuad = 0;
4051 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004052 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004053
Nate Begemanb9a47b82009-02-23 08:49:38 +00004054 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4055 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004056 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004057 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004059 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004060 }
Eric Christopherfd179292009-08-27 18:07:15 +00004061
Nate Begemanb9a47b82009-02-23 08:49:38 +00004062 // If we have SSSE3, and all words of the result are from 1 input vector,
4063 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4064 // is present, fall back to case 4.
4065 if (TLI.getSubtarget()->hasSSSE3()) {
4066 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004067
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004069 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004070 // mask, and elements that come from V1 in the V2 mask, so that the two
4071 // results can be OR'd together.
4072 bool TwoInputs = V1Used && V2Used;
4073 for (unsigned i = 0; i != 8; ++i) {
4074 int EltIdx = MaskVals[i] * 2;
4075 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4077 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 continue;
4079 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004080 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4081 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004082 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004084 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004085 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004088 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004089
Nate Begemanb9a47b82009-02-23 08:49:38 +00004090 // Calculate the shuffle mask for the second input, shuffle it, and
4091 // OR it with the first shuffled input.
4092 pshufbMask.clear();
4093 for (unsigned i = 0; i != 8; ++i) {
4094 int EltIdx = MaskVals[i] * 2;
4095 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4097 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 continue;
4099 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004100 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4101 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004102 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004104 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004105 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 MVT::v16i8, &pshufbMask[0], 16));
4107 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4108 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004109 }
4110
4111 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4112 // and update MaskVals with new element order.
4113 BitVector InOrder(8);
4114 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004115 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004116 for (int i = 0; i != 4; ++i) {
4117 int idx = MaskVals[i];
4118 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004119 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004120 InOrder.set(i);
4121 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004122 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004123 InOrder.set(i);
4124 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004125 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004126 }
4127 }
4128 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004129 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004131 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004132 }
Eric Christopherfd179292009-08-27 18:07:15 +00004133
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4135 // and update MaskVals with the new element order.
4136 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004138 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004139 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004140 for (unsigned i = 4; i != 8; ++i) {
4141 int idx = MaskVals[i];
4142 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004143 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004144 InOrder.set(i);
4145 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004146 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004147 InOrder.set(i);
4148 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004149 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004150 }
4151 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004153 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004154 }
Eric Christopherfd179292009-08-27 18:07:15 +00004155
Nate Begemanb9a47b82009-02-23 08:49:38 +00004156 // In case BestHi & BestLo were both -1, which means each quadword has a word
4157 // from each of the four input quadwords, calculate the InOrder bitvector now
4158 // before falling through to the insert/extract cleanup.
4159 if (BestLoQuad == -1 && BestHiQuad == -1) {
4160 NewV = V1;
4161 for (int i = 0; i != 8; ++i)
4162 if (MaskVals[i] < 0 || MaskVals[i] == i)
4163 InOrder.set(i);
4164 }
Eric Christopherfd179292009-08-27 18:07:15 +00004165
Nate Begemanb9a47b82009-02-23 08:49:38 +00004166 // The other elements are put in the right place using pextrw and pinsrw.
4167 for (unsigned i = 0; i != 8; ++i) {
4168 if (InOrder[i])
4169 continue;
4170 int EltIdx = MaskVals[i];
4171 if (EltIdx < 0)
4172 continue;
4173 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004175 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004176 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004177 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004179 DAG.getIntPtrConstant(i));
4180 }
4181 return NewV;
4182}
4183
4184// v16i8 shuffles - Prefer shuffles in the following order:
4185// 1. [ssse3] 1 x pshufb
4186// 2. [ssse3] 2 x pshufb + 1 x por
4187// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4188static
Nate Begeman9008ca62009-04-27 18:41:29 +00004189SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4190 SelectionDAG &DAG, X86TargetLowering &TLI) {
4191 SDValue V1 = SVOp->getOperand(0);
4192 SDValue V2 = SVOp->getOperand(1);
4193 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004194 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004196
Nate Begemanb9a47b82009-02-23 08:49:38 +00004197 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004198 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 // present, fall back to case 3.
4200 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4201 bool V1Only = true;
4202 bool V2Only = true;
4203 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004205 if (EltIdx < 0)
4206 continue;
4207 if (EltIdx < 16)
4208 V2Only = false;
4209 else
4210 V1Only = false;
4211 }
Eric Christopherfd179292009-08-27 18:07:15 +00004212
Nate Begemanb9a47b82009-02-23 08:49:38 +00004213 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4214 if (TLI.getSubtarget()->hasSSSE3()) {
4215 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004216
Nate Begemanb9a47b82009-02-23 08:49:38 +00004217 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004218 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 //
4220 // Otherwise, we have elements from both input vectors, and must zero out
4221 // elements that come from V2 in the first mask, and V1 in the second mask
4222 // so that we can OR them together.
4223 bool TwoInputs = !(V1Only || V2Only);
4224 for (unsigned i = 0; i != 16; ++i) {
4225 int EltIdx = MaskVals[i];
4226 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004228 continue;
4229 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004231 }
4232 // If all the elements are from V2, assign it to V1 and return after
4233 // building the first pshufb.
4234 if (V2Only)
4235 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004237 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 if (!TwoInputs)
4240 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004241
Nate Begemanb9a47b82009-02-23 08:49:38 +00004242 // Calculate the shuffle mask for the second input, shuffle it, and
4243 // OR it with the first shuffled input.
4244 pshufbMask.clear();
4245 for (unsigned i = 0; i != 16; ++i) {
4246 int EltIdx = MaskVals[i];
4247 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 continue;
4250 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004252 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004254 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 MVT::v16i8, &pshufbMask[0], 16));
4256 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004257 }
Eric Christopherfd179292009-08-27 18:07:15 +00004258
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 // No SSSE3 - Calculate in place words and then fix all out of place words
4260 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4261 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4263 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004264 SDValue NewV = V2Only ? V2 : V1;
4265 for (int i = 0; i != 8; ++i) {
4266 int Elt0 = MaskVals[i*2];
4267 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004268
Nate Begemanb9a47b82009-02-23 08:49:38 +00004269 // This word of the result is all undef, skip it.
4270 if (Elt0 < 0 && Elt1 < 0)
4271 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004272
Nate Begemanb9a47b82009-02-23 08:49:38 +00004273 // This word of the result is already in the correct place, skip it.
4274 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4275 continue;
4276 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4277 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004278
Nate Begemanb9a47b82009-02-23 08:49:38 +00004279 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4280 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4281 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004282
4283 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4284 // using a single extract together, load it and store it.
4285 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004287 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004289 DAG.getIntPtrConstant(i));
4290 continue;
4291 }
4292
Nate Begemanb9a47b82009-02-23 08:49:38 +00004293 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004294 // source byte is not also odd, shift the extracted word left 8 bits
4295 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004296 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004298 DAG.getIntPtrConstant(Elt1 / 2));
4299 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004300 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004301 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004302 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4304 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004305 }
4306 // If Elt0 is defined, extract it from the appropriate source. If the
4307 // source byte is not also even, shift the extracted word right 8 bits. If
4308 // Elt1 was also defined, OR the extracted values together before
4309 // inserting them in the result.
4310 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004312 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4313 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004315 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004316 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004317 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4318 DAG.getConstant(0x00FF, MVT::i16));
4319 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004320 : InsElt0;
4321 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004323 DAG.getIntPtrConstant(i));
4324 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004326}
4327
Evan Cheng7a831ce2007-12-15 03:00:47 +00004328/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4329/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4330/// done when every pair / quad of shuffle mask elements point to elements in
4331/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004332/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4333static
Nate Begeman9008ca62009-04-27 18:41:29 +00004334SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4335 SelectionDAG &DAG,
4336 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004337 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004338 SDValue V1 = SVOp->getOperand(0);
4339 SDValue V2 = SVOp->getOperand(1);
4340 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004341 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004343 EVT MaskEltVT = MaskVT.getVectorElementType();
4344 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004346 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 case MVT::v4f32: NewVT = MVT::v2f64; break;
4348 case MVT::v4i32: NewVT = MVT::v2i64; break;
4349 case MVT::v8i16: NewVT = MVT::v4i32; break;
4350 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004351 }
4352
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004353 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004354 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004355 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004356 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004358 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 int Scale = NumElems / NewWidth;
4360 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004361 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 int StartIdx = -1;
4363 for (int j = 0; j < Scale; ++j) {
4364 int EltIdx = SVOp->getMaskElt(i+j);
4365 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004366 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004368 StartIdx = EltIdx - (EltIdx % Scale);
4369 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004370 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004371 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004372 if (StartIdx == -1)
4373 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004374 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004375 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004376 }
4377
Dale Johannesenace16102009-02-03 19:33:06 +00004378 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4379 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004380 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004381}
4382
Evan Chengd880b972008-05-09 21:53:03 +00004383/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004384///
Owen Andersone50ed302009-08-10 22:56:29 +00004385static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004386 SDValue SrcOp, SelectionDAG &DAG,
4387 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004389 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004390 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004391 LD = dyn_cast<LoadSDNode>(SrcOp);
4392 if (!LD) {
4393 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4394 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004395 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4396 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004397 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4398 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004399 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004400 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004402 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4403 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4404 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4405 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004406 SrcOp.getOperand(0)
4407 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004408 }
4409 }
4410 }
4411
Dale Johannesenace16102009-02-03 19:33:06 +00004412 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4413 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004414 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004415 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004416}
4417
Evan Chengace3c172008-07-22 21:13:36 +00004418/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4419/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004420static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004421LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4422 SDValue V1 = SVOp->getOperand(0);
4423 SDValue V2 = SVOp->getOperand(1);
4424 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004425 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004426
Evan Chengace3c172008-07-22 21:13:36 +00004427 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004428 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004429 SmallVector<int, 8> Mask1(4U, -1);
4430 SmallVector<int, 8> PermMask;
4431 SVOp->getMask(PermMask);
4432
Evan Chengace3c172008-07-22 21:13:36 +00004433 unsigned NumHi = 0;
4434 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004435 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 int Idx = PermMask[i];
4437 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004438 Locs[i] = std::make_pair(-1, -1);
4439 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4441 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004442 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004443 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004444 NumLo++;
4445 } else {
4446 Locs[i] = std::make_pair(1, NumHi);
4447 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004449 NumHi++;
4450 }
4451 }
4452 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004453
Evan Chengace3c172008-07-22 21:13:36 +00004454 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004455 // If no more than two elements come from either vector. This can be
4456 // implemented with two shuffles. First shuffle gather the elements.
4457 // The second shuffle, which takes the first shuffle as both of its
4458 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004460
Nate Begeman9008ca62009-04-27 18:41:29 +00004461 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004462
Evan Chengace3c172008-07-22 21:13:36 +00004463 for (unsigned i = 0; i != 4; ++i) {
4464 if (Locs[i].first == -1)
4465 continue;
4466 else {
4467 unsigned Idx = (i < 2) ? 0 : 4;
4468 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004470 }
4471 }
4472
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004474 } else if (NumLo == 3 || NumHi == 3) {
4475 // Otherwise, we must have three elements from one vector, call it X, and
4476 // one element from the other, call it Y. First, use a shufps to build an
4477 // intermediate vector with the one element from Y and the element from X
4478 // that will be in the same half in the final destination (the indexes don't
4479 // matter). Then, use a shufps to build the final vector, taking the half
4480 // containing the element from Y from the intermediate, and the other half
4481 // from X.
4482 if (NumHi == 3) {
4483 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004485 std::swap(V1, V2);
4486 }
4487
4488 // Find the element from V2.
4489 unsigned HiIndex;
4490 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004491 int Val = PermMask[HiIndex];
4492 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004493 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004494 if (Val >= 4)
4495 break;
4496 }
4497
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 Mask1[0] = PermMask[HiIndex];
4499 Mask1[1] = -1;
4500 Mask1[2] = PermMask[HiIndex^1];
4501 Mask1[3] = -1;
4502 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004503
4504 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 Mask1[0] = PermMask[0];
4506 Mask1[1] = PermMask[1];
4507 Mask1[2] = HiIndex & 1 ? 6 : 4;
4508 Mask1[3] = HiIndex & 1 ? 4 : 6;
4509 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004510 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004511 Mask1[0] = HiIndex & 1 ? 2 : 0;
4512 Mask1[1] = HiIndex & 1 ? 0 : 2;
4513 Mask1[2] = PermMask[2];
4514 Mask1[3] = PermMask[3];
4515 if (Mask1[2] >= 0)
4516 Mask1[2] += 4;
4517 if (Mask1[3] >= 0)
4518 Mask1[3] += 4;
4519 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004520 }
Evan Chengace3c172008-07-22 21:13:36 +00004521 }
4522
4523 // Break it into (shuffle shuffle_hi, shuffle_lo).
4524 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 SmallVector<int,8> LoMask(4U, -1);
4526 SmallVector<int,8> HiMask(4U, -1);
4527
4528 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004529 unsigned MaskIdx = 0;
4530 unsigned LoIdx = 0;
4531 unsigned HiIdx = 2;
4532 for (unsigned i = 0; i != 4; ++i) {
4533 if (i == 2) {
4534 MaskPtr = &HiMask;
4535 MaskIdx = 1;
4536 LoIdx = 0;
4537 HiIdx = 2;
4538 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 int Idx = PermMask[i];
4540 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004541 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004542 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004543 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004544 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004545 LoIdx++;
4546 } else {
4547 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004548 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004549 HiIdx++;
4550 }
4551 }
4552
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4554 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4555 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004556 for (unsigned i = 0; i != 4; ++i) {
4557 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004559 } else {
4560 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004562 }
4563 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004564 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004565}
4566
Dan Gohman475871a2008-07-27 21:46:04 +00004567SDValue
4568X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004570 SDValue V1 = Op.getOperand(0);
4571 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004572 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004573 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004575 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4577 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004578 bool V1IsSplat = false;
4579 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004580
Nate Begeman9008ca62009-04-27 18:41:29 +00004581 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004582 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004583
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 // Promote splats to v4f32.
4585 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004586 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004587 return Op;
4588 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004589 }
4590
Evan Cheng7a831ce2007-12-15 03:00:47 +00004591 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4592 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004593 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004595 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004596 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004597 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004599 // FIXME: Figure out a cleaner way to do this.
4600 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004601 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004602 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004603 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4605 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4606 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004607 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004608 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004609 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4610 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004611 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004612 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004613 }
4614 }
Eric Christopherfd179292009-08-27 18:07:15 +00004615
Nate Begeman9008ca62009-04-27 18:41:29 +00004616 if (X86::isPSHUFDMask(SVOp))
4617 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004618
Evan Chengf26ffe92008-05-29 08:22:04 +00004619 // Check if this can be converted into a logical shift.
4620 bool isLeft = false;
4621 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004622 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004623 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004624 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004625 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004626 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004627 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004628 EVT EltVT = VT.getVectorElementType();
4629 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004630 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004631 }
Eric Christopherfd179292009-08-27 18:07:15 +00004632
Nate Begeman9008ca62009-04-27 18:41:29 +00004633 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004634 if (V1IsUndef)
4635 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004636 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004637 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004638 if (!isMMX)
4639 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004640 }
Eric Christopherfd179292009-08-27 18:07:15 +00004641
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 // FIXME: fold these into legal mask.
4643 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4644 X86::isMOVSLDUPMask(SVOp) ||
4645 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004646 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004647 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004648 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649
Nate Begeman9008ca62009-04-27 18:41:29 +00004650 if (ShouldXformToMOVHLPS(SVOp) ||
4651 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4652 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653
Evan Chengf26ffe92008-05-29 08:22:04 +00004654 if (isShift) {
4655 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004656 EVT EltVT = VT.getVectorElementType();
4657 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004658 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004659 }
Eric Christopherfd179292009-08-27 18:07:15 +00004660
Evan Cheng9eca5e82006-10-25 21:49:50 +00004661 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004662 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4663 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004664 V1IsSplat = isSplatVector(V1.getNode());
4665 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004666
Chris Lattner8a594482007-11-25 00:24:49 +00004667 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004668 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004669 Op = CommuteVectorShuffle(SVOp, DAG);
4670 SVOp = cast<ShuffleVectorSDNode>(Op);
4671 V1 = SVOp->getOperand(0);
4672 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004673 std::swap(V1IsSplat, V2IsSplat);
4674 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004675 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004676 }
4677
Nate Begeman9008ca62009-04-27 18:41:29 +00004678 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4679 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004680 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 return V1;
4682 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4683 // the instruction selector will not match, so get a canonical MOVL with
4684 // swapped operands to undo the commute.
4685 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004686 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004687
Nate Begeman9008ca62009-04-27 18:41:29 +00004688 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4689 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4690 X86::isUNPCKLMask(SVOp) ||
4691 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004692 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004693
Evan Cheng9bbbb982006-10-25 20:48:19 +00004694 if (V2IsSplat) {
4695 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004696 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004697 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004698 SDValue NewMask = NormalizeMask(SVOp, DAG);
4699 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4700 if (NSVOp != SVOp) {
4701 if (X86::isUNPCKLMask(NSVOp, true)) {
4702 return NewMask;
4703 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4704 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004705 }
4706 }
4707 }
4708
Evan Cheng9eca5e82006-10-25 21:49:50 +00004709 if (Commuted) {
4710 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004711 // FIXME: this seems wrong.
4712 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4713 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4714 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4715 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4716 X86::isUNPCKLMask(NewSVOp) ||
4717 X86::isUNPCKHMask(NewSVOp))
4718 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004719 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004720
Nate Begemanb9a47b82009-02-23 08:49:38 +00004721 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004722
4723 // Normalize the node to match x86 shuffle ops if needed
4724 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4725 return CommuteVectorShuffle(SVOp, DAG);
4726
4727 // Check for legal shuffle and return?
4728 SmallVector<int, 16> PermMask;
4729 SVOp->getMask(PermMask);
4730 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004731 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004732
Evan Cheng14b32e12007-12-11 01:46:18 +00004733 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004736 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004737 return NewOp;
4738 }
4739
Owen Anderson825b72b2009-08-11 20:47:22 +00004740 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004741 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004742 if (NewOp.getNode())
4743 return NewOp;
4744 }
Eric Christopherfd179292009-08-27 18:07:15 +00004745
Evan Chengace3c172008-07-22 21:13:36 +00004746 // Handle all 4 wide cases with a number of shuffles except for MMX.
4747 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004748 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004749
Dan Gohman475871a2008-07-27 21:46:04 +00004750 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004751}
4752
Dan Gohman475871a2008-07-27 21:46:04 +00004753SDValue
4754X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004755 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004756 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004757 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004758 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004759 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004760 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004761 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004762 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004763 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004764 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004765 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4766 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4767 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4769 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004770 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004772 Op.getOperand(0)),
4773 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004775 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004776 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004777 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004778 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004780 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4781 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004782 // result has a single use which is a store or a bitcast to i32. And in
4783 // the case of a store, it's not worth it if the index is a constant 0,
4784 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004785 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004786 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004787 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004788 if ((User->getOpcode() != ISD::STORE ||
4789 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4790 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004791 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004792 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004793 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4795 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004796 Op.getOperand(0)),
4797 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004798 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4799 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004800 // ExtractPS works with constant index.
4801 if (isa<ConstantSDNode>(Op.getOperand(1)))
4802 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004803 }
Dan Gohman475871a2008-07-27 21:46:04 +00004804 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004805}
4806
4807
Dan Gohman475871a2008-07-27 21:46:04 +00004808SDValue
4809X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004810 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004812
Evan Cheng62a3f152008-03-24 21:52:23 +00004813 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004814 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004815 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004816 return Res;
4817 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004818
Owen Andersone50ed302009-08-10 22:56:29 +00004819 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004820 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004823 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004824 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004825 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4827 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004828 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004829 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004830 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004831 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004832 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004833 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004834 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004835 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004836 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004837 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004838 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004839 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004840 if (Idx == 0)
4841 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004842
Evan Cheng0db9fe62006-04-25 20:13:52 +00004843 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004844 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004845 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004846 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004847 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004848 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004849 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004850 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004851 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4852 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4853 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004854 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004855 if (Idx == 0)
4856 return Op;
4857
4858 // UNPCKHPD the element to the lowest double word, then movsd.
4859 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4860 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004862 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004863 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004864 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004865 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004866 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004867 }
4868
Dan Gohman475871a2008-07-27 21:46:04 +00004869 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004870}
4871
Dan Gohman475871a2008-07-27 21:46:04 +00004872SDValue
4873X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004874 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004875 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004876 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004877
Dan Gohman475871a2008-07-27 21:46:04 +00004878 SDValue N0 = Op.getOperand(0);
4879 SDValue N1 = Op.getOperand(1);
4880 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004881
Dan Gohman8a55ce42009-09-23 21:02:20 +00004882 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004883 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004884 unsigned Opc;
4885 if (VT == MVT::v8i16)
4886 Opc = X86ISD::PINSRW;
4887 else if (VT == MVT::v4i16)
4888 Opc = X86ISD::MMX_PINSRW;
4889 else if (VT == MVT::v16i8)
4890 Opc = X86ISD::PINSRB;
4891 else
4892 Opc = X86ISD::PINSRB;
4893
Nate Begeman14d12ca2008-02-11 04:19:36 +00004894 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4895 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004896 if (N1.getValueType() != MVT::i32)
4897 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4898 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004899 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004900 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004901 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004902 // Bits [7:6] of the constant are the source select. This will always be
4903 // zero here. The DAG Combiner may combine an extract_elt index into these
4904 // bits. For example (insert (extract, 3), 2) could be matched by putting
4905 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004906 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004907 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004908 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004909 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004910 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004911 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004912 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004913 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004914 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004915 // PINSR* works with constant index.
4916 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004917 }
Dan Gohman475871a2008-07-27 21:46:04 +00004918 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004919}
4920
Dan Gohman475871a2008-07-27 21:46:04 +00004921SDValue
4922X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004923 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004924 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004925
4926 if (Subtarget->hasSSE41())
4927 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4928
Dan Gohman8a55ce42009-09-23 21:02:20 +00004929 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004930 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004931
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004932 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004933 SDValue N0 = Op.getOperand(0);
4934 SDValue N1 = Op.getOperand(1);
4935 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004936
Dan Gohman8a55ce42009-09-23 21:02:20 +00004937 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004938 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4939 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 if (N1.getValueType() != MVT::i32)
4941 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4942 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004944 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4945 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004946 }
Dan Gohman475871a2008-07-27 21:46:04 +00004947 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948}
4949
Dan Gohman475871a2008-07-27 21:46:04 +00004950SDValue
4951X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004952 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004953 if (Op.getValueType() == MVT::v2f32)
4954 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4955 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4956 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004957 Op.getOperand(0))));
4958
Owen Anderson825b72b2009-08-11 20:47:22 +00004959 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4960 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004961
Owen Anderson825b72b2009-08-11 20:47:22 +00004962 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4963 EVT VT = MVT::v2i32;
4964 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004965 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004966 case MVT::v16i8:
4967 case MVT::v8i16:
4968 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004969 break;
4970 }
Dale Johannesenace16102009-02-03 19:33:06 +00004971 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4972 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004973}
4974
Bill Wendling056292f2008-09-16 21:48:12 +00004975// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4976// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4977// one of the above mentioned nodes. It has to be wrapped because otherwise
4978// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4979// be used to form addressing mode. These wrapped nodes will be selected
4980// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004981SDValue
4982X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004983 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004984
Chris Lattner41621a22009-06-26 19:22:52 +00004985 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4986 // global base reg.
4987 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004988 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004989 CodeModel::Model M = getTargetMachine().getCodeModel();
4990
Chris Lattner4f066492009-07-11 20:29:19 +00004991 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004992 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004993 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004994 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004995 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004996 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004997 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004998
Evan Cheng1606e8e2009-03-13 07:51:59 +00004999 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005000 CP->getAlignment(),
5001 CP->getOffset(), OpFlag);
5002 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005003 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005004 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005005 if (OpFlag) {
5006 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005007 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00005008 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005009 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005010 }
5011
5012 return Result;
5013}
5014
Chris Lattner18c59872009-06-27 04:16:01 +00005015SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
5016 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005017
Chris Lattner18c59872009-06-27 04:16:01 +00005018 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5019 // global base reg.
5020 unsigned char OpFlag = 0;
5021 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005022 CodeModel::Model M = getTargetMachine().getCodeModel();
5023
Chris Lattner4f066492009-07-11 20:29:19 +00005024 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005025 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005026 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005027 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005028 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005029 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005030 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005031
Chris Lattner18c59872009-06-27 04:16:01 +00005032 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5033 OpFlag);
5034 DebugLoc DL = JT->getDebugLoc();
5035 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005036
Chris Lattner18c59872009-06-27 04:16:01 +00005037 // With PIC, the address is actually $g + Offset.
5038 if (OpFlag) {
5039 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5040 DAG.getNode(X86ISD::GlobalBaseReg,
5041 DebugLoc::getUnknownLoc(), getPointerTy()),
5042 Result);
5043 }
Eric Christopherfd179292009-08-27 18:07:15 +00005044
Chris Lattner18c59872009-06-27 04:16:01 +00005045 return Result;
5046}
5047
5048SDValue
5049X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5050 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005051
Chris Lattner18c59872009-06-27 04:16:01 +00005052 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5053 // global base reg.
5054 unsigned char OpFlag = 0;
5055 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005056 CodeModel::Model M = getTargetMachine().getCodeModel();
5057
Chris Lattner4f066492009-07-11 20:29:19 +00005058 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005059 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005060 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005061 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005062 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005063 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005064 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005065
Chris Lattner18c59872009-06-27 04:16:01 +00005066 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005067
Chris Lattner18c59872009-06-27 04:16:01 +00005068 DebugLoc DL = Op.getDebugLoc();
5069 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005070
5071
Chris Lattner18c59872009-06-27 04:16:01 +00005072 // With PIC, the address is actually $g + Offset.
5073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005074 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005075 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5076 DAG.getNode(X86ISD::GlobalBaseReg,
5077 DebugLoc::getUnknownLoc(),
5078 getPointerTy()),
5079 Result);
5080 }
Eric Christopherfd179292009-08-27 18:07:15 +00005081
Chris Lattner18c59872009-06-27 04:16:01 +00005082 return Result;
5083}
5084
Dan Gohman475871a2008-07-27 21:46:04 +00005085SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005086X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005087 // Create the TargetBlockAddressAddress node.
5088 unsigned char OpFlags =
5089 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005090 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00005091 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5092 DebugLoc dl = Op.getDebugLoc();
5093 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5094 /*isTarget=*/true, OpFlags);
5095
Dan Gohmanf705adb2009-10-30 01:28:02 +00005096 if (Subtarget->isPICStyleRIPRel() &&
5097 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005098 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5099 else
5100 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005101
Dan Gohman29cbade2009-11-20 23:18:13 +00005102 // With PIC, the address is actually $g + Offset.
5103 if (isGlobalRelativeToPICBase(OpFlags)) {
5104 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5105 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5106 Result);
5107 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005108
5109 return Result;
5110}
5111
5112SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005113X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005114 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005115 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005116 // Create the TargetGlobalAddress node, folding in the constant
5117 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005118 unsigned char OpFlags =
5119 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005120 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005121 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005122 if (OpFlags == X86II::MO_NO_FLAG &&
5123 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005124 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005125 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005126 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005127 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005128 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005129 }
Eric Christopherfd179292009-08-27 18:07:15 +00005130
Chris Lattner4f066492009-07-11 20:29:19 +00005131 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005132 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005133 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5134 else
5135 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005136
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005137 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005138 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005139 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5140 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005141 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005143
Chris Lattner36c25012009-07-10 07:34:39 +00005144 // For globals that require a load from a stub to get the address, emit the
5145 // load.
5146 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005147 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005148 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005149
Dan Gohman6520e202008-10-18 02:06:02 +00005150 // If there was a non-zero offset that we didn't fold, create an explicit
5151 // addition for it.
5152 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005153 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005154 DAG.getConstant(Offset, getPointerTy()));
5155
Evan Cheng0db9fe62006-04-25 20:13:52 +00005156 return Result;
5157}
5158
Evan Chengda43bcf2008-09-24 00:05:32 +00005159SDValue
5160X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5161 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005162 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005163 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005164}
5165
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005166static SDValue
5167GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005168 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005169 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005170 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005171 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005172 DebugLoc dl = GA->getDebugLoc();
5173 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5174 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005175 GA->getOffset(),
5176 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005177 if (InFlag) {
5178 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005179 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005180 } else {
5181 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005182 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005183 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005184
5185 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5186 MFI->setHasCalls(true);
5187
Rafael Espindola15f1b662009-04-24 12:59:40 +00005188 SDValue Flag = Chain.getValue(1);
5189 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005190}
5191
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005192// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005193static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005194LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005195 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005196 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005197 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5198 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005199 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005200 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005201 PtrVT), InFlag);
5202 InFlag = Chain.getValue(1);
5203
Chris Lattnerb903bed2009-06-26 21:20:29 +00005204 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005205}
5206
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005207// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005208static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005209LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005210 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005211 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5212 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005213}
5214
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005215// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5216// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005217static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005218 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005219 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005220 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005221 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005222 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5223 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005224 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005225 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005226
5227 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005228 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005229
Chris Lattnerb903bed2009-06-26 21:20:29 +00005230 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005231 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5232 // initialexec.
5233 unsigned WrapperKind = X86ISD::Wrapper;
5234 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005235 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005236 } else if (is64Bit) {
5237 assert(model == TLSModel::InitialExec);
5238 OperandFlags = X86II::MO_GOTTPOFF;
5239 WrapperKind = X86ISD::WrapperRIP;
5240 } else {
5241 assert(model == TLSModel::InitialExec);
5242 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005243 }
Eric Christopherfd179292009-08-27 18:07:15 +00005244
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005245 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5246 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005247 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005248 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005249 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005250
Rafael Espindola9a580232009-02-27 13:37:18 +00005251 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005252 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005253 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005254
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005255 // The address of the thread local variable is the add of the thread
5256 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005257 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005258}
5259
Dan Gohman475871a2008-07-27 21:46:04 +00005260SDValue
5261X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005262 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005263 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005264 assert(Subtarget->isTargetELF() &&
5265 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005266 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005267 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005268
Chris Lattnerb903bed2009-06-26 21:20:29 +00005269 // If GV is an alias then use the aliasee for determining
5270 // thread-localness.
5271 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5272 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005273
Chris Lattnerb903bed2009-06-26 21:20:29 +00005274 TLSModel::Model model = getTLSModel(GV,
5275 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005276
Chris Lattnerb903bed2009-06-26 21:20:29 +00005277 switch (model) {
5278 case TLSModel::GeneralDynamic:
5279 case TLSModel::LocalDynamic: // not implemented
5280 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005281 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005282 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005283
Chris Lattnerb903bed2009-06-26 21:20:29 +00005284 case TLSModel::InitialExec:
5285 case TLSModel::LocalExec:
5286 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5287 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005288 }
Eric Christopherfd179292009-08-27 18:07:15 +00005289
Torok Edwinc23197a2009-07-14 16:55:14 +00005290 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005291 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005292}
5293
Evan Cheng0db9fe62006-04-25 20:13:52 +00005294
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005295/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005296/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005297SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005298 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005299 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005300 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005301 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005302 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue ShOpLo = Op.getOperand(0);
5304 SDValue ShOpHi = Op.getOperand(1);
5305 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005306 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005307 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005308 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005309
Dan Gohman475871a2008-07-27 21:46:04 +00005310 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005311 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005312 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5313 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005314 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005315 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5316 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005317 }
Evan Chenge3413162006-01-09 18:33:28 +00005318
Owen Anderson825b72b2009-08-11 20:47:22 +00005319 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5320 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005321 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005322 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005323
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005326 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5327 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005328
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005329 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005330 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5331 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005332 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005333 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5334 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005335 }
5336
Dan Gohman475871a2008-07-27 21:46:04 +00005337 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005338 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005339}
Evan Chenga3195e82006-01-12 22:54:21 +00005340
Dan Gohman475871a2008-07-27 21:46:04 +00005341SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005342 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005343
5344 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005346 return Op;
5347 }
5348 return SDValue();
5349 }
5350
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005352 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005353
Eli Friedman36df4992009-05-27 00:47:34 +00005354 // These are really Legal; return the operand so the caller accepts it as
5355 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005356 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005357 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005358 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005359 Subtarget->is64Bit()) {
5360 return Op;
5361 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005362
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005363 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005364 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005366 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005367 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005368 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005369 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005370 PseudoSourceValue::getFixedStack(SSFI), 0,
5371 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005372 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5373}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005374
Owen Andersone50ed302009-08-10 22:56:29 +00005375SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005376 SDValue StackSlot,
5377 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005379 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005380 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005381 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005382 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005383 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005384 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005385 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005386 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005387 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005388 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005389
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005390 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005392 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005393
5394 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5395 // shouldn't be necessary except that RFP cannot be live across
5396 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005397 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005398 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005399 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005401 SDValue Ops[] = {
5402 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5403 };
5404 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005405 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005406 PseudoSourceValue::getFixedStack(SSFI), 0,
5407 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005408 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005409
Evan Cheng0db9fe62006-04-25 20:13:52 +00005410 return Result;
5411}
5412
Bill Wendling8b8a6362009-01-17 03:56:04 +00005413// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5414SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5415 // This algorithm is not obvious. Here it is in C code, more or less:
5416 /*
5417 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5418 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5419 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005420
Bill Wendling8b8a6362009-01-17 03:56:04 +00005421 // Copy ints to xmm registers.
5422 __m128i xh = _mm_cvtsi32_si128( hi );
5423 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005424
Bill Wendling8b8a6362009-01-17 03:56:04 +00005425 // Combine into low half of a single xmm register.
5426 __m128i x = _mm_unpacklo_epi32( xh, xl );
5427 __m128d d;
5428 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005429
Bill Wendling8b8a6362009-01-17 03:56:04 +00005430 // Merge in appropriate exponents to give the integer bits the right
5431 // magnitude.
5432 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005433
Bill Wendling8b8a6362009-01-17 03:56:04 +00005434 // Subtract away the biases to deal with the IEEE-754 double precision
5435 // implicit 1.
5436 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005437
Bill Wendling8b8a6362009-01-17 03:56:04 +00005438 // All conversions up to here are exact. The correctly rounded result is
5439 // calculated using the current rounding mode using the following
5440 // horizontal add.
5441 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5442 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5443 // store doesn't really need to be here (except
5444 // maybe to zero the other double)
5445 return sd;
5446 }
5447 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005448
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005449 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005450 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005451
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005452 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005453 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005454 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5455 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5456 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5457 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005458 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005459 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005460
Bill Wendling8b8a6362009-01-17 03:56:04 +00005461 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005462 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005463 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005464 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005465 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005466 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005467 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005468
Owen Anderson825b72b2009-08-11 20:47:22 +00005469 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5470 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005471 Op.getOperand(0),
5472 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005473 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5474 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005475 Op.getOperand(0),
5476 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005477 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5478 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005479 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005480 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005481 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5482 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5483 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005484 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005485 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005486 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005487
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005488 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005489 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005490 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5491 DAG.getUNDEF(MVT::v2f64), ShufMask);
5492 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5493 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005494 DAG.getIntPtrConstant(0));
5495}
5496
Bill Wendling8b8a6362009-01-17 03:56:04 +00005497// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5498SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005499 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005500 // FP constant to bias correct the final result.
5501 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005502 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005503
5504 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5506 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005507 Op.getOperand(0),
5508 DAG.getIntPtrConstant(0)));
5509
Owen Anderson825b72b2009-08-11 20:47:22 +00005510 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5511 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005512 DAG.getIntPtrConstant(0));
5513
5514 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5516 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005517 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005518 MVT::v2f64, Load)),
5519 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005520 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005521 MVT::v2f64, Bias)));
5522 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5523 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005524 DAG.getIntPtrConstant(0));
5525
5526 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005527 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005528
5529 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005530 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005531
Owen Anderson825b72b2009-08-11 20:47:22 +00005532 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005533 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005534 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005535 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005536 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005537 }
5538
5539 // Handle final rounding.
5540 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005541}
5542
5543SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005544 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005545 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005546
Evan Chenga06ec9e2009-01-19 08:08:22 +00005547 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5548 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5549 // the optimization here.
5550 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005551 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005552
Owen Andersone50ed302009-08-10 22:56:29 +00005553 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005555 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005557 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005558
Bill Wendling8b8a6362009-01-17 03:56:04 +00005559 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005561 return LowerUINT_TO_FP_i32(Op, DAG);
5562 }
5563
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005565
5566 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005568 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5569 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5570 getPointerTy(), StackSlot, WordOff);
5571 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005572 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005574 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005576}
5577
Dan Gohman475871a2008-07-27 21:46:04 +00005578std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005579FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005580 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005581
Owen Andersone50ed302009-08-10 22:56:29 +00005582 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005583
5584 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5586 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005587 }
5588
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5590 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005591 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005592
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005593 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005595 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005596 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005597 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005599 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005600 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005601
Evan Cheng87c89352007-10-15 20:11:21 +00005602 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5603 // stack slot.
5604 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005605 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005606 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005608
Evan Cheng0db9fe62006-04-25 20:13:52 +00005609 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005610 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005611 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5613 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5614 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005615 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005616
Dan Gohman475871a2008-07-27 21:46:04 +00005617 SDValue Chain = DAG.getEntryNode();
5618 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005619 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005620 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005621 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005622 PseudoSourceValue::getFixedStack(SSFI), 0,
5623 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005626 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5627 };
Dale Johannesenace16102009-02-03 19:33:06 +00005628 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005629 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005630 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005631 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5632 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005633
Evan Cheng0db9fe62006-04-25 20:13:52 +00005634 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005635 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005637
Chris Lattner27a6c732007-11-24 07:07:01 +00005638 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005639}
5640
Dan Gohman475871a2008-07-27 21:46:04 +00005641SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005642 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005643 if (Op.getValueType() == MVT::v2i32 &&
5644 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005645 return Op;
5646 }
5647 return SDValue();
5648 }
5649
Eli Friedman948e95a2009-05-23 09:59:16 +00005650 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005651 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005652 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5653 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005654
Chris Lattner27a6c732007-11-24 07:07:01 +00005655 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005656 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005657 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005658}
5659
Eli Friedman948e95a2009-05-23 09:59:16 +00005660SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5661 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5662 SDValue FIST = Vals.first, StackSlot = Vals.second;
5663 assert(FIST.getNode() && "Unexpected failure");
5664
5665 // Load the result.
5666 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005667 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005668}
5669
Dan Gohman475871a2008-07-27 21:46:04 +00005670SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005671 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005672 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005673 EVT VT = Op.getValueType();
5674 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005675 if (VT.isVector())
5676 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005677 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005679 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005680 CV.push_back(C);
5681 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005682 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005683 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005684 CV.push_back(C);
5685 CV.push_back(C);
5686 CV.push_back(C);
5687 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005688 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005689 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005690 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005691 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005692 PseudoSourceValue::getConstantPool(), 0,
5693 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005694 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005695}
5696
Dan Gohman475871a2008-07-27 21:46:04 +00005697SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005698 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005699 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005700 EVT VT = Op.getValueType();
5701 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005702 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005703 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005704 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005705 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005706 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005707 CV.push_back(C);
5708 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005709 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005710 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005711 CV.push_back(C);
5712 CV.push_back(C);
5713 CV.push_back(C);
5714 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005715 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005716 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005717 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005718 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005719 PseudoSourceValue::getConstantPool(), 0,
5720 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005721 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005722 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005723 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5724 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005725 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005727 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005728 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005729 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005730}
5731
Dan Gohman475871a2008-07-27 21:46:04 +00005732SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005733 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005734 SDValue Op0 = Op.getOperand(0);
5735 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005736 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005737 EVT VT = Op.getValueType();
5738 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005739
5740 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005741 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005742 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005743 SrcVT = VT;
5744 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005745 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005746 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005747 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005748 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005749 }
5750
5751 // At this point the operands and the result should have the same
5752 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005753
Evan Cheng68c47cb2007-01-05 07:55:56 +00005754 // First get the sign bit of second operand.
5755 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005756 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005757 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5758 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005759 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005760 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5761 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5762 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5763 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005764 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005765 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005766 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005767 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005768 PseudoSourceValue::getConstantPool(), 0,
5769 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005770 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005771
5772 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005773 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 // Op0 is MVT::f32, Op1 is MVT::f64.
5775 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5776 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5777 DAG.getConstant(32, MVT::i32));
5778 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5779 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005780 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005781 }
5782
Evan Cheng73d6cf12007-01-05 21:37:56 +00005783 // Clear first operand sign bit.
5784 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005785 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005786 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5787 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005788 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005789 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5790 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5791 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5792 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005793 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005794 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005795 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005796 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005797 PseudoSourceValue::getConstantPool(), 0,
5798 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005799 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005800
5801 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005802 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005803}
5804
Dan Gohman076aee32009-03-04 19:44:21 +00005805/// Emit nodes that will be selected as "test Op0,Op0", or something
5806/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005807SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5808 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005809 DebugLoc dl = Op.getDebugLoc();
5810
Dan Gohman31125812009-03-07 01:58:32 +00005811 // CF and OF aren't always set the way we want. Determine which
5812 // of these we need.
5813 bool NeedCF = false;
5814 bool NeedOF = false;
5815 switch (X86CC) {
5816 case X86::COND_A: case X86::COND_AE:
5817 case X86::COND_B: case X86::COND_BE:
5818 NeedCF = true;
5819 break;
5820 case X86::COND_G: case X86::COND_GE:
5821 case X86::COND_L: case X86::COND_LE:
5822 case X86::COND_O: case X86::COND_NO:
5823 NeedOF = true;
5824 break;
5825 default: break;
5826 }
5827
Dan Gohman076aee32009-03-04 19:44:21 +00005828 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005829 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5830 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5831 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005832 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005833 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005834 switch (Op.getNode()->getOpcode()) {
5835 case ISD::ADD:
5836 // Due to an isel shortcoming, be conservative if this add is likely to
5837 // be selected as part of a load-modify-store instruction. When the root
5838 // node in a match is a store, isel doesn't know how to remap non-chain
5839 // non-flag uses of other nodes in the match, such as the ADD in this
5840 // case. This leads to the ADD being left around and reselected, with
5841 // the result being two adds in the output.
5842 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5843 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5844 if (UI->getOpcode() == ISD::STORE)
5845 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005846 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005847 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5848 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005849 if (C->getAPIntValue() == 1) {
5850 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005851 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005852 break;
5853 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005854 // An add of negative one (subtract of one) will be selected as a DEC.
5855 if (C->getAPIntValue().isAllOnesValue()) {
5856 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005857 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005858 break;
5859 }
5860 }
Dan Gohman076aee32009-03-04 19:44:21 +00005861 // Otherwise use a regular EFLAGS-setting add.
5862 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005863 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005864 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005865 case ISD::AND: {
5866 // If the primary and result isn't used, don't bother using X86ISD::AND,
5867 // because a TEST instruction will be better.
5868 bool NonFlagUse = false;
5869 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005870 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5871 SDNode *User = *UI;
5872 unsigned UOpNo = UI.getOperandNo();
5873 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5874 // Look pass truncate.
5875 UOpNo = User->use_begin().getOperandNo();
5876 User = *User->use_begin();
5877 }
5878 if (User->getOpcode() != ISD::BRCOND &&
5879 User->getOpcode() != ISD::SETCC &&
5880 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005881 NonFlagUse = true;
5882 break;
5883 }
Evan Cheng17751da2010-01-07 00:54:06 +00005884 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005885 if (!NonFlagUse)
5886 break;
5887 }
5888 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005889 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005890 case ISD::OR:
5891 case ISD::XOR:
5892 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005893 // likely to be selected as part of a load-modify-store instruction.
5894 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5895 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5896 if (UI->getOpcode() == ISD::STORE)
5897 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005898 // Otherwise use a regular EFLAGS-setting instruction.
5899 switch (Op.getNode()->getOpcode()) {
5900 case ISD::SUB: Opcode = X86ISD::SUB; break;
5901 case ISD::OR: Opcode = X86ISD::OR; break;
5902 case ISD::XOR: Opcode = X86ISD::XOR; break;
5903 case ISD::AND: Opcode = X86ISD::AND; break;
5904 default: llvm_unreachable("unexpected operator!");
5905 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005906 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005907 break;
5908 case X86ISD::ADD:
5909 case X86ISD::SUB:
5910 case X86ISD::INC:
5911 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005912 case X86ISD::OR:
5913 case X86ISD::XOR:
5914 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005915 return SDValue(Op.getNode(), 1);
5916 default:
5917 default_case:
5918 break;
5919 }
5920 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005921 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005922 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005923 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005924 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005925 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005926 DAG.ReplaceAllUsesWith(Op, New);
5927 return SDValue(New.getNode(), 1);
5928 }
5929 }
5930
5931 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005933 DAG.getConstant(0, Op.getValueType()));
5934}
5935
5936/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5937/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005938SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5939 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005940 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5941 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005942 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005943
5944 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005946}
5947
Evan Chengd40d03e2010-01-06 19:38:29 +00005948/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5949/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00005950static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005951 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005952 SDValue Op0 = And.getOperand(0);
5953 SDValue Op1 = And.getOperand(1);
5954 if (Op0.getOpcode() == ISD::TRUNCATE)
5955 Op0 = Op0.getOperand(0);
5956 if (Op1.getOpcode() == ISD::TRUNCATE)
5957 Op1 = Op1.getOperand(0);
5958
Evan Chengd40d03e2010-01-06 19:38:29 +00005959 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00005960 if (Op1.getOpcode() == ISD::SHL) {
5961 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
5962 if (And10C->getZExtValue() == 1) {
5963 LHS = Op0;
5964 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005965 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005966 } else if (Op0.getOpcode() == ISD::SHL) {
5967 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
5968 if (And00C->getZExtValue() == 1) {
5969 LHS = Op1;
5970 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00005971 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005972 } else if (Op1.getOpcode() == ISD::Constant) {
5973 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
5974 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00005975 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5976 LHS = AndLHS.getOperand(0);
5977 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005978 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005979 }
Evan Cheng0488db92007-09-25 01:57:46 +00005980
Evan Chengd40d03e2010-01-06 19:38:29 +00005981 if (LHS.getNode()) {
5982 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5983 // instruction. Since the shift amount is in-range-or-undefined, we know
5984 // that doing a bittest on the i16 value is ok. We extend to i32 because
5985 // the encoding for the i16 version is larger than the i32 version.
5986 if (LHS.getValueType() == MVT::i8)
5987 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005988
Evan Chengd40d03e2010-01-06 19:38:29 +00005989 // If the operand types disagree, extend the shift amount to match. Since
5990 // BT ignores high bits (like shifts) we can use anyextend.
5991 if (LHS.getValueType() != RHS.getValueType())
5992 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005993
Evan Chengd40d03e2010-01-06 19:38:29 +00005994 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5995 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5996 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5997 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005998 }
5999
Evan Cheng54de3ea2010-01-05 06:52:31 +00006000 return SDValue();
6001}
6002
6003SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
6004 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6005 SDValue Op0 = Op.getOperand(0);
6006 SDValue Op1 = Op.getOperand(1);
6007 DebugLoc dl = Op.getDebugLoc();
6008 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6009
6010 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006011 // Lower (X & (1 << N)) == 0 to BT(X, N).
6012 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6013 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6014 if (Op0.getOpcode() == ISD::AND &&
6015 Op0.hasOneUse() &&
6016 Op1.getOpcode() == ISD::Constant &&
6017 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6018 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6019 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6020 if (NewSetCC.getNode())
6021 return NewSetCC;
6022 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006023
Evan Cheng2c755ba2010-02-27 07:36:59 +00006024 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6025 if (Op0.getOpcode() == X86ISD::SETCC &&
6026 Op1.getOpcode() == ISD::Constant &&
6027 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6028 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6029 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6030 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6031 bool Invert = (CC == ISD::SETNE) ^
6032 cast<ConstantSDNode>(Op1)->isNullValue();
6033 if (Invert)
6034 CCode = X86::GetOppositeBranchCondition(CCode);
6035 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6036 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6037 }
6038
Chris Lattnere55484e2008-12-25 05:34:37 +00006039 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
6040 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006041 if (X86CC == X86::COND_INVALID)
6042 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006043
Dan Gohman31125812009-03-07 01:58:32 +00006044 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006045
6046 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006047 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006048 return DAG.getNode(ISD::AND, dl, MVT::i8,
6049 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6050 DAG.getConstant(X86CC, MVT::i8), Cond),
6051 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006052
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6054 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006055}
6056
Dan Gohman475871a2008-07-27 21:46:04 +00006057SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6058 SDValue Cond;
6059 SDValue Op0 = Op.getOperand(0);
6060 SDValue Op1 = Op.getOperand(1);
6061 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006062 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006063 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6064 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006065 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006066
6067 if (isFP) {
6068 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006069 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006070 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6071 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006072 bool Swap = false;
6073
6074 switch (SetCCOpcode) {
6075 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006076 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006077 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006078 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006079 case ISD::SETGT: Swap = true; // Fallthrough
6080 case ISD::SETLT:
6081 case ISD::SETOLT: SSECC = 1; break;
6082 case ISD::SETOGE:
6083 case ISD::SETGE: Swap = true; // Fallthrough
6084 case ISD::SETLE:
6085 case ISD::SETOLE: SSECC = 2; break;
6086 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006087 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006088 case ISD::SETNE: SSECC = 4; break;
6089 case ISD::SETULE: Swap = true;
6090 case ISD::SETUGE: SSECC = 5; break;
6091 case ISD::SETULT: Swap = true;
6092 case ISD::SETUGT: SSECC = 6; break;
6093 case ISD::SETO: SSECC = 7; break;
6094 }
6095 if (Swap)
6096 std::swap(Op0, Op1);
6097
Nate Begemanfb8ead02008-07-25 19:05:58 +00006098 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006099 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006100 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006101 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6103 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006104 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006105 }
6106 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006107 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6109 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006110 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006111 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006112 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006113 }
6114 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006115 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006117
Nate Begeman30a0de92008-07-17 16:51:19 +00006118 // We are handling one of the integer comparisons here. Since SSE only has
6119 // GT and EQ comparisons for integer, swapping operands and multiple
6120 // operations may be required for some comparisons.
6121 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6122 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006123
Owen Anderson825b72b2009-08-11 20:47:22 +00006124 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006125 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006126 case MVT::v8i8:
6127 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6128 case MVT::v4i16:
6129 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6130 case MVT::v2i32:
6131 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6132 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006134
Nate Begeman30a0de92008-07-17 16:51:19 +00006135 switch (SetCCOpcode) {
6136 default: break;
6137 case ISD::SETNE: Invert = true;
6138 case ISD::SETEQ: Opc = EQOpc; break;
6139 case ISD::SETLT: Swap = true;
6140 case ISD::SETGT: Opc = GTOpc; break;
6141 case ISD::SETGE: Swap = true;
6142 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6143 case ISD::SETULT: Swap = true;
6144 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6145 case ISD::SETUGE: Swap = true;
6146 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6147 }
6148 if (Swap)
6149 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006150
Nate Begeman30a0de92008-07-17 16:51:19 +00006151 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6152 // bits of the inputs before performing those operations.
6153 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006154 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006155 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6156 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006157 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006158 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6159 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006160 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6161 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006163
Dale Johannesenace16102009-02-03 19:33:06 +00006164 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006165
6166 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006167 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006168 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006169
Nate Begeman30a0de92008-07-17 16:51:19 +00006170 return Result;
6171}
Evan Cheng0488db92007-09-25 01:57:46 +00006172
Evan Cheng370e5342008-12-03 08:38:43 +00006173// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006174static bool isX86LogicalCmp(SDValue Op) {
6175 unsigned Opc = Op.getNode()->getOpcode();
6176 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6177 return true;
6178 if (Op.getResNo() == 1 &&
6179 (Opc == X86ISD::ADD ||
6180 Opc == X86ISD::SUB ||
6181 Opc == X86ISD::SMUL ||
6182 Opc == X86ISD::UMUL ||
6183 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006184 Opc == X86ISD::DEC ||
6185 Opc == X86ISD::OR ||
6186 Opc == X86ISD::XOR ||
6187 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006188 return true;
6189
6190 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006191}
6192
Dan Gohman475871a2008-07-27 21:46:04 +00006193SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006194 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006195 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006196 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006197 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006198
Dan Gohman1a492952009-10-20 16:22:37 +00006199 if (Cond.getOpcode() == ISD::SETCC) {
6200 SDValue NewCond = LowerSETCC(Cond, DAG);
6201 if (NewCond.getNode())
6202 Cond = NewCond;
6203 }
Evan Cheng734503b2006-09-11 02:19:56 +00006204
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006205 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6206 SDValue Op1 = Op.getOperand(1);
6207 SDValue Op2 = Op.getOperand(2);
6208 if (Cond.getOpcode() == X86ISD::SETCC &&
6209 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6210 SDValue Cmp = Cond.getOperand(1);
6211 if (Cmp.getOpcode() == X86ISD::CMP) {
6212 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6213 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6214 ConstantSDNode *RHSC =
6215 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6216 if (N1C && N1C->isAllOnesValue() &&
6217 N2C && N2C->isNullValue() &&
6218 RHSC && RHSC->isNullValue()) {
6219 SDValue CmpOp0 = Cmp.getOperand(0);
Evan Cheng5fef8bc2010-01-28 01:57:22 +00006220 Cmp = DAG.getNode(X86ISD::CMP, dl, CmpOp0.getValueType(),
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006221 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6222 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6223 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6224 }
6225 }
6226 }
6227
Evan Chengad9c0a32009-12-15 00:53:42 +00006228 // Look pass (and (setcc_carry (cmp ...)), 1).
6229 if (Cond.getOpcode() == ISD::AND &&
6230 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6231 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6232 if (C && C->getAPIntValue() == 1)
6233 Cond = Cond.getOperand(0);
6234 }
6235
Evan Cheng3f41d662007-10-08 22:16:29 +00006236 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6237 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006238 if (Cond.getOpcode() == X86ISD::SETCC ||
6239 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006240 CC = Cond.getOperand(0);
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006243 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006244 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006245
Evan Cheng3f41d662007-10-08 22:16:29 +00006246 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006247 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006248 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006249 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006250
Chris Lattnerd1980a52009-03-12 06:52:53 +00006251 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6252 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006253 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006254 addTest = false;
6255 }
6256 }
6257
6258 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006259 // Look pass the truncate.
6260 if (Cond.getOpcode() == ISD::TRUNCATE)
6261 Cond = Cond.getOperand(0);
6262
6263 // We know the result of AND is compared against zero. Try to match
6264 // it to BT.
6265 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6266 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6267 if (NewSetCC.getNode()) {
6268 CC = NewSetCC.getOperand(0);
6269 Cond = NewSetCC.getOperand(1);
6270 addTest = false;
6271 }
6272 }
6273 }
6274
6275 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006276 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006277 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006278 }
6279
Evan Cheng0488db92007-09-25 01:57:46 +00006280 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6281 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006282 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6283 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006284 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006285}
6286
Evan Cheng370e5342008-12-03 08:38:43 +00006287// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6288// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6289// from the AND / OR.
6290static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6291 Opc = Op.getOpcode();
6292 if (Opc != ISD::OR && Opc != ISD::AND)
6293 return false;
6294 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6295 Op.getOperand(0).hasOneUse() &&
6296 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6297 Op.getOperand(1).hasOneUse());
6298}
6299
Evan Cheng961d6d42009-02-02 08:19:07 +00006300// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6301// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006302static bool isXor1OfSetCC(SDValue Op) {
6303 if (Op.getOpcode() != ISD::XOR)
6304 return false;
6305 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6306 if (N1C && N1C->getAPIntValue() == 1) {
6307 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6308 Op.getOperand(0).hasOneUse();
6309 }
6310 return false;
6311}
6312
Dan Gohman475871a2008-07-27 21:46:04 +00006313SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006314 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006315 SDValue Chain = Op.getOperand(0);
6316 SDValue Cond = Op.getOperand(1);
6317 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006318 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006319 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006320
Dan Gohman1a492952009-10-20 16:22:37 +00006321 if (Cond.getOpcode() == ISD::SETCC) {
6322 SDValue NewCond = LowerSETCC(Cond, DAG);
6323 if (NewCond.getNode())
6324 Cond = NewCond;
6325 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006326#if 0
6327 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006328 else if (Cond.getOpcode() == X86ISD::ADD ||
6329 Cond.getOpcode() == X86ISD::SUB ||
6330 Cond.getOpcode() == X86ISD::SMUL ||
6331 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006332 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006333#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006334
Evan Chengad9c0a32009-12-15 00:53:42 +00006335 // Look pass (and (setcc_carry (cmp ...)), 1).
6336 if (Cond.getOpcode() == ISD::AND &&
6337 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6338 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6339 if (C && C->getAPIntValue() == 1)
6340 Cond = Cond.getOperand(0);
6341 }
6342
Evan Cheng3f41d662007-10-08 22:16:29 +00006343 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6344 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006345 if (Cond.getOpcode() == X86ISD::SETCC ||
6346 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006347 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006348
Dan Gohman475871a2008-07-27 21:46:04 +00006349 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006350 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006351 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006352 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006353 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006354 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006355 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006356 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006357 default: break;
6358 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006359 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006360 // These can only come from an arithmetic instruction with overflow,
6361 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006362 Cond = Cond.getNode()->getOperand(1);
6363 addTest = false;
6364 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006365 }
Evan Cheng0488db92007-09-25 01:57:46 +00006366 }
Evan Cheng370e5342008-12-03 08:38:43 +00006367 } else {
6368 unsigned CondOpc;
6369 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6370 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006371 if (CondOpc == ISD::OR) {
6372 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6373 // two branches instead of an explicit OR instruction with a
6374 // separate test.
6375 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006376 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006377 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006378 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006379 Chain, Dest, CC, Cmp);
6380 CC = Cond.getOperand(1).getOperand(0);
6381 Cond = Cmp;
6382 addTest = false;
6383 }
6384 } else { // ISD::AND
6385 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6386 // two branches instead of an explicit AND instruction with a
6387 // separate test. However, we only do this if this block doesn't
6388 // have a fall-through edge, because this requires an explicit
6389 // jmp when the condition is false.
6390 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006391 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006392 Op.getNode()->hasOneUse()) {
6393 X86::CondCode CCode =
6394 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6395 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006396 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006397 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6398 // Look for an unconditional branch following this conditional branch.
6399 // We need this because we need to reverse the successors in order
6400 // to implement FCMP_OEQ.
6401 if (User.getOpcode() == ISD::BR) {
6402 SDValue FalseBB = User.getOperand(1);
6403 SDValue NewBR =
6404 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6405 assert(NewBR == User);
6406 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006407
Dale Johannesene4d209d2009-02-03 20:21:25 +00006408 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006409 Chain, Dest, CC, Cmp);
6410 X86::CondCode CCode =
6411 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6412 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006414 Cond = Cmp;
6415 addTest = false;
6416 }
6417 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006418 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006419 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6420 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6421 // It should be transformed during dag combiner except when the condition
6422 // is set by a arithmetics with overflow node.
6423 X86::CondCode CCode =
6424 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6425 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006426 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006427 Cond = Cond.getOperand(0).getOperand(1);
6428 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006429 }
Evan Cheng0488db92007-09-25 01:57:46 +00006430 }
6431
6432 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006433 // Look pass the truncate.
6434 if (Cond.getOpcode() == ISD::TRUNCATE)
6435 Cond = Cond.getOperand(0);
6436
6437 // We know the result of AND is compared against zero. Try to match
6438 // it to BT.
6439 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6440 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6441 if (NewSetCC.getNode()) {
6442 CC = NewSetCC.getOperand(0);
6443 Cond = NewSetCC.getOperand(1);
6444 addTest = false;
6445 }
6446 }
6447 }
6448
6449 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006450 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006451 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006452 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006453 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006454 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006455}
6456
Anton Korobeynikove060b532007-04-17 19:34:00 +00006457
6458// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6459// Calls to _alloca is needed to probe the stack when allocating more than 4k
6460// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6461// that the guard pages used by the OS virtual memory manager are allocated in
6462// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006463SDValue
6464X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006465 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006466 assert(Subtarget->isTargetCygMing() &&
6467 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006468 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006469
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006470 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006471 SDValue Chain = Op.getOperand(0);
6472 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006473 // FIXME: Ensure alignment here
6474
Dan Gohman475871a2008-07-27 21:46:04 +00006475 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006476
Owen Andersone50ed302009-08-10 22:56:29 +00006477 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006478 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006479
Dale Johannesendd64c412009-02-04 00:33:20 +00006480 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006481 Flag = Chain.getValue(1);
6482
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006483 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006484
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006485 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6486 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006487
Dale Johannesendd64c412009-02-04 00:33:20 +00006488 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006489
Dan Gohman475871a2008-07-27 21:46:04 +00006490 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006491 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006492}
6493
Dan Gohman475871a2008-07-27 21:46:04 +00006494SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006495X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006496 SDValue Chain,
6497 SDValue Dst, SDValue Src,
6498 SDValue Size, unsigned Align,
6499 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006500 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006501 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006502
Bill Wendling6f287b22008-09-30 21:22:07 +00006503 // If not DWORD aligned or size is more than the threshold, call the library.
6504 // The libc version is likely to be faster for these cases. It can use the
6505 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006506 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006507 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006508 ConstantSize->getZExtValue() >
6509 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006510 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006511
6512 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006513 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006514
Bill Wendling6158d842008-10-01 00:59:58 +00006515 if (const char *bzeroEntry = V &&
6516 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006517 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006518 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006519 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006520 TargetLowering::ArgListEntry Entry;
6521 Entry.Node = Dst;
6522 Entry.Ty = IntPtrTy;
6523 Args.push_back(Entry);
6524 Entry.Node = Size;
6525 Args.push_back(Entry);
6526 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006527 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6528 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006529 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006530 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006531 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006532 }
6533
Dan Gohman707e0182008-04-12 04:36:06 +00006534 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006535 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006536 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006537
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006538 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006539 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006540 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006541 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006542 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006543 unsigned BytesLeft = 0;
6544 bool TwoRepStos = false;
6545 if (ValC) {
6546 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006547 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006548
Evan Cheng0db9fe62006-04-25 20:13:52 +00006549 // If the value is a constant, then we can potentially use larger sets.
6550 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006551 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006552 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006553 ValReg = X86::AX;
6554 Val = (Val << 8) | Val;
6555 break;
6556 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006558 ValReg = X86::EAX;
6559 Val = (Val << 8) | Val;
6560 Val = (Val << 16) | Val;
6561 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006562 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006563 ValReg = X86::RAX;
6564 Val = (Val << 32) | Val;
6565 }
6566 break;
6567 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006568 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006569 ValReg = X86::AL;
6570 Count = DAG.getIntPtrConstant(SizeVal);
6571 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006572 }
6573
Owen Anderson825b72b2009-08-11 20:47:22 +00006574 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006575 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006576 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6577 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006578 }
6579
Dale Johannesen0f502f62009-02-03 22:26:09 +00006580 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006581 InFlag);
6582 InFlag = Chain.getValue(1);
6583 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006584 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006585 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006586 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006587 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006588 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006589
Scott Michelfdc40a02009-02-17 22:15:04 +00006590 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006591 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006592 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006593 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006594 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006595 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006596 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006597 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006598
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006600 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6601 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006602
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 if (TwoRepStos) {
6604 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006605 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006606 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006607 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006608 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6609 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006610 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006611 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006612 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006614 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6615 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006616 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006617 // Handle the last 1 - 7 bytes.
6618 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006619 EVT AddrVT = Dst.getValueType();
6620 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006621
Dale Johannesen0f502f62009-02-03 22:26:09 +00006622 Chain = DAG.getMemset(Chain, dl,
6623 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006624 DAG.getConstant(Offset, AddrVT)),
6625 Src,
6626 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006627 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006628 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006629
Dan Gohman707e0182008-04-12 04:36:06 +00006630 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006631 return Chain;
6632}
Evan Cheng11e15b32006-04-03 20:53:28 +00006633
Dan Gohman475871a2008-07-27 21:46:04 +00006634SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006635X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006636 SDValue Chain, SDValue Dst, SDValue Src,
6637 SDValue Size, unsigned Align,
6638 bool AlwaysInline,
6639 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006640 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006641 // This requires the copy size to be a constant, preferrably
6642 // within a subtarget-specific limit.
6643 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6644 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006645 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006646 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006647 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006648 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006649
Evan Cheng1887c1c2008-08-21 21:00:15 +00006650 /// If not DWORD aligned, call the library.
6651 if ((Align & 3) != 0)
6652 return SDValue();
6653
6654 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006655 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006656 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006657 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006658
Duncan Sands83ec4b62008-06-06 12:08:01 +00006659 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006660 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006661 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006662 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006663
Dan Gohman475871a2008-07-27 21:46:04 +00006664 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006665 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006666 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006667 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006669 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006670 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006671 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006673 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006674 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006675 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676 InFlag = Chain.getValue(1);
6677
Owen Anderson825b72b2009-08-11 20:47:22 +00006678 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006679 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6680 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6681 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006682
Dan Gohman475871a2008-07-27 21:46:04 +00006683 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006684 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006685 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006686 // Handle the last 1 - 7 bytes.
6687 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006688 EVT DstVT = Dst.getValueType();
6689 EVT SrcVT = Src.getValueType();
6690 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006691 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006692 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006693 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006694 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006695 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006696 DAG.getConstant(BytesLeft, SizeVT),
6697 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006698 DstSV, DstSVOff + Offset,
6699 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006700 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006701
Owen Anderson825b72b2009-08-11 20:47:22 +00006702 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006703 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006704}
6705
Dan Gohman475871a2008-07-27 21:46:04 +00006706SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006707 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006708 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006709
Evan Cheng25ab6902006-09-08 06:48:29 +00006710 if (!Subtarget->is64Bit()) {
6711 // vastart just stores the address of the VarArgsFrameIndex slot into the
6712 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006713 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006714 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6715 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006716 }
6717
6718 // __va_list_tag:
6719 // gp_offset (0 - 6 * 8)
6720 // fp_offset (48 - 48 + 8 * 16)
6721 // overflow_arg_area (point to parameters coming in memory).
6722 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006723 SmallVector<SDValue, 8> MemOps;
6724 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006725 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006726 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene67c9d422010-02-15 16:53:33 +00006727 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6728 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006729 MemOps.push_back(Store);
6730
6731 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006732 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006733 FIN, DAG.getIntPtrConstant(4));
6734 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006735 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006736 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006737 MemOps.push_back(Store);
6738
6739 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006740 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006741 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006742 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006743 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6744 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006745 MemOps.push_back(Store);
6746
6747 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006748 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006749 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006750 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006751 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6752 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006753 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006754 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006755 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756}
6757
Dan Gohman475871a2008-07-27 21:46:04 +00006758SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006759 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6760 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006761 SDValue Chain = Op.getOperand(0);
6762 SDValue SrcPtr = Op.getOperand(1);
6763 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006764
Torok Edwindac237e2009-07-08 20:53:28 +00006765 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006766 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006767}
6768
Dan Gohman475871a2008-07-27 21:46:04 +00006769SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006770 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006771 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006772 SDValue Chain = Op.getOperand(0);
6773 SDValue DstPtr = Op.getOperand(1);
6774 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006775 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6776 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006777 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006778
Dale Johannesendd64c412009-02-04 00:33:20 +00006779 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006780 DAG.getIntPtrConstant(24), 8, false,
6781 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006782}
6783
Dan Gohman475871a2008-07-27 21:46:04 +00006784SDValue
6785X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006786 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006787 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006788 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006789 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006790 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791 case Intrinsic::x86_sse_comieq_ss:
6792 case Intrinsic::x86_sse_comilt_ss:
6793 case Intrinsic::x86_sse_comile_ss:
6794 case Intrinsic::x86_sse_comigt_ss:
6795 case Intrinsic::x86_sse_comige_ss:
6796 case Intrinsic::x86_sse_comineq_ss:
6797 case Intrinsic::x86_sse_ucomieq_ss:
6798 case Intrinsic::x86_sse_ucomilt_ss:
6799 case Intrinsic::x86_sse_ucomile_ss:
6800 case Intrinsic::x86_sse_ucomigt_ss:
6801 case Intrinsic::x86_sse_ucomige_ss:
6802 case Intrinsic::x86_sse_ucomineq_ss:
6803 case Intrinsic::x86_sse2_comieq_sd:
6804 case Intrinsic::x86_sse2_comilt_sd:
6805 case Intrinsic::x86_sse2_comile_sd:
6806 case Intrinsic::x86_sse2_comigt_sd:
6807 case Intrinsic::x86_sse2_comige_sd:
6808 case Intrinsic::x86_sse2_comineq_sd:
6809 case Intrinsic::x86_sse2_ucomieq_sd:
6810 case Intrinsic::x86_sse2_ucomilt_sd:
6811 case Intrinsic::x86_sse2_ucomile_sd:
6812 case Intrinsic::x86_sse2_ucomigt_sd:
6813 case Intrinsic::x86_sse2_ucomige_sd:
6814 case Intrinsic::x86_sse2_ucomineq_sd: {
6815 unsigned Opc = 0;
6816 ISD::CondCode CC = ISD::SETCC_INVALID;
6817 switch (IntNo) {
6818 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006819 case Intrinsic::x86_sse_comieq_ss:
6820 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006821 Opc = X86ISD::COMI;
6822 CC = ISD::SETEQ;
6823 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006824 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006825 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 Opc = X86ISD::COMI;
6827 CC = ISD::SETLT;
6828 break;
6829 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006830 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831 Opc = X86ISD::COMI;
6832 CC = ISD::SETLE;
6833 break;
6834 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006835 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006836 Opc = X86ISD::COMI;
6837 CC = ISD::SETGT;
6838 break;
6839 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006840 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006841 Opc = X86ISD::COMI;
6842 CC = ISD::SETGE;
6843 break;
6844 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006845 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846 Opc = X86ISD::COMI;
6847 CC = ISD::SETNE;
6848 break;
6849 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006850 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006851 Opc = X86ISD::UCOMI;
6852 CC = ISD::SETEQ;
6853 break;
6854 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006855 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006856 Opc = X86ISD::UCOMI;
6857 CC = ISD::SETLT;
6858 break;
6859 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006860 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006861 Opc = X86ISD::UCOMI;
6862 CC = ISD::SETLE;
6863 break;
6864 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006865 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006866 Opc = X86ISD::UCOMI;
6867 CC = ISD::SETGT;
6868 break;
6869 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006870 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006871 Opc = X86ISD::UCOMI;
6872 CC = ISD::SETGE;
6873 break;
6874 case Intrinsic::x86_sse_ucomineq_ss:
6875 case Intrinsic::x86_sse2_ucomineq_sd:
6876 Opc = X86ISD::UCOMI;
6877 CC = ISD::SETNE;
6878 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006879 }
Evan Cheng734503b2006-09-11 02:19:56 +00006880
Dan Gohman475871a2008-07-27 21:46:04 +00006881 SDValue LHS = Op.getOperand(1);
6882 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006883 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006884 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006885 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6886 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6887 DAG.getConstant(X86CC, MVT::i8), Cond);
6888 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006889 }
Eric Christopher71c67532009-07-29 00:28:05 +00006890 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006891 // an integer value, not just an instruction so lower it to the ptest
6892 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006893 case Intrinsic::x86_sse41_ptestz:
6894 case Intrinsic::x86_sse41_ptestc:
6895 case Intrinsic::x86_sse41_ptestnzc:{
6896 unsigned X86CC = 0;
6897 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006898 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006899 case Intrinsic::x86_sse41_ptestz:
6900 // ZF = 1
6901 X86CC = X86::COND_E;
6902 break;
6903 case Intrinsic::x86_sse41_ptestc:
6904 // CF = 1
6905 X86CC = X86::COND_B;
6906 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006907 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006908 // ZF and CF = 0
6909 X86CC = X86::COND_A;
6910 break;
6911 }
Eric Christopherfd179292009-08-27 18:07:15 +00006912
Eric Christopher71c67532009-07-29 00:28:05 +00006913 SDValue LHS = Op.getOperand(1);
6914 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006915 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6916 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6917 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6918 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006919 }
Evan Cheng5759f972008-05-04 09:15:50 +00006920
6921 // Fix vector shift instructions where the last operand is a non-immediate
6922 // i32 value.
6923 case Intrinsic::x86_sse2_pslli_w:
6924 case Intrinsic::x86_sse2_pslli_d:
6925 case Intrinsic::x86_sse2_pslli_q:
6926 case Intrinsic::x86_sse2_psrli_w:
6927 case Intrinsic::x86_sse2_psrli_d:
6928 case Intrinsic::x86_sse2_psrli_q:
6929 case Intrinsic::x86_sse2_psrai_w:
6930 case Intrinsic::x86_sse2_psrai_d:
6931 case Intrinsic::x86_mmx_pslli_w:
6932 case Intrinsic::x86_mmx_pslli_d:
6933 case Intrinsic::x86_mmx_pslli_q:
6934 case Intrinsic::x86_mmx_psrli_w:
6935 case Intrinsic::x86_mmx_psrli_d:
6936 case Intrinsic::x86_mmx_psrli_q:
6937 case Intrinsic::x86_mmx_psrai_w:
6938 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006939 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006940 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006941 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006942
6943 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006944 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006945 switch (IntNo) {
6946 case Intrinsic::x86_sse2_pslli_w:
6947 NewIntNo = Intrinsic::x86_sse2_psll_w;
6948 break;
6949 case Intrinsic::x86_sse2_pslli_d:
6950 NewIntNo = Intrinsic::x86_sse2_psll_d;
6951 break;
6952 case Intrinsic::x86_sse2_pslli_q:
6953 NewIntNo = Intrinsic::x86_sse2_psll_q;
6954 break;
6955 case Intrinsic::x86_sse2_psrli_w:
6956 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6957 break;
6958 case Intrinsic::x86_sse2_psrli_d:
6959 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6960 break;
6961 case Intrinsic::x86_sse2_psrli_q:
6962 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6963 break;
6964 case Intrinsic::x86_sse2_psrai_w:
6965 NewIntNo = Intrinsic::x86_sse2_psra_w;
6966 break;
6967 case Intrinsic::x86_sse2_psrai_d:
6968 NewIntNo = Intrinsic::x86_sse2_psra_d;
6969 break;
6970 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006971 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006972 switch (IntNo) {
6973 case Intrinsic::x86_mmx_pslli_w:
6974 NewIntNo = Intrinsic::x86_mmx_psll_w;
6975 break;
6976 case Intrinsic::x86_mmx_pslli_d:
6977 NewIntNo = Intrinsic::x86_mmx_psll_d;
6978 break;
6979 case Intrinsic::x86_mmx_pslli_q:
6980 NewIntNo = Intrinsic::x86_mmx_psll_q;
6981 break;
6982 case Intrinsic::x86_mmx_psrli_w:
6983 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6984 break;
6985 case Intrinsic::x86_mmx_psrli_d:
6986 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6987 break;
6988 case Intrinsic::x86_mmx_psrli_q:
6989 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6990 break;
6991 case Intrinsic::x86_mmx_psrai_w:
6992 NewIntNo = Intrinsic::x86_mmx_psra_w;
6993 break;
6994 case Intrinsic::x86_mmx_psrai_d:
6995 NewIntNo = Intrinsic::x86_mmx_psra_d;
6996 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006997 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006998 }
6999 break;
7000 }
7001 }
Mon P Wangefa42202009-09-03 19:56:25 +00007002
7003 // The vector shift intrinsics with scalars uses 32b shift amounts but
7004 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7005 // to be zero.
7006 SDValue ShOps[4];
7007 ShOps[0] = ShAmt;
7008 ShOps[1] = DAG.getConstant(0, MVT::i32);
7009 if (ShAmtVT == MVT::v4i32) {
7010 ShOps[2] = DAG.getUNDEF(MVT::i32);
7011 ShOps[3] = DAG.getUNDEF(MVT::i32);
7012 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7013 } else {
7014 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7015 }
7016
Owen Andersone50ed302009-08-10 22:56:29 +00007017 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007018 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007019 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007021 Op.getOperand(1), ShAmt);
7022 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007023 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007024}
Evan Cheng72261582005-12-20 06:22:03 +00007025
Dan Gohman475871a2008-07-27 21:46:04 +00007026SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00007027 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007028 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007029
7030 if (Depth > 0) {
7031 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7032 SDValue Offset =
7033 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007034 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007035 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007036 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007037 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007038 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007039 }
7040
7041 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007042 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007043 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007044 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007045}
7046
Dan Gohman475871a2008-07-27 21:46:04 +00007047SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00007048 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7049 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007050 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007051 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007052 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7053 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007054 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007055 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007056 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7057 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007058 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007059}
7060
Dan Gohman475871a2008-07-27 21:46:04 +00007061SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007062 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007063 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007064}
7065
Dan Gohman475871a2008-07-27 21:46:04 +00007066SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007067{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007068 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007069 SDValue Chain = Op.getOperand(0);
7070 SDValue Offset = Op.getOperand(1);
7071 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007072 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007073
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007074 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7075 getPointerTy());
7076 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007077
Dale Johannesene4d209d2009-02-03 20:21:25 +00007078 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007079 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007080 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007081 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007082 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007083 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007084
Dale Johannesene4d209d2009-02-03 20:21:25 +00007085 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007087 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007088}
7089
Dan Gohman475871a2008-07-27 21:46:04 +00007090SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007091 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007092 SDValue Root = Op.getOperand(0);
7093 SDValue Trmp = Op.getOperand(1); // trampoline
7094 SDValue FPtr = Op.getOperand(2); // nested function
7095 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007096 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007097
Dan Gohman69de1932008-02-06 22:27:42 +00007098 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007099
7100 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007101 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007102
7103 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007104 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7105 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007106
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007107 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7108 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007109
7110 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7111
7112 // Load the pointer to the nested function into R11.
7113 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007115 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007116 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007117
Owen Anderson825b72b2009-08-11 20:47:22 +00007118 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7119 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007120 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7121 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007122
7123 // Load the 'nest' parameter value into R10.
7124 // R10 is specified in X86CallingConv.td
7125 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007126 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7127 DAG.getConstant(10, MVT::i64));
7128 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007129 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007130
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7132 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007133 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7134 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007135
7136 // Jump to the nested function.
7137 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7139 DAG.getConstant(20, MVT::i64));
7140 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007141 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007142
7143 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007144 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7145 DAG.getConstant(22, MVT::i64));
7146 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007147 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007148
Dan Gohman475871a2008-07-27 21:46:04 +00007149 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007150 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007151 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007152 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007153 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007154 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007155 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007156 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007157
7158 switch (CC) {
7159 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007160 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007161 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007162 case CallingConv::X86_StdCall: {
7163 // Pass 'nest' parameter in ECX.
7164 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007165 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007166
7167 // Check that ECX wasn't needed by an 'inreg' parameter.
7168 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007169 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007170
Chris Lattner58d74912008-03-12 17:45:29 +00007171 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007172 unsigned InRegCount = 0;
7173 unsigned Idx = 1;
7174
7175 for (FunctionType::param_iterator I = FTy->param_begin(),
7176 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007177 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007178 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007179 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007180
7181 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00007182 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007183 }
7184 }
7185 break;
7186 }
7187 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007188 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007189 // Pass 'nest' parameter in EAX.
7190 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007191 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007192 break;
7193 }
7194
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue OutChains[4];
7196 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007197
Owen Anderson825b72b2009-08-11 20:47:22 +00007198 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7199 DAG.getConstant(10, MVT::i32));
7200 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007201
Chris Lattnera62fe662010-02-05 19:20:30 +00007202 // This is storing the opcode for MOV32ri.
7203 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007204 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007205 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007207 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007208
Owen Anderson825b72b2009-08-11 20:47:22 +00007209 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7210 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007211 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7212 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007213
Chris Lattnera62fe662010-02-05 19:20:30 +00007214 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007215 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7216 DAG.getConstant(5, MVT::i32));
7217 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007218 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007219
Owen Anderson825b72b2009-08-11 20:47:22 +00007220 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7221 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007222 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7223 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007224
Dan Gohman475871a2008-07-27 21:46:04 +00007225 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007226 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007227 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007228 }
7229}
7230
Dan Gohman475871a2008-07-27 21:46:04 +00007231SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007232 /*
7233 The rounding mode is in bits 11:10 of FPSR, and has the following
7234 settings:
7235 00 Round to nearest
7236 01 Round to -inf
7237 10 Round to +inf
7238 11 Round to 0
7239
7240 FLT_ROUNDS, on the other hand, expects the following:
7241 -1 Undefined
7242 0 Round to 0
7243 1 Round to nearest
7244 2 Round to +inf
7245 3 Round to -inf
7246
7247 To perform the conversion, we do:
7248 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7249 */
7250
7251 MachineFunction &MF = DAG.getMachineFunction();
7252 const TargetMachine &TM = MF.getTarget();
7253 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7254 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007255 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007256 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007257
7258 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007259 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007260 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007261
Owen Anderson825b72b2009-08-11 20:47:22 +00007262 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007263 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007264
7265 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007266 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7267 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007268
7269 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007270 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007271 DAG.getNode(ISD::SRL, dl, MVT::i16,
7272 DAG.getNode(ISD::AND, dl, MVT::i16,
7273 CWD, DAG.getConstant(0x800, MVT::i16)),
7274 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007275 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007276 DAG.getNode(ISD::SRL, dl, MVT::i16,
7277 DAG.getNode(ISD::AND, dl, MVT::i16,
7278 CWD, DAG.getConstant(0x400, MVT::i16)),
7279 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007280
Dan Gohman475871a2008-07-27 21:46:04 +00007281 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 DAG.getNode(ISD::AND, dl, MVT::i16,
7283 DAG.getNode(ISD::ADD, dl, MVT::i16,
7284 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7285 DAG.getConstant(1, MVT::i16)),
7286 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007287
7288
Duncan Sands83ec4b62008-06-06 12:08:01 +00007289 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007290 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007291}
7292
Dan Gohman475871a2008-07-27 21:46:04 +00007293SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007294 EVT VT = Op.getValueType();
7295 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007296 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007297 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007298
7299 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007300 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007301 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007302 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007303 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007304 }
Evan Cheng18efe262007-12-14 02:13:44 +00007305
Evan Cheng152804e2007-12-14 08:30:15 +00007306 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007307 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007308 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007309
7310 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007311 SDValue Ops[] = {
7312 Op,
7313 DAG.getConstant(NumBits+NumBits-1, OpVT),
7314 DAG.getConstant(X86::COND_E, MVT::i8),
7315 Op.getValue(1)
7316 };
7317 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007318
7319 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007320 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007321
Owen Anderson825b72b2009-08-11 20:47:22 +00007322 if (VT == MVT::i8)
7323 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007324 return Op;
7325}
7326
Dan Gohman475871a2008-07-27 21:46:04 +00007327SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007328 EVT VT = Op.getValueType();
7329 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007330 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007331 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007332
7333 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007334 if (VT == MVT::i8) {
7335 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007336 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007337 }
Evan Cheng152804e2007-12-14 08:30:15 +00007338
7339 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007340 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007341 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007342
7343 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007344 SDValue Ops[] = {
7345 Op,
7346 DAG.getConstant(NumBits, OpVT),
7347 DAG.getConstant(X86::COND_E, MVT::i8),
7348 Op.getValue(1)
7349 };
7350 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007351
Owen Anderson825b72b2009-08-11 20:47:22 +00007352 if (VT == MVT::i8)
7353 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007354 return Op;
7355}
7356
Mon P Wangaf9b9522008-12-18 21:42:19 +00007357SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007358 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007360 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007361
Mon P Wangaf9b9522008-12-18 21:42:19 +00007362 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7363 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7364 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7365 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7366 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7367 //
7368 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7369 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7370 // return AloBlo + AloBhi + AhiBlo;
7371
7372 SDValue A = Op.getOperand(0);
7373 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007374
Dale Johannesene4d209d2009-02-03 20:21:25 +00007375 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007376 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7377 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007378 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007379 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7380 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007381 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007382 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007383 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007384 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007385 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007386 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007387 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007389 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007390 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007391 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7392 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007393 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007394 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7395 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007396 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7397 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007398 return Res;
7399}
7400
7401
Bill Wendling74c37652008-12-09 22:08:41 +00007402SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7403 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7404 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007405 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7406 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007407 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007408 SDValue LHS = N->getOperand(0);
7409 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007410 unsigned BaseOp = 0;
7411 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007412 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007413
7414 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007415 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007416 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007417 // A subtract of one will be selected as a INC. Note that INC doesn't
7418 // set CF, so we can't do this for UADDO.
7419 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7420 if (C->getAPIntValue() == 1) {
7421 BaseOp = X86ISD::INC;
7422 Cond = X86::COND_O;
7423 break;
7424 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007425 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007426 Cond = X86::COND_O;
7427 break;
7428 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007429 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007430 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007431 break;
7432 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007433 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7434 // set CF, so we can't do this for USUBO.
7435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7436 if (C->getAPIntValue() == 1) {
7437 BaseOp = X86ISD::DEC;
7438 Cond = X86::COND_O;
7439 break;
7440 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007441 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007442 Cond = X86::COND_O;
7443 break;
7444 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007445 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007446 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007447 break;
7448 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007449 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007450 Cond = X86::COND_O;
7451 break;
7452 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007453 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007454 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007455 break;
7456 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007457
Bill Wendling61edeb52008-12-02 01:06:39 +00007458 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007459 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007460 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007461
Bill Wendling61edeb52008-12-02 01:06:39 +00007462 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007463 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007465
Bill Wendling61edeb52008-12-02 01:06:39 +00007466 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7467 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007468}
7469
Dan Gohman475871a2008-07-27 21:46:04 +00007470SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007471 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007472 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007473 unsigned Reg = 0;
7474 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007475 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007476 default:
7477 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 case MVT::i8: Reg = X86::AL; size = 1; break;
7479 case MVT::i16: Reg = X86::AX; size = 2; break;
7480 case MVT::i32: Reg = X86::EAX; size = 4; break;
7481 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007482 assert(Subtarget->is64Bit() && "Node not type legal!");
7483 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007484 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007485 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007486 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007487 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007488 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007489 Op.getOperand(1),
7490 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007492 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007493 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007494 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007495 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007496 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007497 return cpOut;
7498}
7499
Duncan Sands1607f052008-12-01 11:39:25 +00007500SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007501 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007502 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007503 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007504 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007505 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007506 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007507 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7508 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007509 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7511 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007512 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007513 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007514 rdx.getValue(1)
7515 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007516 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007517}
7518
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007519SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7520 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007521 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007522 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007523 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007524 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007525 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007526 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007527 Node->getOperand(0),
7528 Node->getOperand(1), negOp,
7529 cast<AtomicSDNode>(Node)->getSrcValue(),
7530 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007531}
7532
Evan Cheng0db9fe62006-04-25 20:13:52 +00007533/// LowerOperation - Provide custom lowering hooks for some operations.
7534///
Dan Gohman475871a2008-07-27 21:46:04 +00007535SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007536 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007537 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007538 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7539 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007540 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007541 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007542 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7543 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7544 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7545 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7546 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7547 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007548 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007549 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007550 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007551 case ISD::SHL_PARTS:
7552 case ISD::SRA_PARTS:
7553 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7554 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007555 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007556 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007557 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007558 case ISD::FABS: return LowerFABS(Op, DAG);
7559 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007560 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007561 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007562 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007563 case ISD::SELECT: return LowerSELECT(Op, DAG);
7564 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007565 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007566 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007567 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007568 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007569 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007570 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7571 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007572 case ISD::FRAME_TO_ARGS_OFFSET:
7573 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007574 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007575 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007576 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007577 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007578 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7579 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007580 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007581 case ISD::SADDO:
7582 case ISD::UADDO:
7583 case ISD::SSUBO:
7584 case ISD::USUBO:
7585 case ISD::SMULO:
7586 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007587 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007588 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007589}
7590
Duncan Sands1607f052008-12-01 11:39:25 +00007591void X86TargetLowering::
7592ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7593 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007594 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007595 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007596 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007597
7598 SDValue Chain = Node->getOperand(0);
7599 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007601 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007602 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007603 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007604 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007605 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007606 SDValue Result =
7607 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7608 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007609 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007610 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007611 Results.push_back(Result.getValue(2));
7612}
7613
Duncan Sands126d9072008-07-04 11:47:58 +00007614/// ReplaceNodeResults - Replace a node with an illegal result type
7615/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007616void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7617 SmallVectorImpl<SDValue>&Results,
7618 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007619 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007620 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007621 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007622 assert(false && "Do not know how to custom type legalize this operation!");
7623 return;
7624 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007625 std::pair<SDValue,SDValue> Vals =
7626 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007627 SDValue FIST = Vals.first, StackSlot = Vals.second;
7628 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007629 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007630 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007631 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7632 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007633 }
7634 return;
7635 }
7636 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007637 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007638 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007639 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007641 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007643 eax.getValue(2));
7644 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7645 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007646 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007647 Results.push_back(edx.getValue(1));
7648 return;
7649 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007650 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007651 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007652 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007653 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007654 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7655 DAG.getConstant(0, MVT::i32));
7656 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7657 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007658 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7659 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007660 cpInL.getValue(1));
7661 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007662 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7663 DAG.getConstant(0, MVT::i32));
7664 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7665 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007666 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007667 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007668 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007669 swapInL.getValue(1));
7670 SDValue Ops[] = { swapInH.getValue(0),
7671 N->getOperand(1),
7672 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007673 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007674 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007675 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007676 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007677 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007678 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007679 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007680 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007681 Results.push_back(cpOutH.getValue(1));
7682 return;
7683 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007684 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007685 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7686 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007687 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007688 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7689 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007690 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007691 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7692 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007693 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007694 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7695 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007696 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007697 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7698 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007699 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007700 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7701 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007702 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007703 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7704 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007705 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007706}
7707
Evan Cheng72261582005-12-20 06:22:03 +00007708const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7709 switch (Opcode) {
7710 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007711 case X86ISD::BSF: return "X86ISD::BSF";
7712 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007713 case X86ISD::SHLD: return "X86ISD::SHLD";
7714 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007715 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007716 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007717 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007718 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007719 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007720 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007721 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7722 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7723 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007724 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007725 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007726 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007727 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007728 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007729 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007730 case X86ISD::COMI: return "X86ISD::COMI";
7731 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007732 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007733 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007734 case X86ISD::CMOV: return "X86ISD::CMOV";
7735 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007736 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007737 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7738 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007739 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007740 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007741 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007742 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007743 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007744 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7745 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007746 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007747 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007748 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007749 case X86ISD::FMAX: return "X86ISD::FMAX";
7750 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007751 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7752 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007753 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007754 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007755 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007756 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007757 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007758 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7759 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007760 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7761 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7762 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7763 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7764 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7765 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007766 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7767 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007768 case X86ISD::VSHL: return "X86ISD::VSHL";
7769 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007770 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7771 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7772 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7773 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7774 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7775 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7776 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7777 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7778 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7779 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007780 case X86ISD::ADD: return "X86ISD::ADD";
7781 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007782 case X86ISD::SMUL: return "X86ISD::SMUL";
7783 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007784 case X86ISD::INC: return "X86ISD::INC";
7785 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007786 case X86ISD::OR: return "X86ISD::OR";
7787 case X86ISD::XOR: return "X86ISD::XOR";
7788 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007789 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007790 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007791 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007792 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007793 }
7794}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007795
Chris Lattnerc9addb72007-03-30 23:15:24 +00007796// isLegalAddressingMode - Return true if the addressing mode represented
7797// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007798bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007799 const Type *Ty) const {
7800 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007801 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007802
Chris Lattnerc9addb72007-03-30 23:15:24 +00007803 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007804 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007805 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007806
Chris Lattnerc9addb72007-03-30 23:15:24 +00007807 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007808 unsigned GVFlags =
7809 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007810
Chris Lattnerdfed4132009-07-10 07:38:24 +00007811 // If a reference to this global requires an extra load, we can't fold it.
7812 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007813 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007814
Chris Lattnerdfed4132009-07-10 07:38:24 +00007815 // If BaseGV requires a register for the PIC base, we cannot also have a
7816 // BaseReg specified.
7817 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007818 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007819
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007820 // If lower 4G is not available, then we must use rip-relative addressing.
7821 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7822 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007824
Chris Lattnerc9addb72007-03-30 23:15:24 +00007825 switch (AM.Scale) {
7826 case 0:
7827 case 1:
7828 case 2:
7829 case 4:
7830 case 8:
7831 // These scales always work.
7832 break;
7833 case 3:
7834 case 5:
7835 case 9:
7836 // These scales are formed with basereg+scalereg. Only accept if there is
7837 // no basereg yet.
7838 if (AM.HasBaseReg)
7839 return false;
7840 break;
7841 default: // Other stuff never works.
7842 return false;
7843 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007844
Chris Lattnerc9addb72007-03-30 23:15:24 +00007845 return true;
7846}
7847
7848
Evan Cheng2bd122c2007-10-26 01:56:11 +00007849bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007850 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007851 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007852 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7853 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007854 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007855 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007856 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007857}
7858
Owen Andersone50ed302009-08-10 22:56:29 +00007859bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007860 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007861 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007862 unsigned NumBits1 = VT1.getSizeInBits();
7863 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007864 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007865 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007866 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007867}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007868
Dan Gohman97121ba2009-04-08 00:15:30 +00007869bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007870 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007871 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007872}
7873
Owen Andersone50ed302009-08-10 22:56:29 +00007874bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007875 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007876 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007877}
7878
Owen Andersone50ed302009-08-10 22:56:29 +00007879bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007880 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007881 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007882}
7883
Evan Cheng60c07e12006-07-05 22:17:51 +00007884/// isShuffleMaskLegal - Targets can use this to indicate that they only
7885/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7886/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7887/// are assumed to be legal.
7888bool
Eric Christopherfd179292009-08-27 18:07:15 +00007889X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007890 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007891 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007892 if (VT.getSizeInBits() == 64)
7893 return false;
7894
Nate Begemana09008b2009-10-19 02:17:23 +00007895 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007896 return (VT.getVectorNumElements() == 2 ||
7897 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7898 isMOVLMask(M, VT) ||
7899 isSHUFPMask(M, VT) ||
7900 isPSHUFDMask(M, VT) ||
7901 isPSHUFHWMask(M, VT) ||
7902 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007903 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007904 isUNPCKLMask(M, VT) ||
7905 isUNPCKHMask(M, VT) ||
7906 isUNPCKL_v_undef_Mask(M, VT) ||
7907 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007908}
7909
Dan Gohman7d8143f2008-04-09 20:09:42 +00007910bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007911X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007912 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007913 unsigned NumElts = VT.getVectorNumElements();
7914 // FIXME: This collection of masks seems suspect.
7915 if (NumElts == 2)
7916 return true;
7917 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7918 return (isMOVLMask(Mask, VT) ||
7919 isCommutedMOVLMask(Mask, VT, true) ||
7920 isSHUFPMask(Mask, VT) ||
7921 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007922 }
7923 return false;
7924}
7925
7926//===----------------------------------------------------------------------===//
7927// X86 Scheduler Hooks
7928//===----------------------------------------------------------------------===//
7929
Mon P Wang63307c32008-05-05 19:05:59 +00007930// private utility function
7931MachineBasicBlock *
7932X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7933 MachineBasicBlock *MBB,
7934 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007935 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007936 unsigned LoadOpc,
7937 unsigned CXchgOpc,
7938 unsigned copyOpc,
7939 unsigned notOpc,
7940 unsigned EAXreg,
7941 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007942 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007943 // For the atomic bitwise operator, we generate
7944 // thisMBB:
7945 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007946 // ld t1 = [bitinstr.addr]
7947 // op t2 = t1, [bitinstr.val]
7948 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007949 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7950 // bz newMBB
7951 // fallthrough -->nextMBB
7952 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7953 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007954 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007955 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007956
Mon P Wang63307c32008-05-05 19:05:59 +00007957 /// First build the CFG
7958 MachineFunction *F = MBB->getParent();
7959 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007960 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7961 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7962 F->insert(MBBIter, newMBB);
7963 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007964
Mon P Wang63307c32008-05-05 19:05:59 +00007965 // Move all successors to thisMBB to nextMBB
7966 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007967
Mon P Wang63307c32008-05-05 19:05:59 +00007968 // Update thisMBB to fall through to newMBB
7969 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007970
Mon P Wang63307c32008-05-05 19:05:59 +00007971 // newMBB jumps to itself and fall through to nextMBB
7972 newMBB->addSuccessor(nextMBB);
7973 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007974
Mon P Wang63307c32008-05-05 19:05:59 +00007975 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007976 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007977 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007978 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007979 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007980 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007981 int numArgs = bInstr->getNumOperands() - 1;
7982 for (int i=0; i < numArgs; ++i)
7983 argOpers[i] = &bInstr->getOperand(i+1);
7984
7985 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007986 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7987 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007988
Dale Johannesen140be2d2008-08-19 18:47:28 +00007989 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007990 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007991 for (int i=0; i <= lastAddrIndx; ++i)
7992 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007993
Dale Johannesen140be2d2008-08-19 18:47:28 +00007994 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007995 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007997 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007998 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007999 tt = t1;
8000
Dale Johannesen140be2d2008-08-19 18:47:28 +00008001 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008002 assert((argOpers[valArgIndx]->isReg() ||
8003 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008004 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008005 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008006 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008007 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008008 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008009 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008010 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008011
Dale Johannesene4d209d2009-02-03 20:21:25 +00008012 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008013 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008014
Dale Johannesene4d209d2009-02-03 20:21:25 +00008015 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008016 for (int i=0; i <= lastAddrIndx; ++i)
8017 (*MIB).addOperand(*argOpers[i]);
8018 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008019 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008020 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8021 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008022
Dale Johannesene4d209d2009-02-03 20:21:25 +00008023 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008024 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008025
Mon P Wang63307c32008-05-05 19:05:59 +00008026 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008027 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008028
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008029 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008030 return nextMBB;
8031}
8032
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008033// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008034MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008035X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8036 MachineBasicBlock *MBB,
8037 unsigned regOpcL,
8038 unsigned regOpcH,
8039 unsigned immOpcL,
8040 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008041 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008042 // For the atomic bitwise operator, we generate
8043 // thisMBB (instructions are in pairs, except cmpxchg8b)
8044 // ld t1,t2 = [bitinstr.addr]
8045 // newMBB:
8046 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8047 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008048 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008049 // mov ECX, EBX <- t5, t6
8050 // mov EAX, EDX <- t1, t2
8051 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8052 // mov t3, t4 <- EAX, EDX
8053 // bz newMBB
8054 // result in out1, out2
8055 // fallthrough -->nextMBB
8056
8057 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8058 const unsigned LoadOpc = X86::MOV32rm;
8059 const unsigned copyOpc = X86::MOV32rr;
8060 const unsigned NotOpc = X86::NOT32r;
8061 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8062 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8063 MachineFunction::iterator MBBIter = MBB;
8064 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008065
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008066 /// First build the CFG
8067 MachineFunction *F = MBB->getParent();
8068 MachineBasicBlock *thisMBB = MBB;
8069 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8070 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8071 F->insert(MBBIter, newMBB);
8072 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008073
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008074 // Move all successors to thisMBB to nextMBB
8075 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008076
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008077 // Update thisMBB to fall through to newMBB
8078 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008079
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080 // newMBB jumps to itself and fall through to nextMBB
8081 newMBB->addSuccessor(nextMBB);
8082 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008083
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008085 // Insert instructions into newMBB based on incoming instruction
8086 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008087 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008088 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008089 MachineOperand& dest1Oper = bInstr->getOperand(0);
8090 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008091 MachineOperand* argOpers[2 + X86AddrNumOperands];
8092 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008093 argOpers[i] = &bInstr->getOperand(i+2);
8094
Evan Chengad5b52f2010-01-08 19:14:57 +00008095 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008096 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008097
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008098 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008099 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008100 for (int i=0; i <= lastAddrIndx; ++i)
8101 (*MIB).addOperand(*argOpers[i]);
8102 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008103 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008104 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008105 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008106 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008107 MachineOperand newOp3 = *(argOpers[3]);
8108 if (newOp3.isImm())
8109 newOp3.setImm(newOp3.getImm()+4);
8110 else
8111 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008112 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008113 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008114
8115 // t3/4 are defined later, at the bottom of the loop
8116 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8117 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008118 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008119 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008120 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008121 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8122
Evan Cheng306b4ca2010-01-08 23:41:50 +00008123 // The subsequent operations should be using the destination registers of
8124 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008125 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008126 t1 = F->getRegInfo().createVirtualRegister(RC);
8127 t2 = F->getRegInfo().createVirtualRegister(RC);
8128 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8129 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008130 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008131 t1 = dest1Oper.getReg();
8132 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008133 }
8134
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008135 int valArgIndx = lastAddrIndx + 1;
8136 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008137 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008138 "invalid operand");
8139 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8140 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008141 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008142 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008143 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008144 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008145 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008146 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008147 (*MIB).addOperand(*argOpers[valArgIndx]);
8148 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008149 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008150 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008151 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008152 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008153 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008154 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008155 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008156 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008157 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008158 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008159
Dale Johannesene4d209d2009-02-03 20:21:25 +00008160 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008161 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008162 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008163 MIB.addReg(t2);
8164
Dale Johannesene4d209d2009-02-03 20:21:25 +00008165 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008166 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008167 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008168 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008169
Dale Johannesene4d209d2009-02-03 20:21:25 +00008170 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008171 for (int i=0; i <= lastAddrIndx; ++i)
8172 (*MIB).addOperand(*argOpers[i]);
8173
8174 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008175 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8176 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008177
Dale Johannesene4d209d2009-02-03 20:21:25 +00008178 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008179 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008180 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008181 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008182
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008183 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008184 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008185
8186 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8187 return nextMBB;
8188}
8189
8190// private utility function
8191MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008192X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8193 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008194 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008195 // For the atomic min/max operator, we generate
8196 // thisMBB:
8197 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008198 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008199 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008200 // cmp t1, t2
8201 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008202 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008203 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8204 // bz newMBB
8205 // fallthrough -->nextMBB
8206 //
8207 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8208 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008209 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008210 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008211
Mon P Wang63307c32008-05-05 19:05:59 +00008212 /// First build the CFG
8213 MachineFunction *F = MBB->getParent();
8214 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008215 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8216 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8217 F->insert(MBBIter, newMBB);
8218 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Dan Gohmand6708ea2009-08-15 01:38:56 +00008220 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008221 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008222
Mon P Wang63307c32008-05-05 19:05:59 +00008223 // Update thisMBB to fall through to newMBB
8224 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008225
Mon P Wang63307c32008-05-05 19:05:59 +00008226 // newMBB jumps to newMBB and fall through to nextMBB
8227 newMBB->addSuccessor(nextMBB);
8228 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008229
Dale Johannesene4d209d2009-02-03 20:21:25 +00008230 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008231 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008232 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008233 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008234 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008235 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008236 int numArgs = mInstr->getNumOperands() - 1;
8237 for (int i=0; i < numArgs; ++i)
8238 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008239
Mon P Wang63307c32008-05-05 19:05:59 +00008240 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008241 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8242 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008243
Mon P Wangab3e7472008-05-05 22:56:23 +00008244 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008245 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008246 for (int i=0; i <= lastAddrIndx; ++i)
8247 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008248
Mon P Wang63307c32008-05-05 19:05:59 +00008249 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008250 assert((argOpers[valArgIndx]->isReg() ||
8251 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008252 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008253
8254 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008255 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008256 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008257 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008259 (*MIB).addOperand(*argOpers[valArgIndx]);
8260
Dale Johannesene4d209d2009-02-03 20:21:25 +00008261 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008262 MIB.addReg(t1);
8263
Dale Johannesene4d209d2009-02-03 20:21:25 +00008264 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008265 MIB.addReg(t1);
8266 MIB.addReg(t2);
8267
8268 // Generate movc
8269 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008270 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008271 MIB.addReg(t2);
8272 MIB.addReg(t1);
8273
8274 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008275 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008276 for (int i=0; i <= lastAddrIndx; ++i)
8277 (*MIB).addOperand(*argOpers[i]);
8278 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008279 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008280 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8281 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008282
Dale Johannesene4d209d2009-02-03 20:21:25 +00008283 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008284 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008285
Mon P Wang63307c32008-05-05 19:05:59 +00008286 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008287 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008288
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008289 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008290 return nextMBB;
8291}
8292
Eric Christopherf83a5de2009-08-27 18:08:16 +00008293// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8294// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008295MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008296X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008297 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008298
8299 MachineFunction *F = BB->getParent();
8300 DebugLoc dl = MI->getDebugLoc();
8301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8302
8303 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008304 if (memArg)
8305 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8306 else
8307 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008308
8309 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8310
8311 for (unsigned i = 0; i < numArgs; ++i) {
8312 MachineOperand &Op = MI->getOperand(i+1);
8313
8314 if (!(Op.isReg() && Op.isImplicit()))
8315 MIB.addOperand(Op);
8316 }
8317
8318 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8319 .addReg(X86::XMM0);
8320
8321 F->DeleteMachineInstr(MI);
8322
8323 return BB;
8324}
8325
8326MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008327X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8328 MachineInstr *MI,
8329 MachineBasicBlock *MBB) const {
8330 // Emit code to save XMM registers to the stack. The ABI says that the
8331 // number of registers to save is given in %al, so it's theoretically
8332 // possible to do an indirect jump trick to avoid saving all of them,
8333 // however this code takes a simpler approach and just executes all
8334 // of the stores if %al is non-zero. It's less code, and it's probably
8335 // easier on the hardware branch predictor, and stores aren't all that
8336 // expensive anyway.
8337
8338 // Create the new basic blocks. One block contains all the XMM stores,
8339 // and one block is the final destination regardless of whether any
8340 // stores were performed.
8341 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8342 MachineFunction *F = MBB->getParent();
8343 MachineFunction::iterator MBBIter = MBB;
8344 ++MBBIter;
8345 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8346 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8347 F->insert(MBBIter, XMMSaveMBB);
8348 F->insert(MBBIter, EndMBB);
8349
8350 // Set up the CFG.
8351 // Move any original successors of MBB to the end block.
8352 EndMBB->transferSuccessors(MBB);
8353 // The original block will now fall through to the XMM save block.
8354 MBB->addSuccessor(XMMSaveMBB);
8355 // The XMMSaveMBB will fall through to the end block.
8356 XMMSaveMBB->addSuccessor(EndMBB);
8357
8358 // Now add the instructions.
8359 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8360 DebugLoc DL = MI->getDebugLoc();
8361
8362 unsigned CountReg = MI->getOperand(0).getReg();
8363 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8364 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8365
8366 if (!Subtarget->isTargetWin64()) {
8367 // If %al is 0, branch around the XMM save block.
8368 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008369 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008370 MBB->addSuccessor(EndMBB);
8371 }
8372
8373 // In the XMM save block, save all the XMM argument registers.
8374 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8375 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008376 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008377 F->getMachineMemOperand(
8378 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8379 MachineMemOperand::MOStore, Offset,
8380 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008381 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8382 .addFrameIndex(RegSaveFrameIndex)
8383 .addImm(/*Scale=*/1)
8384 .addReg(/*IndexReg=*/0)
8385 .addImm(/*Disp=*/Offset)
8386 .addReg(/*Segment=*/0)
8387 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008388 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008389 }
8390
8391 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8392
8393 return EndMBB;
8394}
Mon P Wang63307c32008-05-05 19:05:59 +00008395
Evan Cheng60c07e12006-07-05 22:17:51 +00008396MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008397X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008398 MachineBasicBlock *BB,
8399 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008400 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8401 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008402
Chris Lattner52600972009-09-02 05:57:00 +00008403 // To "insert" a SELECT_CC instruction, we actually have to insert the
8404 // diamond control-flow pattern. The incoming instruction knows the
8405 // destination vreg to set, the condition code register to branch on, the
8406 // true/false values to select between, and a branch opcode to use.
8407 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8408 MachineFunction::iterator It = BB;
8409 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008410
Chris Lattner52600972009-09-02 05:57:00 +00008411 // thisMBB:
8412 // ...
8413 // TrueVal = ...
8414 // cmpTY ccX, r1, r2
8415 // bCC copy1MBB
8416 // fallthrough --> copy0MBB
8417 MachineBasicBlock *thisMBB = BB;
8418 MachineFunction *F = BB->getParent();
8419 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8420 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8421 unsigned Opc =
8422 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8423 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8424 F->insert(It, copy0MBB);
8425 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008426 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008427 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008428 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008429 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008430 E = BB->succ_end(); I != E; ++I) {
8431 EM->insert(std::make_pair(*I, sinkMBB));
8432 sinkMBB->addSuccessor(*I);
8433 }
8434 // Next, remove all successors of the current block, and add the true
8435 // and fallthrough blocks as its successors.
8436 while (!BB->succ_empty())
8437 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008438 // Add the true and fallthrough blocks as its successors.
8439 BB->addSuccessor(copy0MBB);
8440 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008441
Chris Lattner52600972009-09-02 05:57:00 +00008442 // copy0MBB:
8443 // %FalseValue = ...
8444 // # fallthrough to sinkMBB
8445 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008446
Chris Lattner52600972009-09-02 05:57:00 +00008447 // Update machine-CFG edges
8448 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008449
Chris Lattner52600972009-09-02 05:57:00 +00008450 // sinkMBB:
8451 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8452 // ...
8453 BB = sinkMBB;
8454 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8455 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8456 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8457
8458 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8459 return BB;
8460}
8461
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008462MachineBasicBlock *
8463X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8464 MachineBasicBlock *BB,
8465 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8466 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8467 DebugLoc DL = MI->getDebugLoc();
8468 MachineFunction *F = BB->getParent();
8469
8470 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8471 // non-trivial part is impdef of ESP.
8472 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8473 // mingw-w64.
8474
8475 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8476 .addExternalSymbol("_alloca")
8477 .addReg(X86::EAX, RegState::Implicit)
8478 .addReg(X86::ESP, RegState::Implicit)
8479 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8480 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8481
8482 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8483 return BB;
8484}
Chris Lattner52600972009-09-02 05:57:00 +00008485
8486MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008487X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008488 MachineBasicBlock *BB,
8489 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008490 switch (MI->getOpcode()) {
8491 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008492 case X86::MINGW_ALLOCA:
8493 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008494 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008495 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008496 case X86::CMOV_FR32:
8497 case X86::CMOV_FR64:
8498 case X86::CMOV_V4F32:
8499 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008500 case X86::CMOV_V2I64:
Evan Chengce319102009-09-19 09:51:03 +00008501 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008502
Dale Johannesen849f2142007-07-03 00:53:03 +00008503 case X86::FP32_TO_INT16_IN_MEM:
8504 case X86::FP32_TO_INT32_IN_MEM:
8505 case X86::FP32_TO_INT64_IN_MEM:
8506 case X86::FP64_TO_INT16_IN_MEM:
8507 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008508 case X86::FP64_TO_INT64_IN_MEM:
8509 case X86::FP80_TO_INT16_IN_MEM:
8510 case X86::FP80_TO_INT32_IN_MEM:
8511 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008512 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8513 DebugLoc DL = MI->getDebugLoc();
8514
Evan Cheng60c07e12006-07-05 22:17:51 +00008515 // Change the floating point control register to use "round towards zero"
8516 // mode when truncating to an integer value.
8517 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008518 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008519 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008520
8521 // Load the old value of the high byte of the control word...
8522 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008523 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008524 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008525 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008526
8527 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008528 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008529 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008530
8531 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008532 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008533
8534 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008535 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008536 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008537
8538 // Get the X86 opcode to use.
8539 unsigned Opc;
8540 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008541 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008542 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8543 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8544 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8545 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8546 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8547 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008548 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8549 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8550 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008551 }
8552
8553 X86AddressMode AM;
8554 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008555 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008556 AM.BaseType = X86AddressMode::RegBase;
8557 AM.Base.Reg = Op.getReg();
8558 } else {
8559 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008560 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008561 }
8562 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008563 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008564 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008565 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008566 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008567 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008568 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008569 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008570 AM.GV = Op.getGlobal();
8571 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008572 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008573 }
Chris Lattner52600972009-09-02 05:57:00 +00008574 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008575 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008576
8577 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008578 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008579
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008580 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008581 return BB;
8582 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008583 // DBG_VALUE. Only the frame index case is done here.
8584 case X86::DBG_VALUE: {
8585 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8586 DebugLoc DL = MI->getDebugLoc();
8587 X86AddressMode AM;
8588 MachineFunction *F = BB->getParent();
8589 AM.BaseType = X86AddressMode::FrameIndexBase;
8590 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8591 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8592 addImm(MI->getOperand(1).getImm()).
8593 addMetadata(MI->getOperand(2).getMetadata());
8594 F->DeleteMachineInstr(MI); // Remove pseudo.
8595 return BB;
8596 }
8597
Eric Christopherb120ab42009-08-18 22:50:32 +00008598 // String/text processing lowering.
8599 case X86::PCMPISTRM128REG:
8600 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8601 case X86::PCMPISTRM128MEM:
8602 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8603 case X86::PCMPESTRM128REG:
8604 return EmitPCMP(MI, BB, 5, false /* in mem */);
8605 case X86::PCMPESTRM128MEM:
8606 return EmitPCMP(MI, BB, 5, true /* in mem */);
8607
8608 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008609 case X86::ATOMAND32:
8610 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008611 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008612 X86::LCMPXCHG32, X86::MOV32rr,
8613 X86::NOT32r, X86::EAX,
8614 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008615 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008616 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8617 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008618 X86::LCMPXCHG32, X86::MOV32rr,
8619 X86::NOT32r, X86::EAX,
8620 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008621 case X86::ATOMXOR32:
8622 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008623 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008624 X86::LCMPXCHG32, X86::MOV32rr,
8625 X86::NOT32r, X86::EAX,
8626 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008627 case X86::ATOMNAND32:
8628 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008629 X86::AND32ri, X86::MOV32rm,
8630 X86::LCMPXCHG32, X86::MOV32rr,
8631 X86::NOT32r, X86::EAX,
8632 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008633 case X86::ATOMMIN32:
8634 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8635 case X86::ATOMMAX32:
8636 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8637 case X86::ATOMUMIN32:
8638 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8639 case X86::ATOMUMAX32:
8640 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008641
8642 case X86::ATOMAND16:
8643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8644 X86::AND16ri, X86::MOV16rm,
8645 X86::LCMPXCHG16, X86::MOV16rr,
8646 X86::NOT16r, X86::AX,
8647 X86::GR16RegisterClass);
8648 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008650 X86::OR16ri, X86::MOV16rm,
8651 X86::LCMPXCHG16, X86::MOV16rr,
8652 X86::NOT16r, X86::AX,
8653 X86::GR16RegisterClass);
8654 case X86::ATOMXOR16:
8655 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8656 X86::XOR16ri, X86::MOV16rm,
8657 X86::LCMPXCHG16, X86::MOV16rr,
8658 X86::NOT16r, X86::AX,
8659 X86::GR16RegisterClass);
8660 case X86::ATOMNAND16:
8661 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8662 X86::AND16ri, X86::MOV16rm,
8663 X86::LCMPXCHG16, X86::MOV16rr,
8664 X86::NOT16r, X86::AX,
8665 X86::GR16RegisterClass, true);
8666 case X86::ATOMMIN16:
8667 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8668 case X86::ATOMMAX16:
8669 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8670 case X86::ATOMUMIN16:
8671 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8672 case X86::ATOMUMAX16:
8673 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8674
8675 case X86::ATOMAND8:
8676 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8677 X86::AND8ri, X86::MOV8rm,
8678 X86::LCMPXCHG8, X86::MOV8rr,
8679 X86::NOT8r, X86::AL,
8680 X86::GR8RegisterClass);
8681 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008682 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008683 X86::OR8ri, X86::MOV8rm,
8684 X86::LCMPXCHG8, X86::MOV8rr,
8685 X86::NOT8r, X86::AL,
8686 X86::GR8RegisterClass);
8687 case X86::ATOMXOR8:
8688 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8689 X86::XOR8ri, X86::MOV8rm,
8690 X86::LCMPXCHG8, X86::MOV8rr,
8691 X86::NOT8r, X86::AL,
8692 X86::GR8RegisterClass);
8693 case X86::ATOMNAND8:
8694 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8695 X86::AND8ri, X86::MOV8rm,
8696 X86::LCMPXCHG8, X86::MOV8rr,
8697 X86::NOT8r, X86::AL,
8698 X86::GR8RegisterClass, true);
8699 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008700 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008701 case X86::ATOMAND64:
8702 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008703 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008704 X86::LCMPXCHG64, X86::MOV64rr,
8705 X86::NOT64r, X86::RAX,
8706 X86::GR64RegisterClass);
8707 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008708 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8709 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008710 X86::LCMPXCHG64, X86::MOV64rr,
8711 X86::NOT64r, X86::RAX,
8712 X86::GR64RegisterClass);
8713 case X86::ATOMXOR64:
8714 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008715 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008716 X86::LCMPXCHG64, X86::MOV64rr,
8717 X86::NOT64r, X86::RAX,
8718 X86::GR64RegisterClass);
8719 case X86::ATOMNAND64:
8720 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8721 X86::AND64ri32, X86::MOV64rm,
8722 X86::LCMPXCHG64, X86::MOV64rr,
8723 X86::NOT64r, X86::RAX,
8724 X86::GR64RegisterClass, true);
8725 case X86::ATOMMIN64:
8726 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8727 case X86::ATOMMAX64:
8728 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8729 case X86::ATOMUMIN64:
8730 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8731 case X86::ATOMUMAX64:
8732 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008733
8734 // This group does 64-bit operations on a 32-bit host.
8735 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008736 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008737 X86::AND32rr, X86::AND32rr,
8738 X86::AND32ri, X86::AND32ri,
8739 false);
8740 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008741 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008742 X86::OR32rr, X86::OR32rr,
8743 X86::OR32ri, X86::OR32ri,
8744 false);
8745 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008746 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008747 X86::XOR32rr, X86::XOR32rr,
8748 X86::XOR32ri, X86::XOR32ri,
8749 false);
8750 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008751 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008752 X86::AND32rr, X86::AND32rr,
8753 X86::AND32ri, X86::AND32ri,
8754 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008755 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008756 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008757 X86::ADD32rr, X86::ADC32rr,
8758 X86::ADD32ri, X86::ADC32ri,
8759 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008760 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008761 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008762 X86::SUB32rr, X86::SBB32rr,
8763 X86::SUB32ri, X86::SBB32ri,
8764 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008765 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008766 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008767 X86::MOV32rr, X86::MOV32rr,
8768 X86::MOV32ri, X86::MOV32ri,
8769 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008770 case X86::VASTART_SAVE_XMM_REGS:
8771 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008772 }
8773}
8774
8775//===----------------------------------------------------------------------===//
8776// X86 Optimization Hooks
8777//===----------------------------------------------------------------------===//
8778
Dan Gohman475871a2008-07-27 21:46:04 +00008779void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008780 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008781 APInt &KnownZero,
8782 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008783 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008784 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008785 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008786 assert((Opc >= ISD::BUILTIN_OP_END ||
8787 Opc == ISD::INTRINSIC_WO_CHAIN ||
8788 Opc == ISD::INTRINSIC_W_CHAIN ||
8789 Opc == ISD::INTRINSIC_VOID) &&
8790 "Should use MaskedValueIsZero if you don't know whether Op"
8791 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008792
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008793 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008794 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008795 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008796 case X86ISD::ADD:
8797 case X86ISD::SUB:
8798 case X86ISD::SMUL:
8799 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008800 case X86ISD::INC:
8801 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008802 case X86ISD::OR:
8803 case X86ISD::XOR:
8804 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008805 // These nodes' second result is a boolean.
8806 if (Op.getResNo() == 0)
8807 break;
8808 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008809 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008810 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8811 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008812 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008813 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008814}
Chris Lattner259e97c2006-01-31 19:43:35 +00008815
Evan Cheng206ee9d2006-07-07 08:33:52 +00008816/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008817/// node is a GlobalAddress + offset.
8818bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8819 GlobalValue* &GA, int64_t &Offset) const{
8820 if (N->getOpcode() == X86ISD::Wrapper) {
8821 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008822 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008823 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008824 return true;
8825 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008826 }
Evan Chengad4196b2008-05-12 19:56:52 +00008827 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008828}
8829
Nate Begeman9008ca62009-04-27 18:41:29 +00008830static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008831 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008832 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008833 SelectionDAG &DAG, MachineFrameInfo *MFI,
8834 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008835 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008836 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008837 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008838 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008839 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008840 return false;
8841 continue;
8842 }
8843
Dan Gohman475871a2008-07-27 21:46:04 +00008844 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008845 if (!Elt.getNode() ||
8846 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008847 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008848 if (!LDBase) {
8849 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008850 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008851 LDBase = cast<LoadSDNode>(Elt.getNode());
8852 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008853 continue;
8854 }
8855 if (Elt.getOpcode() == ISD::UNDEF)
8856 continue;
8857
Nate Begemanabc01992009-06-05 21:37:30 +00008858 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008859 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008860 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008861 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008862 }
8863 return true;
8864}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008865
8866/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8867/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8868/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008869/// order. In the case of v2i64, it will see if it can rewrite the
8870/// shuffle to be an appropriate build vector so it can take advantage of
8871// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008872static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008873 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008874 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008875 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008876 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008877 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8878 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008879
Eli Friedman7a5e5552009-06-07 06:52:44 +00008880 if (VT.getSizeInBits() != 128)
8881 return SDValue();
8882
Mon P Wang1e955802009-04-03 02:43:30 +00008883 // Try to combine a vector_shuffle into a 128-bit load.
8884 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008885 LoadSDNode *LD = NULL;
8886 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008887 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008888 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008889 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008890
Eli Friedman7a5e5552009-06-07 06:52:44 +00008891 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008892 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008893 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8894 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008895 LD->isVolatile(), LD->isNonTemporal(), 0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008896 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008897 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008898 LD->isVolatile(), LD->isNonTemporal(),
8899 LD->getAlignment());
Eli Friedman7a5e5552009-06-07 06:52:44 +00008900 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008901 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008902 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8903 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008904 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8905 }
8906 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008907}
Evan Chengd880b972008-05-09 21:53:03 +00008908
Chris Lattner83e6c992006-10-04 06:57:07 +00008909/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008910static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008911 const X86Subtarget *Subtarget) {
8912 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008913 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008914 // Get the LHS/RHS of the select.
8915 SDValue LHS = N->getOperand(1);
8916 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008917
Dan Gohman670e5392009-09-21 18:03:22 +00008918 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008919 // instructions match the semantics of the common C idiom x<y?x:y but not
8920 // x<=y?x:y, because of how they handle negative zero (which can be
8921 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008922 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008923 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008924 Cond.getOpcode() == ISD::SETCC) {
8925 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008926
Chris Lattner47b4ce82009-03-11 05:48:52 +00008927 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008928 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008929 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8930 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008931 switch (CC) {
8932 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008933 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008934 // Converting this to a min would handle NaNs incorrectly, and swapping
8935 // the operands would cause it to handle comparisons between positive
8936 // and negative zero incorrectly.
8937 if (!FiniteOnlyFPMath() &&
8938 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8939 if (!UnsafeFPMath &&
8940 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8941 break;
8942 std::swap(LHS, RHS);
8943 }
Dan Gohman670e5392009-09-21 18:03:22 +00008944 Opcode = X86ISD::FMIN;
8945 break;
8946 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008947 // Converting this to a min would handle comparisons between positive
8948 // and negative zero incorrectly.
8949 if (!UnsafeFPMath &&
8950 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8951 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008952 Opcode = X86ISD::FMIN;
8953 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008954 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008955 // Converting this to a min would handle both negative zeros and NaNs
8956 // incorrectly, but we can swap the operands to fix both.
8957 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008958 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008959 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008960 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008961 Opcode = X86ISD::FMIN;
8962 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008963
Dan Gohman670e5392009-09-21 18:03:22 +00008964 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008965 // Converting this to a max would handle comparisons between positive
8966 // and negative zero incorrectly.
8967 if (!UnsafeFPMath &&
8968 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8969 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008970 Opcode = X86ISD::FMAX;
8971 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008972 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008973 // Converting this to a max would handle NaNs incorrectly, and swapping
8974 // the operands would cause it to handle comparisons between positive
8975 // and negative zero incorrectly.
8976 if (!FiniteOnlyFPMath() &&
8977 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8978 if (!UnsafeFPMath &&
8979 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8980 break;
8981 std::swap(LHS, RHS);
8982 }
Dan Gohman670e5392009-09-21 18:03:22 +00008983 Opcode = X86ISD::FMAX;
8984 break;
8985 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008986 // Converting this to a max would handle both negative zeros and NaNs
8987 // incorrectly, but we can swap the operands to fix both.
8988 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008989 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008990 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008991 case ISD::SETGE:
8992 Opcode = X86ISD::FMAX;
8993 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008994 }
Dan Gohman670e5392009-09-21 18:03:22 +00008995 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008996 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8997 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008998 switch (CC) {
8999 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009000 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009001 // Converting this to a min would handle comparisons between positive
9002 // and negative zero incorrectly, and swapping the operands would
9003 // cause it to handle NaNs incorrectly.
9004 if (!UnsafeFPMath &&
9005 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9006 if (!FiniteOnlyFPMath() &&
9007 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9008 break;
9009 std::swap(LHS, RHS);
9010 }
Dan Gohman670e5392009-09-21 18:03:22 +00009011 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009012 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009013 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009014 // Converting this to a min would handle NaNs incorrectly.
9015 if (!UnsafeFPMath &&
9016 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9017 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009018 Opcode = X86ISD::FMIN;
9019 break;
9020 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009021 // Converting this to a min would handle both negative zeros and NaNs
9022 // incorrectly, but we can swap the operands to fix both.
9023 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009024 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009025 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009026 case ISD::SETGE:
9027 Opcode = X86ISD::FMIN;
9028 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009029
Dan Gohman670e5392009-09-21 18:03:22 +00009030 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009031 // Converting this to a max would handle NaNs incorrectly.
9032 if (!FiniteOnlyFPMath() &&
9033 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9034 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009035 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009036 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009037 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009038 // Converting this to a max would handle comparisons between positive
9039 // and negative zero incorrectly, and swapping the operands would
9040 // cause it to handle NaNs incorrectly.
9041 if (!UnsafeFPMath &&
9042 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9043 if (!FiniteOnlyFPMath() &&
9044 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9045 break;
9046 std::swap(LHS, RHS);
9047 }
Dan Gohman670e5392009-09-21 18:03:22 +00009048 Opcode = X86ISD::FMAX;
9049 break;
9050 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009051 // Converting this to a max would handle both negative zeros and NaNs
9052 // incorrectly, but we can swap the operands to fix both.
9053 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009054 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009055 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009056 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009057 Opcode = X86ISD::FMAX;
9058 break;
9059 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009060 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009061
Chris Lattner47b4ce82009-03-11 05:48:52 +00009062 if (Opcode)
9063 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009064 }
Eric Christopherfd179292009-08-27 18:07:15 +00009065
Chris Lattnerd1980a52009-03-12 06:52:53 +00009066 // If this is a select between two integer constants, try to do some
9067 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009068 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9069 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009070 // Don't do this for crazy integer types.
9071 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9072 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009073 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009074 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009075
Chris Lattnercee56e72009-03-13 05:53:31 +00009076 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009077 // Efficiently invertible.
9078 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9079 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9080 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9081 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009082 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009083 }
Eric Christopherfd179292009-08-27 18:07:15 +00009084
Chris Lattnerd1980a52009-03-12 06:52:53 +00009085 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009086 if (FalseC->getAPIntValue() == 0 &&
9087 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009088 if (NeedsCondInvert) // Invert the condition if needed.
9089 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9090 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009091
Chris Lattnerd1980a52009-03-12 06:52:53 +00009092 // Zero extend the condition if needed.
9093 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009094
Chris Lattnercee56e72009-03-13 05:53:31 +00009095 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009096 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009097 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009098 }
Eric Christopherfd179292009-08-27 18:07:15 +00009099
Chris Lattner97a29a52009-03-13 05:22:11 +00009100 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009101 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009102 if (NeedsCondInvert) // Invert the condition if needed.
9103 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9104 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009105
Chris Lattner97a29a52009-03-13 05:22:11 +00009106 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009107 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9108 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009109 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009110 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009111 }
Eric Christopherfd179292009-08-27 18:07:15 +00009112
Chris Lattnercee56e72009-03-13 05:53:31 +00009113 // Optimize cases that will turn into an LEA instruction. This requires
9114 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009115 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009116 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009117 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009118
Chris Lattnercee56e72009-03-13 05:53:31 +00009119 bool isFastMultiplier = false;
9120 if (Diff < 10) {
9121 switch ((unsigned char)Diff) {
9122 default: break;
9123 case 1: // result = add base, cond
9124 case 2: // result = lea base( , cond*2)
9125 case 3: // result = lea base(cond, cond*2)
9126 case 4: // result = lea base( , cond*4)
9127 case 5: // result = lea base(cond, cond*4)
9128 case 8: // result = lea base( , cond*8)
9129 case 9: // result = lea base(cond, cond*8)
9130 isFastMultiplier = true;
9131 break;
9132 }
9133 }
Eric Christopherfd179292009-08-27 18:07:15 +00009134
Chris Lattnercee56e72009-03-13 05:53:31 +00009135 if (isFastMultiplier) {
9136 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9137 if (NeedsCondInvert) // Invert the condition if needed.
9138 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9139 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009140
Chris Lattnercee56e72009-03-13 05:53:31 +00009141 // Zero extend the condition if needed.
9142 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9143 Cond);
9144 // Scale the condition by the difference.
9145 if (Diff != 1)
9146 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9147 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009148
Chris Lattnercee56e72009-03-13 05:53:31 +00009149 // Add the base if non-zero.
9150 if (FalseC->getAPIntValue() != 0)
9151 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9152 SDValue(FalseC, 0));
9153 return Cond;
9154 }
Eric Christopherfd179292009-08-27 18:07:15 +00009155 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009156 }
9157 }
Eric Christopherfd179292009-08-27 18:07:15 +00009158
Dan Gohman475871a2008-07-27 21:46:04 +00009159 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009160}
9161
Chris Lattnerd1980a52009-03-12 06:52:53 +00009162/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9163static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9164 TargetLowering::DAGCombinerInfo &DCI) {
9165 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009166
Chris Lattnerd1980a52009-03-12 06:52:53 +00009167 // If the flag operand isn't dead, don't touch this CMOV.
9168 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9169 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009170
Chris Lattnerd1980a52009-03-12 06:52:53 +00009171 // If this is a select between two integer constants, try to do some
9172 // optimizations. Note that the operands are ordered the opposite of SELECT
9173 // operands.
9174 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9175 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9176 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9177 // larger than FalseC (the false value).
9178 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009179
Chris Lattnerd1980a52009-03-12 06:52:53 +00009180 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9181 CC = X86::GetOppositeBranchCondition(CC);
9182 std::swap(TrueC, FalseC);
9183 }
Eric Christopherfd179292009-08-27 18:07:15 +00009184
Chris Lattnerd1980a52009-03-12 06:52:53 +00009185 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009186 // This is efficient for any integer data type (including i8/i16) and
9187 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009188 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9189 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9191 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009192
Chris Lattnerd1980a52009-03-12 06:52:53 +00009193 // Zero extend the condition if needed.
9194 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009195
Chris Lattnerd1980a52009-03-12 06:52:53 +00009196 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9197 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009198 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009199 if (N->getNumValues() == 2) // Dead flag value?
9200 return DCI.CombineTo(N, Cond, SDValue());
9201 return Cond;
9202 }
Eric Christopherfd179292009-08-27 18:07:15 +00009203
Chris Lattnercee56e72009-03-13 05:53:31 +00009204 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9205 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009206 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9207 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009208 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9209 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009210
Chris Lattner97a29a52009-03-13 05:22:11 +00009211 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009212 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9213 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009214 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9215 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009216
Chris Lattner97a29a52009-03-13 05:22:11 +00009217 if (N->getNumValues() == 2) // Dead flag value?
9218 return DCI.CombineTo(N, Cond, SDValue());
9219 return Cond;
9220 }
Eric Christopherfd179292009-08-27 18:07:15 +00009221
Chris Lattnercee56e72009-03-13 05:53:31 +00009222 // Optimize cases that will turn into an LEA instruction. This requires
9223 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009224 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009225 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009226 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009227
Chris Lattnercee56e72009-03-13 05:53:31 +00009228 bool isFastMultiplier = false;
9229 if (Diff < 10) {
9230 switch ((unsigned char)Diff) {
9231 default: break;
9232 case 1: // result = add base, cond
9233 case 2: // result = lea base( , cond*2)
9234 case 3: // result = lea base(cond, cond*2)
9235 case 4: // result = lea base( , cond*4)
9236 case 5: // result = lea base(cond, cond*4)
9237 case 8: // result = lea base( , cond*8)
9238 case 9: // result = lea base(cond, cond*8)
9239 isFastMultiplier = true;
9240 break;
9241 }
9242 }
Eric Christopherfd179292009-08-27 18:07:15 +00009243
Chris Lattnercee56e72009-03-13 05:53:31 +00009244 if (isFastMultiplier) {
9245 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9246 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009247 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9248 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009249 // Zero extend the condition if needed.
9250 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9251 Cond);
9252 // Scale the condition by the difference.
9253 if (Diff != 1)
9254 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9255 DAG.getConstant(Diff, Cond.getValueType()));
9256
9257 // Add the base if non-zero.
9258 if (FalseC->getAPIntValue() != 0)
9259 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9260 SDValue(FalseC, 0));
9261 if (N->getNumValues() == 2) // Dead flag value?
9262 return DCI.CombineTo(N, Cond, SDValue());
9263 return Cond;
9264 }
Eric Christopherfd179292009-08-27 18:07:15 +00009265 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009266 }
9267 }
9268 return SDValue();
9269}
9270
9271
Evan Cheng0b0cd912009-03-28 05:57:29 +00009272/// PerformMulCombine - Optimize a single multiply with constant into two
9273/// in order to implement it with two cheaper instructions, e.g.
9274/// LEA + SHL, LEA + LEA.
9275static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9276 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009277 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9278 return SDValue();
9279
Owen Andersone50ed302009-08-10 22:56:29 +00009280 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009281 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009282 return SDValue();
9283
9284 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9285 if (!C)
9286 return SDValue();
9287 uint64_t MulAmt = C->getZExtValue();
9288 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9289 return SDValue();
9290
9291 uint64_t MulAmt1 = 0;
9292 uint64_t MulAmt2 = 0;
9293 if ((MulAmt % 9) == 0) {
9294 MulAmt1 = 9;
9295 MulAmt2 = MulAmt / 9;
9296 } else if ((MulAmt % 5) == 0) {
9297 MulAmt1 = 5;
9298 MulAmt2 = MulAmt / 5;
9299 } else if ((MulAmt % 3) == 0) {
9300 MulAmt1 = 3;
9301 MulAmt2 = MulAmt / 3;
9302 }
9303 if (MulAmt2 &&
9304 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9305 DebugLoc DL = N->getDebugLoc();
9306
9307 if (isPowerOf2_64(MulAmt2) &&
9308 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9309 // If second multiplifer is pow2, issue it first. We want the multiply by
9310 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9311 // is an add.
9312 std::swap(MulAmt1, MulAmt2);
9313
9314 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009315 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009316 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009317 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009318 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009319 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009320 DAG.getConstant(MulAmt1, VT));
9321
Eric Christopherfd179292009-08-27 18:07:15 +00009322 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009323 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009324 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009325 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009326 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009327 DAG.getConstant(MulAmt2, VT));
9328
9329 // Do not add new nodes to DAG combiner worklist.
9330 DCI.CombineTo(N, NewMul, false);
9331 }
9332 return SDValue();
9333}
9334
Evan Chengad9c0a32009-12-15 00:53:42 +00009335static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9336 SDValue N0 = N->getOperand(0);
9337 SDValue N1 = N->getOperand(1);
9338 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9339 EVT VT = N0.getValueType();
9340
9341 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9342 // since the result of setcc_c is all zero's or all ones.
9343 if (N1C && N0.getOpcode() == ISD::AND &&
9344 N0.getOperand(1).getOpcode() == ISD::Constant) {
9345 SDValue N00 = N0.getOperand(0);
9346 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9347 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9348 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9349 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9350 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9351 APInt ShAmt = N1C->getAPIntValue();
9352 Mask = Mask.shl(ShAmt);
9353 if (Mask != 0)
9354 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9355 N00, DAG.getConstant(Mask, VT));
9356 }
9357 }
9358
9359 return SDValue();
9360}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009361
Nate Begeman740ab032009-01-26 00:52:55 +00009362/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9363/// when possible.
9364static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9365 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009366 EVT VT = N->getValueType(0);
9367 if (!VT.isVector() && VT.isInteger() &&
9368 N->getOpcode() == ISD::SHL)
9369 return PerformSHLCombine(N, DAG);
9370
Nate Begeman740ab032009-01-26 00:52:55 +00009371 // On X86 with SSE2 support, we can transform this to a vector shift if
9372 // all elements are shifted by the same amount. We can't do this in legalize
9373 // because the a constant vector is typically transformed to a constant pool
9374 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009375 if (!Subtarget->hasSSE2())
9376 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009377
Owen Anderson825b72b2009-08-11 20:47:22 +00009378 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009379 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009380
Mon P Wang3becd092009-01-28 08:12:05 +00009381 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009382 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009383 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009384 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009385 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9386 unsigned NumElts = VT.getVectorNumElements();
9387 unsigned i = 0;
9388 for (; i != NumElts; ++i) {
9389 SDValue Arg = ShAmtOp.getOperand(i);
9390 if (Arg.getOpcode() == ISD::UNDEF) continue;
9391 BaseShAmt = Arg;
9392 break;
9393 }
9394 for (; i != NumElts; ++i) {
9395 SDValue Arg = ShAmtOp.getOperand(i);
9396 if (Arg.getOpcode() == ISD::UNDEF) continue;
9397 if (Arg != BaseShAmt) {
9398 return SDValue();
9399 }
9400 }
9401 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009402 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009403 SDValue InVec = ShAmtOp.getOperand(0);
9404 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9405 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9406 unsigned i = 0;
9407 for (; i != NumElts; ++i) {
9408 SDValue Arg = InVec.getOperand(i);
9409 if (Arg.getOpcode() == ISD::UNDEF) continue;
9410 BaseShAmt = Arg;
9411 break;
9412 }
9413 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9414 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009415 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009416 if (C->getZExtValue() == SplatIdx)
9417 BaseShAmt = InVec.getOperand(1);
9418 }
9419 }
9420 if (BaseShAmt.getNode() == 0)
9421 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9422 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009423 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009424 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009425
Mon P Wangefa42202009-09-03 19:56:25 +00009426 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009427 if (EltVT.bitsGT(MVT::i32))
9428 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9429 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009430 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009431
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009432 // The shift amount is identical so we can do a vector shift.
9433 SDValue ValOp = N->getOperand(0);
9434 switch (N->getOpcode()) {
9435 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009436 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009437 break;
9438 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009439 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009440 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009441 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009442 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009443 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009444 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009446 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009447 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009448 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009450 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009451 break;
9452 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009453 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009454 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009455 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009456 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009457 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009458 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009459 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009460 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009461 break;
9462 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009463 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009464 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009465 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009466 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009467 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009468 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009469 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009470 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009471 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009472 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009473 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009474 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009475 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009476 }
9477 return SDValue();
9478}
9479
Evan Cheng760d1942010-01-04 21:22:48 +00009480static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9481 const X86Subtarget *Subtarget) {
9482 EVT VT = N->getValueType(0);
9483 if (VT != MVT::i64 || !Subtarget->is64Bit())
9484 return SDValue();
9485
9486 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9487 SDValue N0 = N->getOperand(0);
9488 SDValue N1 = N->getOperand(1);
9489 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9490 std::swap(N0, N1);
9491 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9492 return SDValue();
9493
9494 SDValue ShAmt0 = N0.getOperand(1);
9495 if (ShAmt0.getValueType() != MVT::i8)
9496 return SDValue();
9497 SDValue ShAmt1 = N1.getOperand(1);
9498 if (ShAmt1.getValueType() != MVT::i8)
9499 return SDValue();
9500 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9501 ShAmt0 = ShAmt0.getOperand(0);
9502 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9503 ShAmt1 = ShAmt1.getOperand(0);
9504
9505 DebugLoc DL = N->getDebugLoc();
9506 unsigned Opc = X86ISD::SHLD;
9507 SDValue Op0 = N0.getOperand(0);
9508 SDValue Op1 = N1.getOperand(0);
9509 if (ShAmt0.getOpcode() == ISD::SUB) {
9510 Opc = X86ISD::SHRD;
9511 std::swap(Op0, Op1);
9512 std::swap(ShAmt0, ShAmt1);
9513 }
9514
9515 if (ShAmt1.getOpcode() == ISD::SUB) {
9516 SDValue Sum = ShAmt1.getOperand(0);
9517 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9518 if (SumC->getSExtValue() == 64 &&
9519 ShAmt1.getOperand(1) == ShAmt0)
9520 return DAG.getNode(Opc, DL, VT,
9521 Op0, Op1,
9522 DAG.getNode(ISD::TRUNCATE, DL,
9523 MVT::i8, ShAmt0));
9524 }
9525 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9526 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9527 if (ShAmt0C &&
9528 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9529 return DAG.getNode(Opc, DL, VT,
9530 N0.getOperand(0), N1.getOperand(0),
9531 DAG.getNode(ISD::TRUNCATE, DL,
9532 MVT::i8, ShAmt0));
9533 }
9534
9535 return SDValue();
9536}
9537
Chris Lattner149a4e52008-02-22 02:09:43 +00009538/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009539static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009540 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009541 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9542 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009543 // A preferable solution to the general problem is to figure out the right
9544 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009545
9546 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009547 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009548 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009549 if (VT.getSizeInBits() != 64)
9550 return SDValue();
9551
Devang Patel578efa92009-06-05 21:57:13 +00009552 const Function *F = DAG.getMachineFunction().getFunction();
9553 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009554 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009555 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009556 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009557 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009558 isa<LoadSDNode>(St->getValue()) &&
9559 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9560 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009561 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009562 LoadSDNode *Ld = 0;
9563 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009564 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009565 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009566 // Must be a store of a load. We currently handle two cases: the load
9567 // is a direct child, and it's under an intervening TokenFactor. It is
9568 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009569 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009570 Ld = cast<LoadSDNode>(St->getChain());
9571 else if (St->getValue().hasOneUse() &&
9572 ChainVal->getOpcode() == ISD::TokenFactor) {
9573 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009574 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009575 TokenFactorIndex = i;
9576 Ld = cast<LoadSDNode>(St->getValue());
9577 } else
9578 Ops.push_back(ChainVal->getOperand(i));
9579 }
9580 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009581
Evan Cheng536e6672009-03-12 05:59:15 +00009582 if (!Ld || !ISD::isNormalLoad(Ld))
9583 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009584
Evan Cheng536e6672009-03-12 05:59:15 +00009585 // If this is not the MMX case, i.e. we are just turning i64 load/store
9586 // into f64 load/store, avoid the transformation if there are multiple
9587 // uses of the loaded value.
9588 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9589 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009590
Evan Cheng536e6672009-03-12 05:59:15 +00009591 DebugLoc LdDL = Ld->getDebugLoc();
9592 DebugLoc StDL = N->getDebugLoc();
9593 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9594 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9595 // pair instead.
9596 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009598 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9599 Ld->getBasePtr(), Ld->getSrcValue(),
9600 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009601 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009602 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009603 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009604 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009605 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009606 Ops.size());
9607 }
Evan Cheng536e6672009-03-12 05:59:15 +00009608 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009609 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009610 St->isVolatile(), St->isNonTemporal(),
9611 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009612 }
Evan Cheng536e6672009-03-12 05:59:15 +00009613
9614 // Otherwise, lower to two pairs of 32-bit loads / stores.
9615 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009616 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9617 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009618
Owen Anderson825b72b2009-08-11 20:47:22 +00009619 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009620 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009621 Ld->isVolatile(), Ld->isNonTemporal(),
9622 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009623 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009624 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009625 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009626 MinAlign(Ld->getAlignment(), 4));
9627
9628 SDValue NewChain = LoLd.getValue(1);
9629 if (TokenFactorIndex != -1) {
9630 Ops.push_back(LoLd);
9631 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009632 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009633 Ops.size());
9634 }
9635
9636 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009637 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9638 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009639
9640 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9641 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009642 St->isVolatile(), St->isNonTemporal(),
9643 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009644 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9645 St->getSrcValue(),
9646 St->getSrcValueOffset() + 4,
9647 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009648 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009649 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009650 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009651 }
Dan Gohman475871a2008-07-27 21:46:04 +00009652 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009653}
9654
Chris Lattner6cf73262008-01-25 06:14:17 +00009655/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9656/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009657static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009658 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9659 // F[X]OR(0.0, x) -> x
9660 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009661 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9662 if (C->getValueAPF().isPosZero())
9663 return N->getOperand(1);
9664 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9665 if (C->getValueAPF().isPosZero())
9666 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009667 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009668}
9669
9670/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009671static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009672 // FAND(0.0, x) -> 0.0
9673 // FAND(x, 0.0) -> 0.0
9674 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9675 if (C->getValueAPF().isPosZero())
9676 return N->getOperand(0);
9677 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9678 if (C->getValueAPF().isPosZero())
9679 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009680 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009681}
9682
Dan Gohmane5af2d32009-01-29 01:59:02 +00009683static SDValue PerformBTCombine(SDNode *N,
9684 SelectionDAG &DAG,
9685 TargetLowering::DAGCombinerInfo &DCI) {
9686 // BT ignores high bits in the bit index operand.
9687 SDValue Op1 = N->getOperand(1);
9688 if (Op1.hasOneUse()) {
9689 unsigned BitWidth = Op1.getValueSizeInBits();
9690 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9691 APInt KnownZero, KnownOne;
9692 TargetLowering::TargetLoweringOpt TLO(DAG);
9693 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9694 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9695 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9696 DCI.CommitTargetLoweringOpt(TLO);
9697 }
9698 return SDValue();
9699}
Chris Lattner83e6c992006-10-04 06:57:07 +00009700
Eli Friedman7a5e5552009-06-07 06:52:44 +00009701static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9702 SDValue Op = N->getOperand(0);
9703 if (Op.getOpcode() == ISD::BIT_CONVERT)
9704 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009705 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009706 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009707 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009708 OpVT.getVectorElementType().getSizeInBits()) {
9709 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9710 }
9711 return SDValue();
9712}
9713
Owen Anderson99177002009-06-29 18:04:45 +00009714// On X86 and X86-64, atomic operations are lowered to locked instructions.
9715// Locked instructions, in turn, have implicit fence semantics (all memory
9716// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009717// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009718// fence-atomic-fence.
9719static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9720 SDValue atomic = N->getOperand(0);
9721 switch (atomic.getOpcode()) {
9722 case ISD::ATOMIC_CMP_SWAP:
9723 case ISD::ATOMIC_SWAP:
9724 case ISD::ATOMIC_LOAD_ADD:
9725 case ISD::ATOMIC_LOAD_SUB:
9726 case ISD::ATOMIC_LOAD_AND:
9727 case ISD::ATOMIC_LOAD_OR:
9728 case ISD::ATOMIC_LOAD_XOR:
9729 case ISD::ATOMIC_LOAD_NAND:
9730 case ISD::ATOMIC_LOAD_MIN:
9731 case ISD::ATOMIC_LOAD_MAX:
9732 case ISD::ATOMIC_LOAD_UMIN:
9733 case ISD::ATOMIC_LOAD_UMAX:
9734 break;
9735 default:
9736 return SDValue();
9737 }
Eric Christopherfd179292009-08-27 18:07:15 +00009738
Owen Anderson99177002009-06-29 18:04:45 +00009739 SDValue fence = atomic.getOperand(0);
9740 if (fence.getOpcode() != ISD::MEMBARRIER)
9741 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009742
Owen Anderson99177002009-06-29 18:04:45 +00009743 switch (atomic.getOpcode()) {
9744 case ISD::ATOMIC_CMP_SWAP:
9745 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9746 atomic.getOperand(1), atomic.getOperand(2),
9747 atomic.getOperand(3));
9748 case ISD::ATOMIC_SWAP:
9749 case ISD::ATOMIC_LOAD_ADD:
9750 case ISD::ATOMIC_LOAD_SUB:
9751 case ISD::ATOMIC_LOAD_AND:
9752 case ISD::ATOMIC_LOAD_OR:
9753 case ISD::ATOMIC_LOAD_XOR:
9754 case ISD::ATOMIC_LOAD_NAND:
9755 case ISD::ATOMIC_LOAD_MIN:
9756 case ISD::ATOMIC_LOAD_MAX:
9757 case ISD::ATOMIC_LOAD_UMIN:
9758 case ISD::ATOMIC_LOAD_UMAX:
9759 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9760 atomic.getOperand(1), atomic.getOperand(2));
9761 default:
9762 return SDValue();
9763 }
9764}
9765
Evan Cheng2e489c42009-12-16 00:53:11 +00009766static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9767 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9768 // (and (i32 x86isd::setcc_carry), 1)
9769 // This eliminates the zext. This transformation is necessary because
9770 // ISD::SETCC is always legalized to i8.
9771 DebugLoc dl = N->getDebugLoc();
9772 SDValue N0 = N->getOperand(0);
9773 EVT VT = N->getValueType(0);
9774 if (N0.getOpcode() == ISD::AND &&
9775 N0.hasOneUse() &&
9776 N0.getOperand(0).hasOneUse()) {
9777 SDValue N00 = N0.getOperand(0);
9778 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9779 return SDValue();
9780 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9781 if (!C || C->getZExtValue() != 1)
9782 return SDValue();
9783 return DAG.getNode(ISD::AND, dl, VT,
9784 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9785 N00.getOperand(0), N00.getOperand(1)),
9786 DAG.getConstant(1, VT));
9787 }
9788
9789 return SDValue();
9790}
9791
Dan Gohman475871a2008-07-27 21:46:04 +00009792SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009793 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009794 SelectionDAG &DAG = DCI.DAG;
9795 switch (N->getOpcode()) {
9796 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009797 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009798 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009799 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009800 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009801 case ISD::SHL:
9802 case ISD::SRA:
9803 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009804 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009805 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009806 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009807 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9808 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009809 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009810 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009811 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009812 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009813 }
9814
Dan Gohman475871a2008-07-27 21:46:04 +00009815 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009816}
9817
Evan Cheng60c07e12006-07-05 22:17:51 +00009818//===----------------------------------------------------------------------===//
9819// X86 Inline Assembly Support
9820//===----------------------------------------------------------------------===//
9821
Chris Lattnerb8105652009-07-20 17:51:36 +00009822static bool LowerToBSwap(CallInst *CI) {
9823 // FIXME: this should verify that we are targetting a 486 or better. If not,
9824 // we will turn this bswap into something that will be lowered to logical ops
9825 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9826 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009827
Chris Lattnerb8105652009-07-20 17:51:36 +00009828 // Verify this is a simple bswap.
9829 if (CI->getNumOperands() != 2 ||
9830 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009831 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009833
Chris Lattnerb8105652009-07-20 17:51:36 +00009834 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9835 if (!Ty || Ty->getBitWidth() % 16 != 0)
9836 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009837
Chris Lattnerb8105652009-07-20 17:51:36 +00009838 // Okay, we can do this xform, do so now.
9839 const Type *Tys[] = { Ty };
9840 Module *M = CI->getParent()->getParent()->getParent();
9841 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009842
Chris Lattnerb8105652009-07-20 17:51:36 +00009843 Value *Op = CI->getOperand(1);
9844 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009845
Chris Lattnerb8105652009-07-20 17:51:36 +00009846 CI->replaceAllUsesWith(Op);
9847 CI->eraseFromParent();
9848 return true;
9849}
9850
9851bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9852 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9853 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9854
9855 std::string AsmStr = IA->getAsmString();
9856
9857 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009858 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009859 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9860
9861 switch (AsmPieces.size()) {
9862 default: return false;
9863 case 1:
9864 AsmStr = AsmPieces[0];
9865 AsmPieces.clear();
9866 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9867
9868 // bswap $0
9869 if (AsmPieces.size() == 2 &&
9870 (AsmPieces[0] == "bswap" ||
9871 AsmPieces[0] == "bswapq" ||
9872 AsmPieces[0] == "bswapl") &&
9873 (AsmPieces[1] == "$0" ||
9874 AsmPieces[1] == "${0:q}")) {
9875 // No need to check constraints, nothing other than the equivalent of
9876 // "=r,0" would be valid here.
9877 return LowerToBSwap(CI);
9878 }
9879 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009880 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009881 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009882 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009883 AsmPieces[1] == "$$8," &&
9884 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009885 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9886 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009887 const std::string &Constraints = IA->getConstraintString();
9888 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009889 std::sort(AsmPieces.begin(), AsmPieces.end());
9890 if (AsmPieces.size() == 4 &&
9891 AsmPieces[0] == "~{cc}" &&
9892 AsmPieces[1] == "~{dirflag}" &&
9893 AsmPieces[2] == "~{flags}" &&
9894 AsmPieces[3] == "~{fpsr}") {
9895 return LowerToBSwap(CI);
9896 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009897 }
9898 break;
9899 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009900 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009901 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009902 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9903 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9904 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009905 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009906 SplitString(AsmPieces[0], Words, " \t");
9907 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9908 Words.clear();
9909 SplitString(AsmPieces[1], Words, " \t");
9910 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9911 Words.clear();
9912 SplitString(AsmPieces[2], Words, " \t,");
9913 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9914 Words[2] == "%edx") {
9915 return LowerToBSwap(CI);
9916 }
9917 }
9918 }
9919 }
9920 break;
9921 }
9922 return false;
9923}
9924
9925
9926
Chris Lattnerf4dff842006-07-11 02:54:03 +00009927/// getConstraintType - Given a constraint letter, return the type of
9928/// constraint it is for this target.
9929X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009930X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9931 if (Constraint.size() == 1) {
9932 switch (Constraint[0]) {
9933 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009934 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009935 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009936 case 'r':
9937 case 'R':
9938 case 'l':
9939 case 'q':
9940 case 'Q':
9941 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009942 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009943 case 'Y':
9944 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009945 case 'e':
9946 case 'Z':
9947 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009948 default:
9949 break;
9950 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009951 }
Chris Lattner4234f572007-03-25 02:14:49 +00009952 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009953}
9954
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009955/// LowerXConstraint - try to replace an X constraint, which matches anything,
9956/// with another that has more specific requirements based on the type of the
9957/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009958const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009959LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009960 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9961 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009962 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009963 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009964 return "Y";
9965 if (Subtarget->hasSSE1())
9966 return "x";
9967 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009968
Chris Lattner5e764232008-04-26 23:02:14 +00009969 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009970}
9971
Chris Lattner48884cd2007-08-25 00:47:38 +00009972/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9973/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009974void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009975 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009976 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009977 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009978 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009979 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009980
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009981 switch (Constraint) {
9982 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009983 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009984 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009985 if (C->getZExtValue() <= 31) {
9986 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009987 break;
9988 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009989 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009990 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009991 case 'J':
9992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009993 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009994 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9995 break;
9996 }
9997 }
9998 return;
9999 case 'K':
10000 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010001 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010002 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10003 break;
10004 }
10005 }
10006 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010007 case 'N':
10008 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010009 if (C->getZExtValue() <= 255) {
10010 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010011 break;
10012 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010013 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010014 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010015 case 'e': {
10016 // 32-bit signed value
10017 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10018 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010019 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10020 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010021 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010022 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010023 break;
10024 }
10025 // FIXME gcc accepts some relocatable values here too, but only in certain
10026 // memory models; it's complicated.
10027 }
10028 return;
10029 }
10030 case 'Z': {
10031 // 32-bit unsigned value
10032 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10033 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010034 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10035 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010036 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10037 break;
10038 }
10039 }
10040 // FIXME gcc accepts some relocatable values here too, but only in certain
10041 // memory models; it's complicated.
10042 return;
10043 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010044 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010045 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010046 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010047 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010048 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010049 break;
10050 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010051
Chris Lattnerdc43a882007-05-03 16:52:29 +000010052 // If we are in non-pic codegen mode, we allow the address of a global (with
10053 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010054 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010055 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010056
Chris Lattner49921962009-05-08 18:23:14 +000010057 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10058 while (1) {
10059 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10060 Offset += GA->getOffset();
10061 break;
10062 } else if (Op.getOpcode() == ISD::ADD) {
10063 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10064 Offset += C->getZExtValue();
10065 Op = Op.getOperand(0);
10066 continue;
10067 }
10068 } else if (Op.getOpcode() == ISD::SUB) {
10069 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10070 Offset += -C->getZExtValue();
10071 Op = Op.getOperand(0);
10072 continue;
10073 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010074 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010075
Chris Lattner49921962009-05-08 18:23:14 +000010076 // Otherwise, this isn't something we can handle, reject it.
10077 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010078 }
Eric Christopherfd179292009-08-27 18:07:15 +000010079
Chris Lattner36c25012009-07-10 07:34:39 +000010080 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010081 // If we require an extra load to get this address, as in PIC mode, we
10082 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010083 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10084 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010085 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010086
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010087 if (hasMemory)
10088 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10089 else
10090 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010091 Result = Op;
10092 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010093 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010094 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010095
Gabor Greifba36cb52008-08-28 21:40:38 +000010096 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010097 Ops.push_back(Result);
10098 return;
10099 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010100 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10101 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010102}
10103
Chris Lattner259e97c2006-01-31 19:43:35 +000010104std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010105getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010106 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010107 if (Constraint.size() == 1) {
10108 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010109 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010110 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010111 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10112 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010113 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010114 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10115 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10116 X86::R10D,X86::R11D,X86::R12D,
10117 X86::R13D,X86::R14D,X86::R15D,
10118 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010119 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010120 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10121 X86::SI, X86::DI, X86::R8W,X86::R9W,
10122 X86::R10W,X86::R11W,X86::R12W,
10123 X86::R13W,X86::R14W,X86::R15W,
10124 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010125 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010126 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10127 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10128 X86::R10B,X86::R11B,X86::R12B,
10129 X86::R13B,X86::R14B,X86::R15B,
10130 X86::BPL, X86::SPL, 0);
10131
Owen Anderson825b72b2009-08-11 20:47:22 +000010132 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010133 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10134 X86::RSI, X86::RDI, X86::R8, X86::R9,
10135 X86::R10, X86::R11, X86::R12,
10136 X86::R13, X86::R14, X86::R15,
10137 X86::RBP, X86::RSP, 0);
10138
10139 break;
10140 }
Eric Christopherfd179292009-08-27 18:07:15 +000010141 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010142 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010143 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010144 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010145 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010146 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010147 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010148 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010149 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010150 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10151 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010152 }
10153 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010154
Chris Lattner1efa40f2006-02-22 00:56:39 +000010155 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010156}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010157
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010158std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010159X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010160 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010161 // First, see if this is a constraint that directly corresponds to an LLVM
10162 // register class.
10163 if (Constraint.size() == 1) {
10164 // GCC Constraint Letters
10165 switch (Constraint[0]) {
10166 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010167 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010168 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010169 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010170 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010171 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010172 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010173 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010174 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010175 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010176 case 'R': // LEGACY_REGS
10177 if (VT == MVT::i8)
10178 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10179 if (VT == MVT::i16)
10180 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10181 if (VT == MVT::i32 || !Subtarget->is64Bit())
10182 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10183 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010184 case 'f': // FP Stack registers.
10185 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10186 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010187 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010188 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010189 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010190 return std::make_pair(0U, X86::RFP64RegisterClass);
10191 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010192 case 'y': // MMX_REGS if MMX allowed.
10193 if (!Subtarget->hasMMX()) break;
10194 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010195 case 'Y': // SSE_REGS if SSE2 allowed
10196 if (!Subtarget->hasSSE2()) break;
10197 // FALL THROUGH.
10198 case 'x': // SSE_REGS if SSE1 allowed
10199 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010200
Owen Anderson825b72b2009-08-11 20:47:22 +000010201 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010202 default: break;
10203 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010204 case MVT::f32:
10205 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010206 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010207 case MVT::f64:
10208 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010209 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010210 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010211 case MVT::v16i8:
10212 case MVT::v8i16:
10213 case MVT::v4i32:
10214 case MVT::v2i64:
10215 case MVT::v4f32:
10216 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010217 return std::make_pair(0U, X86::VR128RegisterClass);
10218 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010219 break;
10220 }
10221 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010222
Chris Lattnerf76d1802006-07-31 23:26:50 +000010223 // Use the default implementation in TargetLowering to convert the register
10224 // constraint into a member of a register class.
10225 std::pair<unsigned, const TargetRegisterClass*> Res;
10226 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010227
10228 // Not found as a standard register?
10229 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010230 // Map st(0) -> st(7) -> ST0
10231 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10232 tolower(Constraint[1]) == 's' &&
10233 tolower(Constraint[2]) == 't' &&
10234 Constraint[3] == '(' &&
10235 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10236 Constraint[5] == ')' &&
10237 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010238
Chris Lattner56d77c72009-09-13 22:41:48 +000010239 Res.first = X86::ST0+Constraint[4]-'0';
10240 Res.second = X86::RFP80RegisterClass;
10241 return Res;
10242 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010243
Chris Lattner56d77c72009-09-13 22:41:48 +000010244 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010245 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010246 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010247 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010248 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010249 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010250
10251 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010252 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010253 Res.first = X86::EFLAGS;
10254 Res.second = X86::CCRRegisterClass;
10255 return Res;
10256 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010257
Dale Johannesen330169f2008-11-13 21:52:36 +000010258 // 'A' means EAX + EDX.
10259 if (Constraint == "A") {
10260 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010261 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010262 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010263 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010264 return Res;
10265 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010266
Chris Lattnerf76d1802006-07-31 23:26:50 +000010267 // Otherwise, check to see if this is a register class of the wrong value
10268 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10269 // turn into {ax},{dx}.
10270 if (Res.second->hasType(VT))
10271 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010272
Chris Lattnerf76d1802006-07-31 23:26:50 +000010273 // All of the single-register GCC register classes map their values onto
10274 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10275 // really want an 8-bit or 32-bit register, map to the appropriate register
10276 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010277 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010279 unsigned DestReg = 0;
10280 switch (Res.first) {
10281 default: break;
10282 case X86::AX: DestReg = X86::AL; break;
10283 case X86::DX: DestReg = X86::DL; break;
10284 case X86::CX: DestReg = X86::CL; break;
10285 case X86::BX: DestReg = X86::BL; break;
10286 }
10287 if (DestReg) {
10288 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010289 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010290 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010291 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010292 unsigned DestReg = 0;
10293 switch (Res.first) {
10294 default: break;
10295 case X86::AX: DestReg = X86::EAX; break;
10296 case X86::DX: DestReg = X86::EDX; break;
10297 case X86::CX: DestReg = X86::ECX; break;
10298 case X86::BX: DestReg = X86::EBX; break;
10299 case X86::SI: DestReg = X86::ESI; break;
10300 case X86::DI: DestReg = X86::EDI; break;
10301 case X86::BP: DestReg = X86::EBP; break;
10302 case X86::SP: DestReg = X86::ESP; break;
10303 }
10304 if (DestReg) {
10305 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010306 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010307 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010308 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010309 unsigned DestReg = 0;
10310 switch (Res.first) {
10311 default: break;
10312 case X86::AX: DestReg = X86::RAX; break;
10313 case X86::DX: DestReg = X86::RDX; break;
10314 case X86::CX: DestReg = X86::RCX; break;
10315 case X86::BX: DestReg = X86::RBX; break;
10316 case X86::SI: DestReg = X86::RSI; break;
10317 case X86::DI: DestReg = X86::RDI; break;
10318 case X86::BP: DestReg = X86::RBP; break;
10319 case X86::SP: DestReg = X86::RSP; break;
10320 }
10321 if (DestReg) {
10322 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010323 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010324 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010325 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010326 } else if (Res.second == X86::FR32RegisterClass ||
10327 Res.second == X86::FR64RegisterClass ||
10328 Res.second == X86::VR128RegisterClass) {
10329 // Handle references to XMM physical registers that got mapped into the
10330 // wrong class. This can happen with constraints like {xmm0} where the
10331 // target independent register mapper will just pick the first match it can
10332 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010333 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010334 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010335 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010336 Res.second = X86::FR64RegisterClass;
10337 else if (X86::VR128RegisterClass->hasType(VT))
10338 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010339 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010340
Chris Lattnerf76d1802006-07-31 23:26:50 +000010341 return Res;
10342}