blob: 06642a9a77c750a8922cc28eb0619626b33ae382 [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 Lattner1b2eb0e2010-03-13 21:04:28 +00001168 return X86MCTargetExpr::Create(MBB->getSymbol(),
Chris Lattner017ec352010-02-08 22:33:55 +00001169 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) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 const Function* Fn = MF.getFunction();
1531 if (Fn->hasExternalLinkage() &&
1532 Subtarget->isTargetCygMing() &&
1533 Fn->getName() == "main")
1534 FuncInfo->setForceFramePointer(true);
1535
Evan Cheng1bc78042006-04-26 01:20:17 +00001536 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001538 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Chris Lattner29689432010-03-11 00:22:57 +00001540 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1541 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Chris Lattner638402b2007-02-28 07:00:42 +00001543 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
1547 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
1553 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1554 // places.
1555 assert(VA.getValNo() != LastVal &&
1556 "Don't support value assigned to multiple locs yet");
1557 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001561 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001570 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001571 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001572 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1573 RC = X86::VR64RegisterClass;
1574 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001575 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001576
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001577 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001578 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001579
Chris Lattnerf39f7712007-02-28 05:46:49 +00001580 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1581 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1582 // right size.
1583 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001584 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001585 DAG.getValueType(VA.getValVT()));
1586 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001587 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001588 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001589 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001590 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001592 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001593 // Handle MMX values passed in XMM regs.
1594 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1596 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001597 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1598 } else
1599 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001600 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001601 } else {
1602 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001603 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001604 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001605
1606 // If value is passed via pointer - do a load.
1607 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001608 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1609 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001610
Dan Gohman98ca4f22009-08-05 01:29:28 +00001611 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001612 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001613
Dan Gohman61a92132008-04-21 23:59:07 +00001614 // The x86-64 ABI for returning structs by value requires that we copy
1615 // the sret argument into %rax for the return. Save the argument into
1616 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001617 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001618 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1619 unsigned Reg = FuncInfo->getSRetReturnReg();
1620 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001622 FuncInfo->setSRetReturnReg(Reg);
1623 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001624 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001625 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001626 }
1627
Chris Lattnerf39f7712007-02-28 05:46:49 +00001628 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001629 // Align stack specially for tail calls.
1630 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001631 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001632
Evan Cheng1bc78042006-04-26 01:20:17 +00001633 // If the function takes variable number of arguments, make a frame index for
1634 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001635 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001636 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001637 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001638 }
1639 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001640 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1641
1642 // FIXME: We should really autogenerate these arrays
1643 static const unsigned GPR64ArgRegsWin64[] = {
1644 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001645 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001646 static const unsigned XMMArgRegsWin64[] = {
1647 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1648 };
1649 static const unsigned GPR64ArgRegs64Bit[] = {
1650 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1651 };
1652 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001653 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1654 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1655 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001656 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1657
1658 if (IsWin64) {
1659 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1660 GPR64ArgRegs = GPR64ArgRegsWin64;
1661 XMMArgRegs = XMMArgRegsWin64;
1662 } else {
1663 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1664 GPR64ArgRegs = GPR64ArgRegs64Bit;
1665 XMMArgRegs = XMMArgRegs64Bit;
1666 }
1667 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1668 TotalNumIntRegs);
1669 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1670 TotalNumXMMRegs);
1671
Devang Patel578efa92009-06-05 21:57:13 +00001672 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001673 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001674 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001675 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001676 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001677 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001678 // Kernel mode asks for SSE to be disabled, so don't push them
1679 // on the stack.
1680 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001681
Gordon Henriksen86737662008-01-05 16:56:59 +00001682 // For X86-64, if there are vararg parameters that are passed via
1683 // registers, then we must store them to their spots on the stack so they
1684 // may be loaded by deferencing the result of va_next.
1685 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001686 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1687 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001688 TotalNumXMMRegs * 16, 16,
1689 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001690
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001692 SmallVector<SDValue, 8> MemOps;
1693 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001694 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001695 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001696 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1697 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001698 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1699 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001700 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001701 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001702 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001703 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene67c9d422010-02-15 16:53:33 +00001704 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001705 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001706 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001707 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001708
Dan Gohmanface41a2009-08-16 21:24:25 +00001709 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1710 // Now store the XMM (fp + vector) parameter registers.
1711 SmallVector<SDValue, 11> SaveXMMOps;
1712 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001713
Dan Gohmanface41a2009-08-16 21:24:25 +00001714 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1715 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1716 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001717
Dan Gohmanface41a2009-08-16 21:24:25 +00001718 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1719 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001720
Dan Gohmanface41a2009-08-16 21:24:25 +00001721 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1722 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1723 X86::VR128RegisterClass);
1724 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1725 SaveXMMOps.push_back(Val);
1726 }
1727 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1728 MVT::Other,
1729 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001730 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001731
1732 if (!MemOps.empty())
1733 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1734 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001737
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001739 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001741 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001742 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001743 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001744 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001745 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001746 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001747
Gordon Henriksen86737662008-01-05 16:56:59 +00001748 if (!Is64Bit) {
1749 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1752 }
Evan Cheng25caf632006-05-23 21:06:34 +00001753
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001754 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001755
Dan Gohman98ca4f22009-08-05 01:29:28 +00001756 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001757}
1758
Dan Gohman475871a2008-07-27 21:46:04 +00001759SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001760X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1761 SDValue StackPtr, SDValue Arg,
1762 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001763 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001764 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001765 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001766 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001767 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001768 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001769 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001770 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001771 }
Dale Johannesenace16102009-02-03 19:33:06 +00001772 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001773 PseudoSourceValue::getStack(), LocMemOffset,
1774 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001775}
1776
Bill Wendling64e87322009-01-16 19:25:27 +00001777/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001778/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001779SDValue
1780X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001781 SDValue &OutRetAddr, SDValue Chain,
1782 bool IsTailCall, bool Is64Bit,
1783 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001784 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001785 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001786 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001787
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001788 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001789 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001790 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001791}
1792
1793/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1794/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001795static SDValue
1796EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001798 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001799 // Store the return address to the appropriate stack slot.
1800 if (!FPDiff) return Chain;
1801 // Calculate the new stack slot for the return address.
1802 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001803 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001804 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001805 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001806 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001807 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001808 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1809 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001810 return Chain;
1811}
1812
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001814X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001815 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001816 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001817 const SmallVectorImpl<ISD::OutputArg> &Outs,
1818 const SmallVectorImpl<ISD::InputArg> &Ins,
1819 DebugLoc dl, SelectionDAG &DAG,
1820 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001821 MachineFunction &MF = DAG.getMachineFunction();
1822 bool Is64Bit = Subtarget->is64Bit();
1823 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001824 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001825
Evan Cheng5f941932010-02-05 02:21:12 +00001826 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001827 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001828 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1829 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001830 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
Chris Lattnerd96d0722007-02-25 06:40:16 +00002136 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002137 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002139
Evan Chengf22f9b32010-02-06 03:28:46 +00002140 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002141 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2142 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002143 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002145
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002146 Ops.push_back(Chain);
2147 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002148
Dan Gohman98ca4f22009-08-05 01:29:28 +00002149 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002151
Gordon Henriksen86737662008-01-05 16:56:59 +00002152 // Add argument registers to the end of the list so that they are known live
2153 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002154 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2155 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2156 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002157
Evan Cheng586ccac2008-03-18 23:36:35 +00002158 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002160 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2161
2162 // Add an implicit use of AL for x86 vararg functions.
2163 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002164 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002165
Gabor Greifba36cb52008-08-28 21:40:38 +00002166 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002167 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002168
Dan Gohman98ca4f22009-08-05 01:29:28 +00002169 if (isTailCall) {
2170 // If this is the first return lowered for this function, add the regs
2171 // to the liveout set for the function.
2172 if (MF.getRegInfo().liveout_empty()) {
2173 SmallVector<CCValAssign, 16> RVLocs;
2174 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2175 *DAG.getContext());
2176 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2177 for (unsigned i = 0; i != RVLocs.size(); ++i)
2178 if (RVLocs[i].isRegLoc())
2179 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2180 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002181 return DAG.getNode(X86ISD::TC_RETURN, dl,
2182 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002183 }
2184
Dale Johannesenace16102009-02-03 19:33:06 +00002185 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002186 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002187
Chris Lattner2d297092006-05-23 18:50:38 +00002188 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002189 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002190 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002191 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002192 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002193 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002194 // pops the hidden struct pointer, so we have to push it back.
2195 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002196 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002197 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002198 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002199
Gordon Henriksenae636f82008-01-03 16:47:34 +00002200 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002201 if (!IsSibcall) {
2202 Chain = DAG.getCALLSEQ_END(Chain,
2203 DAG.getIntPtrConstant(NumBytes, true),
2204 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2205 true),
2206 InFlag);
2207 InFlag = Chain.getValue(1);
2208 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002209
Chris Lattner3085e152007-02-25 08:59:22 +00002210 // Handle result values, copying them out of physregs into vregs that we
2211 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002212 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2213 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002214}
2215
Evan Cheng25ab6902006-09-08 06:48:29 +00002216
2217//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002218// Fast Calling Convention (tail call) implementation
2219//===----------------------------------------------------------------------===//
2220
2221// Like std call, callee cleans arguments, convention except that ECX is
2222// reserved for storing the tail called function address. Only 2 registers are
2223// free for argument passing (inreg). Tail call optimization is performed
2224// provided:
2225// * tailcallopt is enabled
2226// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002227// On X86_64 architecture with GOT-style position independent code only local
2228// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002229// To keep the stack aligned according to platform abi the function
2230// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2231// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002232// If a tail called function callee has more arguments than the caller the
2233// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002234// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002235// original REtADDR, but before the saved framepointer or the spilled registers
2236// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2237// stack layout:
2238// arg1
2239// arg2
2240// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002241// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002242// move area ]
2243// (possible EBP)
2244// ESI
2245// EDI
2246// local1 ..
2247
2248/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2249/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002250unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002251 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002252 MachineFunction &MF = DAG.getMachineFunction();
2253 const TargetMachine &TM = MF.getTarget();
2254 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2255 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002256 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002257 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002258 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002259 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2260 // Number smaller than 12 so just add the difference.
2261 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2262 } else {
2263 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002265 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002266 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002267 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002268}
2269
Evan Cheng5f941932010-02-05 02:21:12 +00002270/// MatchingStackOffset - Return true if the given stack call argument is
2271/// already available in the same position (relatively) of the caller's
2272/// incoming argument stack.
2273static
2274bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2275 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2276 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002277 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2278 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002279 if (Arg.getOpcode() == ISD::CopyFromReg) {
2280 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2281 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2282 return false;
2283 MachineInstr *Def = MRI->getVRegDef(VR);
2284 if (!Def)
2285 return false;
2286 if (!Flags.isByVal()) {
2287 if (!TII->isLoadFromStackSlot(Def, FI))
2288 return false;
2289 } else {
2290 unsigned Opcode = Def->getOpcode();
2291 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2292 Def->getOperand(1).isFI()) {
2293 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002294 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002295 } else
2296 return false;
2297 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002298 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2299 if (Flags.isByVal())
2300 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002301 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002302 // define @foo(%struct.X* %A) {
2303 // tail call @bar(%struct.X* byval %A)
2304 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002305 return false;
2306 SDValue Ptr = Ld->getBasePtr();
2307 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2308 if (!FINode)
2309 return false;
2310 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002311 } else
2312 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002313
Evan Cheng4cae1332010-03-05 08:38:04 +00002314 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002315 if (!MFI->isFixedObjectIndex(FI))
2316 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002317 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002318}
2319
Dan Gohman98ca4f22009-08-05 01:29:28 +00002320/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2321/// for tail call optimization. Targets which want to do tail call
2322/// optimization should implement this function.
2323bool
2324X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002325 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002326 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002327 bool isCalleeStructRet,
2328 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002329 const SmallVectorImpl<ISD::OutputArg> &Outs,
2330 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002331 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002332 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002333 CalleeCC != CallingConv::C)
2334 return false;
2335
Evan Cheng7096ae42010-01-29 06:45:59 +00002336 // If -tailcallopt is specified, make fastcc functions tail-callable.
2337 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002338 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002339 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002340 CallerF->getCallingConv() == CalleeCC)
2341 return true;
2342 return false;
2343 }
2344
Evan Chengb2c92902010-02-02 02:22:50 +00002345 // Look for obvious safe cases to perform tail call optimization that does not
2346 // requite ABI changes. This is what gcc calls sibcall.
2347
Evan Chenga375d472010-03-15 18:54:48 +00002348 // Do not sibcall optimize vararg calls for now.
Evan Cheng843bd692010-01-31 06:44:49 +00002349 if (isVarArg)
2350 return false;
2351
Evan Chenga375d472010-03-15 18:54:48 +00002352 // Also avoid sibcall optimization if either caller or callee uses struct
2353 // return semantics.
2354 if (isCalleeStructRet || isCallerStructRet)
2355 return false;
2356
Evan Chenga6bff982010-01-30 01:22:00 +00002357 // If the callee takes no arguments then go on to check the results of the
2358 // call.
2359 if (!Outs.empty()) {
2360 // Check if stack adjustment is needed. For now, do not do this if any
2361 // argument is passed on the stack.
2362 SmallVector<CCValAssign, 16> ArgLocs;
2363 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2364 ArgLocs, *DAG.getContext());
2365 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002366 if (CCInfo.getNextStackOffset()) {
2367 MachineFunction &MF = DAG.getMachineFunction();
2368 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2369 return false;
2370 if (Subtarget->isTargetWin64())
2371 // Win64 ABI has additional complications.
2372 return false;
2373
2374 // Check if the arguments are already laid out in the right way as
2375 // the caller's fixed stack objects.
2376 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002377 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2378 const X86InstrInfo *TII =
2379 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002380 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2381 CCValAssign &VA = ArgLocs[i];
2382 EVT RegVT = VA.getLocVT();
2383 SDValue Arg = Outs[i].Val;
2384 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002385 if (VA.getLocInfo() == CCValAssign::Indirect)
2386 return false;
2387 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002388 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2389 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002390 return false;
2391 }
2392 }
2393 }
Evan Chenga6bff982010-01-30 01:22:00 +00002394 }
Evan Chengb1712452010-01-27 06:25:16 +00002395
Evan Cheng86809cc2010-02-03 03:28:02 +00002396 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002397}
2398
Dan Gohman3df24e62008-09-03 23:12:08 +00002399FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002400X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2401 DwarfWriter *dw,
2402 DenseMap<const Value *, unsigned> &vm,
2403 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2404 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002405#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002406 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002407#endif
2408 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002409 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002410#ifndef NDEBUG
2411 , cil
2412#endif
2413 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002414}
2415
2416
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002417//===----------------------------------------------------------------------===//
2418// Other Lowering Hooks
2419//===----------------------------------------------------------------------===//
2420
2421
Dan Gohman475871a2008-07-27 21:46:04 +00002422SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002423 MachineFunction &MF = DAG.getMachineFunction();
2424 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2425 int ReturnAddrIndex = FuncInfo->getRAIndex();
2426
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002427 if (ReturnAddrIndex == 0) {
2428 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002429 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002430 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002431 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002432 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002433 }
2434
Evan Cheng25ab6902006-09-08 06:48:29 +00002435 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002436}
2437
2438
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002439bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2440 bool hasSymbolicDisplacement) {
2441 // Offset should fit into 32 bit immediate field.
2442 if (!isInt32(Offset))
2443 return false;
2444
2445 // If we don't have a symbolic displacement - we don't have any extra
2446 // restrictions.
2447 if (!hasSymbolicDisplacement)
2448 return true;
2449
2450 // FIXME: Some tweaks might be needed for medium code model.
2451 if (M != CodeModel::Small && M != CodeModel::Kernel)
2452 return false;
2453
2454 // For small code model we assume that latest object is 16MB before end of 31
2455 // bits boundary. We may also accept pretty large negative constants knowing
2456 // that all objects are in the positive half of address space.
2457 if (M == CodeModel::Small && Offset < 16*1024*1024)
2458 return true;
2459
2460 // For kernel code model we know that all object resist in the negative half
2461 // of 32bits address space. We may not accept negative offsets, since they may
2462 // be just off and we may accept pretty large positive ones.
2463 if (M == CodeModel::Kernel && Offset > 0)
2464 return true;
2465
2466 return false;
2467}
2468
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002469/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2470/// specific condition code, returning the condition code and the LHS/RHS of the
2471/// comparison to make.
2472static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2473 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002474 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002475 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2476 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2477 // X > -1 -> X == 0, jump !sign.
2478 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002479 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002480 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2481 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002482 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002483 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002484 // X < 1 -> X <= 0
2485 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002486 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002487 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002488 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002489
Evan Chengd9558e02006-01-06 00:43:03 +00002490 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002491 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002492 case ISD::SETEQ: return X86::COND_E;
2493 case ISD::SETGT: return X86::COND_G;
2494 case ISD::SETGE: return X86::COND_GE;
2495 case ISD::SETLT: return X86::COND_L;
2496 case ISD::SETLE: return X86::COND_LE;
2497 case ISD::SETNE: return X86::COND_NE;
2498 case ISD::SETULT: return X86::COND_B;
2499 case ISD::SETUGT: return X86::COND_A;
2500 case ISD::SETULE: return X86::COND_BE;
2501 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002502 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002503 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002504
Chris Lattner4c78e022008-12-23 23:42:27 +00002505 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002506
Chris Lattner4c78e022008-12-23 23:42:27 +00002507 // If LHS is a foldable load, but RHS is not, flip the condition.
2508 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2509 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2510 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2511 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002512 }
2513
Chris Lattner4c78e022008-12-23 23:42:27 +00002514 switch (SetCCOpcode) {
2515 default: break;
2516 case ISD::SETOLT:
2517 case ISD::SETOLE:
2518 case ISD::SETUGT:
2519 case ISD::SETUGE:
2520 std::swap(LHS, RHS);
2521 break;
2522 }
2523
2524 // On a floating point condition, the flags are set as follows:
2525 // ZF PF CF op
2526 // 0 | 0 | 0 | X > Y
2527 // 0 | 0 | 1 | X < Y
2528 // 1 | 0 | 0 | X == Y
2529 // 1 | 1 | 1 | unordered
2530 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002531 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002532 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002533 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002534 case ISD::SETOLT: // flipped
2535 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002536 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002537 case ISD::SETOLE: // flipped
2538 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002539 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002540 case ISD::SETUGT: // flipped
2541 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002542 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002543 case ISD::SETUGE: // flipped
2544 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002545 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002546 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002547 case ISD::SETNE: return X86::COND_NE;
2548 case ISD::SETUO: return X86::COND_P;
2549 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002550 case ISD::SETOEQ:
2551 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002552 }
Evan Chengd9558e02006-01-06 00:43:03 +00002553}
2554
Evan Cheng4a460802006-01-11 00:33:36 +00002555/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2556/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002557/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002558static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002559 switch (X86CC) {
2560 default:
2561 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002562 case X86::COND_B:
2563 case X86::COND_BE:
2564 case X86::COND_E:
2565 case X86::COND_P:
2566 case X86::COND_A:
2567 case X86::COND_AE:
2568 case X86::COND_NE:
2569 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002570 return true;
2571 }
2572}
2573
Evan Chengeb2f9692009-10-27 19:56:55 +00002574/// isFPImmLegal - Returns true if the target can instruction select the
2575/// specified FP immediate natively. If false, the legalizer will
2576/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002577bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002578 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2579 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2580 return true;
2581 }
2582 return false;
2583}
2584
Nate Begeman9008ca62009-04-27 18:41:29 +00002585/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2586/// the specified range (L, H].
2587static bool isUndefOrInRange(int Val, int Low, int Hi) {
2588 return (Val < 0) || (Val >= Low && Val < Hi);
2589}
2590
2591/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2592/// specified value.
2593static bool isUndefOrEqual(int Val, int CmpVal) {
2594 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002595 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002597}
2598
Nate Begeman9008ca62009-04-27 18:41:29 +00002599/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2600/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2601/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002602static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002603 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002605 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002606 return (Mask[0] < 2 && Mask[1] < 2);
2607 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002608}
2609
Nate Begeman9008ca62009-04-27 18:41:29 +00002610bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002611 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 N->getMask(M);
2613 return ::isPSHUFDMask(M, N->getValueType(0));
2614}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002615
Nate Begeman9008ca62009-04-27 18:41:29 +00002616/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2617/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002618static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002619 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002620 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002621
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 // Lower quadword copied in order or undef.
2623 for (int i = 0; i != 4; ++i)
2624 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002625 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002626
Evan Cheng506d3df2006-03-29 23:07:14 +00002627 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002628 for (int i = 4; i != 8; ++i)
2629 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002630 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002631
Evan Cheng506d3df2006-03-29 23:07:14 +00002632 return true;
2633}
2634
Nate Begeman9008ca62009-04-27 18:41:29 +00002635bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002636 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002637 N->getMask(M);
2638 return ::isPSHUFHWMask(M, N->getValueType(0));
2639}
Evan Cheng506d3df2006-03-29 23:07:14 +00002640
Nate Begeman9008ca62009-04-27 18:41:29 +00002641/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2642/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002643static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002644 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002645 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002646
Rafael Espindola15684b22009-04-24 12:40:33 +00002647 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002648 for (int i = 4; i != 8; ++i)
2649 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002650 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002651
Rafael Espindola15684b22009-04-24 12:40:33 +00002652 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002653 for (int i = 0; i != 4; ++i)
2654 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002655 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002656
Rafael Espindola15684b22009-04-24 12:40:33 +00002657 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002658}
2659
Nate Begeman9008ca62009-04-27 18:41:29 +00002660bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002661 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002662 N->getMask(M);
2663 return ::isPSHUFLWMask(M, N->getValueType(0));
2664}
2665
Nate Begemana09008b2009-10-19 02:17:23 +00002666/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2667/// is suitable for input to PALIGNR.
2668static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2669 bool hasSSSE3) {
2670 int i, e = VT.getVectorNumElements();
2671
2672 // Do not handle v2i64 / v2f64 shuffles with palignr.
2673 if (e < 4 || !hasSSSE3)
2674 return false;
2675
2676 for (i = 0; i != e; ++i)
2677 if (Mask[i] >= 0)
2678 break;
2679
2680 // All undef, not a palignr.
2681 if (i == e)
2682 return false;
2683
2684 // Determine if it's ok to perform a palignr with only the LHS, since we
2685 // don't have access to the actual shuffle elements to see if RHS is undef.
2686 bool Unary = Mask[i] < (int)e;
2687 bool NeedsUnary = false;
2688
2689 int s = Mask[i] - i;
2690
2691 // Check the rest of the elements to see if they are consecutive.
2692 for (++i; i != e; ++i) {
2693 int m = Mask[i];
2694 if (m < 0)
2695 continue;
2696
2697 Unary = Unary && (m < (int)e);
2698 NeedsUnary = NeedsUnary || (m < s);
2699
2700 if (NeedsUnary && !Unary)
2701 return false;
2702 if (Unary && m != ((s+i) & (e-1)))
2703 return false;
2704 if (!Unary && m != (s+i))
2705 return false;
2706 }
2707 return true;
2708}
2709
2710bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2711 SmallVector<int, 8> M;
2712 N->getMask(M);
2713 return ::isPALIGNRMask(M, N->getValueType(0), true);
2714}
2715
Evan Cheng14aed5e2006-03-24 01:18:28 +00002716/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2717/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002718static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002719 int NumElems = VT.getVectorNumElements();
2720 if (NumElems != 2 && NumElems != 4)
2721 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002722
Nate Begeman9008ca62009-04-27 18:41:29 +00002723 int Half = NumElems / 2;
2724 for (int i = 0; i < Half; ++i)
2725 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002726 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002727 for (int i = Half; i < NumElems; ++i)
2728 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002729 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002730
Evan Cheng14aed5e2006-03-24 01:18:28 +00002731 return true;
2732}
2733
Nate Begeman9008ca62009-04-27 18:41:29 +00002734bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2735 SmallVector<int, 8> M;
2736 N->getMask(M);
2737 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002738}
2739
Evan Cheng213d2cf2007-05-17 18:45:50 +00002740/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002741/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2742/// half elements to come from vector 1 (which would equal the dest.) and
2743/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002744static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002746
2747 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002748 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002749
Nate Begeman9008ca62009-04-27 18:41:29 +00002750 int Half = NumElems / 2;
2751 for (int i = 0; i < Half; ++i)
2752 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002753 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002754 for (int i = Half; i < NumElems; ++i)
2755 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002756 return false;
2757 return true;
2758}
2759
Nate Begeman9008ca62009-04-27 18:41:29 +00002760static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2761 SmallVector<int, 8> M;
2762 N->getMask(M);
2763 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002764}
2765
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002766/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2767/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002768bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2769 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002770 return false;
2771
Evan Cheng2064a2b2006-03-28 06:50:32 +00002772 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2774 isUndefOrEqual(N->getMaskElt(1), 7) &&
2775 isUndefOrEqual(N->getMaskElt(2), 2) &&
2776 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002777}
2778
Nate Begeman0b10b912009-11-07 23:17:15 +00002779/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2780/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2781/// <2, 3, 2, 3>
2782bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2783 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2784
2785 if (NumElems != 4)
2786 return false;
2787
2788 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2789 isUndefOrEqual(N->getMaskElt(1), 3) &&
2790 isUndefOrEqual(N->getMaskElt(2), 2) &&
2791 isUndefOrEqual(N->getMaskElt(3), 3);
2792}
2793
Evan Cheng5ced1d82006-04-06 23:23:56 +00002794/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2795/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002796bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2797 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002798
Evan Cheng5ced1d82006-04-06 23:23:56 +00002799 if (NumElems != 2 && NumElems != 4)
2800 return false;
2801
Evan Chengc5cdff22006-04-07 21:53:05 +00002802 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002804 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002805
Evan Chengc5cdff22006-04-07 21:53:05 +00002806 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002807 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002808 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002809
2810 return true;
2811}
2812
Nate Begeman0b10b912009-11-07 23:17:15 +00002813/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2814/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2815bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817
Evan Cheng5ced1d82006-04-06 23:23:56 +00002818 if (NumElems != 2 && NumElems != 4)
2819 return false;
2820
Evan Chengc5cdff22006-04-07 21:53:05 +00002821 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002822 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002823 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002824
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 for (unsigned i = 0; i < NumElems/2; ++i)
2826 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002827 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002828
2829 return true;
2830}
2831
Evan Cheng0038e592006-03-28 00:39:58 +00002832/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2833/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002834static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002835 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002836 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002837 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002838 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002839
Nate Begeman9008ca62009-04-27 18:41:29 +00002840 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2841 int BitI = Mask[i];
2842 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002843 if (!isUndefOrEqual(BitI, j))
2844 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002845 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002846 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002847 return false;
2848 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002849 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002850 return false;
2851 }
Evan Cheng0038e592006-03-28 00:39:58 +00002852 }
Evan Cheng0038e592006-03-28 00:39:58 +00002853 return true;
2854}
2855
Nate Begeman9008ca62009-04-27 18:41:29 +00002856bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2857 SmallVector<int, 8> M;
2858 N->getMask(M);
2859 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002860}
2861
Evan Cheng4fcb9222006-03-28 02:43:26 +00002862/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2863/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002864static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002865 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002866 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002867 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002868 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002869
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2871 int BitI = Mask[i];
2872 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002873 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002874 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002875 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002876 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002877 return false;
2878 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002879 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002880 return false;
2881 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002882 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002883 return true;
2884}
2885
Nate Begeman9008ca62009-04-27 18:41:29 +00002886bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2887 SmallVector<int, 8> M;
2888 N->getMask(M);
2889 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002890}
2891
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002892/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2893/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2894/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002895static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002896 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002897 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002898 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002899
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2901 int BitI = Mask[i];
2902 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002903 if (!isUndefOrEqual(BitI, j))
2904 return false;
2905 if (!isUndefOrEqual(BitI1, j))
2906 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002907 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002908 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002909}
2910
Nate Begeman9008ca62009-04-27 18:41:29 +00002911bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2912 SmallVector<int, 8> M;
2913 N->getMask(M);
2914 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2915}
2916
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002917/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2918/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2919/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002920static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002921 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002922 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2923 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002924
Nate Begeman9008ca62009-04-27 18:41:29 +00002925 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2926 int BitI = Mask[i];
2927 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002928 if (!isUndefOrEqual(BitI, j))
2929 return false;
2930 if (!isUndefOrEqual(BitI1, j))
2931 return false;
2932 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002933 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002934}
2935
Nate Begeman9008ca62009-04-27 18:41:29 +00002936bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2937 SmallVector<int, 8> M;
2938 N->getMask(M);
2939 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2940}
2941
Evan Cheng017dcc62006-04-21 01:05:10 +00002942/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2943/// specifies a shuffle of elements that is suitable for input to MOVSS,
2944/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002945static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002946 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002947 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002948
2949 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002950
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002952 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 for (int i = 1; i < NumElts; ++i)
2955 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002956 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002957
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002958 return true;
2959}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002960
Nate Begeman9008ca62009-04-27 18:41:29 +00002961bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2962 SmallVector<int, 8> M;
2963 N->getMask(M);
2964 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002965}
2966
Evan Cheng017dcc62006-04-21 01:05:10 +00002967/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2968/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002969/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002970static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002971 bool V2IsSplat = false, bool V2IsUndef = false) {
2972 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002973 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002974 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002975
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002977 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 for (int i = 1; i < NumOps; ++i)
2980 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2981 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2982 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002983 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002984
Evan Cheng39623da2006-04-20 08:58:49 +00002985 return true;
2986}
2987
Nate Begeman9008ca62009-04-27 18:41:29 +00002988static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002989 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002990 SmallVector<int, 8> M;
2991 N->getMask(M);
2992 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002993}
2994
Evan Chengd9539472006-04-14 21:59:03 +00002995/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2996/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002997bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2998 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002999 return false;
3000
3001 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003002 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003003 int Elt = N->getMaskElt(i);
3004 if (Elt >= 0 && Elt != 1)
3005 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003006 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003007
3008 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003009 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 int Elt = N->getMaskElt(i);
3011 if (Elt >= 0 && Elt != 3)
3012 return false;
3013 if (Elt == 3)
3014 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003015 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003016 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003017 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003018 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003019}
3020
3021/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3022/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003023bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3024 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003025 return false;
3026
3027 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 for (unsigned i = 0; i < 2; ++i)
3029 if (N->getMaskElt(i) > 0)
3030 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003031
3032 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003033 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 int Elt = N->getMaskElt(i);
3035 if (Elt >= 0 && Elt != 2)
3036 return false;
3037 if (Elt == 2)
3038 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003039 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003040 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003041 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003042}
3043
Evan Cheng0b457f02008-09-25 20:50:48 +00003044/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3045/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003046bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3047 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003048
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 for (int i = 0; i < e; ++i)
3050 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003051 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 for (int i = 0; i < e; ++i)
3053 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003054 return false;
3055 return true;
3056}
3057
Evan Cheng63d33002006-03-22 08:01:21 +00003058/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003059/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003060unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3062 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3063
Evan Chengb9df0ca2006-03-22 02:53:00 +00003064 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3065 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003066 for (int i = 0; i < NumOperands; ++i) {
3067 int Val = SVOp->getMaskElt(NumOperands-i-1);
3068 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003069 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003070 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003071 if (i != NumOperands - 1)
3072 Mask <<= Shift;
3073 }
Evan Cheng63d33002006-03-22 08:01:21 +00003074 return Mask;
3075}
3076
Evan Cheng506d3df2006-03-29 23:07:14 +00003077/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003078/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003079unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003081 unsigned Mask = 0;
3082 // 8 nodes, but we only care about the last 4.
3083 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003084 int Val = SVOp->getMaskElt(i);
3085 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003086 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003087 if (i != 4)
3088 Mask <<= 2;
3089 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003090 return Mask;
3091}
3092
3093/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003094/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003095unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003096 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003097 unsigned Mask = 0;
3098 // 8 nodes, but we only care about the first 4.
3099 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 int Val = SVOp->getMaskElt(i);
3101 if (Val >= 0)
3102 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003103 if (i != 0)
3104 Mask <<= 2;
3105 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003106 return Mask;
3107}
3108
Nate Begemana09008b2009-10-19 02:17:23 +00003109/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3110/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3111unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3112 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3113 EVT VVT = N->getValueType(0);
3114 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3115 int Val = 0;
3116
3117 unsigned i, e;
3118 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3119 Val = SVOp->getMaskElt(i);
3120 if (Val >= 0)
3121 break;
3122 }
3123 return (Val - i) * EltSize;
3124}
3125
Evan Cheng37b73872009-07-30 08:33:02 +00003126/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3127/// constant +0.0.
3128bool X86::isZeroNode(SDValue Elt) {
3129 return ((isa<ConstantSDNode>(Elt) &&
3130 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3131 (isa<ConstantFPSDNode>(Elt) &&
3132 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3133}
3134
Nate Begeman9008ca62009-04-27 18:41:29 +00003135/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3136/// their permute mask.
3137static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3138 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003139 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003140 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003142
Nate Begeman5a5ca152009-04-29 05:20:52 +00003143 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003144 int idx = SVOp->getMaskElt(i);
3145 if (idx < 0)
3146 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003147 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003148 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003149 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003151 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3153 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154}
3155
Evan Cheng779ccea2007-12-07 21:30:01 +00003156/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3157/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003158static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003159 unsigned NumElems = VT.getVectorNumElements();
3160 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 int idx = Mask[i];
3162 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003163 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003164 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003165 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003166 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003167 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003168 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003169}
3170
Evan Cheng533a0aa2006-04-19 20:35:22 +00003171/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3172/// match movhlps. The lower half elements should come from upper half of
3173/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003174/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003175static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3176 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003177 return false;
3178 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003180 return false;
3181 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003182 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003183 return false;
3184 return true;
3185}
3186
Evan Cheng5ced1d82006-04-06 23:23:56 +00003187/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003188/// is promoted to a vector. It also returns the LoadSDNode by reference if
3189/// required.
3190static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003191 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3192 return false;
3193 N = N->getOperand(0).getNode();
3194 if (!ISD::isNON_EXTLoad(N))
3195 return false;
3196 if (LD)
3197 *LD = cast<LoadSDNode>(N);
3198 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003199}
3200
Evan Cheng533a0aa2006-04-19 20:35:22 +00003201/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3202/// match movlp{s|d}. The lower half elements should come from lower half of
3203/// V1 (and in order), and the upper half elements should come from the upper
3204/// half of V2 (and in order). And since V1 will become the source of the
3205/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003206static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3207 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003208 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003209 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003210 // Is V2 is a vector load, don't do this transformation. We will try to use
3211 // load folding shufps op.
3212 if (ISD::isNON_EXTLoad(V2))
3213 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003214
Nate Begeman5a5ca152009-04-29 05:20:52 +00003215 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003216
Evan Cheng533a0aa2006-04-19 20:35:22 +00003217 if (NumElems != 2 && NumElems != 4)
3218 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003219 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003220 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003221 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003222 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003223 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003224 return false;
3225 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003226}
3227
Evan Cheng39623da2006-04-20 08:58:49 +00003228/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3229/// all the same.
3230static bool isSplatVector(SDNode *N) {
3231 if (N->getOpcode() != ISD::BUILD_VECTOR)
3232 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003233
Dan Gohman475871a2008-07-27 21:46:04 +00003234 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003235 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3236 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003237 return false;
3238 return true;
3239}
3240
Evan Cheng213d2cf2007-05-17 18:45:50 +00003241/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003242/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003243/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003244static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003245 SDValue V1 = N->getOperand(0);
3246 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003247 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3248 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003249 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003250 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003251 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003252 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3253 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003254 if (Opc != ISD::BUILD_VECTOR ||
3255 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 return false;
3257 } else if (Idx >= 0) {
3258 unsigned Opc = V1.getOpcode();
3259 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3260 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003261 if (Opc != ISD::BUILD_VECTOR ||
3262 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003263 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003264 }
3265 }
3266 return true;
3267}
3268
3269/// getZeroVector - Returns a vector of specified type with all zero elements.
3270///
Owen Andersone50ed302009-08-10 22:56:29 +00003271static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003272 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003273 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003274
Chris Lattner8a594482007-11-25 00:24:49 +00003275 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3276 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003277 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3280 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003281 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003282 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3283 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003284 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003285 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3286 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003287 }
Dale Johannesenace16102009-02-03 19:33:06 +00003288 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003289}
3290
Chris Lattner8a594482007-11-25 00:24:49 +00003291/// getOnesVector - Returns a vector of specified type with all bits set.
3292///
Owen Andersone50ed302009-08-10 22:56:29 +00003293static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003294 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003295
Chris Lattner8a594482007-11-25 00:24:49 +00003296 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3297 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003300 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003302 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003304 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003305}
3306
3307
Evan Cheng39623da2006-04-20 08:58:49 +00003308/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3309/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003310static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003311 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003312 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003313
Evan Cheng39623da2006-04-20 08:58:49 +00003314 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 SmallVector<int, 8> MaskVec;
3316 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003317
Nate Begeman5a5ca152009-04-29 05:20:52 +00003318 for (unsigned i = 0; i != NumElems; ++i) {
3319 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003320 MaskVec[i] = NumElems;
3321 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003322 }
Evan Cheng39623da2006-04-20 08:58:49 +00003323 }
Evan Cheng39623da2006-04-20 08:58:49 +00003324 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003325 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3326 SVOp->getOperand(1), &MaskVec[0]);
3327 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003328}
3329
Evan Cheng017dcc62006-04-21 01:05:10 +00003330/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3331/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003332static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 SDValue V2) {
3334 unsigned NumElems = VT.getVectorNumElements();
3335 SmallVector<int, 8> Mask;
3336 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003337 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 Mask.push_back(i);
3339 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003340}
3341
Nate Begeman9008ca62009-04-27 18:41:29 +00003342/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003343static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003344 SDValue V2) {
3345 unsigned NumElems = VT.getVectorNumElements();
3346 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003347 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 Mask.push_back(i);
3349 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003350 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003352}
3353
Nate Begeman9008ca62009-04-27 18:41:29 +00003354/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003355static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 SDValue V2) {
3357 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003358 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003360 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003361 Mask.push_back(i + Half);
3362 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003363 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003364 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003365}
3366
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003367/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003368static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 bool HasSSE2) {
3370 if (SV->getValueType(0).getVectorNumElements() <= 4)
3371 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003372
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003374 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003375 DebugLoc dl = SV->getDebugLoc();
3376 SDValue V1 = SV->getOperand(0);
3377 int NumElems = VT.getVectorNumElements();
3378 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003379
Nate Begeman9008ca62009-04-27 18:41:29 +00003380 // unpack elements to the correct location
3381 while (NumElems > 4) {
3382 if (EltNo < NumElems/2) {
3383 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3384 } else {
3385 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3386 EltNo -= NumElems/2;
3387 }
3388 NumElems >>= 1;
3389 }
Eric Christopherfd179292009-08-27 18:07:15 +00003390
Nate Begeman9008ca62009-04-27 18:41:29 +00003391 // Perform the splat.
3392 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003393 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3395 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003396}
3397
Evan Chengba05f722006-04-21 23:03:30 +00003398/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003399/// vector of zero or undef vector. This produces a shuffle where the low
3400/// element of V2 is swizzled into the zero/undef vector, landing at element
3401/// 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 +00003402static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003403 bool isZero, bool HasSSE2,
3404 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003405 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003406 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3408 unsigned NumElems = VT.getVectorNumElements();
3409 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003410 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 // If this is the insertion idx, put the low elt of V2 here.
3412 MaskVec.push_back(i == Idx ? NumElems : i);
3413 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003414}
3415
Evan Chengf26ffe92008-05-29 08:22:04 +00003416/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3417/// a shuffle that is zero.
3418static
Nate Begeman9008ca62009-04-27 18:41:29 +00003419unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3420 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003421 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003422 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003423 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 int Idx = SVOp->getMaskElt(Index);
3425 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003426 ++NumZeros;
3427 continue;
3428 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003430 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003431 ++NumZeros;
3432 else
3433 break;
3434 }
3435 return NumZeros;
3436}
3437
3438/// isVectorShift - Returns true if the shuffle can be implemented as a
3439/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003440/// FIXME: split into pslldqi, psrldqi, palignr variants.
3441static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003442 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003443 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003444
3445 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003446 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003447 if (!NumZeros) {
3448 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003449 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003450 if (!NumZeros)
3451 return false;
3452 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003453 bool SeenV1 = false;
3454 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 for (int i = NumZeros; i < NumElems; ++i) {
3456 int Val = isLeft ? (i - NumZeros) : i;
3457 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3458 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003459 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003460 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003461 SeenV1 = true;
3462 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003464 SeenV2 = true;
3465 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003466 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003467 return false;
3468 }
3469 if (SeenV1 && SeenV2)
3470 return false;
3471
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003473 ShAmt = NumZeros;
3474 return true;
3475}
3476
3477
Evan Chengc78d3b42006-04-24 18:01:45 +00003478/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3479///
Dan Gohman475871a2008-07-27 21:46:04 +00003480static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003481 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003482 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003483 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003484 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003485
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003486 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003487 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003488 bool First = true;
3489 for (unsigned i = 0; i < 16; ++i) {
3490 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3491 if (ThisIsNonZero && First) {
3492 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003494 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003496 First = false;
3497 }
3498
3499 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003500 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003501 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3502 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003503 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003505 }
3506 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003507 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3508 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3509 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003510 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003512 } else
3513 ThisElt = LastElt;
3514
Gabor Greifba36cb52008-08-28 21:40:38 +00003515 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003517 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003518 }
3519 }
3520
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003522}
3523
Bill Wendlinga348c562007-03-22 18:42:45 +00003524/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003525///
Dan Gohman475871a2008-07-27 21:46:04 +00003526static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003527 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003528 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003529 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003530 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003531
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003532 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003533 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003534 bool First = true;
3535 for (unsigned i = 0; i < 8; ++i) {
3536 bool isNonZero = (NonZeros & (1 << i)) != 0;
3537 if (isNonZero) {
3538 if (First) {
3539 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003541 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003543 First = false;
3544 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003545 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003547 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003548 }
3549 }
3550
3551 return V;
3552}
3553
Evan Chengf26ffe92008-05-29 08:22:04 +00003554/// getVShift - Return a vector logical shift node.
3555///
Owen Andersone50ed302009-08-10 22:56:29 +00003556static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003557 unsigned NumBits, SelectionDAG &DAG,
3558 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003561 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003562 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3563 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3564 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003565 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003566}
3567
Dan Gohman475871a2008-07-27 21:46:04 +00003568SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003569X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3570 SelectionDAG &DAG) {
3571
3572 // Check if the scalar load can be widened into a vector load. And if
3573 // the address is "base + cst" see if the cst can be "absorbed" into
3574 // the shuffle mask.
3575 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3576 SDValue Ptr = LD->getBasePtr();
3577 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3578 return SDValue();
3579 EVT PVT = LD->getValueType(0);
3580 if (PVT != MVT::i32 && PVT != MVT::f32)
3581 return SDValue();
3582
3583 int FI = -1;
3584 int64_t Offset = 0;
3585 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3586 FI = FINode->getIndex();
3587 Offset = 0;
3588 } else if (Ptr.getOpcode() == ISD::ADD &&
3589 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3590 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3591 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3592 Offset = Ptr.getConstantOperandVal(1);
3593 Ptr = Ptr.getOperand(0);
3594 } else {
3595 return SDValue();
3596 }
3597
3598 SDValue Chain = LD->getChain();
3599 // Make sure the stack object alignment is at least 16.
3600 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3601 if (DAG.InferPtrAlignment(Ptr) < 16) {
3602 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003603 // Can't change the alignment. FIXME: It's possible to compute
3604 // the exact stack offset and reference FI + adjust offset instead.
3605 // If someone *really* cares about this. That's the way to implement it.
3606 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003607 } else {
3608 MFI->setObjectAlignment(FI, 16);
3609 }
3610 }
3611
3612 // (Offset % 16) must be multiple of 4. Then address is then
3613 // Ptr + (Offset & ~15).
3614 if (Offset < 0)
3615 return SDValue();
3616 if ((Offset % 16) & 3)
3617 return SDValue();
3618 int64_t StartOffset = Offset & ~15;
3619 if (StartOffset)
3620 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3621 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3622
3623 int EltNo = (Offset - StartOffset) >> 2;
3624 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3625 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003626 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3627 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003628 // Canonicalize it to a v4i32 shuffle.
3629 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3630 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3631 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3632 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3633 }
3634
3635 return SDValue();
3636}
3637
3638SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003639X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003640 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003641 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003642 if (ISD::isBuildVectorAllZeros(Op.getNode())
3643 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003644 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3645 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3646 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003648 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003649
Gabor Greifba36cb52008-08-28 21:40:38 +00003650 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003651 return getOnesVector(Op.getValueType(), DAG, dl);
3652 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003653 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003654
Owen Andersone50ed302009-08-10 22:56:29 +00003655 EVT VT = Op.getValueType();
3656 EVT ExtVT = VT.getVectorElementType();
3657 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003658
3659 unsigned NumElems = Op.getNumOperands();
3660 unsigned NumZero = 0;
3661 unsigned NumNonZero = 0;
3662 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003663 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003664 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003665 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003666 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003667 if (Elt.getOpcode() == ISD::UNDEF)
3668 continue;
3669 Values.insert(Elt);
3670 if (Elt.getOpcode() != ISD::Constant &&
3671 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003672 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003673 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003674 NumZero++;
3675 else {
3676 NonZeros |= (1 << i);
3677 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003678 }
3679 }
3680
Dan Gohman7f321562007-06-25 16:23:39 +00003681 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003682 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003683 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003684 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003685
Chris Lattner67f453a2008-03-09 05:42:06 +00003686 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003687 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003688 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003689 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003690
Chris Lattner62098042008-03-09 01:05:04 +00003691 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3692 // the value are obviously zero, truncate the value to i32 and do the
3693 // insertion that way. Only do this if the value is non-constant or if the
3694 // value is a constant being inserted into element 0. It is cheaper to do
3695 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003696 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003697 (!IsAllConstants || Idx == 0)) {
3698 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3699 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3701 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003702
Chris Lattner62098042008-03-09 01:05:04 +00003703 // Truncate the value (which may itself be a constant) to i32, and
3704 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003706 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003707 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3708 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003709
Chris Lattner62098042008-03-09 01:05:04 +00003710 // Now we have our 32-bit value zero extended in the low element of
3711 // a vector. If Idx != 0, swizzle it into place.
3712 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003713 SmallVector<int, 4> Mask;
3714 Mask.push_back(Idx);
3715 for (unsigned i = 1; i != VecElts; ++i)
3716 Mask.push_back(i);
3717 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003718 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003719 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003720 }
Dale Johannesenace16102009-02-03 19:33:06 +00003721 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003722 }
3723 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003724
Chris Lattner19f79692008-03-08 22:59:52 +00003725 // If we have a constant or non-constant insertion into the low element of
3726 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3727 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003728 // depending on what the source datatype is.
3729 if (Idx == 0) {
3730 if (NumZero == 0) {
3731 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3733 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003734 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3735 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3736 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3737 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003738 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3739 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3740 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003741 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3742 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3743 Subtarget->hasSSE2(), DAG);
3744 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3745 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003746 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003747
3748 // Is it a vector logical left shift?
3749 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003750 X86::isZeroNode(Op.getOperand(0)) &&
3751 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003752 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003753 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003754 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003755 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003756 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003758
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003759 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003760 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003761
Chris Lattner19f79692008-03-08 22:59:52 +00003762 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3763 // is a non-constant being inserted into an element other than the low one,
3764 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3765 // movd/movss) to move this into the low element, then shuffle it into
3766 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003768 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003769
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003771 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3772 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003773 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003775 MaskVec.push_back(i == Idx ? 0 : 1);
3776 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003777 }
3778 }
3779
Chris Lattner67f453a2008-03-09 05:42:06 +00003780 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003781 if (Values.size() == 1) {
3782 if (EVTBits == 32) {
3783 // Instead of a shuffle like this:
3784 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3785 // Check if it's possible to issue this instead.
3786 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3787 unsigned Idx = CountTrailingZeros_32(NonZeros);
3788 SDValue Item = Op.getOperand(Idx);
3789 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3790 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3791 }
Dan Gohman475871a2008-07-27 21:46:04 +00003792 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003793 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003794
Dan Gohmana3941172007-07-24 22:55:08 +00003795 // A vector full of immediates; various special cases are already
3796 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003797 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003798 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003799
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003800 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003801 if (EVTBits == 64) {
3802 if (NumNonZero == 1) {
3803 // One half is zero or undef.
3804 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003805 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003806 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003807 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3808 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003809 }
Dan Gohman475871a2008-07-27 21:46:04 +00003810 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003811 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003812
3813 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003814 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003815 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003816 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003817 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003818 }
3819
Bill Wendling826f36f2007-03-28 00:57:11 +00003820 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003821 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003822 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003823 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003824 }
3825
3826 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003827 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003828 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003829 if (NumElems == 4 && NumZero > 0) {
3830 for (unsigned i = 0; i < 4; ++i) {
3831 bool isZero = !(NonZeros & (1 << i));
3832 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003833 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003834 else
Dale Johannesenace16102009-02-03 19:33:06 +00003835 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003836 }
3837
3838 for (unsigned i = 0; i < 2; ++i) {
3839 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3840 default: break;
3841 case 0:
3842 V[i] = V[i*2]; // Must be a zero vector.
3843 break;
3844 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003845 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003846 break;
3847 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003848 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 break;
3850 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852 break;
3853 }
3854 }
3855
Nate Begeman9008ca62009-04-27 18:41:29 +00003856 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003857 bool Reverse = (NonZeros & 0x3) == 2;
3858 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003860 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3861 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003862 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3863 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003864 }
3865
3866 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003867 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3868 // values to be inserted is equal to the number of elements, in which case
3869 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003870 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003871 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003872 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003873 getSubtarget()->hasSSE41()) {
3874 V[0] = DAG.getUNDEF(VT);
3875 for (unsigned i = 0; i < NumElems; ++i)
3876 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3877 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3878 Op.getOperand(i), DAG.getIntPtrConstant(i));
3879 return V[0];
3880 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003881 // Expand into a number of unpckl*.
3882 // e.g. for v4f32
3883 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3884 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3885 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003886 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003887 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003888 NumElems >>= 1;
3889 while (NumElems != 0) {
3890 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003891 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003892 NumElems >>= 1;
3893 }
3894 return V[0];
3895 }
3896
Dan Gohman475871a2008-07-27 21:46:04 +00003897 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003898}
3899
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003900SDValue
3901X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3902 // We support concatenate two MMX registers and place them in a MMX
3903 // register. This is better than doing a stack convert.
3904 DebugLoc dl = Op.getDebugLoc();
3905 EVT ResVT = Op.getValueType();
3906 assert(Op.getNumOperands() == 2);
3907 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3908 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3909 int Mask[2];
3910 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3911 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3912 InVec = Op.getOperand(1);
3913 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3914 unsigned NumElts = ResVT.getVectorNumElements();
3915 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3916 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3917 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3918 } else {
3919 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3920 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3921 Mask[0] = 0; Mask[1] = 2;
3922 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3923 }
3924 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3925}
3926
Nate Begemanb9a47b82009-02-23 08:49:38 +00003927// v8i16 shuffles - Prefer shuffles in the following order:
3928// 1. [all] pshuflw, pshufhw, optional move
3929// 2. [ssse3] 1 x pshufb
3930// 3. [ssse3] 2 x pshufb + 1 x por
3931// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003932static
Nate Begeman9008ca62009-04-27 18:41:29 +00003933SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3934 SelectionDAG &DAG, X86TargetLowering &TLI) {
3935 SDValue V1 = SVOp->getOperand(0);
3936 SDValue V2 = SVOp->getOperand(1);
3937 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003938 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003939
Nate Begemanb9a47b82009-02-23 08:49:38 +00003940 // Determine if more than 1 of the words in each of the low and high quadwords
3941 // of the result come from the same quadword of one of the two inputs. Undef
3942 // mask values count as coming from any quadword, for better codegen.
3943 SmallVector<unsigned, 4> LoQuad(4);
3944 SmallVector<unsigned, 4> HiQuad(4);
3945 BitVector InputQuads(4);
3946 for (unsigned i = 0; i < 8; ++i) {
3947 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003948 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003949 MaskVals.push_back(EltIdx);
3950 if (EltIdx < 0) {
3951 ++Quad[0];
3952 ++Quad[1];
3953 ++Quad[2];
3954 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003955 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003956 }
3957 ++Quad[EltIdx / 4];
3958 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003959 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003960
Nate Begemanb9a47b82009-02-23 08:49:38 +00003961 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003962 unsigned MaxQuad = 1;
3963 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003964 if (LoQuad[i] > MaxQuad) {
3965 BestLoQuad = i;
3966 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003967 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003968 }
3969
Nate Begemanb9a47b82009-02-23 08:49:38 +00003970 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003971 MaxQuad = 1;
3972 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003973 if (HiQuad[i] > MaxQuad) {
3974 BestHiQuad = i;
3975 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003976 }
3977 }
3978
Nate Begemanb9a47b82009-02-23 08:49:38 +00003979 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003980 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003981 // single pshufb instruction is necessary. If There are more than 2 input
3982 // quads, disable the next transformation since it does not help SSSE3.
3983 bool V1Used = InputQuads[0] || InputQuads[1];
3984 bool V2Used = InputQuads[2] || InputQuads[3];
3985 if (TLI.getSubtarget()->hasSSSE3()) {
3986 if (InputQuads.count() == 2 && V1Used && V2Used) {
3987 BestLoQuad = InputQuads.find_first();
3988 BestHiQuad = InputQuads.find_next(BestLoQuad);
3989 }
3990 if (InputQuads.count() > 2) {
3991 BestLoQuad = -1;
3992 BestHiQuad = -1;
3993 }
3994 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003995
Nate Begemanb9a47b82009-02-23 08:49:38 +00003996 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3997 // the shuffle mask. If a quad is scored as -1, that means that it contains
3998 // words from all 4 input quadwords.
3999 SDValue NewV;
4000 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004001 SmallVector<int, 8> MaskV;
4002 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4003 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004004 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004005 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4006 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4007 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004008
Nate Begemanb9a47b82009-02-23 08:49:38 +00004009 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4010 // source words for the shuffle, to aid later transformations.
4011 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004012 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004013 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004014 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004015 if (idx != (int)i)
4016 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004017 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004018 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004019 AllWordsInNewV = false;
4020 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004021 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004022
Nate Begemanb9a47b82009-02-23 08:49:38 +00004023 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4024 if (AllWordsInNewV) {
4025 for (int i = 0; i != 8; ++i) {
4026 int idx = MaskVals[i];
4027 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004028 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004029 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004030 if ((idx != i) && idx < 4)
4031 pshufhw = false;
4032 if ((idx != i) && idx > 3)
4033 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004034 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004035 V1 = NewV;
4036 V2Used = false;
4037 BestLoQuad = 0;
4038 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004039 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004040
Nate Begemanb9a47b82009-02-23 08:49:38 +00004041 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4042 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004043 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004044 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004045 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004046 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004047 }
Eric Christopherfd179292009-08-27 18:07:15 +00004048
Nate Begemanb9a47b82009-02-23 08:49:38 +00004049 // If we have SSSE3, and all words of the result are from 1 input vector,
4050 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4051 // is present, fall back to case 4.
4052 if (TLI.getSubtarget()->hasSSSE3()) {
4053 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004054
Nate Begemanb9a47b82009-02-23 08:49:38 +00004055 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004056 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004057 // mask, and elements that come from V1 in the V2 mask, so that the two
4058 // results can be OR'd together.
4059 bool TwoInputs = V1Used && V2Used;
4060 for (unsigned i = 0; i != 8; ++i) {
4061 int EltIdx = MaskVals[i] * 2;
4062 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4064 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 continue;
4066 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4068 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004069 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004071 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004072 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004076
Nate Begemanb9a47b82009-02-23 08:49:38 +00004077 // Calculate the shuffle mask for the second input, shuffle it, and
4078 // OR it with the first shuffled input.
4079 pshufbMask.clear();
4080 for (unsigned i = 0; i != 8; ++i) {
4081 int EltIdx = MaskVals[i] * 2;
4082 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004083 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4084 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 continue;
4086 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4088 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004091 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004092 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 MVT::v16i8, &pshufbMask[0], 16));
4094 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4095 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 }
4097
4098 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4099 // and update MaskVals with new element order.
4100 BitVector InOrder(8);
4101 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004102 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 for (int i = 0; i != 4; ++i) {
4104 int idx = MaskVals[i];
4105 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004106 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004107 InOrder.set(i);
4108 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004109 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004110 InOrder.set(i);
4111 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004112 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004113 }
4114 }
4115 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004116 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004118 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 }
Eric Christopherfd179292009-08-27 18:07:15 +00004120
Nate Begemanb9a47b82009-02-23 08:49:38 +00004121 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4122 // and update MaskVals with the new element order.
4123 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004124 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004125 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004126 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004127 for (unsigned i = 4; i != 8; ++i) {
4128 int idx = MaskVals[i];
4129 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004130 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004131 InOrder.set(i);
4132 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004133 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 InOrder.set(i);
4135 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004136 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004137 }
4138 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004140 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 }
Eric Christopherfd179292009-08-27 18:07:15 +00004142
Nate Begemanb9a47b82009-02-23 08:49:38 +00004143 // In case BestHi & BestLo were both -1, which means each quadword has a word
4144 // from each of the four input quadwords, calculate the InOrder bitvector now
4145 // before falling through to the insert/extract cleanup.
4146 if (BestLoQuad == -1 && BestHiQuad == -1) {
4147 NewV = V1;
4148 for (int i = 0; i != 8; ++i)
4149 if (MaskVals[i] < 0 || MaskVals[i] == i)
4150 InOrder.set(i);
4151 }
Eric Christopherfd179292009-08-27 18:07:15 +00004152
Nate Begemanb9a47b82009-02-23 08:49:38 +00004153 // The other elements are put in the right place using pextrw and pinsrw.
4154 for (unsigned i = 0; i != 8; ++i) {
4155 if (InOrder[i])
4156 continue;
4157 int EltIdx = MaskVals[i];
4158 if (EltIdx < 0)
4159 continue;
4160 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004162 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004164 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004166 DAG.getIntPtrConstant(i));
4167 }
4168 return NewV;
4169}
4170
4171// v16i8 shuffles - Prefer shuffles in the following order:
4172// 1. [ssse3] 1 x pshufb
4173// 2. [ssse3] 2 x pshufb + 1 x por
4174// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4175static
Nate Begeman9008ca62009-04-27 18:41:29 +00004176SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4177 SelectionDAG &DAG, X86TargetLowering &TLI) {
4178 SDValue V1 = SVOp->getOperand(0);
4179 SDValue V2 = SVOp->getOperand(1);
4180 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004181 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004182 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004183
Nate Begemanb9a47b82009-02-23 08:49:38 +00004184 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004185 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004186 // present, fall back to case 3.
4187 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4188 bool V1Only = true;
4189 bool V2Only = true;
4190 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004192 if (EltIdx < 0)
4193 continue;
4194 if (EltIdx < 16)
4195 V2Only = false;
4196 else
4197 V1Only = false;
4198 }
Eric Christopherfd179292009-08-27 18:07:15 +00004199
Nate Begemanb9a47b82009-02-23 08:49:38 +00004200 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4201 if (TLI.getSubtarget()->hasSSSE3()) {
4202 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004203
Nate Begemanb9a47b82009-02-23 08:49:38 +00004204 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004205 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004206 //
4207 // Otherwise, we have elements from both input vectors, and must zero out
4208 // elements that come from V2 in the first mask, and V1 in the second mask
4209 // so that we can OR them together.
4210 bool TwoInputs = !(V1Only || V2Only);
4211 for (unsigned i = 0; i != 16; ++i) {
4212 int EltIdx = MaskVals[i];
4213 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004215 continue;
4216 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004217 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004218 }
4219 // If all the elements are from V2, assign it to V1 and return after
4220 // building the first pshufb.
4221 if (V2Only)
4222 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004224 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004226 if (!TwoInputs)
4227 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004228
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 // Calculate the shuffle mask for the second input, shuffle it, and
4230 // OR it with the first shuffled input.
4231 pshufbMask.clear();
4232 for (unsigned i = 0; i != 16; ++i) {
4233 int EltIdx = MaskVals[i];
4234 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004236 continue;
4237 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004241 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 MVT::v16i8, &pshufbMask[0], 16));
4243 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004244 }
Eric Christopherfd179292009-08-27 18:07:15 +00004245
Nate Begemanb9a47b82009-02-23 08:49:38 +00004246 // No SSSE3 - Calculate in place words and then fix all out of place words
4247 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4248 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4250 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004251 SDValue NewV = V2Only ? V2 : V1;
4252 for (int i = 0; i != 8; ++i) {
4253 int Elt0 = MaskVals[i*2];
4254 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004255
Nate Begemanb9a47b82009-02-23 08:49:38 +00004256 // This word of the result is all undef, skip it.
4257 if (Elt0 < 0 && Elt1 < 0)
4258 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004259
Nate Begemanb9a47b82009-02-23 08:49:38 +00004260 // This word of the result is already in the correct place, skip it.
4261 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4262 continue;
4263 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4264 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004265
Nate Begemanb9a47b82009-02-23 08:49:38 +00004266 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4267 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4268 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004269
4270 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4271 // using a single extract together, load it and store it.
4272 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004273 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004274 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004276 DAG.getIntPtrConstant(i));
4277 continue;
4278 }
4279
Nate Begemanb9a47b82009-02-23 08:49:38 +00004280 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004281 // source byte is not also odd, shift the extracted word left 8 bits
4282 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004283 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004285 DAG.getIntPtrConstant(Elt1 / 2));
4286 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004288 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004289 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4291 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004292 }
4293 // If Elt0 is defined, extract it from the appropriate source. If the
4294 // source byte is not also even, shift the extracted word right 8 bits. If
4295 // Elt1 was also defined, OR the extracted values together before
4296 // inserting them in the result.
4297 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004299 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4300 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004301 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004302 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004303 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4305 DAG.getConstant(0x00FF, MVT::i16));
4306 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004307 : InsElt0;
4308 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004309 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004310 DAG.getIntPtrConstant(i));
4311 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004313}
4314
Evan Cheng7a831ce2007-12-15 03:00:47 +00004315/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4316/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4317/// done when every pair / quad of shuffle mask elements point to elements in
4318/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004319/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4320static
Nate Begeman9008ca62009-04-27 18:41:29 +00004321SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4322 SelectionDAG &DAG,
4323 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004324 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004325 SDValue V1 = SVOp->getOperand(0);
4326 SDValue V2 = SVOp->getOperand(1);
4327 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004328 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004329 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004330 EVT MaskEltVT = MaskVT.getVectorElementType();
4331 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004332 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004333 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004334 case MVT::v4f32: NewVT = MVT::v2f64; break;
4335 case MVT::v4i32: NewVT = MVT::v2i64; break;
4336 case MVT::v8i16: NewVT = MVT::v4i32; break;
4337 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004338 }
4339
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004340 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004341 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004343 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004345 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 int Scale = NumElems / NewWidth;
4347 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004348 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 int StartIdx = -1;
4350 for (int j = 0; j < Scale; ++j) {
4351 int EltIdx = SVOp->getMaskElt(i+j);
4352 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004353 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004354 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004355 StartIdx = EltIdx - (EltIdx % Scale);
4356 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004357 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004358 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004359 if (StartIdx == -1)
4360 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004361 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004362 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004363 }
4364
Dale Johannesenace16102009-02-03 19:33:06 +00004365 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4366 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004367 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004368}
4369
Evan Chengd880b972008-05-09 21:53:03 +00004370/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004371///
Owen Andersone50ed302009-08-10 22:56:29 +00004372static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004373 SDValue SrcOp, SelectionDAG &DAG,
4374 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004376 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004377 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004378 LD = dyn_cast<LoadSDNode>(SrcOp);
4379 if (!LD) {
4380 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4381 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004382 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4383 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004384 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4385 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004386 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004387 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004389 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4390 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4391 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4392 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004393 SrcOp.getOperand(0)
4394 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004395 }
4396 }
4397 }
4398
Dale Johannesenace16102009-02-03 19:33:06 +00004399 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4400 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004401 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004402 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004403}
4404
Evan Chengace3c172008-07-22 21:13:36 +00004405/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4406/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004407static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004408LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4409 SDValue V1 = SVOp->getOperand(0);
4410 SDValue V2 = SVOp->getOperand(1);
4411 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004412 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004413
Evan Chengace3c172008-07-22 21:13:36 +00004414 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004415 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 SmallVector<int, 8> Mask1(4U, -1);
4417 SmallVector<int, 8> PermMask;
4418 SVOp->getMask(PermMask);
4419
Evan Chengace3c172008-07-22 21:13:36 +00004420 unsigned NumHi = 0;
4421 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004422 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004423 int Idx = PermMask[i];
4424 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004425 Locs[i] = std::make_pair(-1, -1);
4426 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004427 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4428 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004429 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004431 NumLo++;
4432 } else {
4433 Locs[i] = std::make_pair(1, NumHi);
4434 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004436 NumHi++;
4437 }
4438 }
4439 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004440
Evan Chengace3c172008-07-22 21:13:36 +00004441 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004442 // If no more than two elements come from either vector. This can be
4443 // implemented with two shuffles. First shuffle gather the elements.
4444 // The second shuffle, which takes the first shuffle as both of its
4445 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004446 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004447
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004449
Evan Chengace3c172008-07-22 21:13:36 +00004450 for (unsigned i = 0; i != 4; ++i) {
4451 if (Locs[i].first == -1)
4452 continue;
4453 else {
4454 unsigned Idx = (i < 2) ? 0 : 4;
4455 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004456 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004457 }
4458 }
4459
Nate Begeman9008ca62009-04-27 18:41:29 +00004460 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004461 } else if (NumLo == 3 || NumHi == 3) {
4462 // Otherwise, we must have three elements from one vector, call it X, and
4463 // one element from the other, call it Y. First, use a shufps to build an
4464 // intermediate vector with the one element from Y and the element from X
4465 // that will be in the same half in the final destination (the indexes don't
4466 // matter). Then, use a shufps to build the final vector, taking the half
4467 // containing the element from Y from the intermediate, and the other half
4468 // from X.
4469 if (NumHi == 3) {
4470 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004471 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004472 std::swap(V1, V2);
4473 }
4474
4475 // Find the element from V2.
4476 unsigned HiIndex;
4477 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004478 int Val = PermMask[HiIndex];
4479 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004480 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004481 if (Val >= 4)
4482 break;
4483 }
4484
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 Mask1[0] = PermMask[HiIndex];
4486 Mask1[1] = -1;
4487 Mask1[2] = PermMask[HiIndex^1];
4488 Mask1[3] = -1;
4489 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004490
4491 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 Mask1[0] = PermMask[0];
4493 Mask1[1] = PermMask[1];
4494 Mask1[2] = HiIndex & 1 ? 6 : 4;
4495 Mask1[3] = HiIndex & 1 ? 4 : 6;
4496 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004497 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004498 Mask1[0] = HiIndex & 1 ? 2 : 0;
4499 Mask1[1] = HiIndex & 1 ? 0 : 2;
4500 Mask1[2] = PermMask[2];
4501 Mask1[3] = PermMask[3];
4502 if (Mask1[2] >= 0)
4503 Mask1[2] += 4;
4504 if (Mask1[3] >= 0)
4505 Mask1[3] += 4;
4506 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004507 }
Evan Chengace3c172008-07-22 21:13:36 +00004508 }
4509
4510 // Break it into (shuffle shuffle_hi, shuffle_lo).
4511 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004512 SmallVector<int,8> LoMask(4U, -1);
4513 SmallVector<int,8> HiMask(4U, -1);
4514
4515 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004516 unsigned MaskIdx = 0;
4517 unsigned LoIdx = 0;
4518 unsigned HiIdx = 2;
4519 for (unsigned i = 0; i != 4; ++i) {
4520 if (i == 2) {
4521 MaskPtr = &HiMask;
4522 MaskIdx = 1;
4523 LoIdx = 0;
4524 HiIdx = 2;
4525 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 int Idx = PermMask[i];
4527 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004528 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004529 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004530 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004531 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004532 LoIdx++;
4533 } else {
4534 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004536 HiIdx++;
4537 }
4538 }
4539
Nate Begeman9008ca62009-04-27 18:41:29 +00004540 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4541 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4542 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004543 for (unsigned i = 0; i != 4; ++i) {
4544 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004545 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004546 } else {
4547 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004548 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004549 }
4550 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004551 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004552}
4553
Dan Gohman475871a2008-07-27 21:46:04 +00004554SDValue
4555X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004556 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue V1 = Op.getOperand(0);
4558 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004559 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004560 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004562 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4564 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004565 bool V1IsSplat = false;
4566 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567
Nate Begeman9008ca62009-04-27 18:41:29 +00004568 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004569 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004570
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 // Promote splats to v4f32.
4572 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004573 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 return Op;
4575 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 }
4577
Evan Cheng7a831ce2007-12-15 03:00:47 +00004578 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4579 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004580 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004581 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004582 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004583 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004584 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004585 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004586 // FIXME: Figure out a cleaner way to do this.
4587 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004588 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004589 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004590 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004591 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4592 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4593 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004594 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004595 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004596 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4597 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004598 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004599 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004600 }
4601 }
Eric Christopherfd179292009-08-27 18:07:15 +00004602
Nate Begeman9008ca62009-04-27 18:41:29 +00004603 if (X86::isPSHUFDMask(SVOp))
4604 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004605
Evan Chengf26ffe92008-05-29 08:22:04 +00004606 // Check if this can be converted into a logical shift.
4607 bool isLeft = false;
4608 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004609 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004610 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004611 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004612 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004613 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004614 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004615 EVT EltVT = VT.getVectorElementType();
4616 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004617 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004618 }
Eric Christopherfd179292009-08-27 18:07:15 +00004619
Nate Begeman9008ca62009-04-27 18:41:29 +00004620 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004621 if (V1IsUndef)
4622 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004623 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004624 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004625 if (!isMMX)
4626 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004627 }
Eric Christopherfd179292009-08-27 18:07:15 +00004628
Nate Begeman9008ca62009-04-27 18:41:29 +00004629 // FIXME: fold these into legal mask.
4630 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4631 X86::isMOVSLDUPMask(SVOp) ||
4632 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004633 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004634 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004635 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004636
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 if (ShouldXformToMOVHLPS(SVOp) ||
4638 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4639 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004640
Evan Chengf26ffe92008-05-29 08:22:04 +00004641 if (isShift) {
4642 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004643 EVT EltVT = VT.getVectorElementType();
4644 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004645 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004646 }
Eric Christopherfd179292009-08-27 18:07:15 +00004647
Evan Cheng9eca5e82006-10-25 21:49:50 +00004648 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004649 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4650 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004651 V1IsSplat = isSplatVector(V1.getNode());
4652 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004653
Chris Lattner8a594482007-11-25 00:24:49 +00004654 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004655 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 Op = CommuteVectorShuffle(SVOp, DAG);
4657 SVOp = cast<ShuffleVectorSDNode>(Op);
4658 V1 = SVOp->getOperand(0);
4659 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004660 std::swap(V1IsSplat, V2IsSplat);
4661 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004662 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004663 }
4664
Nate Begeman9008ca62009-04-27 18:41:29 +00004665 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4666 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004667 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 return V1;
4669 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4670 // the instruction selector will not match, so get a canonical MOVL with
4671 // swapped operands to undo the commute.
4672 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004673 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004674
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4676 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4677 X86::isUNPCKLMask(SVOp) ||
4678 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004679 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004680
Evan Cheng9bbbb982006-10-25 20:48:19 +00004681 if (V2IsSplat) {
4682 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004683 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004684 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 SDValue NewMask = NormalizeMask(SVOp, DAG);
4686 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4687 if (NSVOp != SVOp) {
4688 if (X86::isUNPCKLMask(NSVOp, true)) {
4689 return NewMask;
4690 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4691 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004692 }
4693 }
4694 }
4695
Evan Cheng9eca5e82006-10-25 21:49:50 +00004696 if (Commuted) {
4697 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004698 // FIXME: this seems wrong.
4699 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4700 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4701 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4702 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4703 X86::isUNPCKLMask(NewSVOp) ||
4704 X86::isUNPCKHMask(NewSVOp))
4705 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004706 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004707
Nate Begemanb9a47b82009-02-23 08:49:38 +00004708 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004709
4710 // Normalize the node to match x86 shuffle ops if needed
4711 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4712 return CommuteVectorShuffle(SVOp, DAG);
4713
4714 // Check for legal shuffle and return?
4715 SmallVector<int, 16> PermMask;
4716 SVOp->getMask(PermMask);
4717 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004718 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004719
Evan Cheng14b32e12007-12-11 01:46:18 +00004720 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004721 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004723 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004724 return NewOp;
4725 }
4726
Owen Anderson825b72b2009-08-11 20:47:22 +00004727 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004728 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 if (NewOp.getNode())
4730 return NewOp;
4731 }
Eric Christopherfd179292009-08-27 18:07:15 +00004732
Evan Chengace3c172008-07-22 21:13:36 +00004733 // Handle all 4 wide cases with a number of shuffles except for MMX.
4734 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004735 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004736
Dan Gohman475871a2008-07-27 21:46:04 +00004737 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004738}
4739
Dan Gohman475871a2008-07-27 21:46:04 +00004740SDValue
4741X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004742 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004743 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004744 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004745 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004747 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004748 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004749 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004750 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004751 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004752 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4753 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4754 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004755 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4756 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004757 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004758 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004759 Op.getOperand(0)),
4760 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004761 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004762 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004764 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004765 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004767 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4768 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004769 // result has a single use which is a store or a bitcast to i32. And in
4770 // the case of a store, it's not worth it if the index is a constant 0,
4771 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004772 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004773 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004774 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004775 if ((User->getOpcode() != ISD::STORE ||
4776 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4777 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004778 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004780 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004781 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4782 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004783 Op.getOperand(0)),
4784 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4786 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004787 // ExtractPS works with constant index.
4788 if (isa<ConstantSDNode>(Op.getOperand(1)))
4789 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004790 }
Dan Gohman475871a2008-07-27 21:46:04 +00004791 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004792}
4793
4794
Dan Gohman475871a2008-07-27 21:46:04 +00004795SDValue
4796X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004798 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004799
Evan Cheng62a3f152008-03-24 21:52:23 +00004800 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004802 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004803 return Res;
4804 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004805
Owen Andersone50ed302009-08-10 22:56:29 +00004806 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004807 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004808 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004809 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004811 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004812 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4814 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004815 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004817 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004818 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004819 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004820 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004822 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004823 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004824 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004825 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004826 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004827 if (Idx == 0)
4828 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004829
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004831 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004832 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004833 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004834 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004835 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004836 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004837 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004838 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4839 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4840 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004841 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004842 if (Idx == 0)
4843 return Op;
4844
4845 // UNPCKHPD the element to the lowest double word, then movsd.
4846 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4847 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004848 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004849 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004850 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004851 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004852 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004853 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004854 }
4855
Dan Gohman475871a2008-07-27 21:46:04 +00004856 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004857}
4858
Dan Gohman475871a2008-07-27 21:46:04 +00004859SDValue
4860X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004861 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004862 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004863 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004864
Dan Gohman475871a2008-07-27 21:46:04 +00004865 SDValue N0 = Op.getOperand(0);
4866 SDValue N1 = Op.getOperand(1);
4867 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004868
Dan Gohman8a55ce42009-09-23 21:02:20 +00004869 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004870 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004871 unsigned Opc;
4872 if (VT == MVT::v8i16)
4873 Opc = X86ISD::PINSRW;
4874 else if (VT == MVT::v4i16)
4875 Opc = X86ISD::MMX_PINSRW;
4876 else if (VT == MVT::v16i8)
4877 Opc = X86ISD::PINSRB;
4878 else
4879 Opc = X86ISD::PINSRB;
4880
Nate Begeman14d12ca2008-02-11 04:19:36 +00004881 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4882 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004883 if (N1.getValueType() != MVT::i32)
4884 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4885 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004886 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004887 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004888 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004889 // Bits [7:6] of the constant are the source select. This will always be
4890 // zero here. The DAG Combiner may combine an extract_elt index into these
4891 // bits. For example (insert (extract, 3), 2) could be matched by putting
4892 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004893 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004894 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004895 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004896 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004897 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004898 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004900 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004901 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004902 // PINSR* works with constant index.
4903 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004904 }
Dan Gohman475871a2008-07-27 21:46:04 +00004905 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004906}
4907
Dan Gohman475871a2008-07-27 21:46:04 +00004908SDValue
4909X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004910 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004911 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004912
4913 if (Subtarget->hasSSE41())
4914 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4915
Dan Gohman8a55ce42009-09-23 21:02:20 +00004916 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004917 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004918
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004919 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue N0 = Op.getOperand(0);
4921 SDValue N1 = Op.getOperand(1);
4922 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004923
Dan Gohman8a55ce42009-09-23 21:02:20 +00004924 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004925 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4926 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 if (N1.getValueType() != MVT::i32)
4928 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4929 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004930 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004931 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4932 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 }
Dan Gohman475871a2008-07-27 21:46:04 +00004934 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935}
4936
Dan Gohman475871a2008-07-27 21:46:04 +00004937SDValue
4938X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004939 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 if (Op.getValueType() == MVT::v2f32)
4941 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4942 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4943 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004944 Op.getOperand(0))));
4945
Owen Anderson825b72b2009-08-11 20:47:22 +00004946 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4947 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004948
Owen Anderson825b72b2009-08-11 20:47:22 +00004949 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4950 EVT VT = MVT::v2i32;
4951 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004952 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004953 case MVT::v16i8:
4954 case MVT::v8i16:
4955 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004956 break;
4957 }
Dale Johannesenace16102009-02-03 19:33:06 +00004958 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4959 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004960}
4961
Bill Wendling056292f2008-09-16 21:48:12 +00004962// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4963// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4964// one of the above mentioned nodes. It has to be wrapped because otherwise
4965// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4966// be used to form addressing mode. These wrapped nodes will be selected
4967// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004968SDValue
4969X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004970 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004971
Chris Lattner41621a22009-06-26 19:22:52 +00004972 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4973 // global base reg.
4974 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004975 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004976 CodeModel::Model M = getTargetMachine().getCodeModel();
4977
Chris Lattner4f066492009-07-11 20:29:19 +00004978 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004979 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004980 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004981 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004982 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004983 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004984 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004985
Evan Cheng1606e8e2009-03-13 07:51:59 +00004986 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004987 CP->getAlignment(),
4988 CP->getOffset(), OpFlag);
4989 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004990 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004991 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004992 if (OpFlag) {
4993 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004994 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004995 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004996 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004997 }
4998
4999 return Result;
5000}
5001
Chris Lattner18c59872009-06-27 04:16:01 +00005002SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
5003 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005004
Chris Lattner18c59872009-06-27 04:16:01 +00005005 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5006 // global base reg.
5007 unsigned char OpFlag = 0;
5008 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005009 CodeModel::Model M = getTargetMachine().getCodeModel();
5010
Chris Lattner4f066492009-07-11 20:29:19 +00005011 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005012 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005013 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005014 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005015 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005016 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005017 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005018
Chris Lattner18c59872009-06-27 04:16:01 +00005019 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5020 OpFlag);
5021 DebugLoc DL = JT->getDebugLoc();
5022 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005023
Chris Lattner18c59872009-06-27 04:16:01 +00005024 // With PIC, the address is actually $g + Offset.
5025 if (OpFlag) {
5026 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5027 DAG.getNode(X86ISD::GlobalBaseReg,
5028 DebugLoc::getUnknownLoc(), getPointerTy()),
5029 Result);
5030 }
Eric Christopherfd179292009-08-27 18:07:15 +00005031
Chris Lattner18c59872009-06-27 04:16:01 +00005032 return Result;
5033}
5034
5035SDValue
5036X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5037 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005038
Chris Lattner18c59872009-06-27 04:16:01 +00005039 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5040 // global base reg.
5041 unsigned char OpFlag = 0;
5042 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005043 CodeModel::Model M = getTargetMachine().getCodeModel();
5044
Chris Lattner4f066492009-07-11 20:29:19 +00005045 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005046 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005047 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005048 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005049 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005050 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005051 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005052
Chris Lattner18c59872009-06-27 04:16:01 +00005053 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005054
Chris Lattner18c59872009-06-27 04:16:01 +00005055 DebugLoc DL = Op.getDebugLoc();
5056 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005057
5058
Chris Lattner18c59872009-06-27 04:16:01 +00005059 // With PIC, the address is actually $g + Offset.
5060 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005061 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005062 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5063 DAG.getNode(X86ISD::GlobalBaseReg,
5064 DebugLoc::getUnknownLoc(),
5065 getPointerTy()),
5066 Result);
5067 }
Eric Christopherfd179292009-08-27 18:07:15 +00005068
Chris Lattner18c59872009-06-27 04:16:01 +00005069 return Result;
5070}
5071
Dan Gohman475871a2008-07-27 21:46:04 +00005072SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005073X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005074 // Create the TargetBlockAddressAddress node.
5075 unsigned char OpFlags =
5076 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005077 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00005078 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5079 DebugLoc dl = Op.getDebugLoc();
5080 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5081 /*isTarget=*/true, OpFlags);
5082
Dan Gohmanf705adb2009-10-30 01:28:02 +00005083 if (Subtarget->isPICStyleRIPRel() &&
5084 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005085 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5086 else
5087 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005088
Dan Gohman29cbade2009-11-20 23:18:13 +00005089 // With PIC, the address is actually $g + Offset.
5090 if (isGlobalRelativeToPICBase(OpFlags)) {
5091 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5092 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5093 Result);
5094 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005095
5096 return Result;
5097}
5098
5099SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005100X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005101 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005102 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005103 // Create the TargetGlobalAddress node, folding in the constant
5104 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005105 unsigned char OpFlags =
5106 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005107 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005108 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005109 if (OpFlags == X86II::MO_NO_FLAG &&
5110 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005111 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005112 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005113 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005114 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005115 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005116 }
Eric Christopherfd179292009-08-27 18:07:15 +00005117
Chris Lattner4f066492009-07-11 20:29:19 +00005118 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005119 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005120 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5121 else
5122 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005123
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005124 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005125 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005126 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5127 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005128 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005129 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005130
Chris Lattner36c25012009-07-10 07:34:39 +00005131 // For globals that require a load from a stub to get the address, emit the
5132 // load.
5133 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005134 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005135 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005136
Dan Gohman6520e202008-10-18 02:06:02 +00005137 // If there was a non-zero offset that we didn't fold, create an explicit
5138 // addition for it.
5139 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005140 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005141 DAG.getConstant(Offset, getPointerTy()));
5142
Evan Cheng0db9fe62006-04-25 20:13:52 +00005143 return Result;
5144}
5145
Evan Chengda43bcf2008-09-24 00:05:32 +00005146SDValue
5147X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5148 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005149 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005150 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005151}
5152
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005153static SDValue
5154GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005155 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005156 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005157 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005158 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005159 DebugLoc dl = GA->getDebugLoc();
5160 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5161 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005162 GA->getOffset(),
5163 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005164 if (InFlag) {
5165 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005166 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005167 } else {
5168 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005169 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005170 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005171
5172 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5173 MFI->setHasCalls(true);
5174
Rafael Espindola15f1b662009-04-24 12:59:40 +00005175 SDValue Flag = Chain.getValue(1);
5176 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005177}
5178
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005179// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005180static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005181LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005182 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005183 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005184 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5185 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005186 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005187 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005188 PtrVT), InFlag);
5189 InFlag = Chain.getValue(1);
5190
Chris Lattnerb903bed2009-06-26 21:20:29 +00005191 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005192}
5193
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005194// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005195static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005196LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005197 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005198 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5199 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005200}
5201
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005202// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5203// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005204static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005205 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005206 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005207 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005208 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005209 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5210 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005211 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005212 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005213
5214 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005215 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005216
Chris Lattnerb903bed2009-06-26 21:20:29 +00005217 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005218 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5219 // initialexec.
5220 unsigned WrapperKind = X86ISD::Wrapper;
5221 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005222 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005223 } else if (is64Bit) {
5224 assert(model == TLSModel::InitialExec);
5225 OperandFlags = X86II::MO_GOTTPOFF;
5226 WrapperKind = X86ISD::WrapperRIP;
5227 } else {
5228 assert(model == TLSModel::InitialExec);
5229 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005230 }
Eric Christopherfd179292009-08-27 18:07:15 +00005231
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005232 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5233 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005234 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005235 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005236 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005237
Rafael Espindola9a580232009-02-27 13:37:18 +00005238 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005239 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005240 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005241
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005242 // The address of the thread local variable is the add of the thread
5243 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005244 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005245}
5246
Dan Gohman475871a2008-07-27 21:46:04 +00005247SDValue
5248X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005249 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005250 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005251 assert(Subtarget->isTargetELF() &&
5252 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005253 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005254 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005255
Chris Lattnerb903bed2009-06-26 21:20:29 +00005256 // If GV is an alias then use the aliasee for determining
5257 // thread-localness.
5258 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5259 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005260
Chris Lattnerb903bed2009-06-26 21:20:29 +00005261 TLSModel::Model model = getTLSModel(GV,
5262 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005263
Chris Lattnerb903bed2009-06-26 21:20:29 +00005264 switch (model) {
5265 case TLSModel::GeneralDynamic:
5266 case TLSModel::LocalDynamic: // not implemented
5267 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005268 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005269 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005270
Chris Lattnerb903bed2009-06-26 21:20:29 +00005271 case TLSModel::InitialExec:
5272 case TLSModel::LocalExec:
5273 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5274 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005275 }
Eric Christopherfd179292009-08-27 18:07:15 +00005276
Torok Edwinc23197a2009-07-14 16:55:14 +00005277 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005278 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005279}
5280
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005282/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005283/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005284SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005285 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005286 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005287 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005288 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005289 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue ShOpLo = Op.getOperand(0);
5291 SDValue ShOpHi = Op.getOperand(1);
5292 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005293 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005294 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005295 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005296
Dan Gohman475871a2008-07-27 21:46:04 +00005297 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005298 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005299 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5300 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005301 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005302 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5303 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005304 }
Evan Chenge3413162006-01-09 18:33:28 +00005305
Owen Anderson825b72b2009-08-11 20:47:22 +00005306 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5307 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005308 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005309 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005310
Dan Gohman475871a2008-07-27 21:46:04 +00005311 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005313 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5314 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005315
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005316 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005317 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5318 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005319 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005320 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5321 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005322 }
5323
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005325 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005326}
Evan Chenga3195e82006-01-12 22:54:21 +00005327
Dan Gohman475871a2008-07-27 21:46:04 +00005328SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005329 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005330
5331 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005332 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005333 return Op;
5334 }
5335 return SDValue();
5336 }
5337
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005339 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005340
Eli Friedman36df4992009-05-27 00:47:34 +00005341 // These are really Legal; return the operand so the caller accepts it as
5342 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005343 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005344 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005346 Subtarget->is64Bit()) {
5347 return Op;
5348 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005349
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005350 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005351 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005353 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005354 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005355 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005356 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005357 PseudoSourceValue::getFixedStack(SSFI), 0,
5358 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005359 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5360}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005361
Owen Andersone50ed302009-08-10 22:56:29 +00005362SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005363 SDValue StackSlot,
5364 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005366 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005367 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005368 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005369 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005370 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005371 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005372 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005373 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005374 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005375 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005377 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005378 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005379 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005380
5381 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5382 // shouldn't be necessary except that RFP cannot be live across
5383 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005384 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005385 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005387 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005388 SDValue Ops[] = {
5389 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5390 };
5391 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005392 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005393 PseudoSourceValue::getFixedStack(SSFI), 0,
5394 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005395 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005396
Evan Cheng0db9fe62006-04-25 20:13:52 +00005397 return Result;
5398}
5399
Bill Wendling8b8a6362009-01-17 03:56:04 +00005400// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5401SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5402 // This algorithm is not obvious. Here it is in C code, more or less:
5403 /*
5404 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5405 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5406 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005407
Bill Wendling8b8a6362009-01-17 03:56:04 +00005408 // Copy ints to xmm registers.
5409 __m128i xh = _mm_cvtsi32_si128( hi );
5410 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005411
Bill Wendling8b8a6362009-01-17 03:56:04 +00005412 // Combine into low half of a single xmm register.
5413 __m128i x = _mm_unpacklo_epi32( xh, xl );
5414 __m128d d;
5415 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005416
Bill Wendling8b8a6362009-01-17 03:56:04 +00005417 // Merge in appropriate exponents to give the integer bits the right
5418 // magnitude.
5419 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005420
Bill Wendling8b8a6362009-01-17 03:56:04 +00005421 // Subtract away the biases to deal with the IEEE-754 double precision
5422 // implicit 1.
5423 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005424
Bill Wendling8b8a6362009-01-17 03:56:04 +00005425 // All conversions up to here are exact. The correctly rounded result is
5426 // calculated using the current rounding mode using the following
5427 // horizontal add.
5428 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5429 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5430 // store doesn't really need to be here (except
5431 // maybe to zero the other double)
5432 return sd;
5433 }
5434 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005435
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005436 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005437 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005438
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005439 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005440 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005441 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5442 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5443 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5444 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005445 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005446 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005447
Bill Wendling8b8a6362009-01-17 03:56:04 +00005448 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005449 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005450 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005451 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005452 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005453 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005454 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005455
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5457 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005458 Op.getOperand(0),
5459 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005460 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5461 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005462 Op.getOperand(0),
5463 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005464 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5465 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005466 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005467 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005468 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5469 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5470 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005471 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005472 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005473 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005474
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005475 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005476 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005477 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5478 DAG.getUNDEF(MVT::v2f64), ShufMask);
5479 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5480 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005481 DAG.getIntPtrConstant(0));
5482}
5483
Bill Wendling8b8a6362009-01-17 03:56:04 +00005484// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5485SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005486 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005487 // FP constant to bias correct the final result.
5488 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005489 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005490
5491 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005492 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5493 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005494 Op.getOperand(0),
5495 DAG.getIntPtrConstant(0)));
5496
Owen Anderson825b72b2009-08-11 20:47:22 +00005497 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5498 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005499 DAG.getIntPtrConstant(0));
5500
5501 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005502 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5503 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005504 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 MVT::v2f64, Load)),
5506 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005507 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 MVT::v2f64, Bias)));
5509 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5510 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005511 DAG.getIntPtrConstant(0));
5512
5513 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005514 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005515
5516 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005517 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005518
Owen Anderson825b72b2009-08-11 20:47:22 +00005519 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005520 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005521 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005522 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005523 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005524 }
5525
5526 // Handle final rounding.
5527 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005528}
5529
5530SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005531 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005532 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005533
Evan Chenga06ec9e2009-01-19 08:08:22 +00005534 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5535 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5536 // the optimization here.
5537 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005538 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005539
Owen Andersone50ed302009-08-10 22:56:29 +00005540 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005541 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005542 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005543 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005544 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005545
Bill Wendling8b8a6362009-01-17 03:56:04 +00005546 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005547 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005548 return LowerUINT_TO_FP_i32(Op, DAG);
5549 }
5550
Owen Anderson825b72b2009-08-11 20:47:22 +00005551 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005552
5553 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005555 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5556 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5557 getPointerTy(), StackSlot, WordOff);
5558 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005559 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005561 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005563}
5564
Dan Gohman475871a2008-07-27 21:46:04 +00005565std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005566FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005567 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005568
Owen Andersone50ed302009-08-10 22:56:29 +00005569 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005570
5571 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5573 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005574 }
5575
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5577 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005578 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005579
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005580 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005582 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005583 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005584 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005586 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005587 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005588
Evan Cheng87c89352007-10-15 20:11:21 +00005589 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5590 // stack slot.
5591 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005592 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005593 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005594 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005595
Evan Cheng0db9fe62006-04-25 20:13:52 +00005596 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005597 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005598 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005599 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5600 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5601 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005602 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005603
Dan Gohman475871a2008-07-27 21:46:04 +00005604 SDValue Chain = DAG.getEntryNode();
5605 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005606 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005607 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005608 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005609 PseudoSourceValue::getFixedStack(SSFI), 0,
5610 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005612 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005613 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5614 };
Dale Johannesenace16102009-02-03 19:33:06 +00005615 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005616 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005617 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005618 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5619 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005620
Evan Cheng0db9fe62006-04-25 20:13:52 +00005621 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005624
Chris Lattner27a6c732007-11-24 07:07:01 +00005625 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005626}
5627
Dan Gohman475871a2008-07-27 21:46:04 +00005628SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005629 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 if (Op.getValueType() == MVT::v2i32 &&
5631 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005632 return Op;
5633 }
5634 return SDValue();
5635 }
5636
Eli Friedman948e95a2009-05-23 09:59:16 +00005637 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005638 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005639 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5640 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005641
Chris Lattner27a6c732007-11-24 07:07:01 +00005642 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005643 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005644 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005645}
5646
Eli Friedman948e95a2009-05-23 09:59:16 +00005647SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5648 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5649 SDValue FIST = Vals.first, StackSlot = Vals.second;
5650 assert(FIST.getNode() && "Unexpected failure");
5651
5652 // Load the result.
5653 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005654 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005655}
5656
Dan Gohman475871a2008-07-27 21:46:04 +00005657SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005658 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005659 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005660 EVT VT = Op.getValueType();
5661 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005662 if (VT.isVector())
5663 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005664 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005666 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005667 CV.push_back(C);
5668 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005669 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005670 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005671 CV.push_back(C);
5672 CV.push_back(C);
5673 CV.push_back(C);
5674 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005675 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005676 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005677 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005678 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005679 PseudoSourceValue::getConstantPool(), 0,
5680 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005681 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005682}
5683
Dan Gohman475871a2008-07-27 21:46:04 +00005684SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005685 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005686 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005687 EVT VT = Op.getValueType();
5688 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005689 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005690 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005691 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005693 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005694 CV.push_back(C);
5695 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005696 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005697 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005698 CV.push_back(C);
5699 CV.push_back(C);
5700 CV.push_back(C);
5701 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005702 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005703 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005704 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005705 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005706 PseudoSourceValue::getConstantPool(), 0,
5707 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005708 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005709 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005710 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5711 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005712 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005713 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005714 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005715 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005716 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005717}
5718
Dan Gohman475871a2008-07-27 21:46:04 +00005719SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005720 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue Op0 = Op.getOperand(0);
5722 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005723 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005724 EVT VT = Op.getValueType();
5725 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005726
5727 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005728 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005729 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005730 SrcVT = VT;
5731 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005732 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005733 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005734 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005735 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005736 }
5737
5738 // At this point the operands and the result should have the same
5739 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005740
Evan Cheng68c47cb2007-01-05 07:55:56 +00005741 // First get the sign bit of second operand.
5742 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005744 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5745 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005746 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005747 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5748 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5749 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5750 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005751 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005752 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005753 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005754 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005755 PseudoSourceValue::getConstantPool(), 0,
5756 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005757 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005758
5759 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005760 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005761 // Op0 is MVT::f32, Op1 is MVT::f64.
5762 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5763 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5764 DAG.getConstant(32, MVT::i32));
5765 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5766 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005767 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005768 }
5769
Evan Cheng73d6cf12007-01-05 21:37:56 +00005770 // Clear first operand sign bit.
5771 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005772 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005773 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5774 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005775 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005776 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5777 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5778 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5779 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005780 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005781 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005782 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005783 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005784 PseudoSourceValue::getConstantPool(), 0,
5785 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005786 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005787
5788 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005789 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005790}
5791
Dan Gohman076aee32009-03-04 19:44:21 +00005792/// Emit nodes that will be selected as "test Op0,Op0", or something
5793/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005794SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5795 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005796 DebugLoc dl = Op.getDebugLoc();
5797
Dan Gohman31125812009-03-07 01:58:32 +00005798 // CF and OF aren't always set the way we want. Determine which
5799 // of these we need.
5800 bool NeedCF = false;
5801 bool NeedOF = false;
5802 switch (X86CC) {
5803 case X86::COND_A: case X86::COND_AE:
5804 case X86::COND_B: case X86::COND_BE:
5805 NeedCF = true;
5806 break;
5807 case X86::COND_G: case X86::COND_GE:
5808 case X86::COND_L: case X86::COND_LE:
5809 case X86::COND_O: case X86::COND_NO:
5810 NeedOF = true;
5811 break;
5812 default: break;
5813 }
5814
Dan Gohman076aee32009-03-04 19:44:21 +00005815 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005816 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5817 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5818 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005819 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005820 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005821 switch (Op.getNode()->getOpcode()) {
5822 case ISD::ADD:
5823 // Due to an isel shortcoming, be conservative if this add is likely to
5824 // be selected as part of a load-modify-store instruction. When the root
5825 // node in a match is a store, isel doesn't know how to remap non-chain
5826 // non-flag uses of other nodes in the match, such as the ADD in this
5827 // case. This leads to the ADD being left around and reselected, with
5828 // the result being two adds in the output.
5829 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5830 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5831 if (UI->getOpcode() == ISD::STORE)
5832 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005833 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005834 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5835 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005836 if (C->getAPIntValue() == 1) {
5837 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005838 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005839 break;
5840 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005841 // An add of negative one (subtract of one) will be selected as a DEC.
5842 if (C->getAPIntValue().isAllOnesValue()) {
5843 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005844 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005845 break;
5846 }
5847 }
Dan Gohman076aee32009-03-04 19:44:21 +00005848 // Otherwise use a regular EFLAGS-setting add.
5849 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005850 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005851 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005852 case ISD::AND: {
5853 // If the primary and result isn't used, don't bother using X86ISD::AND,
5854 // because a TEST instruction will be better.
5855 bool NonFlagUse = false;
5856 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005857 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5858 SDNode *User = *UI;
5859 unsigned UOpNo = UI.getOperandNo();
5860 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5861 // Look pass truncate.
5862 UOpNo = User->use_begin().getOperandNo();
5863 User = *User->use_begin();
5864 }
5865 if (User->getOpcode() != ISD::BRCOND &&
5866 User->getOpcode() != ISD::SETCC &&
5867 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005868 NonFlagUse = true;
5869 break;
5870 }
Evan Cheng17751da2010-01-07 00:54:06 +00005871 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005872 if (!NonFlagUse)
5873 break;
5874 }
5875 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005876 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005877 case ISD::OR:
5878 case ISD::XOR:
5879 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005880 // likely to be selected as part of a load-modify-store instruction.
5881 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5882 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5883 if (UI->getOpcode() == ISD::STORE)
5884 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005885 // Otherwise use a regular EFLAGS-setting instruction.
5886 switch (Op.getNode()->getOpcode()) {
5887 case ISD::SUB: Opcode = X86ISD::SUB; break;
5888 case ISD::OR: Opcode = X86ISD::OR; break;
5889 case ISD::XOR: Opcode = X86ISD::XOR; break;
5890 case ISD::AND: Opcode = X86ISD::AND; break;
5891 default: llvm_unreachable("unexpected operator!");
5892 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005893 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005894 break;
5895 case X86ISD::ADD:
5896 case X86ISD::SUB:
5897 case X86ISD::INC:
5898 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005899 case X86ISD::OR:
5900 case X86ISD::XOR:
5901 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005902 return SDValue(Op.getNode(), 1);
5903 default:
5904 default_case:
5905 break;
5906 }
5907 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005908 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005909 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005910 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005911 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005912 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005913 DAG.ReplaceAllUsesWith(Op, New);
5914 return SDValue(New.getNode(), 1);
5915 }
5916 }
5917
5918 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005919 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005920 DAG.getConstant(0, Op.getValueType()));
5921}
5922
5923/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5924/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005925SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5926 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005927 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5928 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005929 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005930
5931 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005932 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005933}
5934
Evan Chengd40d03e2010-01-06 19:38:29 +00005935/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5936/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00005937static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005938 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005939 SDValue Op0 = And.getOperand(0);
5940 SDValue Op1 = And.getOperand(1);
5941 if (Op0.getOpcode() == ISD::TRUNCATE)
5942 Op0 = Op0.getOperand(0);
5943 if (Op1.getOpcode() == ISD::TRUNCATE)
5944 Op1 = Op1.getOperand(0);
5945
Evan Chengd40d03e2010-01-06 19:38:29 +00005946 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00005947 if (Op1.getOpcode() == ISD::SHL) {
5948 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
5949 if (And10C->getZExtValue() == 1) {
5950 LHS = Op0;
5951 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005952 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005953 } else if (Op0.getOpcode() == ISD::SHL) {
5954 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
5955 if (And00C->getZExtValue() == 1) {
5956 LHS = Op1;
5957 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00005958 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005959 } else if (Op1.getOpcode() == ISD::Constant) {
5960 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
5961 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00005962 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5963 LHS = AndLHS.getOperand(0);
5964 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005965 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005966 }
Evan Cheng0488db92007-09-25 01:57:46 +00005967
Evan Chengd40d03e2010-01-06 19:38:29 +00005968 if (LHS.getNode()) {
5969 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5970 // instruction. Since the shift amount is in-range-or-undefined, we know
5971 // that doing a bittest on the i16 value is ok. We extend to i32 because
5972 // the encoding for the i16 version is larger than the i32 version.
5973 if (LHS.getValueType() == MVT::i8)
5974 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005975
Evan Chengd40d03e2010-01-06 19:38:29 +00005976 // If the operand types disagree, extend the shift amount to match. Since
5977 // BT ignores high bits (like shifts) we can use anyextend.
5978 if (LHS.getValueType() != RHS.getValueType())
5979 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005980
Evan Chengd40d03e2010-01-06 19:38:29 +00005981 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5982 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5983 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5984 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005985 }
5986
Evan Cheng54de3ea2010-01-05 06:52:31 +00005987 return SDValue();
5988}
5989
5990SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5991 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5992 SDValue Op0 = Op.getOperand(0);
5993 SDValue Op1 = Op.getOperand(1);
5994 DebugLoc dl = Op.getDebugLoc();
5995 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5996
5997 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005998 // Lower (X & (1 << N)) == 0 to BT(X, N).
5999 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6000 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6001 if (Op0.getOpcode() == ISD::AND &&
6002 Op0.hasOneUse() &&
6003 Op1.getOpcode() == ISD::Constant &&
6004 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6005 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6006 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6007 if (NewSetCC.getNode())
6008 return NewSetCC;
6009 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006010
Evan Cheng2c755ba2010-02-27 07:36:59 +00006011 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6012 if (Op0.getOpcode() == X86ISD::SETCC &&
6013 Op1.getOpcode() == ISD::Constant &&
6014 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6015 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6016 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6017 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6018 bool Invert = (CC == ISD::SETNE) ^
6019 cast<ConstantSDNode>(Op1)->isNullValue();
6020 if (Invert)
6021 CCode = X86::GetOppositeBranchCondition(CCode);
6022 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6023 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6024 }
6025
Chris Lattnere55484e2008-12-25 05:34:37 +00006026 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
6027 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006028 if (X86CC == X86::COND_INVALID)
6029 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006030
Dan Gohman31125812009-03-07 01:58:32 +00006031 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006032
6033 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006034 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006035 return DAG.getNode(ISD::AND, dl, MVT::i8,
6036 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6037 DAG.getConstant(X86CC, MVT::i8), Cond),
6038 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006039
Owen Anderson825b72b2009-08-11 20:47:22 +00006040 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6041 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006042}
6043
Dan Gohman475871a2008-07-27 21:46:04 +00006044SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6045 SDValue Cond;
6046 SDValue Op0 = Op.getOperand(0);
6047 SDValue Op1 = Op.getOperand(1);
6048 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006049 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006050 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6051 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006052 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006053
6054 if (isFP) {
6055 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006056 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6058 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006059 bool Swap = false;
6060
6061 switch (SetCCOpcode) {
6062 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006063 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006064 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006065 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006066 case ISD::SETGT: Swap = true; // Fallthrough
6067 case ISD::SETLT:
6068 case ISD::SETOLT: SSECC = 1; break;
6069 case ISD::SETOGE:
6070 case ISD::SETGE: Swap = true; // Fallthrough
6071 case ISD::SETLE:
6072 case ISD::SETOLE: SSECC = 2; break;
6073 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006074 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006075 case ISD::SETNE: SSECC = 4; break;
6076 case ISD::SETULE: Swap = true;
6077 case ISD::SETUGE: SSECC = 5; break;
6078 case ISD::SETULT: Swap = true;
6079 case ISD::SETUGT: SSECC = 6; break;
6080 case ISD::SETO: SSECC = 7; break;
6081 }
6082 if (Swap)
6083 std::swap(Op0, Op1);
6084
Nate Begemanfb8ead02008-07-25 19:05:58 +00006085 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006086 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006087 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006088 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006089 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6090 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006091 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006092 }
6093 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006094 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006095 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6096 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006097 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006098 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006099 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006100 }
6101 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006102 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006103 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006104
Nate Begeman30a0de92008-07-17 16:51:19 +00006105 // We are handling one of the integer comparisons here. Since SSE only has
6106 // GT and EQ comparisons for integer, swapping operands and multiple
6107 // operations may be required for some comparisons.
6108 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6109 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006110
Owen Anderson825b72b2009-08-11 20:47:22 +00006111 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006112 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006113 case MVT::v8i8:
6114 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6115 case MVT::v4i16:
6116 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6117 case MVT::v2i32:
6118 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6119 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006120 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006121
Nate Begeman30a0de92008-07-17 16:51:19 +00006122 switch (SetCCOpcode) {
6123 default: break;
6124 case ISD::SETNE: Invert = true;
6125 case ISD::SETEQ: Opc = EQOpc; break;
6126 case ISD::SETLT: Swap = true;
6127 case ISD::SETGT: Opc = GTOpc; break;
6128 case ISD::SETGE: Swap = true;
6129 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6130 case ISD::SETULT: Swap = true;
6131 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6132 case ISD::SETUGE: Swap = true;
6133 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6134 }
6135 if (Swap)
6136 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006137
Nate Begeman30a0de92008-07-17 16:51:19 +00006138 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6139 // bits of the inputs before performing those operations.
6140 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006142 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6143 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006144 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006145 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6146 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006147 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6148 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006150
Dale Johannesenace16102009-02-03 19:33:06 +00006151 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006152
6153 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006154 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006155 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006156
Nate Begeman30a0de92008-07-17 16:51:19 +00006157 return Result;
6158}
Evan Cheng0488db92007-09-25 01:57:46 +00006159
Evan Cheng370e5342008-12-03 08:38:43 +00006160// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006161static bool isX86LogicalCmp(SDValue Op) {
6162 unsigned Opc = Op.getNode()->getOpcode();
6163 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6164 return true;
6165 if (Op.getResNo() == 1 &&
6166 (Opc == X86ISD::ADD ||
6167 Opc == X86ISD::SUB ||
6168 Opc == X86ISD::SMUL ||
6169 Opc == X86ISD::UMUL ||
6170 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006171 Opc == X86ISD::DEC ||
6172 Opc == X86ISD::OR ||
6173 Opc == X86ISD::XOR ||
6174 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006175 return true;
6176
6177 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006178}
6179
Dan Gohman475871a2008-07-27 21:46:04 +00006180SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006181 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006182 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006183 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006184 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006185
Dan Gohman1a492952009-10-20 16:22:37 +00006186 if (Cond.getOpcode() == ISD::SETCC) {
6187 SDValue NewCond = LowerSETCC(Cond, DAG);
6188 if (NewCond.getNode())
6189 Cond = NewCond;
6190 }
Evan Cheng734503b2006-09-11 02:19:56 +00006191
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006192 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6193 SDValue Op1 = Op.getOperand(1);
6194 SDValue Op2 = Op.getOperand(2);
6195 if (Cond.getOpcode() == X86ISD::SETCC &&
6196 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6197 SDValue Cmp = Cond.getOperand(1);
6198 if (Cmp.getOpcode() == X86ISD::CMP) {
6199 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6200 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6201 ConstantSDNode *RHSC =
6202 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6203 if (N1C && N1C->isAllOnesValue() &&
6204 N2C && N2C->isNullValue() &&
6205 RHSC && RHSC->isNullValue()) {
6206 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006207 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006208 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6209 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6210 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6211 }
6212 }
6213 }
6214
Evan Chengad9c0a32009-12-15 00:53:42 +00006215 // Look pass (and (setcc_carry (cmp ...)), 1).
6216 if (Cond.getOpcode() == ISD::AND &&
6217 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6218 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6219 if (C && C->getAPIntValue() == 1)
6220 Cond = Cond.getOperand(0);
6221 }
6222
Evan Cheng3f41d662007-10-08 22:16:29 +00006223 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6224 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006225 if (Cond.getOpcode() == X86ISD::SETCC ||
6226 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006227 CC = Cond.getOperand(0);
6228
Dan Gohman475871a2008-07-27 21:46:04 +00006229 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006230 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006231 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006232
Evan Cheng3f41d662007-10-08 22:16:29 +00006233 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006234 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006235 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006236 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006237
Chris Lattnerd1980a52009-03-12 06:52:53 +00006238 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6239 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006240 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006241 addTest = false;
6242 }
6243 }
6244
6245 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006246 // Look pass the truncate.
6247 if (Cond.getOpcode() == ISD::TRUNCATE)
6248 Cond = Cond.getOperand(0);
6249
6250 // We know the result of AND is compared against zero. Try to match
6251 // it to BT.
6252 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6253 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6254 if (NewSetCC.getNode()) {
6255 CC = NewSetCC.getOperand(0);
6256 Cond = NewSetCC.getOperand(1);
6257 addTest = false;
6258 }
6259 }
6260 }
6261
6262 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006263 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006264 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006265 }
6266
Evan Cheng0488db92007-09-25 01:57:46 +00006267 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6268 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006269 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6270 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006271 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006272}
6273
Evan Cheng370e5342008-12-03 08:38:43 +00006274// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6275// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6276// from the AND / OR.
6277static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6278 Opc = Op.getOpcode();
6279 if (Opc != ISD::OR && Opc != ISD::AND)
6280 return false;
6281 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6282 Op.getOperand(0).hasOneUse() &&
6283 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6284 Op.getOperand(1).hasOneUse());
6285}
6286
Evan Cheng961d6d42009-02-02 08:19:07 +00006287// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6288// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006289static bool isXor1OfSetCC(SDValue Op) {
6290 if (Op.getOpcode() != ISD::XOR)
6291 return false;
6292 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6293 if (N1C && N1C->getAPIntValue() == 1) {
6294 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6295 Op.getOperand(0).hasOneUse();
6296 }
6297 return false;
6298}
6299
Dan Gohman475871a2008-07-27 21:46:04 +00006300SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006301 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006302 SDValue Chain = Op.getOperand(0);
6303 SDValue Cond = Op.getOperand(1);
6304 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006305 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006306 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006307
Dan Gohman1a492952009-10-20 16:22:37 +00006308 if (Cond.getOpcode() == ISD::SETCC) {
6309 SDValue NewCond = LowerSETCC(Cond, DAG);
6310 if (NewCond.getNode())
6311 Cond = NewCond;
6312 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006313#if 0
6314 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006315 else if (Cond.getOpcode() == X86ISD::ADD ||
6316 Cond.getOpcode() == X86ISD::SUB ||
6317 Cond.getOpcode() == X86ISD::SMUL ||
6318 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006319 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006320#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006321
Evan Chengad9c0a32009-12-15 00:53:42 +00006322 // Look pass (and (setcc_carry (cmp ...)), 1).
6323 if (Cond.getOpcode() == ISD::AND &&
6324 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6325 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6326 if (C && C->getAPIntValue() == 1)
6327 Cond = Cond.getOperand(0);
6328 }
6329
Evan Cheng3f41d662007-10-08 22:16:29 +00006330 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6331 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006332 if (Cond.getOpcode() == X86ISD::SETCC ||
6333 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006334 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006335
Dan Gohman475871a2008-07-27 21:46:04 +00006336 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006337 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006338 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006339 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006340 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006341 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006342 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006343 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006344 default: break;
6345 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006346 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006347 // These can only come from an arithmetic instruction with overflow,
6348 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006349 Cond = Cond.getNode()->getOperand(1);
6350 addTest = false;
6351 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006352 }
Evan Cheng0488db92007-09-25 01:57:46 +00006353 }
Evan Cheng370e5342008-12-03 08:38:43 +00006354 } else {
6355 unsigned CondOpc;
6356 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6357 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006358 if (CondOpc == ISD::OR) {
6359 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6360 // two branches instead of an explicit OR instruction with a
6361 // separate test.
6362 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006363 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006364 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006365 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006366 Chain, Dest, CC, Cmp);
6367 CC = Cond.getOperand(1).getOperand(0);
6368 Cond = Cmp;
6369 addTest = false;
6370 }
6371 } else { // ISD::AND
6372 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6373 // two branches instead of an explicit AND instruction with a
6374 // separate test. However, we only do this if this block doesn't
6375 // have a fall-through edge, because this requires an explicit
6376 // jmp when the condition is false.
6377 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006378 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006379 Op.getNode()->hasOneUse()) {
6380 X86::CondCode CCode =
6381 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6382 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006383 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006384 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6385 // Look for an unconditional branch following this conditional branch.
6386 // We need this because we need to reverse the successors in order
6387 // to implement FCMP_OEQ.
6388 if (User.getOpcode() == ISD::BR) {
6389 SDValue FalseBB = User.getOperand(1);
6390 SDValue NewBR =
6391 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6392 assert(NewBR == User);
6393 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006394
Dale Johannesene4d209d2009-02-03 20:21:25 +00006395 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006396 Chain, Dest, CC, Cmp);
6397 X86::CondCode CCode =
6398 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6399 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006401 Cond = Cmp;
6402 addTest = false;
6403 }
6404 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006405 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006406 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6407 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6408 // It should be transformed during dag combiner except when the condition
6409 // is set by a arithmetics with overflow node.
6410 X86::CondCode CCode =
6411 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6412 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006413 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006414 Cond = Cond.getOperand(0).getOperand(1);
6415 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006416 }
Evan Cheng0488db92007-09-25 01:57:46 +00006417 }
6418
6419 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006420 // Look pass the truncate.
6421 if (Cond.getOpcode() == ISD::TRUNCATE)
6422 Cond = Cond.getOperand(0);
6423
6424 // We know the result of AND is compared against zero. Try to match
6425 // it to BT.
6426 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6427 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6428 if (NewSetCC.getNode()) {
6429 CC = NewSetCC.getOperand(0);
6430 Cond = NewSetCC.getOperand(1);
6431 addTest = false;
6432 }
6433 }
6434 }
6435
6436 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006437 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006438 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006439 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006440 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006441 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006442}
6443
Anton Korobeynikove060b532007-04-17 19:34:00 +00006444
6445// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6446// Calls to _alloca is needed to probe the stack when allocating more than 4k
6447// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6448// that the guard pages used by the OS virtual memory manager are allocated in
6449// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006450SDValue
6451X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006452 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006453 assert(Subtarget->isTargetCygMing() &&
6454 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006455 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006456
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006457 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006458 SDValue Chain = Op.getOperand(0);
6459 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006460 // FIXME: Ensure alignment here
6461
Dan Gohman475871a2008-07-27 21:46:04 +00006462 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006463
Owen Andersone50ed302009-08-10 22:56:29 +00006464 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006465 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006466
Dale Johannesendd64c412009-02-04 00:33:20 +00006467 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006468 Flag = Chain.getValue(1);
6469
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006470 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006471
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006472 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6473 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006474
Dale Johannesendd64c412009-02-04 00:33:20 +00006475 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006476
Dan Gohman475871a2008-07-27 21:46:04 +00006477 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006478 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006479}
6480
Dan Gohman475871a2008-07-27 21:46:04 +00006481SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006482X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006483 SDValue Chain,
6484 SDValue Dst, SDValue Src,
6485 SDValue Size, unsigned Align,
6486 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006487 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006488 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006489
Bill Wendling6f287b22008-09-30 21:22:07 +00006490 // If not DWORD aligned or size is more than the threshold, call the library.
6491 // The libc version is likely to be faster for these cases. It can use the
6492 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006493 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006494 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006495 ConstantSize->getZExtValue() >
6496 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006497 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006498
6499 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006500 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006501
Bill Wendling6158d842008-10-01 00:59:58 +00006502 if (const char *bzeroEntry = V &&
6503 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006504 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006505 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006506 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006507 TargetLowering::ArgListEntry Entry;
6508 Entry.Node = Dst;
6509 Entry.Ty = IntPtrTy;
6510 Args.push_back(Entry);
6511 Entry.Node = Size;
6512 Args.push_back(Entry);
6513 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006514 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6515 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006516 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006517 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006518 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006519 }
6520
Dan Gohman707e0182008-04-12 04:36:06 +00006521 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006522 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006523 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006524
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006525 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006526 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006527 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006528 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006529 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006530 unsigned BytesLeft = 0;
6531 bool TwoRepStos = false;
6532 if (ValC) {
6533 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006534 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006535
Evan Cheng0db9fe62006-04-25 20:13:52 +00006536 // If the value is a constant, then we can potentially use larger sets.
6537 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006538 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006539 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006540 ValReg = X86::AX;
6541 Val = (Val << 8) | Val;
6542 break;
6543 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006544 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006545 ValReg = X86::EAX;
6546 Val = (Val << 8) | Val;
6547 Val = (Val << 16) | Val;
6548 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006549 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006550 ValReg = X86::RAX;
6551 Val = (Val << 32) | Val;
6552 }
6553 break;
6554 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006555 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006556 ValReg = X86::AL;
6557 Count = DAG.getIntPtrConstant(SizeVal);
6558 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006559 }
6560
Owen Anderson825b72b2009-08-11 20:47:22 +00006561 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006562 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006563 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6564 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006565 }
6566
Dale Johannesen0f502f62009-02-03 22:26:09 +00006567 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006568 InFlag);
6569 InFlag = Chain.getValue(1);
6570 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006571 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006572 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006573 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006574 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006575 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006576
Scott Michelfdc40a02009-02-17 22:15:04 +00006577 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006578 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006579 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006580 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006581 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006582 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006583 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006584 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006585
Owen Anderson825b72b2009-08-11 20:47:22 +00006586 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006587 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6588 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006589
Evan Cheng0db9fe62006-04-25 20:13:52 +00006590 if (TwoRepStos) {
6591 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006592 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006593 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006594 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006595 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6596 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006597 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006598 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006599 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006600 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006601 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6602 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006603 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006604 // Handle the last 1 - 7 bytes.
6605 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006606 EVT AddrVT = Dst.getValueType();
6607 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006608
Dale Johannesen0f502f62009-02-03 22:26:09 +00006609 Chain = DAG.getMemset(Chain, dl,
6610 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006611 DAG.getConstant(Offset, AddrVT)),
6612 Src,
6613 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006614 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006615 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006616
Dan Gohman707e0182008-04-12 04:36:06 +00006617 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618 return Chain;
6619}
Evan Cheng11e15b32006-04-03 20:53:28 +00006620
Dan Gohman475871a2008-07-27 21:46:04 +00006621SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006622X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006623 SDValue Chain, SDValue Dst, SDValue Src,
6624 SDValue Size, unsigned Align,
6625 bool AlwaysInline,
6626 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006627 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006628 // This requires the copy size to be a constant, preferrably
6629 // within a subtarget-specific limit.
6630 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6631 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006632 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006633 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006634 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006635 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006636
Evan Cheng1887c1c2008-08-21 21:00:15 +00006637 /// If not DWORD aligned, call the library.
6638 if ((Align & 3) != 0)
6639 return SDValue();
6640
6641 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006643 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006645
Duncan Sands83ec4b62008-06-06 12:08:01 +00006646 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006647 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006648 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006649 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006650
Dan Gohman475871a2008-07-27 21:46:04 +00006651 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006652 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006653 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006654 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006655 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006656 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006657 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006658 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006659 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006660 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006661 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006662 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663 InFlag = Chain.getValue(1);
6664
Owen Anderson825b72b2009-08-11 20:47:22 +00006665 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006666 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6667 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6668 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006669
Dan Gohman475871a2008-07-27 21:46:04 +00006670 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006671 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006672 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006673 // Handle the last 1 - 7 bytes.
6674 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006675 EVT DstVT = Dst.getValueType();
6676 EVT SrcVT = Src.getValueType();
6677 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006678 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006679 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006680 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006681 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006682 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006683 DAG.getConstant(BytesLeft, SizeVT),
6684 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006685 DstSV, DstSVOff + Offset,
6686 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006687 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006688
Owen Anderson825b72b2009-08-11 20:47:22 +00006689 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006690 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006691}
6692
Dan Gohman475871a2008-07-27 21:46:04 +00006693SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006694 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006695 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006696
Evan Cheng25ab6902006-09-08 06:48:29 +00006697 if (!Subtarget->is64Bit()) {
6698 // vastart just stores the address of the VarArgsFrameIndex slot into the
6699 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006700 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006701 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6702 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006703 }
6704
6705 // __va_list_tag:
6706 // gp_offset (0 - 6 * 8)
6707 // fp_offset (48 - 48 + 8 * 16)
6708 // overflow_arg_area (point to parameters coming in memory).
6709 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SmallVector<SDValue, 8> MemOps;
6711 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006712 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006713 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene67c9d422010-02-15 16:53:33 +00006714 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6715 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006716 MemOps.push_back(Store);
6717
6718 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006719 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006720 FIN, DAG.getIntPtrConstant(4));
6721 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006722 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006723 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006724 MemOps.push_back(Store);
6725
6726 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006727 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006728 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006729 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006730 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6731 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006732 MemOps.push_back(Store);
6733
6734 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006735 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006736 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006737 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006738 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6739 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006740 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006741 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006742 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006743}
6744
Dan Gohman475871a2008-07-27 21:46:04 +00006745SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006746 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6747 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue Chain = Op.getOperand(0);
6749 SDValue SrcPtr = Op.getOperand(1);
6750 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006751
Torok Edwindac237e2009-07-08 20:53:28 +00006752 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006753 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006754}
6755
Dan Gohman475871a2008-07-27 21:46:04 +00006756SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006757 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006758 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006759 SDValue Chain = Op.getOperand(0);
6760 SDValue DstPtr = Op.getOperand(1);
6761 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006762 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6763 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006764 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006765
Dale Johannesendd64c412009-02-04 00:33:20 +00006766 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006767 DAG.getIntPtrConstant(24), 8, false,
6768 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006769}
6770
Dan Gohman475871a2008-07-27 21:46:04 +00006771SDValue
6772X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006773 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006774 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006775 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006776 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006777 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006778 case Intrinsic::x86_sse_comieq_ss:
6779 case Intrinsic::x86_sse_comilt_ss:
6780 case Intrinsic::x86_sse_comile_ss:
6781 case Intrinsic::x86_sse_comigt_ss:
6782 case Intrinsic::x86_sse_comige_ss:
6783 case Intrinsic::x86_sse_comineq_ss:
6784 case Intrinsic::x86_sse_ucomieq_ss:
6785 case Intrinsic::x86_sse_ucomilt_ss:
6786 case Intrinsic::x86_sse_ucomile_ss:
6787 case Intrinsic::x86_sse_ucomigt_ss:
6788 case Intrinsic::x86_sse_ucomige_ss:
6789 case Intrinsic::x86_sse_ucomineq_ss:
6790 case Intrinsic::x86_sse2_comieq_sd:
6791 case Intrinsic::x86_sse2_comilt_sd:
6792 case Intrinsic::x86_sse2_comile_sd:
6793 case Intrinsic::x86_sse2_comigt_sd:
6794 case Intrinsic::x86_sse2_comige_sd:
6795 case Intrinsic::x86_sse2_comineq_sd:
6796 case Intrinsic::x86_sse2_ucomieq_sd:
6797 case Intrinsic::x86_sse2_ucomilt_sd:
6798 case Intrinsic::x86_sse2_ucomile_sd:
6799 case Intrinsic::x86_sse2_ucomigt_sd:
6800 case Intrinsic::x86_sse2_ucomige_sd:
6801 case Intrinsic::x86_sse2_ucomineq_sd: {
6802 unsigned Opc = 0;
6803 ISD::CondCode CC = ISD::SETCC_INVALID;
6804 switch (IntNo) {
6805 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006806 case Intrinsic::x86_sse_comieq_ss:
6807 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006808 Opc = X86ISD::COMI;
6809 CC = ISD::SETEQ;
6810 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006811 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006812 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006813 Opc = X86ISD::COMI;
6814 CC = ISD::SETLT;
6815 break;
6816 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006817 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006818 Opc = X86ISD::COMI;
6819 CC = ISD::SETLE;
6820 break;
6821 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006822 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823 Opc = X86ISD::COMI;
6824 CC = ISD::SETGT;
6825 break;
6826 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006827 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006828 Opc = X86ISD::COMI;
6829 CC = ISD::SETGE;
6830 break;
6831 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006832 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006833 Opc = X86ISD::COMI;
6834 CC = ISD::SETNE;
6835 break;
6836 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006837 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006838 Opc = X86ISD::UCOMI;
6839 CC = ISD::SETEQ;
6840 break;
6841 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006842 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006843 Opc = X86ISD::UCOMI;
6844 CC = ISD::SETLT;
6845 break;
6846 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006847 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006848 Opc = X86ISD::UCOMI;
6849 CC = ISD::SETLE;
6850 break;
6851 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006852 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006853 Opc = X86ISD::UCOMI;
6854 CC = ISD::SETGT;
6855 break;
6856 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006857 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006858 Opc = X86ISD::UCOMI;
6859 CC = ISD::SETGE;
6860 break;
6861 case Intrinsic::x86_sse_ucomineq_ss:
6862 case Intrinsic::x86_sse2_ucomineq_sd:
6863 Opc = X86ISD::UCOMI;
6864 CC = ISD::SETNE;
6865 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006866 }
Evan Cheng734503b2006-09-11 02:19:56 +00006867
Dan Gohman475871a2008-07-27 21:46:04 +00006868 SDValue LHS = Op.getOperand(1);
6869 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006870 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006871 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006872 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6873 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6874 DAG.getConstant(X86CC, MVT::i8), Cond);
6875 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006876 }
Eric Christopher71c67532009-07-29 00:28:05 +00006877 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006878 // an integer value, not just an instruction so lower it to the ptest
6879 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006880 case Intrinsic::x86_sse41_ptestz:
6881 case Intrinsic::x86_sse41_ptestc:
6882 case Intrinsic::x86_sse41_ptestnzc:{
6883 unsigned X86CC = 0;
6884 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006885 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006886 case Intrinsic::x86_sse41_ptestz:
6887 // ZF = 1
6888 X86CC = X86::COND_E;
6889 break;
6890 case Intrinsic::x86_sse41_ptestc:
6891 // CF = 1
6892 X86CC = X86::COND_B;
6893 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006894 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006895 // ZF and CF = 0
6896 X86CC = X86::COND_A;
6897 break;
6898 }
Eric Christopherfd179292009-08-27 18:07:15 +00006899
Eric Christopher71c67532009-07-29 00:28:05 +00006900 SDValue LHS = Op.getOperand(1);
6901 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006902 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6903 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6904 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6905 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006906 }
Evan Cheng5759f972008-05-04 09:15:50 +00006907
6908 // Fix vector shift instructions where the last operand is a non-immediate
6909 // i32 value.
6910 case Intrinsic::x86_sse2_pslli_w:
6911 case Intrinsic::x86_sse2_pslli_d:
6912 case Intrinsic::x86_sse2_pslli_q:
6913 case Intrinsic::x86_sse2_psrli_w:
6914 case Intrinsic::x86_sse2_psrli_d:
6915 case Intrinsic::x86_sse2_psrli_q:
6916 case Intrinsic::x86_sse2_psrai_w:
6917 case Intrinsic::x86_sse2_psrai_d:
6918 case Intrinsic::x86_mmx_pslli_w:
6919 case Intrinsic::x86_mmx_pslli_d:
6920 case Intrinsic::x86_mmx_pslli_q:
6921 case Intrinsic::x86_mmx_psrli_w:
6922 case Intrinsic::x86_mmx_psrli_d:
6923 case Intrinsic::x86_mmx_psrli_q:
6924 case Intrinsic::x86_mmx_psrai_w:
6925 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006926 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006927 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006928 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006929
6930 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006932 switch (IntNo) {
6933 case Intrinsic::x86_sse2_pslli_w:
6934 NewIntNo = Intrinsic::x86_sse2_psll_w;
6935 break;
6936 case Intrinsic::x86_sse2_pslli_d:
6937 NewIntNo = Intrinsic::x86_sse2_psll_d;
6938 break;
6939 case Intrinsic::x86_sse2_pslli_q:
6940 NewIntNo = Intrinsic::x86_sse2_psll_q;
6941 break;
6942 case Intrinsic::x86_sse2_psrli_w:
6943 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6944 break;
6945 case Intrinsic::x86_sse2_psrli_d:
6946 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6947 break;
6948 case Intrinsic::x86_sse2_psrli_q:
6949 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6950 break;
6951 case Intrinsic::x86_sse2_psrai_w:
6952 NewIntNo = Intrinsic::x86_sse2_psra_w;
6953 break;
6954 case Intrinsic::x86_sse2_psrai_d:
6955 NewIntNo = Intrinsic::x86_sse2_psra_d;
6956 break;
6957 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006958 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006959 switch (IntNo) {
6960 case Intrinsic::x86_mmx_pslli_w:
6961 NewIntNo = Intrinsic::x86_mmx_psll_w;
6962 break;
6963 case Intrinsic::x86_mmx_pslli_d:
6964 NewIntNo = Intrinsic::x86_mmx_psll_d;
6965 break;
6966 case Intrinsic::x86_mmx_pslli_q:
6967 NewIntNo = Intrinsic::x86_mmx_psll_q;
6968 break;
6969 case Intrinsic::x86_mmx_psrli_w:
6970 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6971 break;
6972 case Intrinsic::x86_mmx_psrli_d:
6973 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6974 break;
6975 case Intrinsic::x86_mmx_psrli_q:
6976 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6977 break;
6978 case Intrinsic::x86_mmx_psrai_w:
6979 NewIntNo = Intrinsic::x86_mmx_psra_w;
6980 break;
6981 case Intrinsic::x86_mmx_psrai_d:
6982 NewIntNo = Intrinsic::x86_mmx_psra_d;
6983 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006984 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006985 }
6986 break;
6987 }
6988 }
Mon P Wangefa42202009-09-03 19:56:25 +00006989
6990 // The vector shift intrinsics with scalars uses 32b shift amounts but
6991 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6992 // to be zero.
6993 SDValue ShOps[4];
6994 ShOps[0] = ShAmt;
6995 ShOps[1] = DAG.getConstant(0, MVT::i32);
6996 if (ShAmtVT == MVT::v4i32) {
6997 ShOps[2] = DAG.getUNDEF(MVT::i32);
6998 ShOps[3] = DAG.getUNDEF(MVT::i32);
6999 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7000 } else {
7001 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7002 }
7003
Owen Andersone50ed302009-08-10 22:56:29 +00007004 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007005 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007006 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007007 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007008 Op.getOperand(1), ShAmt);
7009 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007010 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007011}
Evan Cheng72261582005-12-20 06:22:03 +00007012
Dan Gohman475871a2008-07-27 21:46:04 +00007013SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00007014 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007015 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007016
7017 if (Depth > 0) {
7018 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7019 SDValue Offset =
7020 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007021 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007022 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007023 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007024 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007025 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007026 }
7027
7028 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007029 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007030 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007031 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007032}
7033
Dan Gohman475871a2008-07-27 21:46:04 +00007034SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00007035 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7036 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007037 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007038 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007039 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7040 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007041 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007042 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007043 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7044 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007045 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007046}
7047
Dan Gohman475871a2008-07-27 21:46:04 +00007048SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007049 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007050 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007051}
7052
Dan Gohman475871a2008-07-27 21:46:04 +00007053SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007054{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007055 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007056 SDValue Chain = Op.getOperand(0);
7057 SDValue Offset = Op.getOperand(1);
7058 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007059 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007060
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007061 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7062 getPointerTy());
7063 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007064
Dale Johannesene4d209d2009-02-03 20:21:25 +00007065 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007066 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007067 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007068 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007069 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007070 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007071
Dale Johannesene4d209d2009-02-03 20:21:25 +00007072 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007074 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007075}
7076
Dan Gohman475871a2008-07-27 21:46:04 +00007077SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007078 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007079 SDValue Root = Op.getOperand(0);
7080 SDValue Trmp = Op.getOperand(1); // trampoline
7081 SDValue FPtr = Op.getOperand(2); // nested function
7082 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007083 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007084
Dan Gohman69de1932008-02-06 22:27:42 +00007085 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007086
7087 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007088 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007089
7090 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007091 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7092 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007093
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007094 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7095 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007096
7097 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7098
7099 // Load the pointer to the nested function into R11.
7100 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007101 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007103 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007104
Owen Anderson825b72b2009-08-11 20:47:22 +00007105 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7106 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007107 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7108 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007109
7110 // Load the 'nest' parameter value into R10.
7111 // R10 is specified in X86CallingConv.td
7112 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007113 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7114 DAG.getConstant(10, MVT::i64));
7115 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007116 Addr, TrmpAddr, 10, 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(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007120 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7121 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007122
7123 // Jump to the nested function.
7124 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007125 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7126 DAG.getConstant(20, MVT::i64));
7127 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007128 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007129
7130 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7132 DAG.getConstant(22, MVT::i64));
7133 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007134 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007135
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007137 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007138 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007139 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007140 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007141 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007142 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007143 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007144
7145 switch (CC) {
7146 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007147 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007148 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007149 case CallingConv::X86_StdCall: {
7150 // Pass 'nest' parameter in ECX.
7151 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007152 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007153
7154 // Check that ECX wasn't needed by an 'inreg' parameter.
7155 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007156 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007157
Chris Lattner58d74912008-03-12 17:45:29 +00007158 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007159 unsigned InRegCount = 0;
7160 unsigned Idx = 1;
7161
7162 for (FunctionType::param_iterator I = FTy->param_begin(),
7163 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007164 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007165 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007166 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007167
7168 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00007169 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007170 }
7171 }
7172 break;
7173 }
7174 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007175 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007176 // Pass 'nest' parameter in EAX.
7177 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007178 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007179 break;
7180 }
7181
Dan Gohman475871a2008-07-27 21:46:04 +00007182 SDValue OutChains[4];
7183 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007184
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7186 DAG.getConstant(10, MVT::i32));
7187 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007188
Chris Lattnera62fe662010-02-05 19:20:30 +00007189 // This is storing the opcode for MOV32ri.
7190 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007191 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007192 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007193 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007194 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007195
Owen Anderson825b72b2009-08-11 20:47:22 +00007196 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7197 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007198 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7199 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007200
Chris Lattnera62fe662010-02-05 19:20:30 +00007201 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007202 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7203 DAG.getConstant(5, MVT::i32));
7204 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007205 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007206
Owen Anderson825b72b2009-08-11 20:47:22 +00007207 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7208 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007209 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7210 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007211
Dan Gohman475871a2008-07-27 21:46:04 +00007212 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007213 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007214 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007215 }
7216}
7217
Dan Gohman475871a2008-07-27 21:46:04 +00007218SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007219 /*
7220 The rounding mode is in bits 11:10 of FPSR, and has the following
7221 settings:
7222 00 Round to nearest
7223 01 Round to -inf
7224 10 Round to +inf
7225 11 Round to 0
7226
7227 FLT_ROUNDS, on the other hand, expects the following:
7228 -1 Undefined
7229 0 Round to 0
7230 1 Round to nearest
7231 2 Round to +inf
7232 3 Round to -inf
7233
7234 To perform the conversion, we do:
7235 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7236 */
7237
7238 MachineFunction &MF = DAG.getMachineFunction();
7239 const TargetMachine &TM = MF.getTarget();
7240 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7241 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007242 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007243 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007244
7245 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007246 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007247 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007248
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007250 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007251
7252 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007253 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7254 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007255
7256 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007257 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007258 DAG.getNode(ISD::SRL, dl, MVT::i16,
7259 DAG.getNode(ISD::AND, dl, MVT::i16,
7260 CWD, DAG.getConstant(0x800, MVT::i16)),
7261 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007262 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007263 DAG.getNode(ISD::SRL, dl, MVT::i16,
7264 DAG.getNode(ISD::AND, dl, MVT::i16,
7265 CWD, DAG.getConstant(0x400, MVT::i16)),
7266 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007267
Dan Gohman475871a2008-07-27 21:46:04 +00007268 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007269 DAG.getNode(ISD::AND, dl, MVT::i16,
7270 DAG.getNode(ISD::ADD, dl, MVT::i16,
7271 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7272 DAG.getConstant(1, MVT::i16)),
7273 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007274
7275
Duncan Sands83ec4b62008-06-06 12:08:01 +00007276 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007277 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007278}
7279
Dan Gohman475871a2008-07-27 21:46:04 +00007280SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007281 EVT VT = Op.getValueType();
7282 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007283 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007284 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007285
7286 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007287 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007288 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007289 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007290 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007291 }
Evan Cheng18efe262007-12-14 02:13:44 +00007292
Evan Cheng152804e2007-12-14 08:30:15 +00007293 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007294 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007295 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007296
7297 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007298 SDValue Ops[] = {
7299 Op,
7300 DAG.getConstant(NumBits+NumBits-1, OpVT),
7301 DAG.getConstant(X86::COND_E, MVT::i8),
7302 Op.getValue(1)
7303 };
7304 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007305
7306 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007307 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007308
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 if (VT == MVT::i8)
7310 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007311 return Op;
7312}
7313
Dan Gohman475871a2008-07-27 21:46:04 +00007314SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007315 EVT VT = Op.getValueType();
7316 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007317 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007318 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007319
7320 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 if (VT == MVT::i8) {
7322 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007323 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007324 }
Evan Cheng152804e2007-12-14 08:30:15 +00007325
7326 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007328 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007329
7330 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007331 SDValue Ops[] = {
7332 Op,
7333 DAG.getConstant(NumBits, OpVT),
7334 DAG.getConstant(X86::COND_E, MVT::i8),
7335 Op.getValue(1)
7336 };
7337 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007338
Owen Anderson825b72b2009-08-11 20:47:22 +00007339 if (VT == MVT::i8)
7340 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007341 return Op;
7342}
7343
Mon P Wangaf9b9522008-12-18 21:42:19 +00007344SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007345 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007346 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007347 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007348
Mon P Wangaf9b9522008-12-18 21:42:19 +00007349 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7350 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7351 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7352 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7353 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7354 //
7355 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7356 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7357 // return AloBlo + AloBhi + AhiBlo;
7358
7359 SDValue A = Op.getOperand(0);
7360 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007361
Dale Johannesene4d209d2009-02-03 20:21:25 +00007362 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007363 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7364 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007365 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007366 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7367 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007368 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007369 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007370 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007371 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007372 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007373 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007374 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007376 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007377 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007378 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7379 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007380 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007381 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7382 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007383 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7384 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007385 return Res;
7386}
7387
7388
Bill Wendling74c37652008-12-09 22:08:41 +00007389SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7390 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7391 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007392 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7393 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007394 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007395 SDValue LHS = N->getOperand(0);
7396 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007397 unsigned BaseOp = 0;
7398 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007399 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007400
7401 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007402 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007403 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007404 // A subtract of one will be selected as a INC. Note that INC doesn't
7405 // set CF, so we can't do this for UADDO.
7406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7407 if (C->getAPIntValue() == 1) {
7408 BaseOp = X86ISD::INC;
7409 Cond = X86::COND_O;
7410 break;
7411 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007412 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007413 Cond = X86::COND_O;
7414 break;
7415 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007416 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007417 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007418 break;
7419 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007420 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7421 // set CF, so we can't do this for USUBO.
7422 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7423 if (C->getAPIntValue() == 1) {
7424 BaseOp = X86ISD::DEC;
7425 Cond = X86::COND_O;
7426 break;
7427 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007428 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007429 Cond = X86::COND_O;
7430 break;
7431 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007432 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007433 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007434 break;
7435 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007436 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007437 Cond = X86::COND_O;
7438 break;
7439 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007440 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007441 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007442 break;
7443 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007444
Bill Wendling61edeb52008-12-02 01:06:39 +00007445 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007446 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007447 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007448
Bill Wendling61edeb52008-12-02 01:06:39 +00007449 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007450 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007451 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007452
Bill Wendling61edeb52008-12-02 01:06:39 +00007453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7454 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007455}
7456
Dan Gohman475871a2008-07-27 21:46:04 +00007457SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007458 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007459 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007460 unsigned Reg = 0;
7461 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007462 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007463 default:
7464 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007465 case MVT::i8: Reg = X86::AL; size = 1; break;
7466 case MVT::i16: Reg = X86::AX; size = 2; break;
7467 case MVT::i32: Reg = X86::EAX; size = 4; break;
7468 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007469 assert(Subtarget->is64Bit() && "Node not type legal!");
7470 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007471 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007472 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007473 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007474 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007475 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007476 Op.getOperand(1),
7477 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007478 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007479 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007480 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007481 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007482 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007483 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007484 return cpOut;
7485}
7486
Duncan Sands1607f052008-12-01 11:39:25 +00007487SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007488 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007489 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007490 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007491 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007492 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007493 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7495 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007496 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007497 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7498 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007499 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007500 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007501 rdx.getValue(1)
7502 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007503 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007504}
7505
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007506SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7507 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007508 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007509 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007510 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007511 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007512 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007513 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007514 Node->getOperand(0),
7515 Node->getOperand(1), negOp,
7516 cast<AtomicSDNode>(Node)->getSrcValue(),
7517 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007518}
7519
Evan Cheng0db9fe62006-04-25 20:13:52 +00007520/// LowerOperation - Provide custom lowering hooks for some operations.
7521///
Dan Gohman475871a2008-07-27 21:46:04 +00007522SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007523 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007524 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007525 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7526 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007527 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007528 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007529 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7530 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7531 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7532 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7533 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7534 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007535 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007536 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007537 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 case ISD::SHL_PARTS:
7539 case ISD::SRA_PARTS:
7540 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7541 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007542 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007543 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007544 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007545 case ISD::FABS: return LowerFABS(Op, DAG);
7546 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007547 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007548 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007549 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007550 case ISD::SELECT: return LowerSELECT(Op, DAG);
7551 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007552 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007553 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007554 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007555 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007556 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007557 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7558 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007559 case ISD::FRAME_TO_ARGS_OFFSET:
7560 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007561 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007562 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007563 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007564 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007565 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7566 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007567 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007568 case ISD::SADDO:
7569 case ISD::UADDO:
7570 case ISD::SSUBO:
7571 case ISD::USUBO:
7572 case ISD::SMULO:
7573 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007574 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007575 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007576}
7577
Duncan Sands1607f052008-12-01 11:39:25 +00007578void X86TargetLowering::
7579ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7580 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007581 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007582 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007583 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007584
7585 SDValue Chain = Node->getOperand(0);
7586 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007587 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007588 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007589 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007590 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007591 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007592 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007593 SDValue Result =
7594 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7595 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007596 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007598 Results.push_back(Result.getValue(2));
7599}
7600
Duncan Sands126d9072008-07-04 11:47:58 +00007601/// ReplaceNodeResults - Replace a node with an illegal result type
7602/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007603void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7604 SmallVectorImpl<SDValue>&Results,
7605 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007606 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007607 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007608 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007609 assert(false && "Do not know how to custom type legalize this operation!");
7610 return;
7611 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007612 std::pair<SDValue,SDValue> Vals =
7613 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007614 SDValue FIST = Vals.first, StackSlot = Vals.second;
7615 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007616 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007617 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007618 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7619 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007620 }
7621 return;
7622 }
7623 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007624 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007625 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007626 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007628 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007629 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007630 eax.getValue(2));
7631 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7632 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007633 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007634 Results.push_back(edx.getValue(1));
7635 return;
7636 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007637 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007638 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007639 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007640 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007641 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7642 DAG.getConstant(0, MVT::i32));
7643 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7644 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007645 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7646 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007647 cpInL.getValue(1));
7648 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7650 DAG.getConstant(0, MVT::i32));
7651 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7652 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007653 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007654 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007655 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007656 swapInL.getValue(1));
7657 SDValue Ops[] = { swapInH.getValue(0),
7658 N->getOperand(1),
7659 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007660 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007662 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007663 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007664 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007665 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007666 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007667 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007668 Results.push_back(cpOutH.getValue(1));
7669 return;
7670 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007671 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007672 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7673 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007674 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007675 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7676 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007677 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007678 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7679 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007680 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007681 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7682 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007683 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007684 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7685 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007686 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007687 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7688 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007689 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007690 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7691 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007692 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007693}
7694
Evan Cheng72261582005-12-20 06:22:03 +00007695const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7696 switch (Opcode) {
7697 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007698 case X86ISD::BSF: return "X86ISD::BSF";
7699 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007700 case X86ISD::SHLD: return "X86ISD::SHLD";
7701 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007702 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007703 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007704 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007705 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007706 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007707 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007708 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7709 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7710 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007711 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007712 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007713 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007714 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007715 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007716 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007717 case X86ISD::COMI: return "X86ISD::COMI";
7718 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007719 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007720 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007721 case X86ISD::CMOV: return "X86ISD::CMOV";
7722 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007723 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007724 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7725 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007726 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007727 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007728 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007729 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007730 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007731 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7732 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007733 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007734 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007735 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007736 case X86ISD::FMAX: return "X86ISD::FMAX";
7737 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007738 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7739 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007740 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007741 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007742 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007743 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007744 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007745 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7746 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007747 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7748 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7749 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7750 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7751 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7752 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007753 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7754 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007755 case X86ISD::VSHL: return "X86ISD::VSHL";
7756 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007757 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7758 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7759 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7760 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7761 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7762 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7763 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7764 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7765 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7766 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007767 case X86ISD::ADD: return "X86ISD::ADD";
7768 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007769 case X86ISD::SMUL: return "X86ISD::SMUL";
7770 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007771 case X86ISD::INC: return "X86ISD::INC";
7772 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007773 case X86ISD::OR: return "X86ISD::OR";
7774 case X86ISD::XOR: return "X86ISD::XOR";
7775 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007776 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007777 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007778 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007779 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007780 }
7781}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007782
Chris Lattnerc9addb72007-03-30 23:15:24 +00007783// isLegalAddressingMode - Return true if the addressing mode represented
7784// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007785bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007786 const Type *Ty) const {
7787 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007788 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007789
Chris Lattnerc9addb72007-03-30 23:15:24 +00007790 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007791 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007792 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007793
Chris Lattnerc9addb72007-03-30 23:15:24 +00007794 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007795 unsigned GVFlags =
7796 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007797
Chris Lattnerdfed4132009-07-10 07:38:24 +00007798 // If a reference to this global requires an extra load, we can't fold it.
7799 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007800 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007801
Chris Lattnerdfed4132009-07-10 07:38:24 +00007802 // If BaseGV requires a register for the PIC base, we cannot also have a
7803 // BaseReg specified.
7804 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007805 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007806
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007807 // If lower 4G is not available, then we must use rip-relative addressing.
7808 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7809 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007811
Chris Lattnerc9addb72007-03-30 23:15:24 +00007812 switch (AM.Scale) {
7813 case 0:
7814 case 1:
7815 case 2:
7816 case 4:
7817 case 8:
7818 // These scales always work.
7819 break;
7820 case 3:
7821 case 5:
7822 case 9:
7823 // These scales are formed with basereg+scalereg. Only accept if there is
7824 // no basereg yet.
7825 if (AM.HasBaseReg)
7826 return false;
7827 break;
7828 default: // Other stuff never works.
7829 return false;
7830 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007831
Chris Lattnerc9addb72007-03-30 23:15:24 +00007832 return true;
7833}
7834
7835
Evan Cheng2bd122c2007-10-26 01:56:11 +00007836bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007837 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007838 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007839 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7840 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007841 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007842 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007843 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007844}
7845
Owen Andersone50ed302009-08-10 22:56:29 +00007846bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007847 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007848 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007849 unsigned NumBits1 = VT1.getSizeInBits();
7850 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007851 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007852 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007853 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007854}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007855
Dan Gohman97121ba2009-04-08 00:15:30 +00007856bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007857 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007858 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007859}
7860
Owen Andersone50ed302009-08-10 22:56:29 +00007861bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007862 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007863 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007864}
7865
Owen Andersone50ed302009-08-10 22:56:29 +00007866bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007867 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007868 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007869}
7870
Evan Cheng60c07e12006-07-05 22:17:51 +00007871/// isShuffleMaskLegal - Targets can use this to indicate that they only
7872/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7873/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7874/// are assumed to be legal.
7875bool
Eric Christopherfd179292009-08-27 18:07:15 +00007876X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007877 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007878 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007879 if (VT.getSizeInBits() == 64)
7880 return false;
7881
Nate Begemana09008b2009-10-19 02:17:23 +00007882 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007883 return (VT.getVectorNumElements() == 2 ||
7884 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7885 isMOVLMask(M, VT) ||
7886 isSHUFPMask(M, VT) ||
7887 isPSHUFDMask(M, VT) ||
7888 isPSHUFHWMask(M, VT) ||
7889 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007890 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007891 isUNPCKLMask(M, VT) ||
7892 isUNPCKHMask(M, VT) ||
7893 isUNPCKL_v_undef_Mask(M, VT) ||
7894 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007895}
7896
Dan Gohman7d8143f2008-04-09 20:09:42 +00007897bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007898X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007899 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007900 unsigned NumElts = VT.getVectorNumElements();
7901 // FIXME: This collection of masks seems suspect.
7902 if (NumElts == 2)
7903 return true;
7904 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7905 return (isMOVLMask(Mask, VT) ||
7906 isCommutedMOVLMask(Mask, VT, true) ||
7907 isSHUFPMask(Mask, VT) ||
7908 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007909 }
7910 return false;
7911}
7912
7913//===----------------------------------------------------------------------===//
7914// X86 Scheduler Hooks
7915//===----------------------------------------------------------------------===//
7916
Mon P Wang63307c32008-05-05 19:05:59 +00007917// private utility function
7918MachineBasicBlock *
7919X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7920 MachineBasicBlock *MBB,
7921 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007922 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007923 unsigned LoadOpc,
7924 unsigned CXchgOpc,
7925 unsigned copyOpc,
7926 unsigned notOpc,
7927 unsigned EAXreg,
7928 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007929 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007930 // For the atomic bitwise operator, we generate
7931 // thisMBB:
7932 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007933 // ld t1 = [bitinstr.addr]
7934 // op t2 = t1, [bitinstr.val]
7935 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007936 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7937 // bz newMBB
7938 // fallthrough -->nextMBB
7939 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7940 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007941 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007942 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007943
Mon P Wang63307c32008-05-05 19:05:59 +00007944 /// First build the CFG
7945 MachineFunction *F = MBB->getParent();
7946 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007947 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7948 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7949 F->insert(MBBIter, newMBB);
7950 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007951
Mon P Wang63307c32008-05-05 19:05:59 +00007952 // Move all successors to thisMBB to nextMBB
7953 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007954
Mon P Wang63307c32008-05-05 19:05:59 +00007955 // Update thisMBB to fall through to newMBB
7956 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007957
Mon P Wang63307c32008-05-05 19:05:59 +00007958 // newMBB jumps to itself and fall through to nextMBB
7959 newMBB->addSuccessor(nextMBB);
7960 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007961
Mon P Wang63307c32008-05-05 19:05:59 +00007962 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007963 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007964 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007965 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007966 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007967 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007968 int numArgs = bInstr->getNumOperands() - 1;
7969 for (int i=0; i < numArgs; ++i)
7970 argOpers[i] = &bInstr->getOperand(i+1);
7971
7972 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007973 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7974 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007975
Dale Johannesen140be2d2008-08-19 18:47:28 +00007976 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007977 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007978 for (int i=0; i <= lastAddrIndx; ++i)
7979 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007980
Dale Johannesen140be2d2008-08-19 18:47:28 +00007981 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007982 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007983 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007985 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007986 tt = t1;
7987
Dale Johannesen140be2d2008-08-19 18:47:28 +00007988 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007989 assert((argOpers[valArgIndx]->isReg() ||
7990 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007991 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007992 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007993 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007994 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007995 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007996 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007997 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007998
Dale Johannesene4d209d2009-02-03 20:21:25 +00007999 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008000 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008001
Dale Johannesene4d209d2009-02-03 20:21:25 +00008002 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008003 for (int i=0; i <= lastAddrIndx; ++i)
8004 (*MIB).addOperand(*argOpers[i]);
8005 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008006 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008007 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8008 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008009
Dale Johannesene4d209d2009-02-03 20:21:25 +00008010 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008011 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008012
Mon P Wang63307c32008-05-05 19:05:59 +00008013 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008014 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008015
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008016 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008017 return nextMBB;
8018}
8019
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008020// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008021MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008022X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8023 MachineBasicBlock *MBB,
8024 unsigned regOpcL,
8025 unsigned regOpcH,
8026 unsigned immOpcL,
8027 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008028 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008029 // For the atomic bitwise operator, we generate
8030 // thisMBB (instructions are in pairs, except cmpxchg8b)
8031 // ld t1,t2 = [bitinstr.addr]
8032 // newMBB:
8033 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8034 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008035 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008036 // mov ECX, EBX <- t5, t6
8037 // mov EAX, EDX <- t1, t2
8038 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8039 // mov t3, t4 <- EAX, EDX
8040 // bz newMBB
8041 // result in out1, out2
8042 // fallthrough -->nextMBB
8043
8044 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8045 const unsigned LoadOpc = X86::MOV32rm;
8046 const unsigned copyOpc = X86::MOV32rr;
8047 const unsigned NotOpc = X86::NOT32r;
8048 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8049 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8050 MachineFunction::iterator MBBIter = MBB;
8051 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008052
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008053 /// First build the CFG
8054 MachineFunction *F = MBB->getParent();
8055 MachineBasicBlock *thisMBB = MBB;
8056 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8057 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8058 F->insert(MBBIter, newMBB);
8059 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008060
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008061 // Move all successors to thisMBB to nextMBB
8062 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008063
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008064 // Update thisMBB to fall through to newMBB
8065 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008066
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008067 // newMBB jumps to itself and fall through to nextMBB
8068 newMBB->addSuccessor(nextMBB);
8069 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008070
Dale Johannesene4d209d2009-02-03 20:21:25 +00008071 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008072 // Insert instructions into newMBB based on incoming instruction
8073 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008074 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008075 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008076 MachineOperand& dest1Oper = bInstr->getOperand(0);
8077 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008078 MachineOperand* argOpers[2 + X86AddrNumOperands];
8079 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080 argOpers[i] = &bInstr->getOperand(i+2);
8081
Evan Chengad5b52f2010-01-08 19:14:57 +00008082 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008083 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008084
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008085 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008086 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008087 for (int i=0; i <= lastAddrIndx; ++i)
8088 (*MIB).addOperand(*argOpers[i]);
8089 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008090 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008091 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008092 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008093 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008094 MachineOperand newOp3 = *(argOpers[3]);
8095 if (newOp3.isImm())
8096 newOp3.setImm(newOp3.getImm()+4);
8097 else
8098 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008099 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008100 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008101
8102 // t3/4 are defined later, at the bottom of the loop
8103 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8104 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008105 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008106 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008107 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008108 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8109
Evan Cheng306b4ca2010-01-08 23:41:50 +00008110 // The subsequent operations should be using the destination registers of
8111 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008112 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008113 t1 = F->getRegInfo().createVirtualRegister(RC);
8114 t2 = F->getRegInfo().createVirtualRegister(RC);
8115 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8116 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008117 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008118 t1 = dest1Oper.getReg();
8119 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008120 }
8121
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008122 int valArgIndx = lastAddrIndx + 1;
8123 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008124 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008125 "invalid operand");
8126 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8127 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008128 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008129 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008130 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008131 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008132 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008133 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008134 (*MIB).addOperand(*argOpers[valArgIndx]);
8135 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008136 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008137 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008138 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008139 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008140 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008141 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008142 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008143 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008144 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008145 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008146
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008148 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008149 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008150 MIB.addReg(t2);
8151
Dale Johannesene4d209d2009-02-03 20:21:25 +00008152 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008153 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008154 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008155 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008156
Dale Johannesene4d209d2009-02-03 20:21:25 +00008157 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008158 for (int i=0; i <= lastAddrIndx; ++i)
8159 (*MIB).addOperand(*argOpers[i]);
8160
8161 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008162 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8163 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008164
Dale Johannesene4d209d2009-02-03 20:21:25 +00008165 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008166 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008167 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008168 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008169
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008170 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008171 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008172
8173 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8174 return nextMBB;
8175}
8176
8177// private utility function
8178MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008179X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8180 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008181 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008182 // For the atomic min/max operator, we generate
8183 // thisMBB:
8184 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008185 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008186 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008187 // cmp t1, t2
8188 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008189 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008190 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8191 // bz newMBB
8192 // fallthrough -->nextMBB
8193 //
8194 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8195 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008196 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008197 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008198
Mon P Wang63307c32008-05-05 19:05:59 +00008199 /// First build the CFG
8200 MachineFunction *F = MBB->getParent();
8201 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008202 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8203 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8204 F->insert(MBBIter, newMBB);
8205 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008206
Dan Gohmand6708ea2009-08-15 01:38:56 +00008207 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008208 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008209
Mon P Wang63307c32008-05-05 19:05:59 +00008210 // Update thisMBB to fall through to newMBB
8211 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008212
Mon P Wang63307c32008-05-05 19:05:59 +00008213 // newMBB jumps to newMBB and fall through to nextMBB
8214 newMBB->addSuccessor(nextMBB);
8215 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008216
Dale Johannesene4d209d2009-02-03 20:21:25 +00008217 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008218 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008219 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008220 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008221 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008222 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008223 int numArgs = mInstr->getNumOperands() - 1;
8224 for (int i=0; i < numArgs; ++i)
8225 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008226
Mon P Wang63307c32008-05-05 19:05:59 +00008227 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008228 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8229 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008230
Mon P Wangab3e7472008-05-05 22:56:23 +00008231 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008232 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008233 for (int i=0; i <= lastAddrIndx; ++i)
8234 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008235
Mon P Wang63307c32008-05-05 19:05:59 +00008236 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008237 assert((argOpers[valArgIndx]->isReg() ||
8238 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008239 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008240
8241 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008242 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008243 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008244 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008245 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008246 (*MIB).addOperand(*argOpers[valArgIndx]);
8247
Dale Johannesene4d209d2009-02-03 20:21:25 +00008248 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008249 MIB.addReg(t1);
8250
Dale Johannesene4d209d2009-02-03 20:21:25 +00008251 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008252 MIB.addReg(t1);
8253 MIB.addReg(t2);
8254
8255 // Generate movc
8256 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008257 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008258 MIB.addReg(t2);
8259 MIB.addReg(t1);
8260
8261 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008262 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008263 for (int i=0; i <= lastAddrIndx; ++i)
8264 (*MIB).addOperand(*argOpers[i]);
8265 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008266 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008267 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8268 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008269
Dale Johannesene4d209d2009-02-03 20:21:25 +00008270 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008271 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008272
Mon P Wang63307c32008-05-05 19:05:59 +00008273 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008274 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008275
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008276 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008277 return nextMBB;
8278}
8279
Eric Christopherf83a5de2009-08-27 18:08:16 +00008280// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8281// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008282MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008283X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008284 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008285
8286 MachineFunction *F = BB->getParent();
8287 DebugLoc dl = MI->getDebugLoc();
8288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8289
8290 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008291 if (memArg)
8292 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8293 else
8294 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008295
8296 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8297
8298 for (unsigned i = 0; i < numArgs; ++i) {
8299 MachineOperand &Op = MI->getOperand(i+1);
8300
8301 if (!(Op.isReg() && Op.isImplicit()))
8302 MIB.addOperand(Op);
8303 }
8304
8305 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8306 .addReg(X86::XMM0);
8307
8308 F->DeleteMachineInstr(MI);
8309
8310 return BB;
8311}
8312
8313MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008314X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8315 MachineInstr *MI,
8316 MachineBasicBlock *MBB) const {
8317 // Emit code to save XMM registers to the stack. The ABI says that the
8318 // number of registers to save is given in %al, so it's theoretically
8319 // possible to do an indirect jump trick to avoid saving all of them,
8320 // however this code takes a simpler approach and just executes all
8321 // of the stores if %al is non-zero. It's less code, and it's probably
8322 // easier on the hardware branch predictor, and stores aren't all that
8323 // expensive anyway.
8324
8325 // Create the new basic blocks. One block contains all the XMM stores,
8326 // and one block is the final destination regardless of whether any
8327 // stores were performed.
8328 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8329 MachineFunction *F = MBB->getParent();
8330 MachineFunction::iterator MBBIter = MBB;
8331 ++MBBIter;
8332 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8333 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8334 F->insert(MBBIter, XMMSaveMBB);
8335 F->insert(MBBIter, EndMBB);
8336
8337 // Set up the CFG.
8338 // Move any original successors of MBB to the end block.
8339 EndMBB->transferSuccessors(MBB);
8340 // The original block will now fall through to the XMM save block.
8341 MBB->addSuccessor(XMMSaveMBB);
8342 // The XMMSaveMBB will fall through to the end block.
8343 XMMSaveMBB->addSuccessor(EndMBB);
8344
8345 // Now add the instructions.
8346 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8347 DebugLoc DL = MI->getDebugLoc();
8348
8349 unsigned CountReg = MI->getOperand(0).getReg();
8350 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8351 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8352
8353 if (!Subtarget->isTargetWin64()) {
8354 // If %al is 0, branch around the XMM save block.
8355 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008356 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008357 MBB->addSuccessor(EndMBB);
8358 }
8359
8360 // In the XMM save block, save all the XMM argument registers.
8361 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8362 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008363 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008364 F->getMachineMemOperand(
8365 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8366 MachineMemOperand::MOStore, Offset,
8367 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008368 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8369 .addFrameIndex(RegSaveFrameIndex)
8370 .addImm(/*Scale=*/1)
8371 .addReg(/*IndexReg=*/0)
8372 .addImm(/*Disp=*/Offset)
8373 .addReg(/*Segment=*/0)
8374 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008375 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008376 }
8377
8378 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8379
8380 return EndMBB;
8381}
Mon P Wang63307c32008-05-05 19:05:59 +00008382
Evan Cheng60c07e12006-07-05 22:17:51 +00008383MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008384X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008385 MachineBasicBlock *BB,
8386 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008387 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8388 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008389
Chris Lattner52600972009-09-02 05:57:00 +00008390 // To "insert" a SELECT_CC instruction, we actually have to insert the
8391 // diamond control-flow pattern. The incoming instruction knows the
8392 // destination vreg to set, the condition code register to branch on, the
8393 // true/false values to select between, and a branch opcode to use.
8394 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8395 MachineFunction::iterator It = BB;
8396 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008397
Chris Lattner52600972009-09-02 05:57:00 +00008398 // thisMBB:
8399 // ...
8400 // TrueVal = ...
8401 // cmpTY ccX, r1, r2
8402 // bCC copy1MBB
8403 // fallthrough --> copy0MBB
8404 MachineBasicBlock *thisMBB = BB;
8405 MachineFunction *F = BB->getParent();
8406 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8407 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8408 unsigned Opc =
8409 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8410 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8411 F->insert(It, copy0MBB);
8412 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008413 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008414 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008415 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008416 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008417 E = BB->succ_end(); I != E; ++I) {
8418 EM->insert(std::make_pair(*I, sinkMBB));
8419 sinkMBB->addSuccessor(*I);
8420 }
8421 // Next, remove all successors of the current block, and add the true
8422 // and fallthrough blocks as its successors.
8423 while (!BB->succ_empty())
8424 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008425 // Add the true and fallthrough blocks as its successors.
8426 BB->addSuccessor(copy0MBB);
8427 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008428
Chris Lattner52600972009-09-02 05:57:00 +00008429 // copy0MBB:
8430 // %FalseValue = ...
8431 // # fallthrough to sinkMBB
8432 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008433
Chris Lattner52600972009-09-02 05:57:00 +00008434 // Update machine-CFG edges
8435 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008436
Chris Lattner52600972009-09-02 05:57:00 +00008437 // sinkMBB:
8438 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8439 // ...
8440 BB = sinkMBB;
8441 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8442 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8443 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8444
8445 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8446 return BB;
8447}
8448
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008449MachineBasicBlock *
8450X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8451 MachineBasicBlock *BB,
8452 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8453 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8454 DebugLoc DL = MI->getDebugLoc();
8455 MachineFunction *F = BB->getParent();
8456
8457 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8458 // non-trivial part is impdef of ESP.
8459 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8460 // mingw-w64.
8461
8462 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8463 .addExternalSymbol("_alloca")
8464 .addReg(X86::EAX, RegState::Implicit)
8465 .addReg(X86::ESP, RegState::Implicit)
8466 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8467 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8468
8469 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8470 return BB;
8471}
Chris Lattner52600972009-09-02 05:57:00 +00008472
8473MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008474X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008475 MachineBasicBlock *BB,
8476 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008477 switch (MI->getOpcode()) {
8478 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008479 case X86::MINGW_ALLOCA:
8480 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008481 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008482 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008483 case X86::CMOV_FR32:
8484 case X86::CMOV_FR64:
8485 case X86::CMOV_V4F32:
8486 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008487 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008488 case X86::CMOV_GR16:
8489 case X86::CMOV_GR32:
8490 case X86::CMOV_RFP32:
8491 case X86::CMOV_RFP64:
8492 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008493 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008494
Dale Johannesen849f2142007-07-03 00:53:03 +00008495 case X86::FP32_TO_INT16_IN_MEM:
8496 case X86::FP32_TO_INT32_IN_MEM:
8497 case X86::FP32_TO_INT64_IN_MEM:
8498 case X86::FP64_TO_INT16_IN_MEM:
8499 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008500 case X86::FP64_TO_INT64_IN_MEM:
8501 case X86::FP80_TO_INT16_IN_MEM:
8502 case X86::FP80_TO_INT32_IN_MEM:
8503 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008504 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8505 DebugLoc DL = MI->getDebugLoc();
8506
Evan Cheng60c07e12006-07-05 22:17:51 +00008507 // Change the floating point control register to use "round towards zero"
8508 // mode when truncating to an integer value.
8509 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008510 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008511 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008512
8513 // Load the old value of the high byte of the control word...
8514 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008515 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008516 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008517 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008518
8519 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008520 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008521 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008522
8523 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008524 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008525
8526 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008527 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008528 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008529
8530 // Get the X86 opcode to use.
8531 unsigned Opc;
8532 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008533 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008534 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8535 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8536 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8537 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8538 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8539 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008540 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8541 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8542 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008543 }
8544
8545 X86AddressMode AM;
8546 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008547 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008548 AM.BaseType = X86AddressMode::RegBase;
8549 AM.Base.Reg = Op.getReg();
8550 } else {
8551 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008552 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008553 }
8554 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008555 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008556 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008557 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008558 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008559 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008560 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008561 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008562 AM.GV = Op.getGlobal();
8563 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008564 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008565 }
Chris Lattner52600972009-09-02 05:57:00 +00008566 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008567 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008568
8569 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008570 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008571
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008572 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008573 return BB;
8574 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008575 // DBG_VALUE. Only the frame index case is done here.
8576 case X86::DBG_VALUE: {
8577 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8578 DebugLoc DL = MI->getDebugLoc();
8579 X86AddressMode AM;
8580 MachineFunction *F = BB->getParent();
8581 AM.BaseType = X86AddressMode::FrameIndexBase;
8582 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8583 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8584 addImm(MI->getOperand(1).getImm()).
8585 addMetadata(MI->getOperand(2).getMetadata());
8586 F->DeleteMachineInstr(MI); // Remove pseudo.
8587 return BB;
8588 }
8589
Eric Christopherb120ab42009-08-18 22:50:32 +00008590 // String/text processing lowering.
8591 case X86::PCMPISTRM128REG:
8592 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8593 case X86::PCMPISTRM128MEM:
8594 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8595 case X86::PCMPESTRM128REG:
8596 return EmitPCMP(MI, BB, 5, false /* in mem */);
8597 case X86::PCMPESTRM128MEM:
8598 return EmitPCMP(MI, BB, 5, true /* in mem */);
8599
8600 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008601 case X86::ATOMAND32:
8602 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008603 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008604 X86::LCMPXCHG32, X86::MOV32rr,
8605 X86::NOT32r, X86::EAX,
8606 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008607 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8609 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008610 X86::LCMPXCHG32, X86::MOV32rr,
8611 X86::NOT32r, X86::EAX,
8612 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008613 case X86::ATOMXOR32:
8614 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008615 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008616 X86::LCMPXCHG32, X86::MOV32rr,
8617 X86::NOT32r, X86::EAX,
8618 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008619 case X86::ATOMNAND32:
8620 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008621 X86::AND32ri, X86::MOV32rm,
8622 X86::LCMPXCHG32, X86::MOV32rr,
8623 X86::NOT32r, X86::EAX,
8624 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008625 case X86::ATOMMIN32:
8626 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8627 case X86::ATOMMAX32:
8628 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8629 case X86::ATOMUMIN32:
8630 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8631 case X86::ATOMUMAX32:
8632 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008633
8634 case X86::ATOMAND16:
8635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8636 X86::AND16ri, X86::MOV16rm,
8637 X86::LCMPXCHG16, X86::MOV16rr,
8638 X86::NOT16r, X86::AX,
8639 X86::GR16RegisterClass);
8640 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008642 X86::OR16ri, X86::MOV16rm,
8643 X86::LCMPXCHG16, X86::MOV16rr,
8644 X86::NOT16r, X86::AX,
8645 X86::GR16RegisterClass);
8646 case X86::ATOMXOR16:
8647 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8648 X86::XOR16ri, X86::MOV16rm,
8649 X86::LCMPXCHG16, X86::MOV16rr,
8650 X86::NOT16r, X86::AX,
8651 X86::GR16RegisterClass);
8652 case X86::ATOMNAND16:
8653 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8654 X86::AND16ri, X86::MOV16rm,
8655 X86::LCMPXCHG16, X86::MOV16rr,
8656 X86::NOT16r, X86::AX,
8657 X86::GR16RegisterClass, true);
8658 case X86::ATOMMIN16:
8659 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8660 case X86::ATOMMAX16:
8661 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8662 case X86::ATOMUMIN16:
8663 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8664 case X86::ATOMUMAX16:
8665 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8666
8667 case X86::ATOMAND8:
8668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8669 X86::AND8ri, X86::MOV8rm,
8670 X86::LCMPXCHG8, X86::MOV8rr,
8671 X86::NOT8r, X86::AL,
8672 X86::GR8RegisterClass);
8673 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008674 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008675 X86::OR8ri, X86::MOV8rm,
8676 X86::LCMPXCHG8, X86::MOV8rr,
8677 X86::NOT8r, X86::AL,
8678 X86::GR8RegisterClass);
8679 case X86::ATOMXOR8:
8680 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8681 X86::XOR8ri, X86::MOV8rm,
8682 X86::LCMPXCHG8, X86::MOV8rr,
8683 X86::NOT8r, X86::AL,
8684 X86::GR8RegisterClass);
8685 case X86::ATOMNAND8:
8686 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8687 X86::AND8ri, X86::MOV8rm,
8688 X86::LCMPXCHG8, X86::MOV8rr,
8689 X86::NOT8r, X86::AL,
8690 X86::GR8RegisterClass, true);
8691 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008692 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008693 case X86::ATOMAND64:
8694 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008695 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008696 X86::LCMPXCHG64, X86::MOV64rr,
8697 X86::NOT64r, X86::RAX,
8698 X86::GR64RegisterClass);
8699 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008700 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8701 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008702 X86::LCMPXCHG64, X86::MOV64rr,
8703 X86::NOT64r, X86::RAX,
8704 X86::GR64RegisterClass);
8705 case X86::ATOMXOR64:
8706 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008707 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008708 X86::LCMPXCHG64, X86::MOV64rr,
8709 X86::NOT64r, X86::RAX,
8710 X86::GR64RegisterClass);
8711 case X86::ATOMNAND64:
8712 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8713 X86::AND64ri32, X86::MOV64rm,
8714 X86::LCMPXCHG64, X86::MOV64rr,
8715 X86::NOT64r, X86::RAX,
8716 X86::GR64RegisterClass, true);
8717 case X86::ATOMMIN64:
8718 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8719 case X86::ATOMMAX64:
8720 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8721 case X86::ATOMUMIN64:
8722 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8723 case X86::ATOMUMAX64:
8724 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008725
8726 // This group does 64-bit operations on a 32-bit host.
8727 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008728 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008729 X86::AND32rr, X86::AND32rr,
8730 X86::AND32ri, X86::AND32ri,
8731 false);
8732 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008733 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008734 X86::OR32rr, X86::OR32rr,
8735 X86::OR32ri, X86::OR32ri,
8736 false);
8737 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008738 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008739 X86::XOR32rr, X86::XOR32rr,
8740 X86::XOR32ri, X86::XOR32ri,
8741 false);
8742 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008743 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008744 X86::AND32rr, X86::AND32rr,
8745 X86::AND32ri, X86::AND32ri,
8746 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008747 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008748 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008749 X86::ADD32rr, X86::ADC32rr,
8750 X86::ADD32ri, X86::ADC32ri,
8751 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008752 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008753 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008754 X86::SUB32rr, X86::SBB32rr,
8755 X86::SUB32ri, X86::SBB32ri,
8756 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008757 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008758 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008759 X86::MOV32rr, X86::MOV32rr,
8760 X86::MOV32ri, X86::MOV32ri,
8761 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008762 case X86::VASTART_SAVE_XMM_REGS:
8763 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008764 }
8765}
8766
8767//===----------------------------------------------------------------------===//
8768// X86 Optimization Hooks
8769//===----------------------------------------------------------------------===//
8770
Dan Gohman475871a2008-07-27 21:46:04 +00008771void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008772 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008773 APInt &KnownZero,
8774 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008775 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008776 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008777 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008778 assert((Opc >= ISD::BUILTIN_OP_END ||
8779 Opc == ISD::INTRINSIC_WO_CHAIN ||
8780 Opc == ISD::INTRINSIC_W_CHAIN ||
8781 Opc == ISD::INTRINSIC_VOID) &&
8782 "Should use MaskedValueIsZero if you don't know whether Op"
8783 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008784
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008785 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008786 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008787 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008788 case X86ISD::ADD:
8789 case X86ISD::SUB:
8790 case X86ISD::SMUL:
8791 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008792 case X86ISD::INC:
8793 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008794 case X86ISD::OR:
8795 case X86ISD::XOR:
8796 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008797 // These nodes' second result is a boolean.
8798 if (Op.getResNo() == 0)
8799 break;
8800 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008801 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008802 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8803 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008804 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008805 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008806}
Chris Lattner259e97c2006-01-31 19:43:35 +00008807
Evan Cheng206ee9d2006-07-07 08:33:52 +00008808/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008809/// node is a GlobalAddress + offset.
8810bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8811 GlobalValue* &GA, int64_t &Offset) const{
8812 if (N->getOpcode() == X86ISD::Wrapper) {
8813 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008814 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008815 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008816 return true;
8817 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008818 }
Evan Chengad4196b2008-05-12 19:56:52 +00008819 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008820}
8821
Nate Begeman9008ca62009-04-27 18:41:29 +00008822static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008823 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008824 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008825 SelectionDAG &DAG, MachineFrameInfo *MFI,
8826 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008827 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008828 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008829 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008830 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008831 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008832 return false;
8833 continue;
8834 }
8835
Dan Gohman475871a2008-07-27 21:46:04 +00008836 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008837 if (!Elt.getNode() ||
8838 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008839 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008840 if (!LDBase) {
8841 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008842 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008843 LDBase = cast<LoadSDNode>(Elt.getNode());
8844 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008845 continue;
8846 }
8847 if (Elt.getOpcode() == ISD::UNDEF)
8848 continue;
8849
Nate Begemanabc01992009-06-05 21:37:30 +00008850 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008851 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008852 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008853 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008854 }
8855 return true;
8856}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008857
8858/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8859/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8860/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008861/// order. In the case of v2i64, it will see if it can rewrite the
8862/// shuffle to be an appropriate build vector so it can take advantage of
8863// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008864static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008865 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008866 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008867 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008868 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008869 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8870 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008871
Eli Friedman7a5e5552009-06-07 06:52:44 +00008872 if (VT.getSizeInBits() != 128)
8873 return SDValue();
8874
Mon P Wang1e955802009-04-03 02:43:30 +00008875 // Try to combine a vector_shuffle into a 128-bit load.
8876 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008877 LoadSDNode *LD = NULL;
8878 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008879 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008880 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008881 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008882
Eli Friedman7a5e5552009-06-07 06:52:44 +00008883 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008884 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008885 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8886 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008887 LD->isVolatile(), LD->isNonTemporal(), 0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008888 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008889 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008890 LD->isVolatile(), LD->isNonTemporal(),
8891 LD->getAlignment());
Eli Friedman7a5e5552009-06-07 06:52:44 +00008892 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008893 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008894 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8895 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008896 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8897 }
8898 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008899}
Evan Chengd880b972008-05-09 21:53:03 +00008900
Chris Lattner83e6c992006-10-04 06:57:07 +00008901/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008902static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008903 const X86Subtarget *Subtarget) {
8904 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008905 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008906 // Get the LHS/RHS of the select.
8907 SDValue LHS = N->getOperand(1);
8908 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008909
Dan Gohman670e5392009-09-21 18:03:22 +00008910 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008911 // instructions match the semantics of the common C idiom x<y?x:y but not
8912 // x<=y?x:y, because of how they handle negative zero (which can be
8913 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008914 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008915 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008916 Cond.getOpcode() == ISD::SETCC) {
8917 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008918
Chris Lattner47b4ce82009-03-11 05:48:52 +00008919 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008920 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008921 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8922 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008923 switch (CC) {
8924 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008925 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008926 // Converting this to a min would handle NaNs incorrectly, and swapping
8927 // the operands would cause it to handle comparisons between positive
8928 // and negative zero incorrectly.
8929 if (!FiniteOnlyFPMath() &&
8930 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8931 if (!UnsafeFPMath &&
8932 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8933 break;
8934 std::swap(LHS, RHS);
8935 }
Dan Gohman670e5392009-09-21 18:03:22 +00008936 Opcode = X86ISD::FMIN;
8937 break;
8938 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008939 // Converting this to a min would handle comparisons between positive
8940 // and negative zero incorrectly.
8941 if (!UnsafeFPMath &&
8942 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8943 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008944 Opcode = X86ISD::FMIN;
8945 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008946 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008947 // Converting this to a min would handle both negative zeros and NaNs
8948 // incorrectly, but we can swap the operands to fix both.
8949 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008950 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008951 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008952 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008953 Opcode = X86ISD::FMIN;
8954 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008955
Dan Gohman670e5392009-09-21 18:03:22 +00008956 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008957 // Converting this to a max would handle comparisons between positive
8958 // and negative zero incorrectly.
8959 if (!UnsafeFPMath &&
8960 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8961 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008962 Opcode = X86ISD::FMAX;
8963 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008964 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008965 // Converting this to a max would handle NaNs incorrectly, and swapping
8966 // the operands would cause it to handle comparisons between positive
8967 // and negative zero incorrectly.
8968 if (!FiniteOnlyFPMath() &&
8969 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8970 if (!UnsafeFPMath &&
8971 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8972 break;
8973 std::swap(LHS, RHS);
8974 }
Dan Gohman670e5392009-09-21 18:03:22 +00008975 Opcode = X86ISD::FMAX;
8976 break;
8977 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008978 // Converting this to a max would handle both negative zeros and NaNs
8979 // incorrectly, but we can swap the operands to fix both.
8980 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008981 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008982 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008983 case ISD::SETGE:
8984 Opcode = X86ISD::FMAX;
8985 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008986 }
Dan Gohman670e5392009-09-21 18:03:22 +00008987 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008988 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8989 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008990 switch (CC) {
8991 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008992 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008993 // Converting this to a min would handle comparisons between positive
8994 // and negative zero incorrectly, and swapping the operands would
8995 // cause it to handle NaNs incorrectly.
8996 if (!UnsafeFPMath &&
8997 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8998 if (!FiniteOnlyFPMath() &&
8999 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9000 break;
9001 std::swap(LHS, RHS);
9002 }
Dan Gohman670e5392009-09-21 18:03:22 +00009003 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009004 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009005 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009006 // Converting this to a min would handle NaNs incorrectly.
9007 if (!UnsafeFPMath &&
9008 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9009 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009010 Opcode = X86ISD::FMIN;
9011 break;
9012 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009013 // Converting this to a min would handle both negative zeros and NaNs
9014 // incorrectly, but we can swap the operands to fix both.
9015 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009016 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009017 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009018 case ISD::SETGE:
9019 Opcode = X86ISD::FMIN;
9020 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009021
Dan Gohman670e5392009-09-21 18:03:22 +00009022 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009023 // Converting this to a max would handle NaNs incorrectly.
9024 if (!FiniteOnlyFPMath() &&
9025 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9026 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009027 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009028 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009029 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009030 // Converting this to a max would handle comparisons between positive
9031 // and negative zero incorrectly, and swapping the operands would
9032 // cause it to handle NaNs incorrectly.
9033 if (!UnsafeFPMath &&
9034 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9035 if (!FiniteOnlyFPMath() &&
9036 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9037 break;
9038 std::swap(LHS, RHS);
9039 }
Dan Gohman670e5392009-09-21 18:03:22 +00009040 Opcode = X86ISD::FMAX;
9041 break;
9042 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009043 // Converting this to a max would handle both negative zeros and NaNs
9044 // incorrectly, but we can swap the operands to fix both.
9045 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009046 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009047 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009048 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009049 Opcode = X86ISD::FMAX;
9050 break;
9051 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009052 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009053
Chris Lattner47b4ce82009-03-11 05:48:52 +00009054 if (Opcode)
9055 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009056 }
Eric Christopherfd179292009-08-27 18:07:15 +00009057
Chris Lattnerd1980a52009-03-12 06:52:53 +00009058 // If this is a select between two integer constants, try to do some
9059 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009060 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9061 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009062 // Don't do this for crazy integer types.
9063 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9064 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009065 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009066 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009067
Chris Lattnercee56e72009-03-13 05:53:31 +00009068 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009069 // Efficiently invertible.
9070 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9071 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9072 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9073 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009074 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009075 }
Eric Christopherfd179292009-08-27 18:07:15 +00009076
Chris Lattnerd1980a52009-03-12 06:52:53 +00009077 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009078 if (FalseC->getAPIntValue() == 0 &&
9079 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009080 if (NeedsCondInvert) // Invert the condition if needed.
9081 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9082 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009083
Chris Lattnerd1980a52009-03-12 06:52:53 +00009084 // Zero extend the condition if needed.
9085 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009086
Chris Lattnercee56e72009-03-13 05:53:31 +00009087 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009088 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009089 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009090 }
Eric Christopherfd179292009-08-27 18:07:15 +00009091
Chris Lattner97a29a52009-03-13 05:22:11 +00009092 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009093 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009094 if (NeedsCondInvert) // Invert the condition if needed.
9095 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9096 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009097
Chris Lattner97a29a52009-03-13 05:22:11 +00009098 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009099 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9100 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009101 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009102 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009103 }
Eric Christopherfd179292009-08-27 18:07:15 +00009104
Chris Lattnercee56e72009-03-13 05:53:31 +00009105 // Optimize cases that will turn into an LEA instruction. This requires
9106 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009107 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009108 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009109 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009110
Chris Lattnercee56e72009-03-13 05:53:31 +00009111 bool isFastMultiplier = false;
9112 if (Diff < 10) {
9113 switch ((unsigned char)Diff) {
9114 default: break;
9115 case 1: // result = add base, cond
9116 case 2: // result = lea base( , cond*2)
9117 case 3: // result = lea base(cond, cond*2)
9118 case 4: // result = lea base( , cond*4)
9119 case 5: // result = lea base(cond, cond*4)
9120 case 8: // result = lea base( , cond*8)
9121 case 9: // result = lea base(cond, cond*8)
9122 isFastMultiplier = true;
9123 break;
9124 }
9125 }
Eric Christopherfd179292009-08-27 18:07:15 +00009126
Chris Lattnercee56e72009-03-13 05:53:31 +00009127 if (isFastMultiplier) {
9128 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9129 if (NeedsCondInvert) // Invert the condition if needed.
9130 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9131 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009132
Chris Lattnercee56e72009-03-13 05:53:31 +00009133 // Zero extend the condition if needed.
9134 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9135 Cond);
9136 // Scale the condition by the difference.
9137 if (Diff != 1)
9138 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9139 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009140
Chris Lattnercee56e72009-03-13 05:53:31 +00009141 // Add the base if non-zero.
9142 if (FalseC->getAPIntValue() != 0)
9143 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9144 SDValue(FalseC, 0));
9145 return Cond;
9146 }
Eric Christopherfd179292009-08-27 18:07:15 +00009147 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009148 }
9149 }
Eric Christopherfd179292009-08-27 18:07:15 +00009150
Dan Gohman475871a2008-07-27 21:46:04 +00009151 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009152}
9153
Chris Lattnerd1980a52009-03-12 06:52:53 +00009154/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9155static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9156 TargetLowering::DAGCombinerInfo &DCI) {
9157 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009158
Chris Lattnerd1980a52009-03-12 06:52:53 +00009159 // If the flag operand isn't dead, don't touch this CMOV.
9160 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9161 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009162
Chris Lattnerd1980a52009-03-12 06:52:53 +00009163 // If this is a select between two integer constants, try to do some
9164 // optimizations. Note that the operands are ordered the opposite of SELECT
9165 // operands.
9166 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9167 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9168 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9169 // larger than FalseC (the false value).
9170 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009171
Chris Lattnerd1980a52009-03-12 06:52:53 +00009172 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9173 CC = X86::GetOppositeBranchCondition(CC);
9174 std::swap(TrueC, FalseC);
9175 }
Eric Christopherfd179292009-08-27 18:07:15 +00009176
Chris Lattnerd1980a52009-03-12 06:52:53 +00009177 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009178 // This is efficient for any integer data type (including i8/i16) and
9179 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009180 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9181 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009182 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9183 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009184
Chris Lattnerd1980a52009-03-12 06:52:53 +00009185 // Zero extend the condition if needed.
9186 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009187
Chris Lattnerd1980a52009-03-12 06:52:53 +00009188 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9189 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009190 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009191 if (N->getNumValues() == 2) // Dead flag value?
9192 return DCI.CombineTo(N, Cond, SDValue());
9193 return Cond;
9194 }
Eric Christopherfd179292009-08-27 18:07:15 +00009195
Chris Lattnercee56e72009-03-13 05:53:31 +00009196 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9197 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009198 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9199 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009200 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9201 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009202
Chris Lattner97a29a52009-03-13 05:22:11 +00009203 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009204 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9205 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009206 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9207 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009208
Chris Lattner97a29a52009-03-13 05:22:11 +00009209 if (N->getNumValues() == 2) // Dead flag value?
9210 return DCI.CombineTo(N, Cond, SDValue());
9211 return Cond;
9212 }
Eric Christopherfd179292009-08-27 18:07:15 +00009213
Chris Lattnercee56e72009-03-13 05:53:31 +00009214 // Optimize cases that will turn into an LEA instruction. This requires
9215 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009216 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009217 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009218 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009219
Chris Lattnercee56e72009-03-13 05:53:31 +00009220 bool isFastMultiplier = false;
9221 if (Diff < 10) {
9222 switch ((unsigned char)Diff) {
9223 default: break;
9224 case 1: // result = add base, cond
9225 case 2: // result = lea base( , cond*2)
9226 case 3: // result = lea base(cond, cond*2)
9227 case 4: // result = lea base( , cond*4)
9228 case 5: // result = lea base(cond, cond*4)
9229 case 8: // result = lea base( , cond*8)
9230 case 9: // result = lea base(cond, cond*8)
9231 isFastMultiplier = true;
9232 break;
9233 }
9234 }
Eric Christopherfd179292009-08-27 18:07:15 +00009235
Chris Lattnercee56e72009-03-13 05:53:31 +00009236 if (isFastMultiplier) {
9237 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9238 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009239 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9240 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009241 // Zero extend the condition if needed.
9242 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9243 Cond);
9244 // Scale the condition by the difference.
9245 if (Diff != 1)
9246 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9247 DAG.getConstant(Diff, Cond.getValueType()));
9248
9249 // Add the base if non-zero.
9250 if (FalseC->getAPIntValue() != 0)
9251 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9252 SDValue(FalseC, 0));
9253 if (N->getNumValues() == 2) // Dead flag value?
9254 return DCI.CombineTo(N, Cond, SDValue());
9255 return Cond;
9256 }
Eric Christopherfd179292009-08-27 18:07:15 +00009257 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009258 }
9259 }
9260 return SDValue();
9261}
9262
9263
Evan Cheng0b0cd912009-03-28 05:57:29 +00009264/// PerformMulCombine - Optimize a single multiply with constant into two
9265/// in order to implement it with two cheaper instructions, e.g.
9266/// LEA + SHL, LEA + LEA.
9267static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9268 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009269 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9270 return SDValue();
9271
Owen Andersone50ed302009-08-10 22:56:29 +00009272 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009273 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009274 return SDValue();
9275
9276 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9277 if (!C)
9278 return SDValue();
9279 uint64_t MulAmt = C->getZExtValue();
9280 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9281 return SDValue();
9282
9283 uint64_t MulAmt1 = 0;
9284 uint64_t MulAmt2 = 0;
9285 if ((MulAmt % 9) == 0) {
9286 MulAmt1 = 9;
9287 MulAmt2 = MulAmt / 9;
9288 } else if ((MulAmt % 5) == 0) {
9289 MulAmt1 = 5;
9290 MulAmt2 = MulAmt / 5;
9291 } else if ((MulAmt % 3) == 0) {
9292 MulAmt1 = 3;
9293 MulAmt2 = MulAmt / 3;
9294 }
9295 if (MulAmt2 &&
9296 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9297 DebugLoc DL = N->getDebugLoc();
9298
9299 if (isPowerOf2_64(MulAmt2) &&
9300 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9301 // If second multiplifer is pow2, issue it first. We want the multiply by
9302 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9303 // is an add.
9304 std::swap(MulAmt1, MulAmt2);
9305
9306 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009307 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009308 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009309 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009310 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009311 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009312 DAG.getConstant(MulAmt1, VT));
9313
Eric Christopherfd179292009-08-27 18:07:15 +00009314 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009315 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009316 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009317 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009318 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009319 DAG.getConstant(MulAmt2, VT));
9320
9321 // Do not add new nodes to DAG combiner worklist.
9322 DCI.CombineTo(N, NewMul, false);
9323 }
9324 return SDValue();
9325}
9326
Evan Chengad9c0a32009-12-15 00:53:42 +00009327static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9328 SDValue N0 = N->getOperand(0);
9329 SDValue N1 = N->getOperand(1);
9330 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9331 EVT VT = N0.getValueType();
9332
9333 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9334 // since the result of setcc_c is all zero's or all ones.
9335 if (N1C && N0.getOpcode() == ISD::AND &&
9336 N0.getOperand(1).getOpcode() == ISD::Constant) {
9337 SDValue N00 = N0.getOperand(0);
9338 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9339 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9340 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9341 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9342 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9343 APInt ShAmt = N1C->getAPIntValue();
9344 Mask = Mask.shl(ShAmt);
9345 if (Mask != 0)
9346 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9347 N00, DAG.getConstant(Mask, VT));
9348 }
9349 }
9350
9351 return SDValue();
9352}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009353
Nate Begeman740ab032009-01-26 00:52:55 +00009354/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9355/// when possible.
9356static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9357 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009358 EVT VT = N->getValueType(0);
9359 if (!VT.isVector() && VT.isInteger() &&
9360 N->getOpcode() == ISD::SHL)
9361 return PerformSHLCombine(N, DAG);
9362
Nate Begeman740ab032009-01-26 00:52:55 +00009363 // On X86 with SSE2 support, we can transform this to a vector shift if
9364 // all elements are shifted by the same amount. We can't do this in legalize
9365 // because the a constant vector is typically transformed to a constant pool
9366 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009367 if (!Subtarget->hasSSE2())
9368 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009369
Owen Anderson825b72b2009-08-11 20:47:22 +00009370 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009371 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009372
Mon P Wang3becd092009-01-28 08:12:05 +00009373 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009374 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009375 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009376 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009377 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9378 unsigned NumElts = VT.getVectorNumElements();
9379 unsigned i = 0;
9380 for (; i != NumElts; ++i) {
9381 SDValue Arg = ShAmtOp.getOperand(i);
9382 if (Arg.getOpcode() == ISD::UNDEF) continue;
9383 BaseShAmt = Arg;
9384 break;
9385 }
9386 for (; i != NumElts; ++i) {
9387 SDValue Arg = ShAmtOp.getOperand(i);
9388 if (Arg.getOpcode() == ISD::UNDEF) continue;
9389 if (Arg != BaseShAmt) {
9390 return SDValue();
9391 }
9392 }
9393 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009394 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009395 SDValue InVec = ShAmtOp.getOperand(0);
9396 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9397 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9398 unsigned i = 0;
9399 for (; i != NumElts; ++i) {
9400 SDValue Arg = InVec.getOperand(i);
9401 if (Arg.getOpcode() == ISD::UNDEF) continue;
9402 BaseShAmt = Arg;
9403 break;
9404 }
9405 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9406 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009407 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009408 if (C->getZExtValue() == SplatIdx)
9409 BaseShAmt = InVec.getOperand(1);
9410 }
9411 }
9412 if (BaseShAmt.getNode() == 0)
9413 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9414 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009415 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009416 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009417
Mon P Wangefa42202009-09-03 19:56:25 +00009418 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009419 if (EltVT.bitsGT(MVT::i32))
9420 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9421 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009422 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009423
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009424 // The shift amount is identical so we can do a vector shift.
9425 SDValue ValOp = N->getOperand(0);
9426 switch (N->getOpcode()) {
9427 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009428 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009429 break;
9430 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009431 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009432 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009433 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009434 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009435 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009436 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009437 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009438 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009439 if (VT == MVT::v8i16)
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_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009442 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009443 break;
9444 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009445 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009446 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009447 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009448 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009450 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009451 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009452 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009453 break;
9454 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009455 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009456 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009457 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009458 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009459 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009460 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009461 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009462 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009463 if (VT == MVT::v8i16)
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_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009466 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009467 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009468 }
9469 return SDValue();
9470}
9471
Evan Cheng760d1942010-01-04 21:22:48 +00009472static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9473 const X86Subtarget *Subtarget) {
9474 EVT VT = N->getValueType(0);
9475 if (VT != MVT::i64 || !Subtarget->is64Bit())
9476 return SDValue();
9477
9478 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9479 SDValue N0 = N->getOperand(0);
9480 SDValue N1 = N->getOperand(1);
9481 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9482 std::swap(N0, N1);
9483 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9484 return SDValue();
9485
9486 SDValue ShAmt0 = N0.getOperand(1);
9487 if (ShAmt0.getValueType() != MVT::i8)
9488 return SDValue();
9489 SDValue ShAmt1 = N1.getOperand(1);
9490 if (ShAmt1.getValueType() != MVT::i8)
9491 return SDValue();
9492 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9493 ShAmt0 = ShAmt0.getOperand(0);
9494 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9495 ShAmt1 = ShAmt1.getOperand(0);
9496
9497 DebugLoc DL = N->getDebugLoc();
9498 unsigned Opc = X86ISD::SHLD;
9499 SDValue Op0 = N0.getOperand(0);
9500 SDValue Op1 = N1.getOperand(0);
9501 if (ShAmt0.getOpcode() == ISD::SUB) {
9502 Opc = X86ISD::SHRD;
9503 std::swap(Op0, Op1);
9504 std::swap(ShAmt0, ShAmt1);
9505 }
9506
9507 if (ShAmt1.getOpcode() == ISD::SUB) {
9508 SDValue Sum = ShAmt1.getOperand(0);
9509 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9510 if (SumC->getSExtValue() == 64 &&
9511 ShAmt1.getOperand(1) == ShAmt0)
9512 return DAG.getNode(Opc, DL, VT,
9513 Op0, Op1,
9514 DAG.getNode(ISD::TRUNCATE, DL,
9515 MVT::i8, ShAmt0));
9516 }
9517 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9518 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9519 if (ShAmt0C &&
9520 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9521 return DAG.getNode(Opc, DL, VT,
9522 N0.getOperand(0), N1.getOperand(0),
9523 DAG.getNode(ISD::TRUNCATE, DL,
9524 MVT::i8, ShAmt0));
9525 }
9526
9527 return SDValue();
9528}
9529
Chris Lattner149a4e52008-02-22 02:09:43 +00009530/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009531static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009532 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009533 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9534 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009535 // A preferable solution to the general problem is to figure out the right
9536 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009537
9538 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009539 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009540 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009541 if (VT.getSizeInBits() != 64)
9542 return SDValue();
9543
Devang Patel578efa92009-06-05 21:57:13 +00009544 const Function *F = DAG.getMachineFunction().getFunction();
9545 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009546 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009547 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009548 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009549 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009550 isa<LoadSDNode>(St->getValue()) &&
9551 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9552 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009553 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009554 LoadSDNode *Ld = 0;
9555 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009556 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009557 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009558 // Must be a store of a load. We currently handle two cases: the load
9559 // is a direct child, and it's under an intervening TokenFactor. It is
9560 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009561 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009562 Ld = cast<LoadSDNode>(St->getChain());
9563 else if (St->getValue().hasOneUse() &&
9564 ChainVal->getOpcode() == ISD::TokenFactor) {
9565 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009566 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009567 TokenFactorIndex = i;
9568 Ld = cast<LoadSDNode>(St->getValue());
9569 } else
9570 Ops.push_back(ChainVal->getOperand(i));
9571 }
9572 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009573
Evan Cheng536e6672009-03-12 05:59:15 +00009574 if (!Ld || !ISD::isNormalLoad(Ld))
9575 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009576
Evan Cheng536e6672009-03-12 05:59:15 +00009577 // If this is not the MMX case, i.e. we are just turning i64 load/store
9578 // into f64 load/store, avoid the transformation if there are multiple
9579 // uses of the loaded value.
9580 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9581 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009582
Evan Cheng536e6672009-03-12 05:59:15 +00009583 DebugLoc LdDL = Ld->getDebugLoc();
9584 DebugLoc StDL = N->getDebugLoc();
9585 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9586 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9587 // pair instead.
9588 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009589 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009590 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9591 Ld->getBasePtr(), Ld->getSrcValue(),
9592 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009593 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009594 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009595 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009596 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009598 Ops.size());
9599 }
Evan Cheng536e6672009-03-12 05:59:15 +00009600 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009601 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009602 St->isVolatile(), St->isNonTemporal(),
9603 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009604 }
Evan Cheng536e6672009-03-12 05:59:15 +00009605
9606 // Otherwise, lower to two pairs of 32-bit loads / stores.
9607 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9609 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009610
Owen Anderson825b72b2009-08-11 20:47:22 +00009611 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009612 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009613 Ld->isVolatile(), Ld->isNonTemporal(),
9614 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009615 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009616 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009617 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009618 MinAlign(Ld->getAlignment(), 4));
9619
9620 SDValue NewChain = LoLd.getValue(1);
9621 if (TokenFactorIndex != -1) {
9622 Ops.push_back(LoLd);
9623 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009624 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009625 Ops.size());
9626 }
9627
9628 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9630 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009631
9632 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9633 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009634 St->isVolatile(), St->isNonTemporal(),
9635 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009636 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9637 St->getSrcValue(),
9638 St->getSrcValueOffset() + 4,
9639 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009640 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009641 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009642 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009643 }
Dan Gohman475871a2008-07-27 21:46:04 +00009644 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009645}
9646
Chris Lattner6cf73262008-01-25 06:14:17 +00009647/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9648/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009649static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009650 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9651 // F[X]OR(0.0, x) -> x
9652 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009653 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9654 if (C->getValueAPF().isPosZero())
9655 return N->getOperand(1);
9656 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9657 if (C->getValueAPF().isPosZero())
9658 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009659 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009660}
9661
9662/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009663static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009664 // FAND(0.0, x) -> 0.0
9665 // FAND(x, 0.0) -> 0.0
9666 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9667 if (C->getValueAPF().isPosZero())
9668 return N->getOperand(0);
9669 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9670 if (C->getValueAPF().isPosZero())
9671 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009672 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009673}
9674
Dan Gohmane5af2d32009-01-29 01:59:02 +00009675static SDValue PerformBTCombine(SDNode *N,
9676 SelectionDAG &DAG,
9677 TargetLowering::DAGCombinerInfo &DCI) {
9678 // BT ignores high bits in the bit index operand.
9679 SDValue Op1 = N->getOperand(1);
9680 if (Op1.hasOneUse()) {
9681 unsigned BitWidth = Op1.getValueSizeInBits();
9682 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9683 APInt KnownZero, KnownOne;
9684 TargetLowering::TargetLoweringOpt TLO(DAG);
9685 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9686 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9687 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9688 DCI.CommitTargetLoweringOpt(TLO);
9689 }
9690 return SDValue();
9691}
Chris Lattner83e6c992006-10-04 06:57:07 +00009692
Eli Friedman7a5e5552009-06-07 06:52:44 +00009693static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9694 SDValue Op = N->getOperand(0);
9695 if (Op.getOpcode() == ISD::BIT_CONVERT)
9696 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009697 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009698 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009699 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009700 OpVT.getVectorElementType().getSizeInBits()) {
9701 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9702 }
9703 return SDValue();
9704}
9705
Owen Anderson99177002009-06-29 18:04:45 +00009706// On X86 and X86-64, atomic operations are lowered to locked instructions.
9707// Locked instructions, in turn, have implicit fence semantics (all memory
9708// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009709// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009710// fence-atomic-fence.
9711static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9712 SDValue atomic = N->getOperand(0);
9713 switch (atomic.getOpcode()) {
9714 case ISD::ATOMIC_CMP_SWAP:
9715 case ISD::ATOMIC_SWAP:
9716 case ISD::ATOMIC_LOAD_ADD:
9717 case ISD::ATOMIC_LOAD_SUB:
9718 case ISD::ATOMIC_LOAD_AND:
9719 case ISD::ATOMIC_LOAD_OR:
9720 case ISD::ATOMIC_LOAD_XOR:
9721 case ISD::ATOMIC_LOAD_NAND:
9722 case ISD::ATOMIC_LOAD_MIN:
9723 case ISD::ATOMIC_LOAD_MAX:
9724 case ISD::ATOMIC_LOAD_UMIN:
9725 case ISD::ATOMIC_LOAD_UMAX:
9726 break;
9727 default:
9728 return SDValue();
9729 }
Eric Christopherfd179292009-08-27 18:07:15 +00009730
Owen Anderson99177002009-06-29 18:04:45 +00009731 SDValue fence = atomic.getOperand(0);
9732 if (fence.getOpcode() != ISD::MEMBARRIER)
9733 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009734
Owen Anderson99177002009-06-29 18:04:45 +00009735 switch (atomic.getOpcode()) {
9736 case ISD::ATOMIC_CMP_SWAP:
9737 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9738 atomic.getOperand(1), atomic.getOperand(2),
9739 atomic.getOperand(3));
9740 case ISD::ATOMIC_SWAP:
9741 case ISD::ATOMIC_LOAD_ADD:
9742 case ISD::ATOMIC_LOAD_SUB:
9743 case ISD::ATOMIC_LOAD_AND:
9744 case ISD::ATOMIC_LOAD_OR:
9745 case ISD::ATOMIC_LOAD_XOR:
9746 case ISD::ATOMIC_LOAD_NAND:
9747 case ISD::ATOMIC_LOAD_MIN:
9748 case ISD::ATOMIC_LOAD_MAX:
9749 case ISD::ATOMIC_LOAD_UMIN:
9750 case ISD::ATOMIC_LOAD_UMAX:
9751 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9752 atomic.getOperand(1), atomic.getOperand(2));
9753 default:
9754 return SDValue();
9755 }
9756}
9757
Evan Cheng2e489c42009-12-16 00:53:11 +00009758static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9759 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9760 // (and (i32 x86isd::setcc_carry), 1)
9761 // This eliminates the zext. This transformation is necessary because
9762 // ISD::SETCC is always legalized to i8.
9763 DebugLoc dl = N->getDebugLoc();
9764 SDValue N0 = N->getOperand(0);
9765 EVT VT = N->getValueType(0);
9766 if (N0.getOpcode() == ISD::AND &&
9767 N0.hasOneUse() &&
9768 N0.getOperand(0).hasOneUse()) {
9769 SDValue N00 = N0.getOperand(0);
9770 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9771 return SDValue();
9772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9773 if (!C || C->getZExtValue() != 1)
9774 return SDValue();
9775 return DAG.getNode(ISD::AND, dl, VT,
9776 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9777 N00.getOperand(0), N00.getOperand(1)),
9778 DAG.getConstant(1, VT));
9779 }
9780
9781 return SDValue();
9782}
9783
Dan Gohman475871a2008-07-27 21:46:04 +00009784SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009785 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009786 SelectionDAG &DAG = DCI.DAG;
9787 switch (N->getOpcode()) {
9788 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009789 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009790 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009791 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009792 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009793 case ISD::SHL:
9794 case ISD::SRA:
9795 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009796 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009797 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009798 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009799 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9800 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009801 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009802 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009803 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009804 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009805 }
9806
Dan Gohman475871a2008-07-27 21:46:04 +00009807 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009808}
9809
Evan Cheng60c07e12006-07-05 22:17:51 +00009810//===----------------------------------------------------------------------===//
9811// X86 Inline Assembly Support
9812//===----------------------------------------------------------------------===//
9813
Chris Lattnerb8105652009-07-20 17:51:36 +00009814static bool LowerToBSwap(CallInst *CI) {
9815 // FIXME: this should verify that we are targetting a 486 or better. If not,
9816 // we will turn this bswap into something that will be lowered to logical ops
9817 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9818 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009819
Chris Lattnerb8105652009-07-20 17:51:36 +00009820 // Verify this is a simple bswap.
9821 if (CI->getNumOperands() != 2 ||
9822 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009823 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009824 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009825
Chris Lattnerb8105652009-07-20 17:51:36 +00009826 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9827 if (!Ty || Ty->getBitWidth() % 16 != 0)
9828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009829
Chris Lattnerb8105652009-07-20 17:51:36 +00009830 // Okay, we can do this xform, do so now.
9831 const Type *Tys[] = { Ty };
9832 Module *M = CI->getParent()->getParent()->getParent();
9833 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009834
Chris Lattnerb8105652009-07-20 17:51:36 +00009835 Value *Op = CI->getOperand(1);
9836 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009837
Chris Lattnerb8105652009-07-20 17:51:36 +00009838 CI->replaceAllUsesWith(Op);
9839 CI->eraseFromParent();
9840 return true;
9841}
9842
9843bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9844 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9845 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9846
9847 std::string AsmStr = IA->getAsmString();
9848
9849 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009850 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009851 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9852
9853 switch (AsmPieces.size()) {
9854 default: return false;
9855 case 1:
9856 AsmStr = AsmPieces[0];
9857 AsmPieces.clear();
9858 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9859
9860 // bswap $0
9861 if (AsmPieces.size() == 2 &&
9862 (AsmPieces[0] == "bswap" ||
9863 AsmPieces[0] == "bswapq" ||
9864 AsmPieces[0] == "bswapl") &&
9865 (AsmPieces[1] == "$0" ||
9866 AsmPieces[1] == "${0:q}")) {
9867 // No need to check constraints, nothing other than the equivalent of
9868 // "=r,0" would be valid here.
9869 return LowerToBSwap(CI);
9870 }
9871 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009872 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009873 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009874 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009875 AsmPieces[1] == "$$8," &&
9876 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009877 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9878 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009879 const std::string &Constraints = IA->getConstraintString();
9880 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009881 std::sort(AsmPieces.begin(), AsmPieces.end());
9882 if (AsmPieces.size() == 4 &&
9883 AsmPieces[0] == "~{cc}" &&
9884 AsmPieces[1] == "~{dirflag}" &&
9885 AsmPieces[2] == "~{flags}" &&
9886 AsmPieces[3] == "~{fpsr}") {
9887 return LowerToBSwap(CI);
9888 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009889 }
9890 break;
9891 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009892 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009893 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009894 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9895 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9896 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009897 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009898 SplitString(AsmPieces[0], Words, " \t");
9899 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9900 Words.clear();
9901 SplitString(AsmPieces[1], Words, " \t");
9902 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9903 Words.clear();
9904 SplitString(AsmPieces[2], Words, " \t,");
9905 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9906 Words[2] == "%edx") {
9907 return LowerToBSwap(CI);
9908 }
9909 }
9910 }
9911 }
9912 break;
9913 }
9914 return false;
9915}
9916
9917
9918
Chris Lattnerf4dff842006-07-11 02:54:03 +00009919/// getConstraintType - Given a constraint letter, return the type of
9920/// constraint it is for this target.
9921X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009922X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9923 if (Constraint.size() == 1) {
9924 switch (Constraint[0]) {
9925 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009926 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009927 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009928 case 'r':
9929 case 'R':
9930 case 'l':
9931 case 'q':
9932 case 'Q':
9933 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009934 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009935 case 'Y':
9936 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009937 case 'e':
9938 case 'Z':
9939 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009940 default:
9941 break;
9942 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009943 }
Chris Lattner4234f572007-03-25 02:14:49 +00009944 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009945}
9946
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009947/// LowerXConstraint - try to replace an X constraint, which matches anything,
9948/// with another that has more specific requirements based on the type of the
9949/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009950const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009951LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009952 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9953 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009954 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009955 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009956 return "Y";
9957 if (Subtarget->hasSSE1())
9958 return "x";
9959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009960
Chris Lattner5e764232008-04-26 23:02:14 +00009961 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009962}
9963
Chris Lattner48884cd2007-08-25 00:47:38 +00009964/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9965/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009966void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009967 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009968 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009969 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009970 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009971 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009972
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009973 switch (Constraint) {
9974 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009975 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009976 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009977 if (C->getZExtValue() <= 31) {
9978 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009979 break;
9980 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009981 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009982 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009983 case 'J':
9984 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009985 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009986 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9987 break;
9988 }
9989 }
9990 return;
9991 case 'K':
9992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009993 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009994 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9995 break;
9996 }
9997 }
9998 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009999 case 'N':
10000 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010001 if (C->getZExtValue() <= 255) {
10002 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010003 break;
10004 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010005 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010006 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010007 case 'e': {
10008 // 32-bit signed value
10009 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10010 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010011 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10012 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010013 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010014 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010015 break;
10016 }
10017 // FIXME gcc accepts some relocatable values here too, but only in certain
10018 // memory models; it's complicated.
10019 }
10020 return;
10021 }
10022 case 'Z': {
10023 // 32-bit unsigned value
10024 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10025 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010026 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10027 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010028 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10029 break;
10030 }
10031 }
10032 // FIXME gcc accepts some relocatable values here too, but only in certain
10033 // memory models; it's complicated.
10034 return;
10035 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010036 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010037 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010038 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010039 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010040 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010041 break;
10042 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010043
Chris Lattnerdc43a882007-05-03 16:52:29 +000010044 // If we are in non-pic codegen mode, we allow the address of a global (with
10045 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010046 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010047 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010048
Chris Lattner49921962009-05-08 18:23:14 +000010049 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10050 while (1) {
10051 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10052 Offset += GA->getOffset();
10053 break;
10054 } else if (Op.getOpcode() == ISD::ADD) {
10055 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10056 Offset += C->getZExtValue();
10057 Op = Op.getOperand(0);
10058 continue;
10059 }
10060 } else if (Op.getOpcode() == ISD::SUB) {
10061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10062 Offset += -C->getZExtValue();
10063 Op = Op.getOperand(0);
10064 continue;
10065 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010066 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010067
Chris Lattner49921962009-05-08 18:23:14 +000010068 // Otherwise, this isn't something we can handle, reject it.
10069 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010070 }
Eric Christopherfd179292009-08-27 18:07:15 +000010071
Chris Lattner36c25012009-07-10 07:34:39 +000010072 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010073 // If we require an extra load to get this address, as in PIC mode, we
10074 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010075 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10076 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010077 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010078
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010079 if (hasMemory)
10080 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10081 else
10082 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010083 Result = Op;
10084 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010085 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010086 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010087
Gabor Greifba36cb52008-08-28 21:40:38 +000010088 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010089 Ops.push_back(Result);
10090 return;
10091 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010092 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10093 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010094}
10095
Chris Lattner259e97c2006-01-31 19:43:35 +000010096std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010097getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010098 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010099 if (Constraint.size() == 1) {
10100 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010101 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010102 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010103 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10104 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010105 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010106 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10107 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10108 X86::R10D,X86::R11D,X86::R12D,
10109 X86::R13D,X86::R14D,X86::R15D,
10110 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010111 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010112 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10113 X86::SI, X86::DI, X86::R8W,X86::R9W,
10114 X86::R10W,X86::R11W,X86::R12W,
10115 X86::R13W,X86::R14W,X86::R15W,
10116 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010117 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010118 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10119 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10120 X86::R10B,X86::R11B,X86::R12B,
10121 X86::R13B,X86::R14B,X86::R15B,
10122 X86::BPL, X86::SPL, 0);
10123
Owen Anderson825b72b2009-08-11 20:47:22 +000010124 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010125 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10126 X86::RSI, X86::RDI, X86::R8, X86::R9,
10127 X86::R10, X86::R11, X86::R12,
10128 X86::R13, X86::R14, X86::R15,
10129 X86::RBP, X86::RSP, 0);
10130
10131 break;
10132 }
Eric Christopherfd179292009-08-27 18:07:15 +000010133 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010134 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010135 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010136 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010137 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010138 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010139 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010140 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010141 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010142 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10143 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010144 }
10145 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010146
Chris Lattner1efa40f2006-02-22 00:56:39 +000010147 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010148}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010149
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010150std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010151X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010152 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010153 // First, see if this is a constraint that directly corresponds to an LLVM
10154 // register class.
10155 if (Constraint.size() == 1) {
10156 // GCC Constraint Letters
10157 switch (Constraint[0]) {
10158 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010159 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010160 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010161 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010162 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010163 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010164 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010165 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010166 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010167 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010168 case 'R': // LEGACY_REGS
10169 if (VT == MVT::i8)
10170 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10171 if (VT == MVT::i16)
10172 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10173 if (VT == MVT::i32 || !Subtarget->is64Bit())
10174 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10175 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010176 case 'f': // FP Stack registers.
10177 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10178 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010179 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010180 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010181 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010182 return std::make_pair(0U, X86::RFP64RegisterClass);
10183 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010184 case 'y': // MMX_REGS if MMX allowed.
10185 if (!Subtarget->hasMMX()) break;
10186 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010187 case 'Y': // SSE_REGS if SSE2 allowed
10188 if (!Subtarget->hasSSE2()) break;
10189 // FALL THROUGH.
10190 case 'x': // SSE_REGS if SSE1 allowed
10191 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010192
Owen Anderson825b72b2009-08-11 20:47:22 +000010193 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010194 default: break;
10195 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010196 case MVT::f32:
10197 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010198 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010199 case MVT::f64:
10200 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010201 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010202 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010203 case MVT::v16i8:
10204 case MVT::v8i16:
10205 case MVT::v4i32:
10206 case MVT::v2i64:
10207 case MVT::v4f32:
10208 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010209 return std::make_pair(0U, X86::VR128RegisterClass);
10210 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010211 break;
10212 }
10213 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010214
Chris Lattnerf76d1802006-07-31 23:26:50 +000010215 // Use the default implementation in TargetLowering to convert the register
10216 // constraint into a member of a register class.
10217 std::pair<unsigned, const TargetRegisterClass*> Res;
10218 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010219
10220 // Not found as a standard register?
10221 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010222 // Map st(0) -> st(7) -> ST0
10223 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10224 tolower(Constraint[1]) == 's' &&
10225 tolower(Constraint[2]) == 't' &&
10226 Constraint[3] == '(' &&
10227 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10228 Constraint[5] == ')' &&
10229 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010230
Chris Lattner56d77c72009-09-13 22:41:48 +000010231 Res.first = X86::ST0+Constraint[4]-'0';
10232 Res.second = X86::RFP80RegisterClass;
10233 return Res;
10234 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010235
Chris Lattner56d77c72009-09-13 22:41:48 +000010236 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010237 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010238 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010239 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010240 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010241 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010242
10243 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010244 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010245 Res.first = X86::EFLAGS;
10246 Res.second = X86::CCRRegisterClass;
10247 return Res;
10248 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010249
Dale Johannesen330169f2008-11-13 21:52:36 +000010250 // 'A' means EAX + EDX.
10251 if (Constraint == "A") {
10252 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010253 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010254 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010255 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010256 return Res;
10257 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010258
Chris Lattnerf76d1802006-07-31 23:26:50 +000010259 // Otherwise, check to see if this is a register class of the wrong value
10260 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10261 // turn into {ax},{dx}.
10262 if (Res.second->hasType(VT))
10263 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010264
Chris Lattnerf76d1802006-07-31 23:26:50 +000010265 // All of the single-register GCC register classes map their values onto
10266 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10267 // really want an 8-bit or 32-bit register, map to the appropriate register
10268 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010269 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010270 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010271 unsigned DestReg = 0;
10272 switch (Res.first) {
10273 default: break;
10274 case X86::AX: DestReg = X86::AL; break;
10275 case X86::DX: DestReg = X86::DL; break;
10276 case X86::CX: DestReg = X86::CL; break;
10277 case X86::BX: DestReg = X86::BL; break;
10278 }
10279 if (DestReg) {
10280 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010281 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010282 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010283 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010284 unsigned DestReg = 0;
10285 switch (Res.first) {
10286 default: break;
10287 case X86::AX: DestReg = X86::EAX; break;
10288 case X86::DX: DestReg = X86::EDX; break;
10289 case X86::CX: DestReg = X86::ECX; break;
10290 case X86::BX: DestReg = X86::EBX; break;
10291 case X86::SI: DestReg = X86::ESI; break;
10292 case X86::DI: DestReg = X86::EDI; break;
10293 case X86::BP: DestReg = X86::EBP; break;
10294 case X86::SP: DestReg = X86::ESP; break;
10295 }
10296 if (DestReg) {
10297 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010298 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010299 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010300 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010301 unsigned DestReg = 0;
10302 switch (Res.first) {
10303 default: break;
10304 case X86::AX: DestReg = X86::RAX; break;
10305 case X86::DX: DestReg = X86::RDX; break;
10306 case X86::CX: DestReg = X86::RCX; break;
10307 case X86::BX: DestReg = X86::RBX; break;
10308 case X86::SI: DestReg = X86::RSI; break;
10309 case X86::DI: DestReg = X86::RDI; break;
10310 case X86::BP: DestReg = X86::RBP; break;
10311 case X86::SP: DestReg = X86::RSP; break;
10312 }
10313 if (DestReg) {
10314 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010315 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010316 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010317 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010318 } else if (Res.second == X86::FR32RegisterClass ||
10319 Res.second == X86::FR64RegisterClass ||
10320 Res.second == X86::VR128RegisterClass) {
10321 // Handle references to XMM physical registers that got mapped into the
10322 // wrong class. This can happen with constraints like {xmm0} where the
10323 // target independent register mapper will just pick the first match it can
10324 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010325 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010326 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010327 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010328 Res.second = X86::FR64RegisterClass;
10329 else if (X86::VR128RegisterClass->hasType(VT))
10330 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010331 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010332
Chris Lattnerf76d1802006-07-31 23:26:50 +000010333 return Res;
10334}